being more precise on the different required behavior of a destructor function for pooled and non-pooled memory

Wed, 30 May 2018 11:13:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 30 May 2018 11:13:52 +0200
changeset 326
3dd7d21fb76b
parent 325
a3e63cb21e20
child 327
fbc33813265b

being more precise on the different required behavior of a destructor function for pooled and non-pooled memory

docs/src/modules.md file | annotate | diff | comparison | revisions
src/ucx/mempool.h file | annotate | diff | comparison | revisions
--- a/docs/src/modules.md	Tue May 29 11:05:12 2018 +0200
+++ b/docs/src/modules.md	Wed May 30 11:13:52 2018 +0200
@@ -455,6 +455,10 @@
 Be aware, that your destructor function should not free any memory, that is
 also managed by the pool.
 Otherwise you might be risking a double-free.
+More precisely, a destructor function set with `ucx_mempool_set_destr()` MUST
+NOT call `free()` on the specified pointer whereas a desructor function
+registered with `ucx_mempool_reg_destr()` MAY (and in most cases will) call
+`free()`.
 
 ## Properties
 
--- a/src/ucx/mempool.h	Tue May 29 11:05:12 2018 +0200
+++ b/src/ucx/mempool.h	Wed May 30 11:13:52 2018 +0200
@@ -164,6 +164,9 @@
  * 
  * The destructor is automatically called when the memory is freed or the
  * pool is destroyed.
+ * A destructor for pooled memory <b>MUST NOT</b> free the memory itself,
+ * as this is done by the pool. Use a destructor to free any resources
+ * managed by the pooled object.
  * 
  * The only requirement for the specified memory is, that it <b>MUST</b> be
  * pooled memory by a UcxMempool or an element-compatible mempool. The pointer
@@ -179,12 +182,18 @@
 
 /**
  * Registers a destructor function for the specified (non-pooled) memory.
- * 
+ *
  * This is useful, if you have memory that has not been allocated by a mempool,
  * but shall be managed by a mempool.
  * 
  * This function creates an entry in the specified mempool and the memory will
  * therefore (logically) convert to pooled memory.
+ * <b>However, this does not cause the memory to be freed automatically!</b>.
+ * If you want to use this function, make the memory pool free non-pooled
+ * memory, the specified destructor function must call <code>free()</code>
+ * by itself. But keep in mind, that you then MUST NOT use this destructor
+ * function with pooled memory (e.g. in ucx_mempool_set_destr()), as it
+ * would cause a double-free.
  * 
  * @param pool the memory pool
  * @param ptr data the destructor is registered for

mercurial