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"
void array_init(Array* arr, size_t init_size) {
arr->ptr = (Entry *)malloc(init_size * sizeof(Entry));
void array_init(Array* arr, size_t size) {
arr->ptr = calloc(size, sizeof(void*));
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) {
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) {

11
util.h
View File

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