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 User functions
|
||||||
[ ] Document new JSON functions
|
[ ] Document new JSON functions
|
||||||
|
|
||||||
[ ] Refactor usage of StrDuplicate()
|
[~] Refactor usage of StrDuplicate()
|
||||||
- Functions that keep strings do the duplication,
|
- Functions that keep strings do the duplication,
|
||||||
NOT their callers; callers free strings when they are
|
NOT their callers; callers free strings when they are
|
||||||
done with them.
|
done with them.
|
||||||
[ ] Remove HashMapGetKey() function
|
[x] Remove HashMapGetKey() function
|
||||||
|
[x] HashMap
|
||||||
|
[ ] JsonValueString()
|
||||||
|
|
||||||
Milestone: v0.3.0
|
Milestone: v0.3.0
|
||||||
-----------------
|
-----------------
|
||||||
|
|
5
src/Db.c
5
src/Db.c
|
@ -293,7 +293,6 @@ DbCacheEvict(Db * db)
|
||||||
pthread_mutex_destroy(&ref->lock);
|
pthread_mutex_destroy(&ref->lock);
|
||||||
|
|
||||||
hash = DbHashKey(ref->name);
|
hash = DbHashKey(ref->name);
|
||||||
Free(HashMapGetKey(db->cache, hash));
|
|
||||||
HashMapDelete(db->cache, hash);
|
HashMapDelete(db->cache, hash);
|
||||||
Free(hash);
|
Free(hash);
|
||||||
|
|
||||||
|
@ -366,7 +365,6 @@ DbClose(Db * db)
|
||||||
|
|
||||||
while (HashMapIterate(db->cache, &key, (void **) &val))
|
while (HashMapIterate(db->cache, &key, (void **) &val))
|
||||||
{
|
{
|
||||||
Free(key);
|
|
||||||
JsonFree(val->json);
|
JsonFree(val->json);
|
||||||
StringArrayFree(val->name);
|
StringArrayFree(val->name);
|
||||||
pthread_mutex_destroy(&val->lock);
|
pthread_mutex_destroy(&val->lock);
|
||||||
|
@ -509,7 +507,7 @@ DbLockFromArr(Db * db, Array * args)
|
||||||
}
|
}
|
||||||
ref->name = name;
|
ref->name = name;
|
||||||
|
|
||||||
HashMapSet(db->cache, StrDuplicate(hash), ref);
|
HashMapSet(db->cache, hash, ref);
|
||||||
db->cacheSize += ref->size;
|
db->cacheSize += ref->size;
|
||||||
|
|
||||||
ref->next = NULL;
|
ref->next = NULL;
|
||||||
|
@ -622,7 +620,6 @@ DbDelete(Db * db, size_t nArgs,...)
|
||||||
{
|
{
|
||||||
pthread_mutex_lock(&ref->lock);
|
pthread_mutex_lock(&ref->lock);
|
||||||
|
|
||||||
Free(HashMapGetKey(db->cache, hash));
|
|
||||||
HashMapDelete(db->cache, hash);
|
HashMapDelete(db->cache, hash);
|
||||||
JsonFree(ref->json);
|
JsonFree(ref->json);
|
||||||
StringArrayFree(ref->name);
|
StringArrayFree(ref->name);
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include <HashMap.h>
|
#include <HashMap.h>
|
||||||
|
|
||||||
#include <Memory.h>
|
#include <Memory.h>
|
||||||
|
#include <Str.h>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -202,6 +203,7 @@ HashMapFree(HashMap * map)
|
||||||
{
|
{
|
||||||
if (map->entries[i])
|
if (map->entries[i])
|
||||||
{
|
{
|
||||||
|
Free(map->entries[i]->key);
|
||||||
Free(map->entries[i]);
|
Free(map->entries[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,40 +246,6 @@ HashMapGet(HashMap * map, const char *key)
|
||||||
return NULL;
|
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
|
int
|
||||||
HashMapIterate(HashMap * map, char **key, void **value)
|
HashMapIterate(HashMap * map, char **key, void **value)
|
||||||
{
|
{
|
||||||
|
@ -340,6 +308,8 @@ HashMapSet(HashMap * map, char *key, void *value)
|
||||||
unsigned long hash;
|
unsigned long hash;
|
||||||
size_t index;
|
size_t index;
|
||||||
|
|
||||||
|
key = StrDuplicate(key);
|
||||||
|
|
||||||
if (!map || !key || !value)
|
if (!map || !key || !value)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -376,6 +346,7 @@ HashMapSet(HashMap * map, char *key, void *value)
|
||||||
if (!bucket->hash)
|
if (!bucket->hash)
|
||||||
{
|
{
|
||||||
bucket->hash = hash;
|
bucket->hash = hash;
|
||||||
|
Free(bucket->key);
|
||||||
bucket->key = key;
|
bucket->key = key;
|
||||||
bucket->value = value;
|
bucket->value = value;
|
||||||
break;
|
break;
|
||||||
|
@ -385,6 +356,9 @@ HashMapSet(HashMap * map, char *key, void *value)
|
||||||
{
|
{
|
||||||
void *oldValue = bucket->value;
|
void *oldValue = bucket->value;
|
||||||
|
|
||||||
|
Free(bucket->key);
|
||||||
|
bucket->key = key;
|
||||||
|
|
||||||
bucket->value = value;
|
bucket->value = value;
|
||||||
return oldValue;
|
return oldValue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -479,8 +479,8 @@ HttpParamDecode(char *in)
|
||||||
if (buf)
|
if (buf)
|
||||||
{
|
{
|
||||||
Free(buf);
|
Free(buf);
|
||||||
Free(decKey);
|
|
||||||
}
|
}
|
||||||
|
Free(decKey);
|
||||||
|
|
||||||
if (*in == '&')
|
if (*in == '&')
|
||||||
{
|
{
|
||||||
|
|
|
@ -133,11 +133,6 @@ HttpServerContextFree(HttpServerContext * c)
|
||||||
|
|
||||||
while (HashMapIterate(c->requestHeaders, &key, &val))
|
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);
|
Free(val);
|
||||||
}
|
}
|
||||||
HashMapFree(c->requestHeaders);
|
HashMapFree(c->requestHeaders);
|
||||||
|
@ -154,11 +149,6 @@ HttpServerContextFree(HttpServerContext * c)
|
||||||
* freeing it because it's probably a stack pointer.
|
* freeing it because it's probably a stack pointer.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (MemoryInfoGet(key))
|
|
||||||
{
|
|
||||||
Free(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (MemoryInfoGet(val))
|
if (MemoryInfoGet(val))
|
||||||
{
|
{
|
||||||
Free(val);
|
Free(val);
|
||||||
|
@ -169,7 +159,6 @@ HttpServerContextFree(HttpServerContext * c)
|
||||||
|
|
||||||
while (HashMapIterate(c->requestParams, &key, &val))
|
while (HashMapIterate(c->requestParams, &key, &val))
|
||||||
{
|
{
|
||||||
Free(key);
|
|
||||||
Free(val);
|
Free(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -600,6 +589,7 @@ HttpServerWorkerThread(void *args)
|
||||||
strcpy(headerValue, headerPtr);
|
strcpy(headerValue, headerPtr);
|
||||||
|
|
||||||
HashMapSet(context->requestHeaders, headerKey, headerValue);
|
HashMapSet(context->requestHeaders, headerKey, headerValue);
|
||||||
|
Free(headerKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
server->requestHandler(context, server->handlerArgs);
|
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))
|
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);
|
JsonValueFree(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -989,6 +976,7 @@ JsonDecodeObject(JsonParserState * state)
|
||||||
|
|
||||||
/* If there's an existing value at this key, discard it. */
|
/* If there's an existing value at this key, discard it. */
|
||||||
JsonValueFree(HashMapSet(obj, key, value));
|
JsonValueFree(HashMapSet(obj, key, value));
|
||||||
|
Free(key);
|
||||||
|
|
||||||
JsonTokenSeek(state);
|
JsonTokenSeek(state);
|
||||||
if (JsonExpect(state, TOKEN_OBJECT_CLOSE))
|
if (JsonExpect(state, TOKEN_OBJECT_CLOSE))
|
||||||
|
|
|
@ -92,7 +92,7 @@ BuildFlows(Array * flows)
|
||||||
ArrayAdd(responseStages, JsonValueString(StrDuplicate(stage->type)));
|
ArrayAdd(responseStages, JsonValueString(StrDuplicate(stage->type)));
|
||||||
if (stage->params)
|
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;
|
JsonValue *val;
|
||||||
|
|
||||||
Free(deviceId);
|
|
||||||
|
|
||||||
val = HashMapDelete(device, "accessToken");
|
val = HashMapDelete(device, "accessToken");
|
||||||
if (val)
|
if (val)
|
||||||
{
|
{
|
||||||
|
@ -324,6 +322,8 @@ UserLogin(User * user, char *password, char *deviceId, char *deviceDisplayName,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Free(deviceId);
|
||||||
|
|
||||||
if (result->refreshToken)
|
if (result->refreshToken)
|
||||||
{
|
{
|
||||||
HashMapSet(device, "refreshToken",
|
HashMapSet(device, "refreshToken",
|
||||||
|
@ -556,17 +556,13 @@ UserDeleteToken(User * user, char *token)
|
||||||
devicejson = HashMapGet(userjson, "devices");
|
devicejson = HashMapGet(userjson, "devices");
|
||||||
if (JsonValueType(devicejson) == JSON_OBJECT)
|
if (JsonValueType(devicejson) == JSON_OBJECT)
|
||||||
{
|
{
|
||||||
char *key;
|
|
||||||
|
|
||||||
/* Delete our object */
|
/* Delete our object */
|
||||||
deviceobject = JsonValueAsObject(devicejson);
|
deviceobject = JsonValueAsObject(devicejson);
|
||||||
key = HashMapGetKey(deviceobject, deviceid);
|
|
||||||
deletedval = HashMapDelete(deviceobject, deviceid);
|
deletedval = HashMapDelete(deviceobject, deviceid);
|
||||||
if (!deletedval)
|
if (!deletedval)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Free(key);
|
|
||||||
JsonValueFree(deletedval);
|
JsonValueFree(deletedval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,9 +42,6 @@ extern void *
|
||||||
extern void *
|
extern void *
|
||||||
HashMapGet(HashMap *, const char *);
|
HashMapGet(HashMap *, const char *);
|
||||||
|
|
||||||
extern void *
|
|
||||||
HashMapGetKey(HashMap *, const char *);
|
|
||||||
|
|
||||||
extern void *
|
extern void *
|
||||||
HashMapDelete(HashMap *, const char *);
|
HashMapDelete(HashMap *, const char *);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue