1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/check.c>
7 #include <ccan/tap/tap.h>
11 static tdb_len_t free_record_length(struct tdb_context *tdb, tdb_off_t off)
13 struct tdb_free_record f;
15 if (tdb_read_convert(tdb, off, &f, sizeof(f)) != 0)
17 if (frec_magic(&f) != TDB_FREE_MAGIC)
22 int main(int argc, char *argv[])
24 tdb_off_t b_off, zone_off;
25 struct tdb_context *tdb;
26 struct tdb_layout *layout;
27 struct tdb_data data, key;
29 unsigned int zone_bits = 16;
31 /* FIXME: Test TDB_CONVERT */
34 data.dptr = (void *)"world";
36 key.dptr = (void *)"hello";
39 /* No coalescing can be done due to EOF */
40 layout = new_tdb_layout(NULL);
41 tdb_layout_add_zone(layout, zone_bits, false);
43 tdb_layout_add_free(layout, len);
44 tdb = tdb_layout_get(layout);
45 zone_off = layout->elem[0].base.off;
46 ok1(tdb_check(tdb, NULL, NULL) == 0);
47 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
49 /* Figure out which bucket free entry is. */
50 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
51 /* Lock and fail to coalesce. */
52 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
53 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
55 tdb_unlock_free_bucket(tdb, b_off);
56 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
57 ok1(tdb_check(tdb, NULL, NULL) == 0);
60 /* No coalescing can be done due to used record */
61 layout = new_tdb_layout(NULL);
62 tdb_layout_add_zone(layout, zone_bits, false);
63 tdb_layout_add_free(layout, 1024);
64 tdb_layout_add_used(layout, key, data, 6);
65 tdb = tdb_layout_get(layout);
66 zone_off = layout->elem[0].base.off;
67 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
68 ok1(tdb_check(tdb, NULL, NULL) == 0);
70 /* Figure out which bucket free entry is. */
71 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
72 /* Lock and fail to coalesce. */
73 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
74 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
76 tdb_unlock_free_bucket(tdb, b_off);
77 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
78 ok1(tdb_check(tdb, NULL, NULL) == 0);
81 /* Coalescing can be done due to two free records, then EOF */
82 layout = new_tdb_layout(NULL);
83 tdb_layout_add_zone(layout, zone_bits, false);
84 tdb_layout_add_free(layout, 1024);
85 tdb_layout_add_free(layout, 2048);
86 tdb = tdb_layout_get(layout);
87 zone_off = layout->elem[0].base.off;
88 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
89 ok1(free_record_length(tdb, layout->elem[2].base.off) == 2048);
90 ok1(tdb_check(tdb, NULL, NULL) == 0);
92 /* Figure out which bucket (first) free entry is. */
93 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
94 /* Lock and coalesce. */
95 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
96 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
98 ok1(!tdb_has_locks(tdb));
99 ok1(free_record_length(tdb, layout->elem[1].base.off)
100 == 1024 + sizeof(struct tdb_used_record) + 2048);
101 ok1(tdb_check(tdb, NULL, NULL) == 0);
104 /* Coalescing can be done due to two free records, then data */
105 layout = new_tdb_layout(NULL);
106 tdb_layout_add_zone(layout, zone_bits, false);
107 tdb_layout_add_free(layout, 1024);
108 tdb_layout_add_free(layout, 512);
109 tdb_layout_add_used(layout, key, data, 6);
110 tdb = tdb_layout_get(layout);
111 zone_off = layout->elem[0].base.off;
112 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
113 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
114 ok1(tdb_check(tdb, NULL, NULL) == 0);
116 /* Figure out which bucket free entry is. */
117 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
118 /* Lock and coalesce. */
119 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
120 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
122 ok1(!tdb_has_locks(tdb));
123 ok1(free_record_length(tdb, layout->elem[1].base.off)
124 == 1024 + sizeof(struct tdb_used_record) + 512);
125 ok1(tdb_check(tdb, NULL, NULL) == 0);
128 /* Coalescing can be done due to three free records, then EOF */
129 layout = new_tdb_layout(NULL);
130 tdb_layout_add_zone(layout, zone_bits, false);
131 tdb_layout_add_free(layout, 1024);
132 tdb_layout_add_free(layout, 512);
133 tdb_layout_add_free(layout, 256);
134 tdb = tdb_layout_get(layout);
135 zone_off = layout->elem[0].base.off;
136 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
137 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
138 ok1(free_record_length(tdb, layout->elem[3].base.off) == 256);
139 ok1(tdb_check(tdb, NULL, NULL) == 0);
141 /* Figure out which bucket free entry is. */
142 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
143 /* Lock and coalesce. */
144 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
145 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
147 ok1(!tdb_has_locks(tdb));
148 ok1(free_record_length(tdb, layout->elem[1].base.off)
149 == 1024 + sizeof(struct tdb_used_record) + 512
150 + sizeof(struct tdb_used_record) + 256);
151 ok1(tdb_check(tdb, NULL, NULL) == 0);
154 /* Coalescing across two zones isn't possible. */
155 layout = new_tdb_layout(NULL);
156 tdb_layout_add_zone(layout, zone_bits, false);
157 tdb_layout_add_zone(layout, zone_bits, true);
158 tdb = tdb_layout_get(layout);
159 zone_off = layout->elem[0].base.off;
160 len = layout->elem[1].free.len;
161 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
162 ok1(tdb_check(tdb, NULL, NULL) == 0);
164 /* Figure out which list free entry is. */
165 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
166 /* Lock and coalesce. */
167 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
168 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
170 tdb_unlock_free_bucket(tdb, b_off);
171 ok1(!tdb_has_locks(tdb));
172 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
173 ok1(tdb_check(tdb, NULL, NULL) == 0);
176 ok1(tap_log_messages == 0);
177 return exit_status();