/** * Shallow cloning of extended message, result is referencing the same data. */ static pmsg_t * pmsg_clone_ext(pmsg_ext_t *mb) { pmsg_ext_t *nmb; pmsg_ext_check_consistency(mb); WALLOC(nmb); *nmb = *mb; /* Struct copy */ nmb->pmsg.m_refcnt = 1; pdata_addref(nmb->pmsg.m_data); return cast_to_pmsg(nmb); }
/** * Shallow cloning of message, making sure we have a plain clone even if * the original was extended. */ pmsg_t * pmsg_clone_plain(const pmsg_t *mb) { pmsg_t *nmb; pmsg_check_consistency(mb); WALLOC(nmb); memcpy(nmb, mb, sizeof *nmb); nmb->magic = PMSG_MAGIC; /* Force plain message */ nmb->m_flags &= ~PMSG_PF_EXT; /* In case original was extended */ nmb->m_refcnt = 1; pdata_addref(nmb->m_data); return nmb; }
/** * Shallow cloning of message, result is referencing the same data. * * This is not the same thing as pmsg_ref() because here a new message block * is created (albeit the data are shared with the original message). */ pmsg_t * pmsg_clone(pmsg_t *mb) { if (pmsg_is_extended(mb)) { return pmsg_clone_ext(cast_to_pmsg_ext(mb)); } else { pmsg_t *nmb; pmsg_check_consistency(mb); WALLOC(nmb); *nmb = *mb; /* Struct copy */ nmb->m_refcnt = 1; pdata_addref(nmb->m_data); return nmb; } }
/** * Extended cloning of message, adds a free routine callback. */ pmsg_t * pmsg_clone_extend(pmsg_t *mb, pmsg_free_t free_cb, void *arg) { pmsg_ext_t *nmb; pmsg_check_consistency(mb); WALLOC(nmb); nmb->pmsg = *mb; /* Struct copy */ nmb->pmsg.magic = PMSG_EXT_MAGIC; pdata_addref(nmb->pmsg.m_data); nmb->pmsg.m_flags |= PMSG_PF_EXT; nmb->pmsg.m_refcnt = 1; nmb->m_free = free_cb; nmb->m_arg = arg; return cast_to_pmsg(nmb); }