mirror of https://gerrit.osmocom.org/libosmocore
msgb: introduce realloc functions
Those are similar to existing *msgb_alloc*() functions but allows to change the size of destination msgb provided it fits the data from source msgb. Change-Id: I36d4c16241d19f0f73c325be4d0e0bdef6813615
This commit is contained in:
parent
a3e5c313ef
commit
2496668d42
|
@ -56,6 +56,8 @@ struct msgb {
|
|||
unsigned char _data[0]; /*!< optional immediate data array */
|
||||
};
|
||||
|
||||
extern struct msgb *msgb_realloc_c(const void *ctx, const struct msgb *msg, uint16_t new_len, const char *name);
|
||||
extern struct msgb *msgb_realloc(const struct msgb *msg, uint16_t new_len, const char *name);
|
||||
extern struct msgb *msgb_alloc_c(const void *ctx, uint16_t size, const char *name);
|
||||
extern struct msgb *msgb_alloc(uint16_t size, const char *name);
|
||||
extern void msgb_free(struct msgb *m);
|
||||
|
|
54
src/msgb.c
54
src/msgb.c
|
@ -314,31 +314,39 @@ void *msgb_talloc_ctx_init(void *root_ctx, unsigned int pool_size)
|
|||
return tall_msgb_ctx;
|
||||
}
|
||||
|
||||
/*! Copy an msgb.
|
||||
/*! Reallocate an msgb.
|
||||
*
|
||||
* This function allocates a new msgb, copies the data buffer of msg,
|
||||
* and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
|
||||
* is not copied.
|
||||
* This function allocates a new msgb with new_len size, copies the data buffer of msg,
|
||||
* and adjusts the pointers (incl l1h-l4h) accordingly. The cb part is not copied.
|
||||
* \param[in] ctx talloc context on which allocation happens
|
||||
* \param[in] msg The old msgb object
|
||||
* \param[in] name Human-readable name to be associated with msgb
|
||||
* \param[in] new_len The length of new msgb object
|
||||
* \param[in] name Human-readable name to be associated with new msgb
|
||||
*/
|
||||
struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name)
|
||||
struct msgb *msgb_realloc_c(const void *ctx, const struct msgb *msg, uint16_t new_len, const char *name)
|
||||
{
|
||||
struct msgb *new_msg;
|
||||
|
||||
new_msg = msgb_alloc_c(ctx, msg->data_len, name);
|
||||
if (new_len < msgb_length(msg)) {
|
||||
LOGP(DLGLOBAL, LOGL_ERROR,
|
||||
"Data from old msgb (%u bytes) won't fit into new msgb (%u bytes) after reallocation\n",
|
||||
msgb_length(msg), new_len);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
new_msg = msgb_alloc_c(ctx, new_len, name);
|
||||
if (!new_msg)
|
||||
return NULL;
|
||||
|
||||
/* copy data */
|
||||
memcpy(new_msg->_data, msg->_data, new_msg->data_len);
|
||||
|
||||
/* copy header */
|
||||
new_msg->len = msg->len;
|
||||
new_msg->data += msg->data - msg->_data;
|
||||
new_msg->head += msg->head - msg->_data;
|
||||
new_msg->tail += msg->tail - msg->_data;
|
||||
|
||||
/* copy data */
|
||||
memcpy(new_msg->data, msg->data, msgb_length(msg));
|
||||
|
||||
if (msg->l1h)
|
||||
new_msg->l1h = new_msg->_data + (msg->l1h - msg->_data);
|
||||
if (msg->l2h)
|
||||
|
@ -351,6 +359,32 @@ struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *na
|
|||
return new_msg;
|
||||
}
|
||||
|
||||
/*! Reallocate an msgb.
|
||||
*
|
||||
* This function allocates a new msgb with new_len size, copies the data buffer of msg,
|
||||
* and adjusts the pointers (incl l1h-l4h) accordingly. The cb part is not copied.
|
||||
* \param[in] msg The old msgb object
|
||||
* \param[in] name Human-readable name to be associated with new msgb
|
||||
*/
|
||||
struct msgb *msgb_realloc(const struct msgb *msg, uint16_t new_len, const char *name)
|
||||
{
|
||||
return msgb_realloc_c(tall_msgb_ctx, msg, new_len, name);
|
||||
}
|
||||
|
||||
/*! Copy an msgb.
|
||||
*
|
||||
* This function allocates a new msgb, copies the data buffer of msg,
|
||||
* and adjusts the pointers (incl l1h-l4h) accordingly. The cb part
|
||||
* is not copied.
|
||||
* \param[in] ctx talloc context on which allocation happens
|
||||
* \param[in] msg The old msgb object
|
||||
* \param[in] name Human-readable name to be associated with msgb
|
||||
*/
|
||||
struct msgb *msgb_copy_c(const void *ctx, const struct msgb *msg, const char *name)
|
||||
{
|
||||
return msgb_realloc_c(ctx, msg, msg->data_len, name);
|
||||
}
|
||||
|
||||
/*! Copy an msgb.
|
||||
*
|
||||
* This function allocates a new msgb, copies the data buffer of msg,
|
||||
|
|
Loading…
Reference in New Issue