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();
41 tdb_layout_add_zone(layout, zone_bits, false);
42 tdb = tdb_layout_get(layout);
43 len = layout->elem[1].free.len;
44 zone_off = layout->elem[0].base.off;
45 ok1(tdb_check(tdb, NULL, NULL) == 0);
46 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
48 /* Figure out which bucket free entry is. */
49 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
50 /* Lock and fail to coalesce. */
51 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
52 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
54 tdb_unlock_free_bucket(tdb, b_off);
55 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
56 ok1(tdb_check(tdb, NULL, NULL) == 0);
59 /* No coalescing can be done due to used record */
60 layout = new_tdb_layout();
61 tdb_layout_add_zone(layout, zone_bits, false);
62 tdb_layout_add_free(layout, 1024);
63 tdb_layout_add_used(layout, key, data, 6);
64 tdb = tdb_layout_get(layout);
65 zone_off = layout->elem[0].base.off;
66 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
67 ok1(tdb_check(tdb, NULL, NULL) == 0);
69 /* Figure out which bucket free entry is. */
70 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
71 /* Lock and fail to coalesce. */
72 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
73 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
75 tdb_unlock_free_bucket(tdb, b_off);
76 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
77 ok1(tdb_check(tdb, NULL, NULL) == 0);
80 /* Coalescing can be done due to two free records, then EOF */
81 layout = new_tdb_layout();
82 tdb_layout_add_zone(layout, zone_bits, false);
83 tdb_layout_add_free(layout, 1024);
84 tdb = tdb_layout_get(layout);
85 zone_off = layout->elem[0].base.off;
86 len = layout->elem[2].free.len;
87 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
88 ok1(free_record_length(tdb, layout->elem[2].base.off) == len);
89 ok1(tdb_check(tdb, NULL, NULL) == 0);
91 /* Figure out which bucket (first) free entry is. */
92 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
93 /* Lock and coalesce. */
94 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
95 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
97 ok1(!tdb_has_locks(tdb));
98 ok1(free_record_length(tdb, layout->elem[1].base.off)
99 == 1024 + sizeof(struct tdb_used_record) + len);
100 ok1(tdb_check(tdb, NULL, NULL) == 0);
103 /* Coalescing can be done due to two free records, then data */
104 layout = new_tdb_layout();
105 tdb_layout_add_zone(layout, zone_bits, false);
106 tdb_layout_add_free(layout, 1024);
107 tdb_layout_add_free(layout, 512);
108 tdb_layout_add_used(layout, key, data, 6);
109 tdb = tdb_layout_get(layout);
110 zone_off = layout->elem[0].base.off;
111 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
112 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
113 ok1(tdb_check(tdb, NULL, NULL) == 0);
115 /* Figure out which bucket free entry is. */
116 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
117 /* Lock and coalesce. */
118 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
119 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
121 ok1(!tdb_has_locks(tdb));
122 ok1(free_record_length(tdb, layout->elem[1].base.off)
123 == 1024 + sizeof(struct tdb_used_record) + 512);
124 ok1(tdb_check(tdb, NULL, NULL) == 0);
127 /* Coalescing can be done due to three free records, then EOF */
128 layout = new_tdb_layout();
129 tdb_layout_add_zone(layout, zone_bits, false);
130 tdb_layout_add_free(layout, 1024);
131 tdb_layout_add_free(layout, 512);
132 tdb = tdb_layout_get(layout);
133 zone_off = layout->elem[0].base.off;
134 len = layout->elem[3].free.len;
135 ok1(free_record_length(tdb, layout->elem[1].base.off) == 1024);
136 ok1(free_record_length(tdb, layout->elem[2].base.off) == 512);
137 ok1(free_record_length(tdb, layout->elem[3].base.off) == len);
138 ok1(tdb_check(tdb, NULL, NULL) == 0);
140 /* Figure out which bucket free entry is. */
141 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, 1024));
142 /* Lock and coalesce. */
143 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
144 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
146 ok1(!tdb_has_locks(tdb));
147 ok1(free_record_length(tdb, layout->elem[1].base.off)
148 == 1024 + sizeof(struct tdb_used_record) + 512
149 + sizeof(struct tdb_used_record) + len);
150 ok1(tdb_check(tdb, NULL, NULL) == 0);
153 /* Coalescing across two zones isn't possible. */
154 layout = new_tdb_layout();
155 tdb_layout_add_zone(layout, zone_bits, false);
156 tdb_layout_add_zone(layout, zone_bits, true);
157 tdb = tdb_layout_get(layout);
158 zone_off = layout->elem[0].base.off;
159 len = layout->elem[1].free.len;
160 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
161 ok1(tdb_check(tdb, NULL, NULL) == 0);
163 /* Figure out which list free entry is. */
164 b_off = bucket_off(zone_off, size_to_bucket(zone_bits, len));
165 /* Lock and coalesce. */
166 ok1(tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == 0);
167 ok1(coalesce(tdb, zone_off, zone_bits, layout->elem[1].base.off,
169 tdb_unlock_free_bucket(tdb, b_off);
170 ok1(!tdb_has_locks(tdb));
171 ok1(free_record_length(tdb, layout->elem[1].base.off) == len);
172 ok1(tdb_check(tdb, NULL, NULL) == 0);
175 ok1(tap_log_messages == 0);
176 return exit_status();