From 929f2a7f4b4f20d74ba3667b5fefab2724b6a19f Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Mon, 17 Jan 2022 17:21:40 +0000 Subject: [PATCH] Made the List generic, though more care needs to be taken when using it since memory leaks can happen when structs are used with it. --- includes/list.h | 4 +-- src/list.c | 31 +++++++-------------- src/main.c | 71 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/includes/list.h b/includes/list.h index 6d01119..ea9fbd7 100644 --- a/includes/list.h +++ b/includes/list.h @@ -1,6 +1,7 @@ #ifndef LIST_H #define LIST_H +#include #include #include #include @@ -14,8 +15,7 @@ typedef struct { } List; List* CreateList(void); -int AddListItem(const char *, List *); -void PrintList(const List*); +int AddListItem(const void *, size_t, List *); void DestroyList(List*); #endif \ No newline at end of file diff --git a/src/list.c b/src/list.c index b10b953..7268d79 100644 --- a/src/list.c +++ b/src/list.c @@ -1,5 +1,4 @@ #include "../includes/list.h" -#include List* CreateList() { List *new = malloc(sizeof(List)); @@ -9,7 +8,7 @@ List* CreateList() { return NULL; } - new->root = malloc(sizeof(char*) * LISTDEFAULTSIZE); + new->root = malloc(sizeof(void*) * LISTDEFAULTSIZE); if (!new->root) { fprintf(stderr, "Failed to malloc() memory for List contents.\n"); @@ -23,14 +22,16 @@ List* CreateList() { return new; } -int AddListItem(const char *value, List* list) { +int AddListItem(const void *value, size_t size, List* list) { if (!list) return -1; + if (!value) return -1; + if (size == 0) return - 1; if (list->capacity < list->size + 1) { - void* ptr = realloc(list->root, sizeof(char*) * list->capacity * 2); + void* ptr = realloc(list->root, sizeof(void*) * list->capacity * 2); //Note: realloc will free list->root if it succeeds. if (!ptr) { - fprintf(stderr, "Failed to resize array with realloc() for new item '%s'.\n", value); + fprintf(stderr, "Failed to resize array with realloc() (%d bytes).\n", list->capacity * 2); return -1; } @@ -38,16 +39,14 @@ int AddListItem(const char *value, List* list) { list->capacity = list->capacity * 2; } - unsigned long valueLength = strlen(value) + 1; //Plus one for the null byte. - - char* item = malloc(valueLength); + void* item = malloc(size); if (!item) { - fprintf(stderr, "Failed to malloc() new memory for '%s' (length: %lu bytes).\n", value, valueLength); + fprintf(stderr, "Failed to malloc() new memory (%lu bytes).\n", size); return -1; } - strncpy(item, value, valueLength); + memcpy(item, value, size); list->root[list->size] = item; list->size++; @@ -55,18 +54,6 @@ int AddListItem(const char *value, List* list) { return 0; } -void PrintList(const List* list) { - if (!list) return; - - const List *current = list; - - printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - - for(int i = 0; i < list->size; i++) { - printf("%d: %s\n", i, list->root[i]); - } -} - void DestroyList(List* list) { if (!list) return; diff --git a/src/main.c b/src/main.c index 428665b..25d9998 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,7 @@ typedef struct { void print_file(char*); List* get_strings(const char*); +void PrintList(const List*); int main(int argc, char* args[]) { const char *input_files[argc - 1]; @@ -25,20 +26,29 @@ int main(int argc, char* args[]) { // print_file(args[i]); // } - print_file(args[1]); - // char str[16]; - // List *list = CreateList(); + //print_file(args[1]); + struct test { + int SomeValue; + char* SomeText; + }; + struct test* thing = malloc(sizeof(struct test)); + thing->SomeText = calloc(1, 16); + List *list = CreateList(); - // for (int i = 0; i < 64; i++) { - // sprintf(str, "%d", i); - // AddListItem(str, list); + for (int i = 0; i < 64; i++) { + sprintf(thing->SomeText, "%d", i); + thing->SomeValue = i; + AddListItem(thing, sizeof(struct test), list); - // if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - // } + if (i % 4 == 0) printf("Size: %d; Capacity: %d\n", list->size, list->capacity); - // PrintList(list); + printf("Some Value: %d Some Text: '%s'\n", ((struct test*) list->root[list->size - 1])->SomeValue, ((struct test*) list->root[list->size - 1])->SomeText); + } + + //PrintList(list); + free(thing->SomeText); - // free(list); + free(list); } void print_file(char* file_path) { @@ -58,23 +68,24 @@ void print_file(char* file_path) { List* list = get_strings(line); if (!list) continue; + if (list->size == 0) continue; - Instruction *inst = malloc(sizeof(Instruction)); - inst->parameters = malloc(sizeof(char*) * 2); + // Instruction *inst = malloc(sizeof(Instruction)); + // inst->parameters = malloc(sizeof(char*) * 2); - if (strcmp(list->root[0], "mov") == 0) { - inst->opcode = "mov"; - inst->count = 2; - inst->parameters[0] = list->root[1]; - inst->parameters[1] = list->root[2]; + // if (strcmp(list->root[0], "mov") == 0) { + // inst->opcode = "mov"; + // inst->count = 2; + // inst->parameters[0] = list->root[1]; + // inst->parameters[1] = list->root[2]; - printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]); - } + // printf("Instruction '%s' with %d params ('%s', '%s')\n", inst->opcode, inst->count, inst->parameters[0], inst->parameters[1]); + // } - free(inst->parameters); - free(inst); + // free(inst->parameters); + // free(inst); - //PrintList(list); + PrintList(list); DestroyList(list); } @@ -96,7 +107,7 @@ List* get_strings(const char* line) { if (line[i] == ' ' || line[i] == ',') { if (index > 0) { currentWord[index] = '\0'; - AddListItem(currentWord, list); + AddListItem(currentWord, strlen(currentWord) + 1, list); } index = 0; continue; @@ -109,7 +120,7 @@ List* get_strings(const char* line) { index++; } currentWord[index] = '\0'; - AddListItem(currentWord, list); + AddListItem(currentWord, strlen(currentWord) + 1, list); } break; } @@ -119,4 +130,16 @@ List* get_strings(const char* line) { } return list; +} + +void PrintList(const List* list) { + if (!list) return; + + const List *current = list; + + printf("Size: %d; Capacity: %d\n", list->size, list->capacity); + + for(int i = 0; i < list->size; i++) { + printf("%d: %s\n", i, list->root[i]); + } } \ No newline at end of file