forked from lda/telodendria
Begin the great StrDuplicate() refactor.
This commit is contained in:
parent
41421557e0
commit
8c96fd8d7d
9 changed files with 19 additions and 75 deletions
6
TODO.txt
6
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
|
||||
-----------------
|
||||
|
|
5
src/Db.c
5
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);
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <HashMap.h>
|
||||
|
||||
#include <Memory.h>
|
||||
#include <Str.h>
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -479,8 +479,8 @@ HttpParamDecode(char *in)
|
|||
if (buf)
|
||||
{
|
||||
Free(buf);
|
||||
Free(decKey);
|
||||
}
|
||||
Free(decKey);
|
||||
|
||||
if (*in == '&')
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
14
src/Json.c
14
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))
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,9 +42,6 @@ extern void *
|
|||
extern void *
|
||||
HashMapGet(HashMap *, const char *);
|
||||
|
||||
extern void *
|
||||
HashMapGetKey(HashMap *, const char *);
|
||||
|
||||
extern void *
|
||||
HashMapDelete(HashMap *, const char *);
|
||||
|
||||
|
|
Loading…
Reference in a new issue