]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'rs/mem-pool-improvements'
authorJunio C Hamano <gitster@pobox.com>
Mon, 8 Jan 2024 22:05:16 +0000 (14:05 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Jan 2024 22:05:17 +0000 (14:05 -0800)
MemPool allocator fixes.

* rs/mem-pool-improvements:
  mem-pool: simplify alignment calculation
  mem-pool: fix big allocations

Makefile
mem-pool.c
t/unit-tests/t-mem-pool.c [new file with mode: 0644]

index 88ba7a3c514c4fc5b98243683b0847346ffac33b..15990ff3122eb61676e4bc39477804e77d04367b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1340,6 +1340,7 @@ THIRD_PARTY_SOURCES += sha1collisiondetection/%
 THIRD_PARTY_SOURCES += sha1dc/%
 
 UNIT_TEST_PROGRAMS += t-basic
+UNIT_TEST_PROGRAMS += t-mem-pool
 UNIT_TEST_PROGRAMS += t-strbuf
 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
 UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
index c34846d176c886ecb9f028ec48bb847402d242f9..c7d62560201984db8efcb675da9b4ede933c5cbd 100644 (file)
@@ -89,9 +89,7 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
        struct mp_block *p = NULL;
        void *r;
 
-       /* round up to a 'GIT_MAX_ALIGNMENT' alignment */
-       if (len & (GIT_MAX_ALIGNMENT - 1))
-               len += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1));
+       len = DIV_ROUND_UP(len, GIT_MAX_ALIGNMENT) * GIT_MAX_ALIGNMENT;
 
        if (pool->mp_block &&
            pool->mp_block->end - pool->mp_block->next_free >= len)
@@ -99,9 +97,9 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
 
        if (!p) {
                if (len >= (pool->block_alloc / 2))
-                       return mem_pool_alloc_block(pool, len, pool->mp_block);
-
-               p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
+                       p = mem_pool_alloc_block(pool, len, pool->mp_block);
+               else
+                       p = mem_pool_alloc_block(pool, pool->block_alloc, NULL);
        }
 
        r = p->next_free;
diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c
new file mode 100644 (file)
index 0000000..a0d57df
--- /dev/null
@@ -0,0 +1,31 @@
+#include "test-lib.h"
+#include "mem-pool.h"
+
+static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc)
+{
+       struct mem_pool pool = { .block_alloc = block_alloc };
+       f(&pool);
+       mem_pool_discard(&pool, 0);
+}
+
+static void t_calloc_100(struct mem_pool *pool)
+{
+       size_t size = 100;
+       char *buffer = mem_pool_calloc(pool, 1, size);
+       for (size_t i = 0; i < size; i++)
+               check_int(buffer[i], ==, 0);
+       if (!check(pool->mp_block != NULL))
+               return;
+       check(pool->mp_block->next_free != NULL);
+       check(pool->mp_block->end != NULL);
+}
+
+int cmd_main(int argc, const char **argv)
+{
+       TEST(setup_static(t_calloc_100, 1024 * 1024),
+            "mem_pool_calloc returns 100 zeroed bytes with big block");
+       TEST(setup_static(t_calloc_100, 1),
+            "mem_pool_calloc returns 100 zeroed bytes with tiny block");
+
+       return test_done();
+}