Examples › Database Examples
database_reclaim
C++17 or laterDataReel 6.4.3Portable C++ (tested on Linux)
Legacy: examples_database/reclaim/testprog.cpp · Modern: examples_modern/database/reclaim/modern.cpp
Modern equivalent of the reclaim test (best fit and first fit reuse of freed blocks). The legacy program already uses std::string; this one also uses C++17: a structured binding over a std::array of the two methods, std::filesystem::path for the file names, a struct returned from a function instead of two out-parameters, and std::string(n, c) buffers checked with ==.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxDatabase | keep gxDatabase: a stack object, errors as exceptions, static_assert(std::is_trivially_copyable_v<Record>) | C++17 | The standard library has no database file; the file format is DataReel's. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_database/reclaim/testprog.cpp 57 lines
// testprog.cpp (examples_database/reclaim) - 11/16/2026: DataReel 6.x port of the 7.4 tests/reclaim_test.cpp. // ReclaimBestFit() reused only exact fits: its best fit so far started at 0 unused bytes, so no bigger free block // was ever taken and the file grew. Checks, for both methods, that a freed bigger block is reused for a smaller // object (the file does not grow), that best fit takes the smallest block that fits among several, that the split // leaves a free block for the rest, and that the deleted/removed block counts stay right. // reclaim_test DIR (exit 0 = all passed) #include "gxdlcode.h" #include <stdio.h> #include <string.h> #include <string> #include "gxdbase.h" static int pass = 0, fail = 0; static void check(bool ok, const std::string &what) { printf("%s %s\n", ok ? "PASS" : "FAIL", what.c_str()); (ok ? pass : fail)++; } static long long freed(gxDatabase &f) { FAU_t d = 0, r = 0; f.DeletedBlocks(&d, &r); return (long long)(d + r); } // free blocks static FAU_t put(gxDatabase &f, size_t n, gxDatabaseReclaimMethod m) { std::string b(n, 'x'); FAU_t a = f.Alloc((__ULWORD__)n, m); if(a) f.Write(b.data(), (__ULWORD__)n, a); return a; } int main(int argc, char **argv) { std::string dir = argc > 1 ? argv[1] : "."; for(int k = 0; k < 2; k++) { gxDatabaseReclaimMethod m = k == 0 ? gxDBASE_RECLAIM_BESTFIT : gxDBASE_RECLAIM_FIRSTFIT; std::string name = k == 0 ? "best fit" : "first fit"; std::string fn = dir + (k == 0 ? "/reclaim_best.gxd" : "/reclaim_first.gxd"); gxDatabase f; if(f.Create(fn.c_str()) != gxDBASE_NO_ERROR) { check(false, name + ": create"); continue; } // blocks of 4000, 1000 and 2000 bytes between small separators (so freed blocks do not merge) FAU_t big = put(f, 4000, m); put(f, 10, m); FAU_t small_blk = put(f, 1000, m); put(f, 10, m); FAU_t mid = put(f, 2000, m); put(f, 10, m); FAU_t eof = f.GetEOF(); f.Delete(big); f.Delete(small_blk); f.Delete(mid); long long d0 = freed(f); // a 700-byte object fits the 1000-byte block best (the 4000 one is first in no particular order) FAU_t a = put(f, 700, m); check(a != 0 && f.GetEOF() == eof, name + ": a 700-byte object reuses a freed block (the file stays " + std::to_string((long long)eof) + " bytes)"); if(k == 0) check(a == small_blk, name + ": it took the smallest block that fits (the 1000-byte one)"); long long d1 = freed(f); check(d1 >= 1 && d1 <= d0, name + ": free blocks " + std::to_string(d0) + " -> " + std::to_string(d1) + " (the rest of the split is free again)"); // the rest of the split block is reusable: more small objects still do not grow the file put(f, 100, m); put(f, 100, m); check(f.GetEOF() == eof, name + ": two more 100-byte objects fit in freed space"); // read back what was written std::string back(700, 0); check(f.Read(&back[0], 700, a) == gxDBASE_NO_ERROR && back == std::string(700, 'x'), name + ": the reused block reads back"); f.Close(); } printf("%d passed, %d failed\n", pass, fail); return fail ? 1 : 0; }
Modern C++ (C++17)
examples_modern/database/reclaim/modern.cpp 77 lines
#include <array> #include <filesystem> #include <iostream> #include <string> #include <string_view> #include <utility> #include "gxdbase.h" namespace fs = std::filesystem; static int pass = 0, fail = 0; static void check(bool ok, const std::string &what) { std::cout << (ok ? "PASS " : "FAIL ") << what << "\n"; (ok ? pass : fail)++; } struct FreeBlocks { FAU_t deleted = 0, removed = 0; FAU_t total() const { return deleted + removed; } }; static FreeBlocks free_blocks(gxDatabase &f) { FreeBlocks fb; f.DeletedBlocks(&fb.deleted, &fb.removed); return fb; } static FAU_t put(gxDatabase &f, std::size_t n, gxDatabaseReclaimMethod m) { const std::string b(n, 'x'); FAU_t a = f.Alloc(__ULWORD__(n), m); if(a) f.Write(b.data(), __ULWORD__(n), a); return a; } int main(int argc, char **argv) { const fs::path dir = argc > 1 ? argv[1] : "."; const std::array<std::pair<gxDatabaseReclaimMethod, std::string_view>, 2> methods{{ {gxDBASE_RECLAIM_BESTFIT, "best fit"}, {gxDBASE_RECLAIM_FIRSTFIT, "first fit"}}}; for(const auto &[m, label] : methods) { const std::string name(label); const fs::path fn = dir / ("modern_reclaim_" + std::string(label.substr(0, label.find(' '))) + ".gxd"); gxDatabase f; if(f.Create(fn.string().c_str()) != gxDBASE_NO_ERROR) { check(false, name + ": create"); continue; } // 4000, 1000 and 2000 byte blocks with small separators between them FAU_t big = put(f, 4000, m); put(f, 10, m); FAU_t small_blk = put(f, 1000, m); put(f, 10, m); FAU_t mid = put(f, 2000, m); put(f, 10, m); const FAU_t eof = FAU_t(f.GetEOF()); for(FAU_t a : {big, small_blk, mid}) f.Delete(a); const FreeBlocks before = free_blocks(f); FAU_t a = put(f, 700, m); check(a != 0 && FAU_t(f.GetEOF()) == eof, name + ": a 700 byte object reuses a freed block"); if(m == gxDBASE_RECLAIM_BESTFIT) check(a == small_blk, name + ": it took the smallest block that fits"); const FreeBlocks after = free_blocks(f); check(after.total() >= 1 && after.total() <= before.total(), name + ": free blocks " + std::to_string(before.total()) + " -> " + std::to_string(after.total())); put(f, 100, m); put(f, 100, m); check(FAU_t(f.GetEOF()) == eof, name + ": two more 100 byte objects fit in freed space"); std::string back(700, '\0'); check(f.Read(back.data(), 700, a) == gxDBASE_NO_ERROR && back == std::string(700, 'x'), name + ": the reused block reads back"); // C++17: non-const data() f.Close(); fs::remove(fn); } std::cout << pass << " passed, " << fail << " failed\n"; return fail ? 1 : 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target database_reclaim ./build-modern/database_reclaim
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
PASS best fit: a 700 byte object reuses a freed block PASS best fit: it took the smallest block that fits PASS best fit: free blocks 3 -> 3 PASS best fit: two more 100 byte objects fit in freed space PASS best fit: the reused block reads back PASS first fit: a 700 byte object reuses a freed block PASS first fit: free blocks 3 -> 3 PASS first fit: two more 100 byte objects fit in freed space PASS first fit: the reused block reads back 9 passed, 0 failed