diff --git a/include/zframe.h b/include/zframe.h index e0e36c2..1b880d3 100644 --- a/include/zframe.h +++ b/include/zframe.h @@ -116,6 +116,10 @@ CZMQ_EXPORT void CZMQ_EXPORT void zframe_reset (zframe_t *self, const void *data, size_t size); +// Set the free callback for frame +CZMQ_EXPORT void + zframe_freefn(zframe_t *self, zframe_free_fn *free_fn, void *arg); + // Self test of this class CZMQ_EXPORT int zframe_test (bool verbose); diff --git a/src/zframe.c b/src/zframe.c index edeec07..cf124ac 100644 --- a/src/zframe.c +++ b/src/zframe.c @@ -49,6 +49,8 @@ struct _zframe_t { zmq_msg_t zmsg; // zmq_msg_t blob for frame int more; // More flag, from last read int zero_copy; // zero-copy flag + zframe_free_fn *free_fn; // destructor callback + void *free_arg; // destructor callback arg }; @@ -116,6 +118,8 @@ zframe_destroy (zframe_t **self_p) assert (self_p); if (*self_p) { zframe_t *self = *self_p; + if (self->free_fn) + (self->free_fn) (self, self->free_arg); zmq_msg_close (&self->zmsg); free (self); *self_p = NULL; @@ -373,6 +377,16 @@ zframe_reset (zframe_t *self, const void *data, size_t size) memcpy (zmq_msg_data (&self->zmsg), data, size); } +void +zframe_freefn (zframe_t *self, zframe_free_fn *free_fn, void *arg) +{ + assert (self); + assert (free_fn); + + self->free_fn = free_fn; + self->free_arg = arg; +} + // -------------------------------------------------------------------------- // Selftest @@ -389,6 +403,12 @@ s_test_free_cb (void *data, void *arg) free (data); } +static void +s_test_free_frame_cb(void *frame, void *arg) +{ + assert (frame); +} + int zframe_test (bool verbose) { @@ -472,6 +492,10 @@ zframe_test (bool verbose) zframe_destroy (&frame); zframe_destroy (&frame_copy); + frame = zframe_new ("callback", 8); + zframe_freefn (frame, s_test_free_frame_cb, NULL); + zframe_destroy (&frame); + zctx_destroy (&ctx); // @end printf ("OK\n");