Format source code

This commit is contained in:
Jordan Bancino 2022-10-13 09:09:26 -04:00
parent 2df0cd9d26
commit 945acd1adf
7 changed files with 317 additions and 270 deletions

View file

@ -39,7 +39,7 @@ struct HashMap
size_t capacity; size_t capacity;
HashMapBucket **entries; HashMapBucket **entries;
unsigned long (*hashFunc) (const char *); unsigned long (*hashFunc) (const char *);
float maxLoad; float maxLoad;
size_t iterator; size_t iterator;
@ -137,7 +137,7 @@ HashMapCreate(void)
map->count = 0; map->count = 0;
map->capacity = 16; map->capacity = 16;
map->iterator = 0; map->iterator = 0;
map->hashFunc = HashMapHashKey; map->hashFunc = HashMapHashKey;
map->entries = calloc(map->capacity, sizeof(HashMapBucket *)); map->entries = calloc(map->capacity, sizeof(HashMapBucket *));
if (!map->entries) if (!map->entries)
@ -285,12 +285,12 @@ HashMapMaxLoadSet(HashMap * map, float load)
void void
HashMapFunctionSet(HashMap * map, unsigned long (*hashFunc) (const char *)) HashMapFunctionSet(HashMap * map, unsigned long (*hashFunc) (const char *))
{ {
if (!map || !hashFunc) if (!map || !hashFunc)
{ {
return; return;
} }
map->hashFunc = hashFunc; map->hashFunc = hashFunc;
} }
void * void *

View file

@ -1,3 +1,26 @@
/*
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Memory.h> #include <Memory.h>
#include <stdio.h> #include <stdio.h>
@ -6,293 +29,293 @@
struct MemoryInfo struct MemoryInfo
{ {
size_t size; size_t size;
const char *file; const char *file;
const char *func; const char *func;
int line; int line;
void *pointer; void *pointer;
MemoryInfo *next; MemoryInfo *next;
MemoryInfo *prev; MemoryInfo *prev;
}; };
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static MemoryInfo *lastAllocation = NULL; static MemoryInfo *lastAllocation = NULL;
static void (*hook)(MemoryAction, MemoryInfo *, void *) = NULL; static void (*hook) (MemoryAction, MemoryInfo *, void *) = NULL;
static void *hookArgs = NULL; static void *hookArgs = NULL;
void * void *
MemoryAllocate(size_t size, const char *file, int line, const char *func) MemoryAllocate(size_t size, const char *file, int line, const char *func)
{ {
void *p; void *p;
MemoryInfo *a; MemoryInfo *a;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
p = malloc(size); p = malloc(size);
if (!p) if (!p)
{ {
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }
a = malloc(sizeof(MemoryInfo)); a = malloc(sizeof(MemoryInfo));
if (!a) if (!a)
{ {
free(p); free(p);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }
a->size = size; a->size = size;
a->file = file; a->file = file;
a->line = line; a->line = line;
a->func = func; a->func = func;
a->pointer = p; a->pointer = p;
a->next = NULL; a->next = NULL;
a->prev = lastAllocation; a->prev = lastAllocation;
if (lastAllocation) if (lastAllocation)
{ {
lastAllocation->next = a; lastAllocation->next = a;
} }
lastAllocation = a; lastAllocation = a;
if (hook) if (hook)
{ {
hook(MEMORY_ALLOCATE, a, hookArgs); hook(MEMORY_ALLOCATE, a, hookArgs);
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return p; return p;
} }
void * void *
MemoryReallocate(void *p, size_t size) MemoryReallocate(void *p, size_t size)
{ {
MemoryInfo *a; MemoryInfo *a;
void *new = NULL; void *new = NULL;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
if (a->pointer == p) if (a->pointer == p)
{ {
new = realloc(p, size); new = realloc(p, size);
if (new) if (new)
{ {
a->pointer = new; a->pointer = new;
a->size = size; a->size = size;
if (hook) if (hook)
{ {
hook(MEMORY_REALLOCATE, a, hookArgs); hook(MEMORY_REALLOCATE, a, hookArgs);
} }
} }
break; break;
} }
a = a->prev; a = a->prev;
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return new; return new;
} }
void void
MemoryFree(void *p) MemoryFree(void *p)
{ {
MemoryInfo *a; MemoryInfo *a;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
if (a->pointer == p) if (a->pointer == p)
{ {
if (a->prev) if (a->prev)
{ {
a->prev->next = a->next; a->prev->next = a->next;
} }
else else
{ {
lastAllocation = a->next; lastAllocation = a->next;
} }
if (a->next) if (a->next)
{ {
a->next->prev = a->prev; a->next->prev = a->prev;
} }
else else
{ {
lastAllocation = a->prev; lastAllocation = a->prev;
} }
if (hook) if (hook)
{ {
hook(MEMORY_FREE, a, hookArgs); hook(MEMORY_FREE, a, hookArgs);
} }
free(a); free(a);
free(p); free(p);
break; break;
} }
a = a->prev; a = a->prev;
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
size_t size_t
MemoryAllocated(void) MemoryAllocated(void)
{ {
MemoryInfo *a; MemoryInfo *a;
size_t total = 0; size_t total = 0;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
total += a->size; total += a->size;
a = a->prev; a = a->prev;
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return total; return total;
} }
void void
MemoryFreeAll(void) MemoryFreeAll(void)
{ {
MemoryInfo *a; MemoryInfo *a;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
MemoryInfo *prev = a->prev; MemoryInfo *prev = a->prev;
free(a->pointer); free(a->pointer);
free(a); free(a);
a = prev; a = prev;
} }
lastAllocation = NULL; lastAllocation = NULL;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
MemoryInfo * MemoryInfo *
MemoryInfoGet(void *p) MemoryInfoGet(void *p)
{ {
MemoryInfo *a; MemoryInfo *a;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
if (a->pointer == p) if (a->pointer == p)
{ {
break; break;
} }
a = a->prev; a = a->prev;
} }
return a; return a;
} }
size_t size_t
MemoryInfoGetSize(MemoryInfo *a) MemoryInfoGetSize(MemoryInfo * a)
{ {
if (!a) if (!a)
{ {
return 0; return 0;
} }
return a->size; return a->size;
} }
const char * const char *
MemoryInfoGetFile(MemoryInfo *a) MemoryInfoGetFile(MemoryInfo * a)
{ {
if (!a) if (!a)
{ {
return NULL; return NULL;
} }
return a->file; return a->file;
} }
const char * const char *
MemoryInfoGetFunc(MemoryInfo *a) MemoryInfoGetFunc(MemoryInfo * a)
{ {
if (!a) if (!a)
{ {
return NULL; return NULL;
} }
return a->func; return a->func;
} }
int int
MemoryInfoGetLine(MemoryInfo *a) MemoryInfoGetLine(MemoryInfo * a)
{ {
if (!a) if (!a)
{ {
return -1; return -1;
} }
return a->line; return a->line;
} }
void * void *
MemoryInfoGetPointer(MemoryInfo *a) MemoryInfoGetPointer(MemoryInfo * a)
{ {
if (!a) if (!a)
{ {
return NULL; return NULL;
} }
return a->pointer; return a->pointer;
} }
void void
MemoryIterate(void (*iterFunc)(MemoryInfo *, void *), void *args) MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
{ {
MemoryInfo *a; MemoryInfo *a;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = lastAllocation; a = lastAllocation;
while (a) while (a)
{ {
iterFunc(a, args); iterFunc(a, args);
a = a->prev; a = a->prev;
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
void void
MemoryHook(void (*memHook)(MemoryAction, MemoryInfo *, void *), void *args) MemoryHook(void (*memHook) (MemoryAction, MemoryInfo *, void *), void *args)
{ {
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
hook = memHook; hook = memHook;
hookArgs = args; hookArgs = args;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }

View file

@ -41,45 +41,46 @@
#include <HttpServer.h> #include <HttpServer.h>
#include <Matrix.h> #include <Matrix.h>
static void TelodendriaMemoryHook(MemoryAction a, MemoryInfo *i, void *args) static void
TelodendriaMemoryHook(MemoryAction a, MemoryInfo * i, void *args)
{ {
LogConfig *lc = (LogConfig *) args; LogConfig *lc = (LogConfig *) args;
char *action; char *action;
switch (a) switch (a)
{ {
case MEMORY_ALLOCATE: case MEMORY_ALLOCATE:
action = "Allocated"; action = "Allocated";
break; break;
case MEMORY_REALLOCATE: case MEMORY_REALLOCATE:
action = "Re-allocated"; action = "Re-allocated";
break; break;
case MEMORY_FREE: case MEMORY_FREE:
action = "Freed"; action = "Freed";
break; break;
default: default:
action = "Unknown operation on"; action = "Unknown operation on";
break; break;
} }
Log(lc, LOG_DEBUG, "%s:%d %s(): %s %lu bytes of memory at %p.", Log(lc, LOG_DEBUG, "%s:%d %s(): %s %lu bytes of memory at %p.",
MemoryInfoGetFile(i), MemoryInfoGetLine(i), MemoryInfoGetFile(i), MemoryInfoGetLine(i),
MemoryInfoGetFunc(i), action, MemoryInfoGetSize(i), MemoryInfoGetFunc(i), action, MemoryInfoGetSize(i),
MemoryInfoGetPointer(i)); MemoryInfoGetPointer(i));
} }
static void static void
TelodendriaMemoryIterator(MemoryInfo *i, void *args) TelodendriaMemoryIterator(MemoryInfo * i, void *args)
{ {
LogConfig *lc = (LogConfig *) args; LogConfig *lc = (LogConfig *) args;
/* We haven't freed the logger memory yet */ /* We haven't freed the logger memory yet */
if (MemoryInfoGetPointer(i) != lc) if (MemoryInfoGetPointer(i) != lc)
{ {
Log(lc, LOG_DEBUG, "%lu bytes of memory leaked from %s() (%s:%d)", Log(lc, LOG_DEBUG, "%lu bytes of memory leaked from %s() (%s:%d)",
MemoryInfoGetSize(i), MemoryInfoGetFunc(i), MemoryInfoGetSize(i), MemoryInfoGetFunc(i),
MemoryInfoGetFile(i), MemoryInfoGetLine(i)); MemoryInfoGetFile(i), MemoryInfoGetLine(i));
} }
} }
static HttpServer *httpServer = NULL; static HttpServer *httpServer = NULL;
@ -156,7 +157,7 @@ main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
MemoryHook(TelodendriaMemoryHook, lc); MemoryHook(TelodendriaMemoryHook, lc);
TelodendriaPrintHeader(lc); TelodendriaPrintHeader(lc);
@ -269,10 +270,10 @@ main(int argc, char **argv)
if (tConfig->flags & TELODENDRIA_LOG_COLOR) if (tConfig->flags & TELODENDRIA_LOG_COLOR)
{ {
LogConfigFlagSet(lc, LOG_FLAG_COLOR); LogConfigFlagSet(lc, LOG_FLAG_COLOR);
} }
else else
{ {
LogConfigFlagClear(lc, LOG_FLAG_COLOR); LogConfigFlagClear(lc, LOG_FLAG_COLOR);
} }
@ -304,23 +305,23 @@ main(int argc, char **argv)
Log(lc, LOG_MESSAGE, "Logging to the log file. Check there for all future messages."); Log(lc, LOG_MESSAGE, "Logging to the log file. Check there for all future messages.");
LogConfigOutputSet(lc, logFile); LogConfigOutputSet(lc, logFile);
} }
else if (tConfig->flags & TELODENDRIA_LOG_STDOUT) else if (tConfig->flags & TELODENDRIA_LOG_STDOUT)
{ {
Log(lc, LOG_DEBUG, "Already logging to standard output."); Log(lc, LOG_DEBUG, "Already logging to standard output.");
} }
else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG) else if (tConfig->flags & TELODENDRIA_LOG_SYSLOG)
{ {
Log(lc, LOG_ERROR, "Logging to the syslog is not yet supported."); Log(lc, LOG_ERROR, "Logging to the syslog is not yet supported.");
exit = EXIT_FAILURE; exit = EXIT_FAILURE;
goto finish; goto finish;
} }
else else
{ {
Log(lc, LOG_ERROR, "Unknown logging method in flags: '%d'", tConfig->flags); Log(lc, LOG_ERROR, "Unknown logging method in flags: '%d'", tConfig->flags);
Log(lc, LOG_ERROR, "This is a programmer error; please report it."); Log(lc, LOG_ERROR, "This is a programmer error; please report it.");
exit = EXIT_FAILURE; exit = EXIT_FAILURE;
goto finish; goto finish;
} }
Log(lc, LOG_DEBUG, "Configuration:"); Log(lc, LOG_DEBUG, "Configuration:");
LogConfigIndent(lc); LogConfigIndent(lc);
@ -444,13 +445,13 @@ finish:
if (httpServer) if (httpServer)
{ {
HttpServerFree(httpServer); HttpServerFree(httpServer);
Log(lc, LOG_DEBUG, "Freed HTTP Server."); Log(lc, LOG_DEBUG, "Freed HTTP Server.");
} }
TelodendriaConfigFree(tConfig); TelodendriaConfigFree(tConfig);
Log(lc, LOG_DEBUG, ""); Log(lc, LOG_DEBUG, "");
MemoryIterate(TelodendriaMemoryIterator, lc); MemoryIterate(TelodendriaMemoryIterator, lc);
Log(lc, LOG_DEBUG, ""); Log(lc, LOG_DEBUG, "");
Log(lc, LOG_DEBUG, "Exiting with code '%d'.", exit); Log(lc, LOG_DEBUG, "Exiting with code '%d'.", exit);
LogConfigFree(lc); LogConfigFree(lc);

View file

@ -356,21 +356,21 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc)
/* Set the actual log output file last */ /* Set the actual log output file last */
if (strcmp(ArrayGet(value, 0), "stdout") == 0) if (strcmp(ArrayGet(value, 0), "stdout") == 0)
{ {
tConfig->flags |= TELODENDRIA_LOG_STDOUT; tConfig->flags |= TELODENDRIA_LOG_STDOUT;
}
else if (strcmp(ArrayGet(value, 0), "file") == 0)
{
tConfig->flags |= TELODENDRIA_LOG_FILE;
}
else if (strcmp(ArrayGet(value, 0), "syslog") == 0)
{
tConfig->flags |= TELODENDRIA_LOG_SYSLOG;
} }
else if (strcmp(ArrayGet(value, 0), "file") == 0)
{
tConfig->flags |= TELODENDRIA_LOG_FILE;
}
else if (strcmp(ArrayGet(value, 0), "syslog") == 0)
{
tConfig->flags |= TELODENDRIA_LOG_SYSLOG;
}
else else
{ {
Log(lc, LOG_ERROR, "Unknown log value '%s', expected 'stdout', 'file', or 'syslog'.", Log(lc, LOG_ERROR, "Unknown log value '%s', expected 'stdout', 'file', or 'syslog'.",
ArrayGet(value, 0)); ArrayGet(value, 0));
goto error; goto error;
} }
return tConfig; return tConfig;

View file

@ -34,7 +34,7 @@ extern void
HashMapMaxLoadSet(HashMap *, float); HashMapMaxLoadSet(HashMap *, float);
extern void extern void
HashMapFunctionSet(HashMap *, unsigned long (*) (const char *)); HashMapFunctionSet(HashMap *, unsigned long (*) (const char *));
extern void * extern void *
HashMapSet(HashMap *, char *, void *); HashMapSet(HashMap *, char *, void *);

View file

@ -1,3 +1,26 @@
/*
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef TELODENDRIA_MEMORY_H #ifndef TELODENDRIA_MEMORY_H
#define TELODENDRIA_MEMORY_H #define TELODENDRIA_MEMORY_H
@ -5,9 +28,9 @@
typedef enum MemoryAction typedef enum MemoryAction
{ {
MEMORY_ALLOCATE, MEMORY_ALLOCATE,
MEMORY_REALLOCATE, MEMORY_REALLOCATE,
MEMORY_FREE MEMORY_FREE
} MemoryAction; } MemoryAction;
#define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__, __FUNCTION__) #define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__, __FUNCTION__)
@ -23,16 +46,16 @@ extern void MemoryFree(void *);
extern size_t MemoryAllocated(void); extern size_t MemoryAllocated(void);
extern void MemoryFreeAll(void); extern void MemoryFreeAll(void);
extern MemoryInfo * MemoryInfoGet(void *); extern MemoryInfo *MemoryInfoGet(void *);
extern size_t MemoryInfoGetSize(MemoryInfo *); extern size_t MemoryInfoGetSize(MemoryInfo *);
extern const char * MemoryInfoGetFile(MemoryInfo *); extern const char *MemoryInfoGetFile(MemoryInfo *);
extern const char * MemoryInfoGetFunc(MemoryInfo *); extern const char *MemoryInfoGetFunc(MemoryInfo *);
extern int MemoryInfoGetLine(MemoryInfo *); extern int MemoryInfoGetLine(MemoryInfo *);
extern void * MemoryInfoGetPointer(MemoryInfo *); extern void *MemoryInfoGetPointer(MemoryInfo *);
extern void MemoryIterate(void (*)(MemoryInfo *, void *), void *); extern void MemoryIterate(void (*) (MemoryInfo *, void *), void *);
extern void MemoryHook(void (*)(MemoryAction, MemoryInfo *, void *), void *); extern void MemoryHook(void (*) (MemoryAction, MemoryInfo *, void *), void *);
#endif #endif

View file

@ -39,9 +39,9 @@ typedef enum TelodendriaConfigFlag
TELODENDRIA_FEDERATION = (1 << 0), TELODENDRIA_FEDERATION = (1 << 0),
TELODENDRIA_REGISTRATION = (1 << 1), TELODENDRIA_REGISTRATION = (1 << 1),
TELODENDRIA_LOG_COLOR = (1 << 2), TELODENDRIA_LOG_COLOR = (1 << 2),
TELODENDRIA_LOG_FILE = (1 << 3), TELODENDRIA_LOG_FILE = (1 << 3),
TELODENDRIA_LOG_STDOUT = (1 << 4), TELODENDRIA_LOG_STDOUT = (1 << 4),
TELODENDRIA_LOG_SYSLOG = (1 << 5) TELODENDRIA_LOG_SYSLOG = (1 << 5)
} TelodendriaConfigFlag; } TelodendriaConfigFlag;
/* /*