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.

This commit is contained in:
2022-01-17 17:21:40 +00:00
parent 2c18b593ab
commit 929f2a7f4b
3 changed files with 58 additions and 48 deletions
+2 -2
View File
@@ -1,6 +1,7 @@
#ifndef LIST_H #ifndef LIST_H
#define LIST_H #define LIST_H
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -14,8 +15,7 @@ typedef struct {
} List; } List;
List* CreateList(void); List* CreateList(void);
int AddListItem(const char *, List *); int AddListItem(const void *, size_t, List *);
void PrintList(const List*);
void DestroyList(List*); void DestroyList(List*);
#endif #endif
+9 -22
View File
@@ -1,5 +1,4 @@
#include "../includes/list.h" #include "../includes/list.h"
#include <stdio.h>
List* CreateList() { List* CreateList() {
List *new = malloc(sizeof(List)); List *new = malloc(sizeof(List));
@@ -9,7 +8,7 @@ List* CreateList() {
return NULL; return NULL;
} }
new->root = malloc(sizeof(char*) * LISTDEFAULTSIZE); new->root = malloc(sizeof(void*) * LISTDEFAULTSIZE);
if (!new->root) { if (!new->root) {
fprintf(stderr, "Failed to malloc() memory for List contents.\n"); fprintf(stderr, "Failed to malloc() memory for List contents.\n");
@@ -23,14 +22,16 @@ List* CreateList() {
return new; return new;
} }
int AddListItem(const char *value, List* list) { int AddListItem(const void *value, size_t size, List* list) {
if (!list) return -1; if (!list) return -1;
if (!value) return -1;
if (size == 0) return - 1;
if (list->capacity < list->size + 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. //Note: realloc will free list->root if it succeeds.
if (!ptr) { 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; return -1;
} }
@@ -38,16 +39,14 @@ int AddListItem(const char *value, List* list) {
list->capacity = list->capacity * 2; list->capacity = list->capacity * 2;
} }
unsigned long valueLength = strlen(value) + 1; //Plus one for the null byte. void* item = malloc(size);
char* item = malloc(valueLength);
if (!item) { 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; return -1;
} }
strncpy(item, value, valueLength); memcpy(item, value, size);
list->root[list->size] = item; list->root[list->size] = item;
list->size++; list->size++;
@@ -55,18 +54,6 @@ int AddListItem(const char *value, List* list) {
return 0; 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) { void DestroyList(List* list) {
if (!list) return; if (!list) return;
+47 -24
View File
@@ -11,6 +11,7 @@ typedef struct {
void print_file(char*); void print_file(char*);
List* get_strings(const char*); List* get_strings(const char*);
void PrintList(const List*);
int main(int argc, char* args[]) { int main(int argc, char* args[]) {
const char *input_files[argc - 1]; const char *input_files[argc - 1];
@@ -25,20 +26,29 @@ int main(int argc, char* args[]) {
// print_file(args[i]); // print_file(args[i]);
// } // }
print_file(args[1]); //print_file(args[1]);
// char str[16]; struct test {
// List *list = CreateList(); 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++) { for (int i = 0; i < 64; i++) {
// sprintf(str, "%d", i); sprintf(thing->SomeText, "%d", i);
// AddListItem(str, list); 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);
}
// free(list); //PrintList(list);
free(thing->SomeText);
free(list);
} }
void print_file(char* file_path) { void print_file(char* file_path) {
@@ -58,23 +68,24 @@ void print_file(char* file_path) {
List* list = get_strings(line); List* list = get_strings(line);
if (!list) continue; if (!list) continue;
if (list->size == 0) continue;
Instruction *inst = malloc(sizeof(Instruction)); // Instruction *inst = malloc(sizeof(Instruction));
inst->parameters = malloc(sizeof(char*) * 2); // inst->parameters = malloc(sizeof(char*) * 2);
if (strcmp(list->root[0], "mov") == 0) { // if (strcmp(list->root[0], "mov") == 0) {
inst->opcode = "mov"; // inst->opcode = "mov";
inst->count = 2; // inst->count = 2;
inst->parameters[0] = list->root[1]; // inst->parameters[0] = list->root[1];
inst->parameters[1] = list->root[2]; // 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->parameters);
free(inst); // free(inst);
//PrintList(list); PrintList(list);
DestroyList(list); DestroyList(list);
} }
@@ -96,7 +107,7 @@ List* get_strings(const char* line) {
if (line[i] == ' ' || line[i] == ',') { if (line[i] == ' ' || line[i] == ',') {
if (index > 0) { if (index > 0) {
currentWord[index] = '\0'; currentWord[index] = '\0';
AddListItem(currentWord, list); AddListItem(currentWord, strlen(currentWord) + 1, list);
} }
index = 0; index = 0;
continue; continue;
@@ -109,7 +120,7 @@ List* get_strings(const char* line) {
index++; index++;
} }
currentWord[index] = '\0'; currentWord[index] = '\0';
AddListItem(currentWord, list); AddListItem(currentWord, strlen(currentWord) + 1, list);
} }
break; break;
} }
@@ -120,3 +131,15 @@ List* get_strings(const char* line) {
return list; 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]);
}
}