.if false ================================================================================== Copyright (c) 2019 Nokia Copyright (c) 2018-2019 AT&T Intellectual Property. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================================================== .fi .if false Mnemonic rmr_realloc_payload.3.xfm Abstract The manual page for the rmr_realloc_payload function. Author E. Scott Daniels Date 30 October 2019 .fi .gv e LIB lib .im &{lib}/man/setup.im &line_len(6i) &h1(RMR Library Functions) &h2(NAME) rmr_realloc_payload &h2(SYNOPSIS ) &indent &ex_start #include extern rmr_mbuf_t* rmr_realloc_payload( rmr_mbuf_t* msg, int new_len, int copy, int clone ); &ex_end &uindent &h2(DESCRIPTION) The &cw(rmr_realloc_payload) function will return a pointer to an RMR message buffer struct (rmr_mbuf_t) which has a payload large enough to accomodate &ital(new_len) bytes. If necessary, the underlying payload is reallocated, and the bytes from the original payload are copied if the &ital(copy) parameter is true (1). If the message passed in has a payload large enough, there is no additional memory allocation and copying. &h3(Cloning The Message Buffer) This function can also be used to generate a separate copy of the original message, with the desired payload size, without destroying the original message buffer or the original payload. A standalone copy is made only when the &ital(clone) parameter is true (1). When cloning, the payload is copied to the cloned message &bold(only) if the &ital(copy) parameter is true. &h3(Message Buffer Metadata) The metadata in the original message buffer (message type, subscription ID, and payload length) will be preserved if the &ital(copy) parameter is true. When this parameter is not true (0), then these values are set to the uninitialised value (-1) for type and ID, and the length is set to 0. &h2(RETURN VALUE) The &cw(rmr_realloc_payload) function returns a pointer to the message buffer with the payload which is large enough to hold &ital(new_len) bytes. If the &ital(clone) option is true, this will be a pointer to the newly cloned message buffer; the original message buffer pointer may still be used to referenced that message. It is the calling application's responsibility to free the memory associateed with both messages using the rmr_free_msg() function. &space When the &ital(clone) option is not used, it is still good practice by the calling application to capture and use this reference as it is possible that the message buffer, and not just the payload buffer, was reallocated. In the event of an error, a nil pointer will be returned and the value of &ital(errno) will be set to reflect the problem. &h2(ERRORS) These value of &ital(errno) will reflect the error condition if a nil pointer is returned: &half_space &beg_dlist(.75i : ^&bold_font ) &di(ENOMEM) Memory allocation of the new payload failed. &half_space &di(EINVAL) The pointer passed in was nil, or refrenced an invalid message, or the required length was not valid. &end_dlist &h2(EXAMPLE) The following code bit illustrates how this function can be used to reallocate a buffer for a return to sender acknowledgement message which is larger than the message received. &space &ex_start if( rmr_payload_size( msg ) < ack_sz ) { // received message too small for ack msg = rmr_realloc_payload( msg, ack_sz, 0, 0 ); // reallocate the message with a payload big enough if( msg == NULL ) { fprintf( stderr, "[ERR] realloc returned a nil pointer: %s\n", strerror( errno ) ); } else { // populate and send ack message } } &ex_end &h2(SEE ALSO ) .ju off rmr_alloc_msg(3), rmr_free_msg(3), rmr_init(3), rmr_payload_size(3), rmr_send_msg(3), rmr_rcv_msg(3), rmr_rcv_specific(3), rmr_rts_msg(3), rmr_ready(3), rmr_fib(3), rmr_has_str(3), rmr_set_stimeout(3), rmr_tokenise(3), rmr_mk_ring(3), rmr_ring_free(3) .ju on