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 [x] Allow logging to the syslog
[ ] Fix bug where the socket stays open after quit. [ ] Fix bug where the socket stays open after quit.
[ ] Figure out how to write unit tests for array/hashmap/etc [ ] Figure out how to write unit tests for array/hashmap/etc
[ ] Fix memory leaks [x] Fix memory leaks
[ ] Make memory info access O(1) [ ] Make memory info access O(1)
Phase 3: Welcome to Matrix Phase 3: Welcome to Matrix

View file

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

View file

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

View file

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

View file

@ -35,13 +35,13 @@ typedef enum MemoryAction
} MemoryAction; } MemoryAction;
#define Malloc(x) MemoryAllocate(x, __FILE__, __LINE__) #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) #define Free(x) MemoryFree(x)
typedef struct MemoryInfo MemoryInfo; typedef struct MemoryInfo MemoryInfo;
extern void *MemoryAllocate(size_t, const char *, int); 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 void MemoryFree(void *);
extern size_t MemoryAllocated(void); extern size_t MemoryAllocated(void);