Change _talloc_total_mem_internal() to ignore memory allocated from a pool when calcu...
[sfrench/samba-autobuild/.git] / lib / talloc / talloc.c
index afc44b338fa6e833e24145bb2c33945817a32ab0..1e25dfde4e1e998872276ef3acb1eec80606cb25 100644 (file)
@@ -1,4 +1,4 @@
-/* 
+/*
    Samba Unix SMB/CIFS implementation.
 
    Samba trivial allocation library - new interface
@@ -7,11 +7,11 @@
 
    Copyright (C) Andrew Tridgell 2004
    Copyright (C) Stefan Metzmacher 2006
-   
+
      ** NOTE! The following LGPL license applies to the talloc
      ** library. This does NOT imply that all of Samba is released
      ** under the LGPL
-   
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
@@ -74,7 +74,7 @@
 
 #define TALLOC_MAGIC_REFERENCE ((const char *)1)
 
-/* by default we abort when given a bad pointer (such as when talloc_free() is called 
+/* by default we abort when given a bad pointer (such as when talloc_free() is called
    on a pointer that came from malloc() */
 #ifndef TALLOC_ABORT
 #define TALLOC_ABORT(reason) abort()
@@ -222,37 +222,6 @@ static struct {
        TC_UNDEFINE_GROW_VALGRIND_CHUNK(_tc, _new_size); \
 } while (0)
 
-#define TALLOC_MEMLIMIT_CHECK(limit, size) do { \
-       struct talloc_memlimit *l; \
-       for (l = limit; l != NULL; l = l->upper) { \
-               if (l->max_size != 0 && \
-                   ((l->max_size <= l->cur_size) || \
-                    (l->max_size - l->cur_size < TC_HDR_SIZE+size))) { \
-                       errno = ENOMEM; \
-                       return NULL; \
-               } \
-       } \
-} while(0)
-
-#define TALLOC_MEMLIMIT_UPDATE(limit, o_size, n_size) do { \
-       struct talloc_memlimit *l; \
-       ssize_t d; \
-       if (o_size == 0) { \
-               d = n_size + TC_HDR_SIZE; \
-       } else { \
-               d = n_size - o_size; \
-       } \
-       for (l = limit; l != NULL; l = l->upper) { \
-               ssize_t new_size = l->cur_size + d; \
-               if (new_size < 0) { \
-                       talloc_abort("cur_size memlimit counter not correct!"); \
-                       errno = EINVAL; \
-                       return NULL; \
-               } \
-               l->cur_size = new_size; \
-       } \
-} while(0)
-
 struct talloc_reference_handle {
        struct talloc_reference_handle *next, *prev;
        void *ptr;
@@ -266,6 +235,15 @@ struct talloc_memlimit {
        size_t cur_size;
 };
 
+static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size);
+static bool talloc_memlimit_update(struct talloc_memlimit *limit,
+                                  size_t old_size, size_t new_size);
+static void talloc_memlimit_grow(struct talloc_memlimit *limit,
+                               size_t size);
+static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
+                               size_t size);
+static void talloc_memlimit_update_on_free(struct talloc_chunk *tc);
+
 typedef int (*talloc_destructor_t)(void *);
 
 struct talloc_chunk {
@@ -282,7 +260,7 @@ struct talloc_chunk {
         * if 'limit' is set it means all *new* children of the context will
         * be limited to a total aggregate size ox max_size for memory
         * allocations.
-        * cur_size is used to kep track of the current use
+        * cur_size is used to keep track of the current use
         */
        struct talloc_memlimit *limit;
 
@@ -394,7 +372,7 @@ static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
 {
        const char *pp = (const char *)ptr;
        struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
-       if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) { 
+       if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
                if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
                        talloc_abort_magic(tc->flags & (~0xF));
                        return NULL;
@@ -585,7 +563,7 @@ static struct talloc_chunk *talloc_alloc_pool(struct talloc_chunk *parent,
        return result;
 }
 
-/* 
+/*
    Allocate a bit of memory as a child of an existing pointer
 */
 static inline void *__talloc(const void *context, size_t size)
@@ -608,7 +586,10 @@ static inline void *__talloc(const void *context, size_t size)
                        limit = ptc->limit;
                }
 
-               TALLOC_MEMLIMIT_CHECK(limit, (TC_HDR_SIZE+size));
+               if (!talloc_memlimit_check(limit, (TC_HDR_SIZE+size))) {
+                       errno = ENOMEM;
+                       return NULL;
+               }
 
                tc = talloc_alloc_pool(ptc, TC_HDR_SIZE+size);
        }
@@ -699,7 +680,7 @@ _PUBLIC_ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
 }
 
 /*
-  increase the reference count on a piece of memory. 
+  increase the reference count on a piece of memory.
 */
 _PUBLIC_ int talloc_increase_ref_count(const void *ptr)
 {
@@ -722,7 +703,7 @@ static int talloc_reference_destructor(struct talloc_reference_handle *handle)
 }
 
 /*
-   more efficient way to add a name to a pointer - the name must point to a 
+   more efficient way to add a name to a pointer - the name must point to a
    true string constant
 */
 static inline void _talloc_set_name_const(const void *ptr, const char *name)
@@ -752,8 +733,8 @@ static inline void *_talloc_named_const(const void *context, size_t size, const
   make a secondary reference to a pointer, hanging off the given context.
   the pointer remains valid until both the original caller and this given
   context are freed.
-  
-  the major use for this is when two different structures need to reference the 
+
+  the major use for this is when two different structures need to reference the
   same underlying data, and you want to be able to free the two instances separately,
   and in either order
 */
@@ -818,7 +799,10 @@ static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
                 */
                pool->hdr.c.pool = tc_pool_first_chunk(pool);
                tc_invalidate_pool(pool);
-       } else if (unlikely(pool->hdr.object_count == 0)) {
+               return;
+       }
+
+       if (unlikely(pool->hdr.object_count == 0)) {
                /*
                 * we mark the freed memory with where we called the free
                 * from. This means on a double free error we can report where
@@ -828,21 +812,30 @@ static inline void _talloc_free_poolmem(struct talloc_chunk *tc,
 
                TC_INVALIDATE_FULL_CHUNK(&pool->hdr.c);
                free(pool);
-       } else if (pool->hdr.c.pool == next_tc) {
+               return;
+       }
+
+       if (pool->hdr.c.pool == next_tc) {
                /*
                 * if pool->pool still points to end of
                 * 'tc' (which is stored in the 'next_tc' variable),
                 * we can reclaim the memory of 'tc'.
                 */
                pool->hdr.c.pool = tc;
+               return;
        }
+
+       /*
+        * Do nothing. The memory is just "wasted", waiting for the pool
+        * itself to be freed.
+        */
 }
 
 static inline void _talloc_free_children_internal(struct talloc_chunk *tc,
                                                  void *ptr,
                                                  const char *location);
 
-/* 
+/*
    internal talloc_free call
 */
 static inline int _talloc_free_internal(void *ptr, const char *location)
@@ -942,8 +935,8 @@ static inline int _talloc_free_internal(void *ptr, const char *location)
 
        /* we mark the freed memory with where we called the free
         * from. This means on a double free error we can report where
-        * the first free came from 
-        */      
+        * the first free came from
+        */
        tc->name = location;
 
        if (tc->flags & TALLOC_FLAG_POOL) {
@@ -955,16 +948,23 @@ static inline int _talloc_free_internal(void *ptr, const char *location)
                }
 
                pool->hdr.object_count--;
-               if (unlikely(pool->hdr.object_count == 0)) {
-                       TC_INVALIDATE_FULL_CHUNK(tc);
-                       free(tc);
+
+               if (likely(pool->hdr.object_count != 0)) {
+                       return 0;
                }
-       } else if (tc->flags & TALLOC_FLAG_POOLMEM) {
-               _talloc_free_poolmem(tc, location);
-       } else {
+
                TC_INVALIDATE_FULL_CHUNK(tc);
                free(tc);
+               return 0;
        }
+
+       if (tc->flags & TALLOC_FLAG_POOLMEM) {
+               _talloc_free_poolmem(tc, location);
+               return 0;
+       }
+
+       TC_INVALIDATE_FULL_CHUNK(tc);
+       free(tc);
        return 0;
 }
 
@@ -972,7 +972,7 @@ static size_t _talloc_total_limit_size(const void *ptr,
                                        struct talloc_memlimit *old_limit,
                                        struct talloc_memlimit *new_limit);
 
-/* 
+/*
    move a lump of memory from one talloc context to another return the
    ptr on success, or NULL if it could not be transferred.
    passing NULL as ptr will always return NULL with no side effects.
@@ -996,7 +996,11 @@ static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
 
                ctx_size = _talloc_total_limit_size(ptr, NULL, NULL);
 
-               TALLOC_MEMLIMIT_UPDATE(tc->limit->upper, ctx_size, 0);
+               if (!talloc_memlimit_update(tc->limit->upper, ctx_size, 0)) {
+                       talloc_abort("cur_size memlimit counter not correct!");
+                       errno = EINVAL;
+                       return NULL;
+               }
 
                if (tc->limit->parent == tc) {
                        tc->limit->upper = NULL;
@@ -1057,7 +1061,7 @@ static void *_talloc_steal_internal(const void *new_ctx, const void *ptr)
        return discard_const_p(void, ptr);
 }
 
-/* 
+/*
    move a lump of memory from one talloc context to another return the
    ptr on success, or NULL if it could not be transferred.
    passing NULL as ptr will always return NULL with no side effects.
@@ -1069,9 +1073,9 @@ _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const cha
        if (unlikely(ptr == NULL)) {
                return NULL;
        }
-       
+
        tc = talloc_chunk_from_ptr(ptr);
-       
+
        if (unlikely(tc->refs != NULL) && talloc_parent(ptr) != new_ctx) {
                struct talloc_reference_handle *h;
 
@@ -1091,11 +1095,11 @@ _PUBLIC_ void *_talloc_steal_loc(const void *new_ctx, const void *ptr, const cha
                talloc_log("WARNING: stealing into talloc child at %s\n", location);
        }
 #endif
-       
+
        return _talloc_steal_internal(new_ctx, ptr);
 }
 
-/* 
+/*
    this is like a talloc_steal(), but you must supply the old
    parent. This resolves the ambiguity in a talloc_steal() which is
    called on a context that has more than one parent (via references)
@@ -1123,7 +1127,7 @@ _PUBLIC_ void *talloc_reparent(const void *old_parent, const void *new_parent, c
                        }
                        return discard_const_p(void, ptr);
                }
-       }       
+       }
 
        /* it wasn't a parent */
        return NULL;
@@ -1423,7 +1427,7 @@ _PUBLIC_ void talloc_free_children(void *ptr)
        }
 }
 
-/* 
+/*
    Allocate a bit of memory as a child of an existing pointer
 */
 _PUBLIC_ void *_talloc(const void *context, size_t size)
@@ -1449,8 +1453,8 @@ _PUBLIC_ void *talloc_named_const(const void *context, size_t size, const char *
        return _talloc_named_const(context, size, name);
 }
 
-/* 
-   free a talloc pointer. This also frees all child pointers of this 
+/*
+   free a talloc pointer. This also frees all child pointers of this
    pointer recursively
 
    return 0 if the memory is actually freed, otherwise -1. The memory
@@ -1464,9 +1468,9 @@ _PUBLIC_ int _talloc_free(void *ptr, const char *location)
        if (unlikely(ptr == NULL)) {
                return -1;
        }
-       
+
        tc = talloc_chunk_from_ptr(ptr);
-       
+
        if (unlikely(tc->refs != NULL)) {
                struct talloc_reference_handle *h;
 
@@ -1486,7 +1490,7 @@ _PUBLIC_ int _talloc_free(void *ptr, const char *location)
                }
                return -1;
        }
-       
+
        return _talloc_free_internal(ptr, location);
 }
 
@@ -1531,7 +1535,10 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
        }
 
        if (tc->limit && (size - tc->size > 0)) {
-               TALLOC_MEMLIMIT_CHECK(tc->limit, (size - tc->size));
+               if (!talloc_memlimit_check(tc->limit, (size - tc->size))) {
+                       errno = ENOMEM;
+                       return NULL;
+               }
        }
 
        /* handle realloc inside a talloc_pool */
@@ -1649,7 +1656,14 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                if (new_chunk_size == old_chunk_size) {
                        TC_UNDEFINE_GROW_CHUNK(tc, size);
                        tc->flags &= ~TALLOC_FLAG_FREE;
-                       TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+                       if (!talloc_memlimit_update(tc->limit,
+                                                       tc->size, size)) {
+                               talloc_abort("cur_size memlimit counter not"
+                                            " correct!");
+                               errno = EINVAL;
+                               return NULL;
+                       }
+
                        tc->size = size;
                        return ptr;
                }
@@ -1665,7 +1679,13 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                        if (space_left >= space_needed) {
                                TC_UNDEFINE_GROW_CHUNK(tc, size);
                                tc->flags &= ~TALLOC_FLAG_FREE;
-                               TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+                               if (!talloc_memlimit_update(tc->limit,
+                                                       tc->size, size)) {
+                                       talloc_abort("cur_size memlimit "
+                                                    "counter not correct!");
+                                       errno = EINVAL;
+                                       return NULL;
+                               }
                                tc->size = size;
                                pool_tc->hdr.c.pool = tc_next_chunk(tc);
                                return ptr;
@@ -1690,9 +1710,9 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
        }
 got_new_ptr:
 #endif
-       if (unlikely(!new_ptr)) {       
-               tc->flags &= ~TALLOC_FLAG_FREE; 
-               return NULL; 
+       if (unlikely(!new_ptr)) {
+               tc->flags &= ~TALLOC_FLAG_FREE;
+               return NULL;
        }
 
        tc = (struct talloc_chunk *)new_ptr;
@@ -1714,7 +1734,11 @@ got_new_ptr:
                tc->next->prev = tc;
        }
 
-       TALLOC_MEMLIMIT_UPDATE(tc->limit, tc->size, size);
+       if (!talloc_memlimit_update(tc->limit, tc->size, size)) {
+               talloc_abort("cur_size memlimit counter not correct!");
+               errno = EINVAL;
+               return NULL;
+       }
        tc->size = size;
        _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
 
@@ -1793,7 +1817,14 @@ static size_t _talloc_total_mem_internal(const void *ptr,
                break;
        case TOTAL_MEM_LIMIT:
                if (likely(tc->name != TALLOC_MAGIC_REFERENCE)) {
-                       total = tc->size + TC_HDR_SIZE;
+                       /*
+                        * Don't count memory allocated from a pool
+                        * when calculating limits. Only count the
+                        * pool itself.
+                        */
+                       if (!(tc->flags & TALLOC_FLAG_POOLMEM)) {
+                               total = tc->size + TC_HDR_SIZE;
+                       }
                }
                break;
        }
@@ -1901,14 +1932,14 @@ static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_
        }
 
        if (depth == 0) {
-               fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n", 
+               fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
                        (max_depth < 0 ? "full " :""), name,
                        (unsigned long)talloc_total_size(ptr),
                        (unsigned long)talloc_total_blocks(ptr));
                return;
        }
 
-       fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n", 
+       fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
                depth*4, "",
                name,
                (unsigned long)talloc_total_size(ptr),
@@ -2047,8 +2078,8 @@ _PUBLIC_ void talloc_enable_leak_report_full(void)
        atexit(talloc_report_null_full);
 }
 
-/* 
-   talloc and zero memory. 
+/*
+   talloc and zero memory.
 */
 _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
 {
@@ -2062,7 +2093,7 @@ _PUBLIC_ void *_talloc_zero(const void *ctx, size_t size, const char *name)
 }
 
 /*
-  memdup with a talloc. 
+  memdup with a talloc.
 */
 _PUBLIC_ void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
 {
@@ -2530,6 +2561,110 @@ static size_t _talloc_total_limit_size(const void *ptr,
                                          old_limit, new_limit);
 }
 
+static bool talloc_memlimit_check(struct talloc_memlimit *limit, size_t size)
+{
+       struct talloc_memlimit *l;
+
+       for (l = limit; l != NULL; l = l->upper) {
+               if (l->max_size != 0 &&
+                   ((l->max_size <= l->cur_size) ||
+                    (l->max_size - l->cur_size < size))) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+/*
+  Update memory limits when freeing a talloc_chunk.
+*/
+static void talloc_memlimit_update_on_free(struct talloc_chunk *tc)
+{
+       if (!tc->limit) {
+               return;
+       }
+
+       /*
+        * Pool entries don't count. Only the pools
+        * themselves are counted as part of the memory
+        * limits.
+        */
+       if (tc->flags & TALLOC_FLAG_POOLMEM) {
+               return;
+       }
+
+       /*
+        * If we are part of a memory limited context hierarchy
+        * we need to subtract the memory used from the counters
+        */
+
+       talloc_memlimit_shrink(tc->limit, tc->size+TC_HDR_SIZE);
+
+       if (tc->limit->parent == tc) {
+               free(tc->limit);
+       }
+
+       tc->limit = NULL;
+}
+
+/*
+  Increase memory limit accounting after a malloc/realloc.
+*/
+static void talloc_memlimit_grow(struct talloc_memlimit *limit,
+                               size_t size)
+{
+       struct talloc_memlimit *l;
+
+       for (l = limit; l != NULL; l = l->upper) {
+               size_t new_cur_size = l->cur_size + size;
+               if (new_cur_size < l->cur_size) {
+                       talloc_abort("logic error in talloc_memlimit_grow\n");
+                       return;
+               }
+               l->cur_size = new_cur_size;
+       }
+}
+
+/*
+  Decrease memory limit accounting after a free/realloc.
+*/
+static void talloc_memlimit_shrink(struct talloc_memlimit *limit,
+                               size_t size)
+{
+       struct talloc_memlimit *l;
+
+       for (l = limit; l != NULL; l = l->upper) {
+               if (l->cur_size < size) {
+                       talloc_abort("logic error in talloc_memlimit_shrink\n");
+                       return;
+               }
+               l->cur_size = l->cur_size - size;
+       }
+}
+
+static bool talloc_memlimit_update(struct talloc_memlimit *limit,
+                                  size_t old_size, size_t new_size)
+{
+       struct talloc_memlimit *l;
+       ssize_t d;
+
+       if (old_size == 0) {
+               d = new_size + TC_HDR_SIZE;
+       } else {
+               d = new_size - old_size;
+       }
+       for (l = limit; l != NULL; l = l->upper) {
+               ssize_t new_cur_size = l->cur_size + d;
+               if (new_cur_size < 0) {
+                       return false;
+               }
+               l->cur_size = new_cur_size;
+       }
+
+       return true;
+}
+
 _PUBLIC_ int talloc_set_memlimit(const void *ctx, size_t max_size)
 {
        struct talloc_chunk *tc = talloc_chunk_from_ptr(ctx);