talloc: Add a separate pool size
authorVolker Lendecke <vl@samba.org>
Fri, 6 Sep 2013 21:20:20 +0000 (14:20 -0700)
committerStefan Metzmacher <metze@samba.org>
Tue, 10 Mar 2015 09:55:38 +0000 (10:55 +0100)
This is necessary to allow talloc pools to be objects on their own. It
is an incompatible change in the sense that talloc_get_size(pool) now
returns 0 instead of the pool size. When the talloc_pooled_object()
call is added, this will start to make sense again.

Maybe we should add a talloc_pool_size call? Or is that overkill?

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
(cherry picked from commit a3d9099d9a96b36df21ee0733adc5210438fe9dc)

lib/talloc/talloc.c

index a553050e6eb48a3669f6beddc4759a564022a892..5d13567e6d8abc91e3a5f837f8df9d0f04ad26cf 100644 (file)
@@ -461,6 +461,7 @@ _PUBLIC_ const char *talloc_parent_name(const void *ptr)
 struct talloc_pool_hdr {
        void *end;
        unsigned int object_count;
+       size_t poolsize;
 };
 
 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
@@ -478,7 +479,7 @@ static struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
 static void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
 {
        struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
-       return (char *)tc + TC_HDR_SIZE + tc->size;
+       return (char *)tc + TC_HDR_SIZE + pool_hdr->poolsize;
 }
 
 static size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
@@ -486,17 +487,18 @@ static size_t tc_pool_space_left(struct talloc_pool_hdr *pool_hdr)
        return (char *)tc_pool_end(pool_hdr) - (char *)pool_hdr->end;
 }
 
-static void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
-{
-       return TC_PTR_FROM_CHUNK(talloc_chunk_from_pool(pool_hdr));
-}
-
 /* If tc is inside a pool, this gives the next neighbour. */
 static void *tc_next_chunk(struct talloc_chunk *tc)
 {
        return (char *)tc + TC_ALIGN16(TC_HDR_SIZE + tc->size);
 }
 
+static void *tc_pool_first_chunk(struct talloc_pool_hdr *pool_hdr)
+{
+       struct talloc_chunk *tc = talloc_chunk_from_pool(pool_hdr);
+       return tc_next_chunk(tc);
+}
+
 /* Mark the whole remaining pool as not accessable */
 static void tc_invalidate_pool(struct talloc_pool_hdr *pool_hdr)
 {
@@ -678,9 +680,11 @@ _PUBLIC_ void *talloc_pool(const void *context, size_t size)
                return NULL;
        }
        tc->flags |= TALLOC_FLAG_POOL;
+       tc->size = 0;
 
        pool_hdr->object_count = 1;
        pool_hdr->end = result;
+       pool_hdr->poolsize = size;
 
        tc_invalidate_pool(pool_hdr);
 
@@ -1847,13 +1851,20 @@ static size_t _talloc_total_mem_internal(const void *ptr,
                         * pool itself.
                         */
                        if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
-                               total = tc->size + TC_HDR_SIZE;
-                               /*
-                                * If this is a pool, remember to
-                                * add the prefix length.
-                                */
                                if (tc->flags & TALLOC_FLAG_POOL) {
-                                       total += TP_HDR_SIZE;
+                                       /*
+                                        * If this is a pool, the allocated
+                                        * size is in the pool header, and
+                                        * remember to add in the prefix
+                                        * length.
+                                        */
+                                       struct talloc_pool_hdr *pool_hdr
+                                                       = talloc_pool_from_chunk(tc);
+                                       total = pool_hdr->poolsize +
+                                                       TC_HDR_SIZE +
+                                                       TP_HDR_SIZE;
+                               } else {
+                                       total = tc->size + TC_HDR_SIZE;
                                }
                        }
                }