- /* New zone can be between zone_bits or larger if we're on the right
- * boundary. */
- for (;;) {
- /* Does this fit the allocation comfortably? */
- if ((1ULL << zone_bits) >= overhead(zone_bits) + wanted) {
- /* Only let enlarge_zone enlarge us once. */
- if (!enlarge_zone)
- break;
- enlarge_zone = false;
- }
- if ((old_size - 1 - sizeof(struct tdb_header))
- & (1 << zone_bits))
- break;
- zone_bits++;
+ /* We are never allowed to cross a power-of-two boundary, and our
+ * minimum zone size is 1 << INITIAL_ZONE_BITS.
+ *
+ * If our filesize is 128k, we can add a 64k or a 128k zone. If it's
+ * 192k, we can only add a 64k zone.
+ *
+ * In other words, our max zone size is (1 << (ffs(filesize) - 1)) */
+ zone_bits = ffs64(old_size - sizeof(struct tdb_header)) - 1;
+ assert(zone_bits >= INITIAL_ZONE_BITS);
+
+ /* Big zones generally good, but more zones wanted if contended. */
+ if (zones_contended(tdb)) {
+ /* If it suffices, make zone same size as last one. */
+ if (zhdr.zone_bits < zone_bits
+ && (1ULL << zhdr.zone_bits) >= overhead(zone_bits)+wanted)
+ zone_bits = zhdr.zone_bits;