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;
HashMapBucket **entries;
unsigned long (*hashFunc) (const char *);
unsigned long (*hashFunc) (const char *);
float maxLoad;
size_t iterator;
@ -137,7 +137,7 @@ HashMapCreate(void)
map->count = 0;
map->capacity = 16;
map->iterator = 0;
map->hashFunc = HashMapHashKey;
map->hashFunc = HashMapHashKey;
map->entries = calloc(map->capacity, sizeof(HashMapBucket *));
if (!map->entries)
@ -285,12 +285,12 @@ HashMapMaxLoadSet(HashMap * map, float load)
void
HashMapFunctionSet(HashMap * map, unsigned long (*hashFunc) (const char *))
{
if (!map || !hashFunc)
{
return;
}
if (!map || !hashFunc)
{
return;
}
map->hashFunc = hashFunc;
map->hashFunc = hashFunc;
}
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 <stdio.h>
@ -6,293 +29,293 @@
struct MemoryInfo
{
size_t size;
const char *file;
const char *func;
int line;
void *pointer;
size_t size;
const char *file;
const char *func;
int line;
void *pointer;
MemoryInfo *next;
MemoryInfo *prev;
MemoryInfo *next;
MemoryInfo *prev;
};
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static MemoryInfo *lastAllocation = NULL;
static void (*hook)(MemoryAction, MemoryInfo *, void *) = NULL;
static void (*hook) (MemoryAction, MemoryInfo *, void *) = NULL;
static void *hookArgs = NULL;
void *
MemoryAllocate(size_t size, const char *file, int line, const char *func)
{
void *p;
MemoryInfo *a;
void *p;
MemoryInfo *a;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
p = malloc(size);
if (!p)
{
pthread_mutex_unlock(&lock);
return NULL;
}
p = malloc(size);
if (!p)
{
pthread_mutex_unlock(&lock);
return NULL;
}
a = malloc(sizeof(MemoryInfo));
if (!a)
{
free(p);
pthread_mutex_unlock(&lock);
return NULL;
}
a = malloc(sizeof(MemoryInfo));
if (!a)
{
free(p);
pthread_mutex_unlock(&lock);
return NULL;
}
a->size = size;
a->file = file;
a->line = line;
a->func = func;
a->pointer = p;
a->next = NULL;
a->prev = lastAllocation;
a->size = size;
a->file = file;
a->line = line;
a->func = func;
a->pointer = p;
a->next = NULL;
a->prev = lastAllocation;
if (lastAllocation)
{
lastAllocation->next = a;
}
if (lastAllocation)
{
lastAllocation->next = a;
}
lastAllocation = a;
lastAllocation = a;
if (hook)
{
hook(MEMORY_ALLOCATE, a, hookArgs);
}
if (hook)
{
hook(MEMORY_ALLOCATE, a, hookArgs);
}
pthread_mutex_unlock(&lock);
return p;
pthread_mutex_unlock(&lock);
return p;
}
void *
MemoryReallocate(void *p, size_t size)
{
MemoryInfo *a;
void *new = NULL;
MemoryInfo *a;
void *new = NULL;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
new = realloc(p, size);
if (new)
{
a->pointer = new;
a->size = size;
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
new = realloc(p, size);
if (new)
{
a->pointer = new;
a->size = size;
if (hook)
{
hook(MEMORY_REALLOCATE, a, hookArgs);
}
}
if (hook)
{
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
MemoryFree(void *p)
{
MemoryInfo *a;
MemoryInfo *a;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
a = lastAllocation;
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
if (a->prev)
{
a->prev->next = a->next;
}
else
{
lastAllocation = a->next;
}
while (a)
{
if (a->pointer == p)
{
if (a->prev)
{
a->prev->next = a->next;
}
else
{
lastAllocation = a->next;
}
if (a->next)
{
a->next->prev = a->prev;
}
else
{
lastAllocation = a->prev;
}
if (a->next)
{
a->next->prev = a->prev;
}
else
{
lastAllocation = a->prev;
}
if (hook)
{
hook(MEMORY_FREE, a, hookArgs);
}
if (hook)
{
hook(MEMORY_FREE, a, hookArgs);
}
free(a);
free(p);
free(a);
free(p);
break;
}
a = a->prev;
}
break;
}
pthread_mutex_unlock(&lock);
a = a->prev;
}
pthread_mutex_unlock(&lock);
}
size_t
MemoryAllocated(void)
{
MemoryInfo *a;
size_t total = 0;
MemoryInfo *a;
size_t total = 0;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
a = lastAllocation;
while (a)
{
total += a->size;
a = a->prev;
}
a = lastAllocation;
while (a)
{
total += a->size;
a = a->prev;
}
pthread_mutex_unlock(&lock);
pthread_mutex_unlock(&lock);
return total;
return total;
}
void
MemoryFreeAll(void)
{
MemoryInfo *a;
MemoryInfo *a;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
a = lastAllocation;
while (a)
{
MemoryInfo *prev = a->prev;
a = lastAllocation;
while (a)
{
MemoryInfo *prev = a->prev;
free(a->pointer);
free(a);
free(a->pointer);
free(a);
a = prev;
}
a = prev;
}
lastAllocation = NULL;
lastAllocation = NULL;
pthread_mutex_unlock(&lock);
pthread_mutex_unlock(&lock);
}
MemoryInfo *
MemoryInfoGet(void *p)
{
MemoryInfo *a;
MemoryInfo *a;
pthread_mutex_lock(&lock);
pthread_mutex_lock(&lock);
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
break;
}
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
break;
}
a = a->prev;
}
a = a->prev;
}
return a;
return a;
}
size_t
MemoryInfoGetSize(MemoryInfo *a)
MemoryInfoGetSize(MemoryInfo * a)
{
if (!a)
{
return 0;
}
if (!a)
{
return 0;
}
return a->size;
return a->size;
}
const char *
MemoryInfoGetFile(MemoryInfo *a)
MemoryInfoGetFile(MemoryInfo * a)
{
if (!a)
{
return NULL;
}
if (!a)
{
return NULL;
}
return a->file;
return a->file;
}
const char *
MemoryInfoGetFunc(MemoryInfo *a)
MemoryInfoGetFunc(MemoryInfo * a)
{
if (!a)
{
return NULL;
}
if (!a)
{
return NULL;
}
return a->func;
return a->func;
}
int
MemoryInfoGetLine(MemoryInfo *a)
MemoryInfoGetLine(MemoryInfo * a)
{
if (!a)
{
return -1;
}
if (!a)
{
return -1;
}
return a->line;
return a->line;
}
void *
MemoryInfoGetPointer(MemoryInfo *a)
MemoryInfoGetPointer(MemoryInfo * a)
{
if (!a)
{
return NULL;
}
if (!a)
{
return NULL;
}
return a->pointer;
return a->pointer;
}
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;
while (a)
{
iterFunc(a, args);
a = a->prev;
}
a = lastAllocation;
while (a)
{
iterFunc(a, args);
a = a->prev;
}
pthread_mutex_unlock(&lock);
pthread_mutex_unlock(&lock);
}
void
MemoryHook(void (*memHook)(MemoryAction, MemoryInfo *, void *), void *args)
MemoryHook(void (*memHook) (MemoryAction, MemoryInfo *, void *), void *args)
{
pthread_mutex_lock(&lock);
hook = memHook;
hookArgs = args;
pthread_mutex_unlock(&lock);
pthread_mutex_lock(&lock);
hook = memHook;
hookArgs = args;
pthread_mutex_unlock(&lock);
}

View file

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

View file

@ -356,21 +356,21 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc)
/* Set the actual log output file last */
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
{
Log(lc, LOG_ERROR, "Unknown log value '%s', expected 'stdout', 'file', or 'syslog'.",
ArrayGet(value, 0));
goto error;
Log(lc, LOG_ERROR, "Unknown log value '%s', expected 'stdout', 'file', or 'syslog'.",
ArrayGet(value, 0));
goto error;
}
return tConfig;

View file

@ -34,7 +34,7 @@ extern void
HashMapMaxLoadSet(HashMap *, float);
extern void
HashMapFunctionSet(HashMap *, unsigned long (*) (const char *));
HashMapFunctionSet(HashMap *, unsigned long (*) (const char *));
extern 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
#define TELODENDRIA_MEMORY_H
@ -5,9 +28,9 @@
typedef enum MemoryAction
{
MEMORY_ALLOCATE,
MEMORY_REALLOCATE,
MEMORY_FREE
MEMORY_ALLOCATE,
MEMORY_REALLOCATE,
MEMORY_FREE
} MemoryAction;
#define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__, __FUNCTION__)
@ -23,16 +46,16 @@ extern void MemoryFree(void *);
extern size_t MemoryAllocated(void);
extern void MemoryFreeAll(void);
extern MemoryInfo * MemoryInfoGet(void *);
extern MemoryInfo *MemoryInfoGet(void *);
extern size_t MemoryInfoGetSize(MemoryInfo *);
extern const char * MemoryInfoGetFile(MemoryInfo *);
extern const char * MemoryInfoGetFunc(MemoryInfo *);
extern const char *MemoryInfoGetFile(MemoryInfo *);
extern const char *MemoryInfoGetFunc(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

View file

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