@matheusbsbs / matheus-bagliano / commits / 4709455db3
Fork 0

Add memory pool library

Add a memory pool library implemented using C macros. The obj_pool_gen() macro creates a type-specific memory pool. The memory pool library is distinguished from the existing specialized allocators in alloc.c by using a contiguous block for all allocations. This means that on one hand, long-lived pointers have to be written as offsets, since the base address changes as the pool grows, but on the other hand, the entire pool can be easily written to the file system. This could allow the memory pool to persist between runs of an application. For the svn importer, such a facility is useful because each svn revision can copy trees and files from any previous revision. The relevant information for all revisions has to persist somehow to support incremental runs. [rr: minor cleanups] [jn: added tests; removed file system backing for now] Signed-off-by: David Barr <david.barr@cordelta.com> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Barr committed Aug 9, 2010 at 17:11 UTC 4709455db3891f6cad9a96a574296b4926f70cbe
5 files changed +260 -1
.gitignore
+1
@@ -167,6 +167,7 @@
167 /test-genrandom
168 /test-index-version
169 /test-match-trees
170 +/test-obj-pool
171 /test-parse-options
172 /test-path-utils
173 /test-run-command
Makefile
+3 -1
@@ -409,6 +409,7 @@ TEST_PROGRAMS_NEED_X += test-delta
409 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
410 TEST_PROGRAMS_NEED_X += test-genrandom
411 TEST_PROGRAMS_NEED_X += test-match-trees
412 +TEST_PROGRAMS_NEED_X += test-obj-pool
413 TEST_PROGRAMS_NEED_X += test-parse-options
414 TEST_PROGRAMS_NEED_X += test-path-utils
415 TEST_PROGRAMS_NEED_X += test-run-command
@@ -1864,7 +1865,8 @@ xdiff-interface.o $(XDIFF_OBJS): \
1865 xdiff/xinclude.h xdiff/xmacros.h xdiff/xdiff.h xdiff/xtypes.h \
1866 xdiff/xutils.h xdiff/xprepare.h xdiff/xdiffi.h xdiff/xemit.h
1867
1867 -$(VCSSVN_OBJS):
1868 +$(VCSSVN_OBJS): \
1869 + vcs-svn/obj_pool.h
1870 endif
1871
1872 exec_cmd.s exec_cmd.o: EXTRA_CPPFLAGS = \
t/t0080-vcs-svn.sh new
+79
@@ -0,0 +1,79 @@
1 +#!/bin/sh
2 +
3 +test_description='check infrastructure for svn importer'
4 +
5 +. ./test-lib.sh
6 +uint32_max=4294967295
7 +
8 +test_expect_success 'obj pool: store data' '
9 + cat <<-\EOF >expected &&
10 + 0
11 + 1
12 + EOF
13 +
14 + test-obj-pool <<-\EOF >actual &&
15 + alloc one 16
16 + set one 13
17 + test one 13
18 + reset one
19 + EOF
20 + test_cmp expected actual
21 +'
22 +
23 +test_expect_success 'obj pool: NULL is offset ~0' '
24 + echo "$uint32_max" >expected &&
25 + echo null one | test-obj-pool >actual &&
26 + test_cmp expected actual
27 +'
28 +
29 +test_expect_success 'obj pool: out-of-bounds access' '
30 + cat <<-EOF >expected &&
31 + 0
32 + 0
33 + $uint32_max
34 + $uint32_max
35 + 16
36 + 20
37 + $uint32_max
38 + EOF
39 +
40 + test-obj-pool <<-\EOF >actual &&
41 + alloc one 16
42 + alloc two 16
43 + offset one 20
44 + offset two 20
45 + alloc one 5
46 + offset one 20
47 + free one 1
48 + offset one 20
49 + reset one
50 + reset two
51 + EOF
52 + test_cmp expected actual
53 +'
54 +
55 +test_expect_success 'obj pool: high-water mark' '
56 + cat <<-\EOF >expected &&
57 + 0
58 + 0
59 + 10
60 + 20
61 + 20
62 + 20
63 + EOF
64 +
65 + test-obj-pool <<-\EOF >actual &&
66 + alloc one 10
67 + committed one
68 + alloc one 10
69 + commit one
70 + committed one
71 + alloc one 10
72 + free one 20
73 + committed one
74 + reset one
75 + EOF
76 + test_cmp expected actual
77 +'
78 +
79 +test_done
test-obj-pool.c new
+116
@@ -0,0 +1,116 @@
1 +/*
2 + * test-obj-pool.c: code to exercise the svn importer's object pool
3 + */
4 +
5 +#include "cache.h"
6 +#include "vcs-svn/obj_pool.h"
7 +
8 +enum pool { POOL_ONE, POOL_TWO };
9 +obj_pool_gen(one, int, 1)
10 +obj_pool_gen(two, int, 4096)
11 +
12 +static uint32_t strtouint32(const char *s)
13 +{
14 + char *end;
15 + uintmax_t n = strtoumax(s, &end, 10);
16 + if (*s == '\0' || (*end != '\n' && *end != '\0'))
17 + die("invalid offset: %s", s);
18 + return (uint32_t) n;
19 +}
20 +
21 +static void handle_command(const char *command, enum pool pool, const char *arg)
22 +{
23 + switch (*command) {
24 + case 'a':
25 + if (!prefixcmp(command, "alloc ")) {
26 + uint32_t n = strtouint32(arg);
27 + printf("%"PRIu32"\n",
28 + pool == POOL_ONE ?
29 + one_alloc(n) : two_alloc(n));
30 + return;
31 + }
32 + case 'c':
33 + if (!prefixcmp(command, "commit ")) {
34 + pool == POOL_ONE ? one_commit() : two_commit();
35 + return;
36 + }
37 + if (!prefixcmp(command, "committed ")) {
38 + printf("%"PRIu32"\n",
39 + pool == POOL_ONE ?
40 + one_pool.committed : two_pool.committed);
41 + return;
42 + }
43 + case 'f':
44 + if (!prefixcmp(command, "free ")) {
45 + uint32_t n = strtouint32(arg);
46 + pool == POOL_ONE ? one_free(n) : two_free(n);
47 + return;
48 + }
49 + case 'n':
50 + if (!prefixcmp(command, "null ")) {
51 + printf("%"PRIu32"\n",
52 + pool == POOL_ONE ?
53 + one_offset(NULL) : two_offset(NULL));
54 + return;
55 + }
56 + case 'o':
57 + if (!prefixcmp(command, "offset ")) {
58 + uint32_t n = strtouint32(arg);
59 + printf("%"PRIu32"\n",
60 + pool == POOL_ONE ?
61 + one_offset(one_pointer(n)) :
62 + two_offset(two_pointer(n)));
63 + return;
64 + }
65 + case 'r':
66 + if (!prefixcmp(command, "reset ")) {
67 + pool == POOL_ONE ? one_reset() : two_reset();
68 + return;
69 + }
70 + case 's':
71 + if (!prefixcmp(command, "set ")) {
72 + uint32_t n = strtouint32(arg);
73 + if (pool == POOL_ONE)
74 + *one_pointer(n) = 1;
75 + else
76 + *two_pointer(n) = 1;
77 + return;
78 + }
79 + case 't':
80 + if (!prefixcmp(command, "test ")) {
81 + uint32_t n = strtouint32(arg);
82 + printf("%d\n", pool == POOL_ONE ?
83 + *one_pointer(n) : *two_pointer(n));
84 + return;
85 + }
86 + default:
87 + die("unrecognized command: %s", command);
88 + }
89 +}
90 +
91 +static void handle_line(const char *line)
92 +{
93 + const char *arg = strchr(line, ' ');
94 + enum pool pool;
95 +
96 + if (arg && !prefixcmp(arg + 1, "one"))
97 + pool = POOL_ONE;
98 + else if (arg && !prefixcmp(arg + 1, "two"))
99 + pool = POOL_TWO;
100 + else
101 + die("no pool specified: %s", line);
102 +
103 + handle_command(line, pool, arg + strlen("one "));
104 +}
105 +
106 +int main(int argc, char *argv[])
107 +{
108 + struct strbuf sb = STRBUF_INIT;
109 + if (argc != 1)
110 + usage("test-obj-str < script");
111 +
112 + while (strbuf_getline(&sb, stdin, '\n') != EOF)
113 + handle_line(sb.buf);
114 + strbuf_release(&sb);
115 + return 0;
116 +}
vcs-svn/obj_pool.h new
+61
@@ -0,0 +1,61 @@
1 +/*
2 + * Licensed under a two-clause BSD-style license.
3 + * See LICENSE for details.
4 + */
5 +
6 +#ifndef OBJ_POOL_H_
7 +#define OBJ_POOL_H_
8 +
9 +#include "git-compat-util.h"
10 +
11 +#define MAYBE_UNUSED __attribute__((__unused__))
12 +
13 +#define obj_pool_gen(pre, obj_t, initial_capacity) \
14 +static struct { \
15 + uint32_t committed; \
16 + uint32_t size; \
17 + uint32_t capacity; \
18 + obj_t *base; \
19 +} pre##_pool = {0, 0, 0, NULL}; \
20 +static MAYBE_UNUSED uint32_t pre##_alloc(uint32_t count) \
21 +{ \
22 + uint32_t offset; \
23 + if (pre##_pool.size + count > pre##_pool.capacity) { \
24 + while (pre##_pool.size + count > pre##_pool.capacity) \
25 + if (pre##_pool.capacity) \
26 + pre##_pool.capacity *= 2; \
27 + else \
28 + pre##_pool.capacity = initial_capacity; \
29 + pre##_pool.base = realloc(pre##_pool.base, \
30 + pre##_pool.capacity * sizeof(obj_t)); \
31 + } \
32 + offset = pre##_pool.size; \
33 + pre##_pool.size += count; \
34 + return offset; \
35 +} \
36 +static MAYBE_UNUSED void pre##_free(uint32_t count) \
37 +{ \
38 + pre##_pool.size -= count; \
39 +} \
40 +static MAYBE_UNUSED uint32_t pre##_offset(obj_t *obj) \
41 +{ \
42 + return obj == NULL ? ~0 : obj - pre##_pool.base; \
43 +} \
44 +static MAYBE_UNUSED obj_t *pre##_pointer(uint32_t offset) \
45 +{ \
46 + return offset >= pre##_pool.size ? NULL : &pre##_pool.base[offset]; \
47 +} \
48 +static MAYBE_UNUSED void pre##_commit(void) \
49 +{ \
50 + pre##_pool.committed = pre##_pool.size; \
51 +} \
52 +static MAYBE_UNUSED void pre##_reset(void) \
53 +{ \
54 + free(pre##_pool.base); \
55 + pre##_pool.base = NULL; \
56 + pre##_pool.size = 0; \
57 + pre##_pool.capacity = 0; \
58 + pre##_pool.committed = 0; \
59 +}
60 +
61 +#endif