From 8c96fd8d7daf8faf0922373f4f475c2d524e76a8 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Thu, 23 Feb 2023 23:19:23 +0000 Subject: [PATCH] Begin the great StrDuplicate() refactor. --- TODO.txt | 6 ++++-- src/Db.c | 5 +---- src/HashMap.c | 42 ++++++++---------------------------------- src/Http.c | 2 +- src/HttpServer.c | 12 +----------- src/Json.c | 14 +------------- src/Uia.c | 2 +- src/User.c | 8 ++------ src/include/HashMap.h | 3 --- 9 files changed, 19 insertions(+), 75 deletions(-) diff --git a/TODO.txt b/TODO.txt index 85d76bf..f55b3b0 100644 --- a/TODO.txt +++ b/TODO.txt @@ -33,11 +33,13 @@ Milestone: v0.2.0 [ ] Document new User functions [ ] Document new JSON functions -[ ] Refactor usage of StrDuplicate() +[~] Refactor usage of StrDuplicate() - Functions that keep strings do the duplication, NOT their callers; callers free strings when they are done with them. - [ ] Remove HashMapGetKey() function + [x] Remove HashMapGetKey() function + [x] HashMap + [ ] JsonValueString() Milestone: v0.3.0 ----------------- diff --git a/src/Db.c b/src/Db.c index baf70ab..8e0677d 100644 --- a/src/Db.c +++ b/src/Db.c @@ -293,7 +293,6 @@ DbCacheEvict(Db * db) pthread_mutex_destroy(&ref->lock); hash = DbHashKey(ref->name); - Free(HashMapGetKey(db->cache, hash)); HashMapDelete(db->cache, hash); Free(hash); @@ -366,7 +365,6 @@ DbClose(Db * db) while (HashMapIterate(db->cache, &key, (void **) &val)) { - Free(key); JsonFree(val->json); StringArrayFree(val->name); pthread_mutex_destroy(&val->lock); @@ -509,7 +507,7 @@ DbLockFromArr(Db * db, Array * args) } ref->name = name; - HashMapSet(db->cache, StrDuplicate(hash), ref); + HashMapSet(db->cache, hash, ref); db->cacheSize += ref->size; ref->next = NULL; @@ -622,7 +620,6 @@ DbDelete(Db * db, size_t nArgs,...) { pthread_mutex_lock(&ref->lock); - Free(HashMapGetKey(db->cache, hash)); HashMapDelete(db->cache, hash); JsonFree(ref->json); StringArrayFree(ref->name); diff --git a/src/HashMap.c b/src/HashMap.c index e7f246d..9c32411 100644 --- a/src/HashMap.c +++ b/src/HashMap.c @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -202,6 +203,7 @@ HashMapFree(HashMap * map) { if (map->entries[i]) { + Free(map->entries[i]->key); Free(map->entries[i]); } } @@ -244,40 +246,6 @@ HashMapGet(HashMap * map, const char *key) return NULL; } -void * -HashMapGetKey(HashMap * map, const char *key) -{ - unsigned long hash; - size_t index; - - if (!map || !key) - { - return NULL; - } - - hash = map->hashFunc(key); - index = hash % map->capacity; - - for (;;) - { - HashMapBucket *bucket = map->entries[index]; - - if (!bucket) - { - break; - } - - if (bucket->hash == hash) - { - return bucket->key; - } - - index = (index + 1) % map->capacity; - } - - return NULL; -} - int HashMapIterate(HashMap * map, char **key, void **value) { @@ -340,6 +308,8 @@ HashMapSet(HashMap * map, char *key, void *value) unsigned long hash; size_t index; + key = StrDuplicate(key); + if (!map || !key || !value) { return NULL; @@ -376,6 +346,7 @@ HashMapSet(HashMap * map, char *key, void *value) if (!bucket->hash) { bucket->hash = hash; + Free(bucket->key); bucket->key = key; bucket->value = value; break; @@ -385,6 +356,9 @@ HashMapSet(HashMap * map, char *key, void *value) { void *oldValue = bucket->value; + Free(bucket->key); + bucket->key = key; + bucket->value = value; return oldValue; } diff --git a/src/Http.c b/src/Http.c index 7d8a081..7ac9ead 100644 --- a/src/Http.c +++ b/src/Http.c @@ -479,8 +479,8 @@ HttpParamDecode(char *in) if (buf) { Free(buf); - Free(decKey); } + Free(decKey); if (*in == '&') { diff --git a/src/HttpServer.c b/src/HttpServer.c index 8988143..bdf5d2d 100644 --- a/src/HttpServer.c +++ b/src/HttpServer.c @@ -133,11 +133,6 @@ HttpServerContextFree(HttpServerContext * c) while (HashMapIterate(c->requestHeaders, &key, &val)) { - /* - * These are always parsed from the request, so they should - * always be on the heap. - */ - Free(key); Free(val); } HashMapFree(c->requestHeaders); @@ -154,11 +149,6 @@ HttpServerContextFree(HttpServerContext * c) * freeing it because it's probably a stack pointer. */ - if (MemoryInfoGet(key)) - { - Free(key); - } - if (MemoryInfoGet(val)) { Free(val); @@ -169,7 +159,6 @@ HttpServerContextFree(HttpServerContext * c) while (HashMapIterate(c->requestParams, &key, &val)) { - Free(key); Free(val); } @@ -600,6 +589,7 @@ HttpServerWorkerThread(void *args) strcpy(headerValue, headerPtr); HashMapSet(context->requestHeaders, headerKey, headerValue); + Free(headerKey); } server->requestHandler(context, server->handlerArgs); diff --git a/src/Json.c b/src/Json.c index e3311cb..1ffe5fc 100644 --- a/src/Json.c +++ b/src/Json.c @@ -659,19 +659,6 @@ JsonFree(HashMap * object) while (HashMapIterate(object, &key, (void **) &value)) { - /* - * The key might not always be on the heap. In cases - * where the JSON object is built programmatically instead - * of with the parser, stack strings will probably have been - * used as the key. - */ - MemoryInfo *i = MemoryInfoGet(key); - - if (i) - { - Free(key); - } - JsonValueFree(value); } @@ -989,6 +976,7 @@ JsonDecodeObject(JsonParserState * state) /* If there's an existing value at this key, discard it. */ JsonValueFree(HashMapSet(obj, key, value)); + Free(key); JsonTokenSeek(state); if (JsonExpect(state, TOKEN_OBJECT_CLOSE)) diff --git a/src/Uia.c b/src/Uia.c index 9617541..eeadc6b 100644 --- a/src/Uia.c +++ b/src/Uia.c @@ -92,7 +92,7 @@ BuildFlows(Array * flows) ArrayAdd(responseStages, JsonValueString(StrDuplicate(stage->type))); if (stage->params) { - JsonValueFree(HashMapSet(responseParams, StrDuplicate(stage->type), JsonValueObject(stage->params))); + JsonValueFree(HashMapSet(responseParams, stage->type, JsonValueObject(stage->params))); } } } diff --git a/src/User.c b/src/User.c index 28aed90..85e585d 100644 --- a/src/User.c +++ b/src/User.c @@ -295,8 +295,6 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName, { JsonValue *val; - Free(deviceId); - val = HashMapDelete(device, "accessToken"); if (val) { @@ -324,6 +322,8 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName, } + Free(deviceId); + if (result->refreshToken) { HashMapSet(device, "refreshToken", @@ -556,17 +556,13 @@ UserDeleteToken(User * user, char *token) devicejson = HashMapGet(userjson, "devices"); if (JsonValueType(devicejson) == JSON_OBJECT) { - char *key; - /* Delete our object */ deviceobject = JsonValueAsObject(devicejson); - key = HashMapGetKey(deviceobject, deviceid); deletedval = HashMapDelete(deviceobject, deviceid); if (!deletedval) { return 0; } - Free(key); JsonValueFree(deletedval); } diff --git a/src/include/HashMap.h b/src/include/HashMap.h index 990ed25..dbf2420 100644 --- a/src/include/HashMap.h +++ b/src/include/HashMap.h @@ -42,9 +42,6 @@ extern void * extern void * HashMapGet(HashMap *, const char *); -extern void * - HashMapGetKey(HashMap *, const char *); - extern void * HashMapDelete(HashMap *, const char *);