Fixed some misc bugs I found while debugging a memory error.

This commit is contained in:
Jordan Bancino 2022-10-31 11:07:32 -04:00
parent 5ca10f298a
commit ead65e7334
5 changed files with 13 additions and 5 deletions

View file

@ -59,7 +59,7 @@ Phase 2: Building a foundation
[x] Allow logging to the syslog
[ ] Fix bug where the socket stays open after quit.
[ ] Figure out how to write unit tests for array/hashmap/etc
[ ] Fix memory leaks
[x] Fix memory leaks
[ ] Make memory info access O(1)
Phase 3: Welcome to Matrix

View file

@ -82,6 +82,7 @@ HashMapGrow(HashMap * map)
newEntries = Malloc(map->capacity * sizeof(HashMapBucket *));
if (!newEntries)
{
map->capacity /= 2;
return 0;
}

View file

@ -424,7 +424,7 @@ HttpParamDecode(char *in)
{
/* Malformed param */
Free(buf);
Free(params);
HashMapFree(params);
return NULL;
}

View file

@ -90,11 +90,16 @@ MemoryAllocate(size_t size, const char *file, int line)
}
void *
MemoryReallocate(void *p, size_t size)
MemoryReallocate(void *p, size_t size, const char *file, int line)
{
MemoryInfo *a;
void *new = NULL;
if (!p)
{
return MemoryAllocate(size, file, line);
}
pthread_mutex_lock(&lock);
a = lastAllocation;
@ -107,6 +112,8 @@ MemoryReallocate(void *p, size_t size)
{
a->pointer = new;
a->size = size;
a->file = file;
a->line = line;
if (hook)
{

View file

@ -35,13 +35,13 @@ typedef enum MemoryAction
} MemoryAction;
#define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__)
#define Realloc(x, s) MemoryReallocate(x, s)
#define Realloc(x, s) MemoryReallocate(x, s, __FILE__, __LINE__)
#define Free(x) MemoryFree(x)
typedef struct MemoryInfo MemoryInfo;
extern void *MemoryAllocate(size_t, const char *, int);
extern void *MemoryReallocate(void *, size_t);
extern void *MemoryReallocate(void *, size_t, const char *, int);
extern void MemoryFree(void *);
extern size_t MemoryAllocated(void);