From 37ee7700f48c3fc4dcb6dffb8473838142b6afc4 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 6 Nov 2022 00:47:17 +0000 Subject: [PATCH] Start writing a function to compute the in-memory size of an object. --- src/Db.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Db.c b/src/Db.c index c50730a..5f43f1d 100644 --- a/src/Db.c +++ b/src/Db.c @@ -24,6 +24,7 @@ #include #include +#include #include @@ -46,6 +47,45 @@ struct DbRef char *file; }; +static ssize_t +DbComputeSize(HashMap *json) +{ + char *key; + JsonValue *val; + MemoryInfo *a; + size_t total; + + if (!json) + { + return -1; + } + + total = 0; + + a = MemoryInfoGet(json); + if (a) + { + total += MemoryInfoGetSize(a); + } + + while (HashMapIterate(json, &key, (void **) &val)) + { + a = MemoryInfoGet(key); + if (a) + { + total += MemoryInfoGetSize(a); + } + + a = MemoryInfoGet(val); + if (a) + { + total += MemoryInfoGetSize(a); + } + } + + return total; +} + Db * DbOpen(const char *dir, size_t cache) {