coap_pdu.c: Support incrementing reference count on const coap_pdu_t parameters

const coap_pdu_t * parameters are passed into application (call-back handler)
code.  If the application code then calls a libcoap API with a
const coap_pdu_t * parameter, it is not possible to increment the reference
counter so that multiple entities can reference the same PDU and an entity's
call to coap_delete_pdu() does not prematurely remove the PDU until all the
entities have finished.

This code allows the (const) PDU's reference counter to be incremented.
This commit is contained in:
Jon Shallow authored and Jon Shallow committed 2025-04-11 16:26:44 +01:00
1 parent 84778a7bb4
commit 72691daec2
2 files changed
+31

No files matched your search

+13
View File
@@ -1626,3 +1626,16 @@ coap_pdu_reference_lkd(coap_pdu_t *pdu) {
}
return pdu;
}
coap_pdu_t *
coap_const_pdu_reference_lkd(const coap_pdu_t *pdu) {
coap_pdu_t *pdu_rw = NULL;
if (pdu != NULL) {
/* Need to do this to not get a compiler warning about const parameters */
memcpy(&pdu_rw, &pdu, sizeof(pdu_rw));
++pdu_rw->ref;
}
return pdu_rw;
}