+#if ISAMC_FREELIST_CHUNK
+static void flush_block (ISAMC is, int cat)
+{
+ char *abuf = is->files[cat].alloc_buf;
+ int block = is->files[cat].head.freelist;
+ if (block && is->files[cat].alloc_entries_num)
+ {
+ memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
+ bf_write (is->files[cat].bf, block, 0, 0, abuf);
+ is->files[cat].alloc_entries_num = 0;
+ }
+ xfree (abuf);
+}
+
+static int alloc_block (ISAMC is, int cat)
+{
+ int block = is->files[cat].head.freelist;
+ char *abuf = is->files[cat].alloc_buf;
+
+ (is->files[cat].no_allocated)++;
+
+ if (!block)
+ {
+ block = (is->files[cat].head.lastblock)++; /* no free list */
+ is->files[cat].head_is_dirty = 1;
+ }
+ else
+ {
+ if (!is->files[cat].alloc_entries_num) /* read first time */
+ {
+ bf_read (is->files[cat].bf, block, 0, 0, abuf);
+ memcpy (&is->files[cat].alloc_entries_num, abuf,
+ sizeof(is->files[cat].alloc_entries_num));
+ assert (is->files[cat].alloc_entries_num > 0);
+ }
+ /* have some free blocks now */
+ assert (is->files[cat].alloc_entries_num > 0);
+ is->files[cat].alloc_entries_num--;
+ if (!is->files[cat].alloc_entries_num) /* last one in block? */
+ {
+ memcpy (&is->files[cat].head.freelist, abuf + sizeof(int),
+ sizeof(int));
+ is->files[cat].head_is_dirty = 1;
+
+ if (is->files[cat].head.freelist)
+ {
+ bf_read (is->files[cat].bf, is->files[cat].head.freelist,
+ 0, 0, abuf);
+ memcpy (&is->files[cat].alloc_entries_num, abuf,
+ sizeof(is->files[cat].alloc_entries_num));
+ assert (is->files[cat].alloc_entries_num);
+ }
+ }
+ else
+ memcpy (&block, abuf + sizeof(int) + sizeof(int) *
+ is->files[cat].alloc_entries_num, sizeof(int));
+ }
+ return block;
+}
+
+static void release_block (ISAMC is, int cat, int pos)
+{
+ char *abuf = is->files[cat].alloc_buf;
+ int block = is->files[cat].head.freelist;
+
+ (is->files[cat].no_released)++;
+
+ if (block && !is->files[cat].alloc_entries_num) /* must read block */
+ {
+ bf_read (is->files[cat].bf, block, 0, 0, abuf);
+ memcpy (&is->files[cat].alloc_entries_num, abuf,
+ sizeof(is->files[cat].alloc_entries_num));
+ assert (is->files[cat].alloc_entries_num > 0);
+ }
+ assert (is->files[cat].alloc_entries_num <= is->files[cat].alloc_entries_max);
+ if (is->files[cat].alloc_entries_num == is->files[cat].alloc_entries_max)
+ {
+ assert (block);
+ memcpy (abuf, &is->files[cat].alloc_entries_num, sizeof(int));
+ bf_write (is->files[cat].bf, block, 0, 0, abuf);
+ is->files[cat].alloc_entries_num = 0;
+ }
+ if (!is->files[cat].alloc_entries_num) /* make new buffer? */
+ {
+ memcpy (abuf + sizeof(int), &block, sizeof(int));
+ is->files[cat].head.freelist = pos;
+ is->files[cat].head_is_dirty = 1;
+ }
+ else
+ {
+ memcpy (abuf + sizeof(int) +
+ is->files[cat].alloc_entries_num*sizeof(int),
+ &pos, sizeof(int));
+ }
+ is->files[cat].alloc_entries_num++;
+}
+#else
+static void flush_block (ISAMC is, int cat)
+{
+ char *abuf = is->files[cat].alloc_buf;
+ xfree (abuf);
+}
+
+static int alloc_block (ISAMC is, int cat)