DEBUG/MEDIUM: memory: add optional control pool memory operations

When DEBUG_MEMORY_POOLS is used, we now use the link pointer at the end
of the pool to store a pointer to the pool, and to control it during
pool_free2() in order to serve four purposes :
  - at any instant we can know what pool an object was allocated from
    when examining memory, hence how we should possibly decode it ;

  - it serves to detect double free when they happen, as the pointer
    cannot be valid after the element is linked into the pool ;

  - it serves to detect if an element is released in the wrong pool ;

  - it serves as a canary, to detect if some buffers experienced an
    overflow before being release.

All these elements will definitely help better troubleshoot strange
situations, or at least confirm that certain conditions did not happen.
diff --git a/src/memory.c b/src/memory.c
index 0363f36..691e1e3 100644
--- a/src/memory.c
+++ b/src/memory.c
@@ -116,6 +116,10 @@
 		pool->free_list = ptr;
 	}
 	pool->used++;
+#ifdef DEBUG_MEMORY_POOLS
+	/* keep track of where the element was allocated from */
+	*POOL_LINK(pool, ptr) = (void *)pool;
+#endif
 	return ptr;
 }