]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: talloc: Remove the ALWAYS_REALLOC code paths. talloc-2.3.2
authorJeremy Allison <jra@samba.org>
Mon, 9 Nov 2020 19:50:09 +0000 (11:50 -0800)
committerJeremy Allison <jra@samba.org>
Tue, 10 Nov 2020 19:49:33 +0000 (19:49 +0000)
This is now never set, and also never tested, and only makes
the talloc code more complicated.

Once this is gone we can start looking at the memlimit
stuff.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Alexander Bokovoy <ab@samba.org>
lib/talloc/talloc.c
lib/talloc/testsuite.c

index ef49429307a1940cf134752301d5da754c0358f2..29da190880ab05eec91b8461d2501502985243cf 100644 (file)
 #include <valgrind.h>
 #endif
 
-/* use this to force every realloc to change the pointer, to stress test
-   code that might not cope */
-#define ALWAYS_REALLOC 0
-
-
 #define MAX_TALLOC_SIZE 0x10000000
 
 #define TALLOC_FLAG_FREE 0x01
@@ -1844,7 +1839,6 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                pool_hdr = tc->pool;
        }
 
-#if (ALWAYS_REALLOC == 0)
        /* don't shrink if we have less than 1k to gain */
        if (size < tc->size && tc->limit == NULL) {
                if (pool_hdr) {
@@ -1878,7 +1872,6 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                 */
                return ptr;
        }
-#endif
 
        /*
         * by resetting magic we catch users of the old memory
@@ -1897,66 +1890,6 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
         */
        _talloc_chunk_set_free(tc, NULL);
 
-#if (ALWAYS_REALLOC != 0)
-       if (pool_hdr) {
-               new_ptr = tc_alloc_pool(tc, size + TC_HDR_SIZE, 0);
-               if (new_ptr == NULL) {
-                       /*
-                        * Couldn't allocate from pool (pool size
-                        * counts as already allocated for memlimit
-                        * purposes). We must check memory limit
-                        * before any real malloc.
-                        */
-                       if (tc->limit) {
-                               /*
-                                * Note we're doing an extra malloc,
-                                * on top of the pool size, so account
-                                * for size only, not the difference
-                                * between old and new size.
-                                */
-                               if (!talloc_memlimit_check(tc->limit, size)) {
-                                       _talloc_chunk_set_not_free(tc);
-                                       errno = ENOMEM;
-                                       return NULL;
-                               }
-                       }
-                       new_ptr = malloc(TC_HDR_SIZE+size);
-                       malloced = true;
-                       new_size = size;
-               }
-
-               if (new_ptr) {
-                       memcpy(new_ptr, tc, MIN(tc->size,size) + TC_HDR_SIZE);
-                       TC_INVALIDATE_FULL_CHUNK(tc);
-                       /*
-                        * Only decrement the object count in the pool once
-                        * we know we're returning a valid new_ptr.
-                        */
-                       pool_hdr->object_count--;
-               }
-       } else {
-               /* We're doing malloc then free here, so record the difference. */
-               old_size = tc->size;
-               new_size = size;
-               /*
-                * We must check memory limit
-                * before any real malloc.
-                */
-               if (tc->limit && (size > old_size)) {
-                       if (!talloc_memlimit_check(tc->limit,
-                                       (size - old_size))) {
-                               _talloc_chunk_set_not_free(tc);
-                               errno = ENOMEM;
-                               return NULL;
-                       }
-               }
-               new_ptr = malloc(size + TC_HDR_SIZE);
-               if (new_ptr) {
-                       memcpy(new_ptr, tc, MIN(tc->size, size) + TC_HDR_SIZE);
-                       free(tc);
-               }
-       }
-#else
        if (pool_hdr) {
                struct talloc_chunk *pool_tc;
                void *next_tc = tc_next_chunk(tc);
@@ -2102,7 +2035,7 @@ _PUBLIC_ void *_talloc_realloc(const void *context, void *ptr, size_t size, cons
                new_ptr = realloc(tc, size + TC_HDR_SIZE);
        }
 got_new_ptr:
-#endif
+
        if (unlikely(!new_ptr)) {
                /*
                 * Ok, this is a strange spot.  We have to put back
index 6f23ad4e18a3de62647119b095646a29930f83cb..282ebc6956db29b2a23cefac22515f4a7c44fdef 100644 (file)
@@ -1307,7 +1307,6 @@ static bool test_pool(void)
        p4 = talloc_size(p3, 1000);
        memset(p4, 0x11, talloc_get_size(p4));
 
-#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
        p2_2 = talloc_realloc_size(pool, p2, 20+1);
        torture_assert("pool realloc 20+1", p2_2 == p2, "failed: pointer changed");
        memset(p2, 0x11, talloc_get_size(p2));
@@ -1372,8 +1371,6 @@ static bool test_pool(void)
        torture_assert("pool alloc 800", p3 == p1, "failed: pointer changed");
        memset(p3, 0x11, talloc_get_size(p3));
 
-#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
-
        talloc_free(pool);
 
        return true;
@@ -1408,7 +1405,6 @@ static bool test_pool_steal(void)
 
        p1_2 = p1;
 
-#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
        p1_2 = talloc_realloc_size(root, p1, 5 * 16);
        torture_assert("pool realloc 5 * 16", p1_2 > p2, "failed: pointer not changed");
        memset(p1_2, 0x11, talloc_get_size(p1_2));
@@ -1420,13 +1416,11 @@ static bool test_pool_steal(void)
        p2_2 = talloc_realloc_size(root, p2, 3 * 16);
        torture_assert("pool realloc 5 * 16", p2_2 == p2, "failed: pointer changed");
        memset(p2_2, 0x11, talloc_get_size(p2_2));
-#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
 
        talloc_free(p1_2);
 
        p2_2 = p2;
 
-#if 1 /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
        /* now we should reclaim the full pool */
        p2_2 = talloc_realloc_size(root, p2, 8 * 16);
        torture_assert("pool realloc 8 * 16", p2_2 == p1, "failed: pointer not expected");
@@ -1438,8 +1432,6 @@ static bool test_pool_steal(void)
        torture_assert("pool realloc 2 * 1024", p2_2 != p1, "failed: pointer not expected");
        memset(p2_2, 0x11, talloc_get_size(p2_2));
 
-#endif /* this relies on ALWAYS_REALLOC == 0 in talloc.c */
-
        talloc_free(p2_2);
 
        talloc_free(root);