1
This commit is contained in:
Alex Yatskov 2016-10-31 19:45:05 -07:00
parent dac50bb425
commit 9b91c0690b
2 changed files with 9 additions and 14 deletions

12
util.c
View File

@ -18,19 +18,19 @@
#include "util.h" #include "util.h"
void array_init(Array* arr, size_t init_size) { void array_init(Array* arr, size_t size) {
arr->ptr = (Entry *)malloc(init_size * sizeof(Entry)); arr->ptr = calloc(size, sizeof(void*));
arr->used = 0; arr->used = 0;
arr->size = init_size; arr->size = size;
} }
Entry* array_new(Array* arr) { void array_push(Array* arr, void* data) {
if (arr->used == arr->size) { if (arr->used == arr->size) {
arr->size *= 2; arr->size *= 2;
arr->ptr = (Entry *)realloc(arr->ptr, arr->size * sizeof(Entry)); arr->ptr = realloc(arr->ptr, arr->size * sizeof(void*));
} }
return &arr->ptr[arr->used++]; arr->ptr[arr->used++] = data;
} }
void array_free(Array* arr) { void array_free(Array* arr) {

11
util.h
View File

@ -22,18 +22,13 @@
#include <stdlib.h> #include <stdlib.h>
typedef struct { typedef struct {
char* header; void** ptr;
char* text;
} Entry;
typedef struct {
Entry* ptr;
size_t used; size_t used;
size_t size; size_t size;
} Array; } Array;
void array_init(Array* arr, size_t init_size); void array_init(Array* arr, size_t size);
Entry* array_new(Array* arr); void array_push(Array* arr, void* data);
void array_free(Array* arr); void array_free(Array* arr);
#endif #endif