Fix memory leaks, more memory tracker stuff as well.

This commit is contained in:
Dale Weiler 2012-04-17 17:16:11 -04:00
parent 477e80f1fb
commit 2bef34a4f7
5 changed files with 58 additions and 18 deletions

View file

@ -154,6 +154,13 @@ typedef struct {
int offset; /* location in globals */
} globals;
VECTOR_MAKE(globals, assembly_constants);
void asm_clear() {
size_t i = 0;
for (; i < assembly_constants_elements; i++)
mem_d(assembly_constants_data[i].name);
mem_d(assembly_constants_data);
}
void asm_parse(FILE *fp) {
char *data = NULL;
@ -321,8 +328,8 @@ void asm_parse(FILE *fp) {
printf("%li: Invalid statement, expression, or decleration\n", line);
end:
//free(data);
mem_d(data);
line ++;
}
asm_clear();
}

19
gmqcc.h
View file

@ -158,6 +158,8 @@ int typedef_add (struct lex_file *file, const char *, const char *);
//===================================================================
void *util_memory_a(unsigned int, unsigned int, const char *);
void util_memory_d(void *, unsigned int, const char *);
void util_meminfo ();
char *util_strdup (const char *);
char *util_strrq (char *);
char *util_strrnl (char *);
@ -185,10 +187,11 @@ int util_getline (char **, size_t *, FILE *);
} \
void *temp = mem_a(N##_allocated * sizeof(T)); \
if (!temp) { \
free(temp); \
mem_d(temp); \
return -1; \
} \
memcpy(temp, N##_data, (N##_elements * sizeof(T))); \
mem_d(N##_data); \
N##_data = (T*)temp; \
} \
N##_data[N##_elements] = element; \
@ -211,13 +214,13 @@ int util_getline (char **, size_t *, FILE *);
* Each paramater incerements by 3 since vector types hold
* 3 components (x,y,z).
*/
#define OFS_NULL 0
#define OFS_RETURN 1
#define OFS_PARM0 (OFS_RETURN+3)
#define OFS_PARM1 (OFS_PARM0 +3)
#define OFS_PARM2 (OFS_PARM1 +3)
#define OFS_PARM3 (OFS_PARM2 +3)
#define OFS_PARM4 (OFS_PARM3 +3)
#define OFS_NULL 0
#define OFS_RETURN 1
#define OFS_PARM0 (OFS_RETURN+3)
#define OFS_PARM1 (OFS_PARM0 +3)
#define OFS_PARM2 (OFS_PARM1 +3)
#define OFS_PARM3 (OFS_PARM2 +3)
#define OFS_PARM4 (OFS_PARM3 +3)
#define OFS_PARM5 (OFS_PARM4 +3)
#define OFS_PARM6 (OFS_PARM5 +3)
#define OFS_PARM7 (OFS_PARM6 +3)

1
main.c
View file

@ -33,5 +33,6 @@ int main(int argc, char **argv) {
asm_init ("test.qs", &fp);
asm_parse(fp);
asm_close(fp);
util_meminfo();
return 0;
}

View file

@ -291,5 +291,14 @@ int parse_gen(struct lex_file *file) {
}
compile_constant_debug();
lex_reset(file);
/* free constants */
{
size_t i;
for (; i < compile_constants_elements; i++) {
mem_d(compile_constants_data[i].name);
mem_d(compile_constants_data[i].string);
}
mem_d(compile_constants_data);
}
return 1;
}

38
util.c
View file

@ -23,7 +23,12 @@
#include <stdarg.h>
#include <errno.h>
#include "gmqcc.h"
unsigned long long mem_ab = 0;
unsigned long long mem_db = 0;
unsigned long long mem_at = 0;
unsigned long long mem_dt = 0;
struct memblock_t {
const char *file;
unsigned int line;
@ -31,14 +36,17 @@ struct memblock_t {
};
void *util_memory_a(unsigned int byte, unsigned int line, const char *file) {
struct memblock_t *data = malloc(sizeof(struct memblock_t) + byte);
struct memblock_t *info = malloc(sizeof(struct memblock_t) + byte);
void *data =(void*)((uintptr_t)info+sizeof(struct memblock_t));
if (!data) return NULL;
data->line = line;
data->byte = byte;
data->file = file;
info->line = line;
info->byte = byte;
info->file = file;
util_debug("MEM", "allocation: %08u (bytes) at %s:%u\n", byte, file, line);
return (void*)((uintptr_t)data+sizeof(struct memblock_t));
util_debug("MEM", "allocation: %08u (bytes) address 0x%08X @ %s:%u\n", byte, data, file, line);
mem_at++;
mem_ab += info->byte;
return data;
}
void util_memory_d(void *ptrn, unsigned int line, const char *file) {
@ -46,10 +54,23 @@ void util_memory_d(void *ptrn, unsigned int line, const char *file) {
void *data = (void*)((uintptr_t)ptrn-sizeof(struct memblock_t));
struct memblock_t *info = (struct memblock_t*)data;
util_debug("MEM", "released: %08u (bytes) at %s:%u\n", info->byte, file, line);
util_debug("MEM", "released: %08u (bytes) address 0x%08X @ %s:%u\n", info->byte, data, file, line);
mem_db += info->byte;
mem_dt++;
free(data);
}
void util_meminfo() {
util_debug("MEM", "Memory information:\n\
Total allocations: %llu\n\
Total deallocations: %llu\n\
Total allocated: %llu (bytes)\n\
Total deallocated: %llu (bytes)\n",
mem_at, mem_dt,
mem_ab, mem_db
);
}
#ifndef mem_d
#define mem_d(x) util_memory_d((x), __LINE__, __FILE__)
#endif
@ -156,7 +177,6 @@ int util_getline(char **lineptr, size_t *n, FILE *stream) {
chr = *n + *lineptr - pos;
strcpy(tmp,*lineptr);
if (!(*lineptr = tmp))
return -1;