tdb2: rename set_header to the more appropriate set_used_header.
[ccan] / ccan / tdb2 / test / layout.c
index 04111b4a697dd2d7bdbe43701e91cd21a6760caa..3049d3214e62736ec95ddbe979cafe7784034593 100644 (file)
@@ -23,23 +23,20 @@ static void add(struct tdb_layout *layout, union tdb_layout_elem elem)
        layout->elem[layout->num_elems++] = elem;
 }
 
-void tdb_layout_add_zone(struct tdb_layout *layout,
-                        unsigned int zone_bits,
-                        bool fill_prev)
+void tdb_layout_add_freelist(struct tdb_layout *layout)
 {
        union tdb_layout_elem elem;
-       if (fill_prev)
-               tdb_layout_add_free(layout, 0);
-       elem.base.type = ZONE;
-       elem.zone.zone_bits = zone_bits;
+       elem.base.type = FREELIST;
        add(layout, elem);
 }
 
-void tdb_layout_add_free(struct tdb_layout *layout, tdb_len_t len)
+void tdb_layout_add_free(struct tdb_layout *layout, tdb_len_t len,
+                        unsigned flist)
 {
        union tdb_layout_elem elem;
        elem.base.type = FREE;
        elem.free.len = len;
+       elem.free.flist_num = flist;
        add(layout, elem);
 }
 
@@ -85,10 +82,9 @@ static tdb_len_t hashtable_len(struct tle_hashtable *htable)
                + htable->extra;
 }
 
-static tdb_len_t zone_header_len(struct tle_zone *zone)
+static tdb_len_t freelist_len(struct tle_freelist *flist)
 {
-       return sizeof(struct free_zone_header)
-               + sizeof(tdb_off_t) * (BUCKETS_FOR_ZONE(zone->zone_bits)+1);
+       return sizeof(struct tdb_freelist);
 }
 
 static void set_free_record(void *mem, tdb_len_t len)
@@ -97,47 +93,53 @@ static void set_free_record(void *mem, tdb_len_t len)
 }
 
 static void set_data_record(void *mem, struct tdb_context *tdb,
-                           struct tle_zone *last_zone,
                            struct tle_used *used)
 {
        struct tdb_used_record *u = mem;
 
-       set_header(tdb, u, used->key.dsize, used->data.dsize,
-                  used->key.dsize + used->data.dsize + used->extra,
-                  tdb_hash(tdb, used->key.dptr, used->key.dsize),
-                  last_zone->zone_bits);
+       set_used_header(tdb, u, used->key.dsize, used->data.dsize,
+                       used->key.dsize + used->data.dsize + used->extra,
+                       tdb_hash(tdb, used->key.dptr, used->key.dsize));
        memcpy(u + 1, used->key.dptr, used->key.dsize);
        memcpy((char *)(u + 1) + used->key.dsize,
               used->data.dptr, used->data.dsize);
 }
 
 static void set_hashtable(void *mem, struct tdb_context *tdb,
-                         struct tle_zone *last_zone,
                          struct tle_hashtable *htable)
 {
        struct tdb_used_record *u = mem;
        tdb_len_t len = sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS;
 
-       set_header(tdb, u, 0, len, len + htable->extra, 0,
-                  last_zone->zone_bits);
+       set_used_header(tdb, u, 0, len, len + htable->extra, 0);
        memset(u + 1, 0, len);
 }
 
-static void set_zone(void *mem, struct tdb_context *tdb,
-                    struct tle_zone *zone)
+static void set_freelist(void *mem, struct tdb_context *tdb,
+                        struct tle_freelist *freelist, struct tdb_header *hdr,
+                        tdb_off_t last_flist)
 {
-       struct free_zone_header *fz = mem;
-       memset(fz, 0, zone_header_len(zone));
-       fz->zone_bits = zone->zone_bits;
+       struct tdb_freelist *flist = mem;
+       memset(flist, 0, sizeof(*flist));
+       set_used_header(tdb, &flist->hdr, 0,
+                       sizeof(*flist) - sizeof(flist->hdr),
+                       sizeof(*flist) - sizeof(flist->hdr), 1);
+
+       if (last_flist) {
+               flist = (struct tdb_freelist *)((char *)hdr + last_flist);
+               flist->next = freelist->base.off;
+       } else {
+               hdr->free_list = freelist->base.off;
+       }
 }
 
 static void add_to_freetable(struct tdb_context *tdb,
-                            struct tle_zone *last_zone,
                             tdb_off_t eoff,
-                            tdb_off_t elen)
+                            tdb_off_t elen,
+                            struct tle_freelist *freelist)
 {
-       add_free_record(tdb, last_zone->zone_bits, eoff,
-                       sizeof(struct tdb_used_record) + elen);
+       tdb->flist_off = freelist->base.off;
+       add_free_record(tdb, eoff, sizeof(struct tdb_used_record) + elen);
 }
 
 static tdb_off_t hbucket_off(tdb_off_t group_start, unsigned ingroup)
@@ -200,19 +202,28 @@ static void add_to_hashtable(struct tdb_context *tdb,
        abort();
 }
 
+static struct tle_freelist *find_flist(struct tdb_layout *layout, unsigned num)
+{
+       unsigned i;
+
+       for (i = 0; i < layout->num_elems; i++) {
+               if (layout->elem[i].base.type != FREELIST)
+                       continue;
+               if (num == 0)
+                       return &layout->elem[i].flist;
+               num--;
+       }
+       abort();
+}
+
 /* FIXME: Support TDB_CONVERT */
 struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
 {
        unsigned int i;
-       tdb_off_t off, len;
-       tdb_len_t zone_left;
+       tdb_off_t off, len, last_flist;
        char *mem;
        struct tdb_context *tdb;
-       struct tle_zone *last_zone = NULL;
-
-       assert(layout->elem[0].base.type == ZONE);
 
-       zone_left = 0;
        off = sizeof(struct tdb_header);
 
        /* First pass of layout: calc lengths */
@@ -220,15 +231,10 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
                union tdb_layout_elem *e = &layout->elem[i];
                e->base.off = off;
                switch (e->base.type) {
-               case ZONE:
-                       assert(zone_left == 0);
-                       len = zone_header_len(&e->zone);
-                       zone_left = 1ULL << e->zone.zone_bits;
+               case FREELIST:
+                       len = freelist_len(&e->flist);
                        break;
                case FREE:
-                       if (e->free.len == 0)
-                               e->free.len = zone_left
-                                       - sizeof(struct tdb_used_record);
                        len = free_record_len(e->free.len);
                        break;
                case DATA:
@@ -241,20 +247,9 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
                        abort();
                }
                off += len;
-               assert(zone_left >= len);
-               zone_left -= len;
-       }
-
-       /* Fill final zone with free record. */
-       if (zone_left != 0) {
-               tdb_layout_add_free(layout,
-                                   zone_left
-                                   - sizeof(struct tdb_used_record));
-               layout->elem[layout->num_elems-1].base.off = off;
-               off += zone_left;
        }
 
-       mem = malloc(off+1);
+       mem = malloc(off);
        /* Now populate our header, cribbing from a real TDB header. */
        tdb = tdb_open(NULL, TDB_INTERNAL, O_RDWR, 0, &tap_log_attr);
        memcpy(mem, tdb->map_ptr, sizeof(struct tdb_header));
@@ -262,39 +257,38 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
        /* Mug the tdb we have to make it use this. */
        free(tdb->map_ptr);
        tdb->map_ptr = mem;
-       tdb->map_size = off+1;
+       tdb->map_size = off;
 
+       last_flist = 0;
        for (i = 0; i < layout->num_elems; i++) {
                union tdb_layout_elem *e = &layout->elem[i];
                switch (e->base.type) {
-               case ZONE:
-                       set_zone(mem + e->base.off, tdb, &e->zone);
-                       last_zone = &e->zone;
+               case FREELIST:
+                       set_freelist(mem + e->base.off, tdb, &e->flist,
+                                    (struct tdb_header *)mem, last_flist);
+                       last_flist = e->base.off;
                        break;
                case FREE:
                        set_free_record(mem + e->base.off, e->free.len);
                        break;
                case DATA:
-                       set_data_record(mem + e->base.off, tdb, last_zone,
-                                       &e->used);
+                       set_data_record(mem + e->base.off, tdb, &e->used);
                        break;
                case HASHTABLE:
-                       set_hashtable(mem + e->base.off, tdb, last_zone,
-                                     &e->hashtable);
+                       set_hashtable(mem + e->base.off, tdb, &e->hashtable);
                        break;
                }
        }
+       /* Must have a free list! */
+       assert(last_flist);
 
        /* Now fill the free and hash tables. */
        for (i = 0; i < layout->num_elems; i++) {
                union tdb_layout_elem *e = &layout->elem[i];
                switch (e->base.type) {
-               case ZONE:
-                       last_zone = &e->zone;
-                       break;
                case FREE:
-                       add_to_freetable(tdb, last_zone,
-                                        e->base.off, e->free.len);
+                       add_to_freetable(tdb, e->base.off, e->free.len,
+                                        find_flist(layout, e->free.flist_num));
                        break;
                case DATA:
                        add_to_hashtable(tdb, e->base.off, e->used.key);
@@ -304,8 +298,7 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
                }
        }
 
-       /* Write tailer. */
-       ((uint8_t *)tdb->map_ptr)[tdb->map_size-1] = last_zone->zone_bits;
+       tdb->flist_off = find_flist(layout, 0)->base.off;
 
        /* Get physical if they asked for it. */
        if (layout->filename) {
@@ -321,5 +314,6 @@ struct tdb_context *tdb_layout_get(struct tdb_layout *layout)
                tdb = tdb_open(layout->filename, TDB_NOMMAP, O_RDWR, 0,
                               &tap_log_attr);
        }
+
        return tdb;
 }