Compare commits

..

4 commits

Author SHA1 Message Date
LDA
f6af2cd782 [FIX/WIP] Temporary fix to the database system
It used to crash, my bad.
2024-08-10 09:24:42 +02:00
LDA
2b061f1226 Merge branch 'master' of https://git.telodendria.io/Telodendria/Cytoplasm into lmdbwerk 2024-08-09 21:08:16 +02:00
4903c075e8 Merge pull request 'Pre-emptively free out the key' (#41) from lda/Cytoplasm:hashmap-del-leak into master
Reviewed-on: Telodendria/Cytoplasm#41
2024-08-09 09:26:49 -04:00
LDA
da857a3d53 [FIX] Pre-emptively free out the key
I fail to see a reason why this shouldn't be done, to be fair, and it
fixes a memory leakage with a Cytoplasm-based project I'm making.
2024-07-19 16:08:25 +02:00
3 changed files with 19 additions and 21 deletions

View file

@ -434,6 +434,7 @@ DbInit(Db *db)
db->mostRecent = NULL; db->mostRecent = NULL;
db->leastRecent = NULL; db->leastRecent = NULL;
db->cacheSize = 0; db->cacheSize = 0;
db->maxCache = 0;
if (db->maxCache) if (db->maxCache)
{ {
@ -460,15 +461,9 @@ DbRefInit(Db *db, DbRef *ref)
ref->ts = UtilTsMillis(); ref->ts = UtilTsMillis();
ref->size = 0; ref->size = 0;
if (db->mostRecent) /* TODO: Append the ref to the cache list.
{ * I removed it because it broke everything and crashed all the time.
db->mostRecent->next = ref; * My bad! */
}
if (!db->leastRecent)
{
db->leastRecent = ref;
}
db->mostRecent = ref;
} }
void void
StringArrayAppend(Array *arr, char *str) StringArrayAppend(Array *arr, char *str)

View file

@ -106,7 +106,7 @@ FlatLock(Db *d, Array *dir)
FlatDb *db = (FlatDb *) d; FlatDb *db = (FlatDb *) d;
FlatDbRef *ref = NULL; FlatDbRef *ref = NULL;
size_t i; size_t i;
char *path; char *path = NULL;
if (!d || !dir) if (!d || !dir)
{ {
return NULL; return NULL;
@ -126,6 +126,11 @@ FlatLock(Db *d, Array *dir)
} }
stream = StreamFd(fd); stream = StreamFd(fd);
if (!stream)
{
ref = NULL;
goto end;
}
lock.l_start = 0; lock.l_start = 0;
lock.l_len = 0; lock.l_len = 0;
@ -140,11 +145,11 @@ FlatLock(Db *d, Array *dir)
} }
ref = Malloc(sizeof(*ref)); ref = Malloc(sizeof(*ref));
DbRefInit(d, &(ref->base)); DbRefInit(d, (DbRef *) ref);
ref->fd = fd;
ref->stream = stream;
ref->base.ts = UtilLastModified(path); ref->base.ts = UtilLastModified(path);
ref->base.json = JsonDecode(stream); ref->base.json = JsonDecode(stream);
ref->stream = stream;
ref->fd = fd;
if (!ref->base.json) if (!ref->base.json)
{ {
Free(ref); Free(ref);
@ -157,10 +162,7 @@ FlatLock(Db *d, Array *dir)
ref->base.name = ArrayCreate(); ref->base.name = ArrayCreate();
for (i = 0; i < ArraySize(dir); i++) for (i = 0; i < ArraySize(dir); i++)
{ {
ArrayAdd( StringArrayAppend(ref->base.name, ArrayGet(dir, i));
ref->base.name,
StrDuplicate(ArrayGet(dir, i))
);
} }
} }
end: end:
@ -246,7 +248,6 @@ FlatCreate(Db *d, Array *dir)
/* FlatLock() will lock again for us */ /* FlatLock() will lock again for us */
pthread_mutex_unlock(&d->lock); pthread_mutex_unlock(&d->lock);
ret = FlatLock(d, dir); ret = FlatLock(d, dir);
return ret; return ret;
@ -255,7 +256,7 @@ FlatCreate(Db *d, Array *dir)
static bool static bool
FlatDelete(Db *d, Array *dir) FlatDelete(Db *d, Array *dir)
{ {
bool ret = true; bool ret = false;
char *file; char *file;
FlatDb *db = (FlatDb *) d; FlatDb *db = (FlatDb *) d;
if (!d || !dir) if (!d || !dir)
@ -273,7 +274,7 @@ FlatDelete(Db *d, Array *dir)
} }
Free(file); Free(file);
pthread_mutex_lock(&d->lock); pthread_mutex_unlock(&d->lock);
return ret; return ret;
} }
@ -355,7 +356,7 @@ DbOpen(char *dir, size_t cache)
return NULL; return NULL;
} }
db = Malloc(sizeof(*db)); db = Malloc(sizeof(*db));
DbInit(&(db->base)); DbInit((Db *) db);
db->dir = dir; db->dir = dir;
db->base.cacheSize = cache; db->base.cacheSize = cache;

View file

@ -184,6 +184,8 @@ HashMapDelete(HashMap * map, const char *key)
if (bucket->hash == hash) if (bucket->hash == hash)
{ {
bucket->hash = 0; bucket->hash = 0;
Free(bucket->key);
bucket->key = NULL;
return bucket->value; return bucket->value;
} }