tc = talloc_chunk_from_ptr(result);
pool_hdr = talloc_pool_from_chunk(tc);
- if (unlikely(tc->flags & TALLOC_FLAG_POOLMEM)) {
- /* We don't handle this correctly, so fail. */
- talloc_log("talloc: cannot allocate pool off another pool %s\n",
- talloc_get_name(context));
- talloc_free(result);
- return NULL;
- }
tc->flags |= TALLOC_FLAG_POOL;
tc->size = 0;
*/
pool_tc->name = location;
- talloc_memlimit_update_on_free(pool_tc);
-
- TC_INVALIDATE_FULL_CHUNK(pool_tc);
- free(pool);
+ if (pool_tc->flags & TALLOC_FLAG_POOLMEM) {
+ _talloc_free_poolmem(pool_tc, location);
+ } else {
+ /*
+ * The talloc_memlimit_update_on_free()
+ * call takes into account the
+ * prefix TP_HDR_SIZE allocated before
+ * the pool talloc_chunk.
+ */
+ talloc_memlimit_update_on_free(pool_tc);
+ TC_INVALIDATE_FULL_CHUNK(pool_tc);
+ free(pool);
+ }
return;
}
static inline int _talloc_free_internal(void *ptr, const char *location)
{
struct talloc_chunk *tc;
+ void *ptr_to_free;
if (unlikely(ptr == NULL)) {
return -1;
}
/*
- * This call takes into account the
- * prefix TP_HDR_SIZE allocated before
- * the pool talloc_chunk.
- */
- talloc_memlimit_update_on_free(tc);
-
- TC_INVALIDATE_FULL_CHUNK(tc);
- free(pool);
- return 0;
+ * With object_count==0, a pool becomes a normal piece of
+ * memory to free. If it's allocated inside a pool, it needs
+ * to be freed as poolmem, else it needs to be just freed.
+ */
+ ptr_to_free = pool;
+ } else {
+ ptr_to_free = tc;
}
if (tc->flags & TALLOC_FLAG_POOLMEM) {
talloc_memlimit_update_on_free(tc);
TC_INVALIDATE_FULL_CHUNK(tc);
- free(tc);
+ free(ptr_to_free);
return 0;
}
/*
* Pool entries don't count. Only the pools
* themselves are counted as part of the memory
- * limits.
+ * limits. Note that this also takes care of
+ * nested pools which have both flags
+ * TALLOC_FLAG_POOLMEM|TALLOC_FLAG_POOL set.
*/
if (tc->flags & TALLOC_FLAG_POOLMEM) {
return;
return true;
}
+static bool test_pool_nest(void)
+{
+ void *p1, *p2, *p3;
+ void *e = talloc_new(NULL);
+
+ p1 = talloc_pool(NULL, 1024);
+ torture_assert("talloc_pool", p1 != NULL, "failed");
+
+ p2 = talloc_pool(p1, 500);
+ torture_assert("talloc_pool", p2 != NULL, "failed");
+
+ p3 = talloc_size(p2, 10);
+
+ talloc_steal(e, p3);
+
+ talloc_free(p2);
+
+ talloc_free(p3);
+
+ talloc_free(p1);
+
+ return true;
+}
+
static bool test_free_ref_null_context(void)
{
void *p1, *p2, *p3;
setlinebuf(stdout);
+ test_reset();
+ ret &= test_pool_nest();
test_reset();
ret &= test_ref1();
test_reset();