3 #include "../private.h"
4 #include "tap-interface.h"
5 #include <ccan/hash/hash.h>
9 #include "helpapi-external-agent.h"
11 static const struct ntdb_context *curr_ntdb;
12 static const struct ntdb_file *curr_file;
14 static int owner_null_count,
15 owner_weird_count, alloc_count, free_count, expand_count;
17 static void *test_alloc(const void *owner, size_t len, void *priv_data)
23 } else if (owner != curr_ntdb && owner != curr_file) {
30 /* The first time, this is the current ntdb, next is
31 * for the file struct. */
35 } else if (!curr_file) {
39 assert(priv_data == &owner_weird_count);
43 static void *test_expand(void *old, size_t newlen, void *priv_data)
47 assert(priv_data == &owner_weird_count);
48 return realloc(old, newlen);
51 static void test_free(void *old, void *priv_data)
53 assert(priv_data == &owner_weird_count);
60 int main(int argc, char *argv[])
63 union ntdb_attribute alloc_attr;
64 struct ntdb_context *ntdb;
65 int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
66 NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
67 NTDB_NOMMAP|NTDB_CONVERT };
68 NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
69 NTDB_DATA data = { (unsigned char *)&j, sizeof(j) };
71 alloc_attr.base.next = &tap_log_attr;
72 alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
74 alloc_attr.alloc.alloc = test_alloc;
75 alloc_attr.alloc.expand = test_expand;
76 alloc_attr.alloc.free = test_free;
77 alloc_attr.alloc.priv_data = &owner_weird_count;
79 plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 700 * 3 + 4) + 1);
81 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
84 ntdb = ntdb_open("run-20-alloc-attr.ntdb", flags[i]|MAYBE_NOSYNC,
85 O_RDWR|O_CREAT|O_TRUNC, 0600, &alloc_attr);
90 for (j = 0; j < 700; j++) {
91 NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
92 ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
93 ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
94 ok1(ntdb_deq(d, data));
95 test_free(d.dptr, &owner_weird_count);
99 ok1(owner_null_count == 2+i*2);
100 ok1(owner_weird_count == 0);
101 ok1(alloc_count == free_count);
102 ok1(expand_count != 0);
105 ok1(tap_log_messages == 0);
106 return exit_status();