aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
blob: 70f9eb6dda9ec61c6ccc70c4ff58dadd4874165f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "utils.h"

#include <stdio.h>
#include <assert.h>
#include <stdarg.h>

result ok(void *value) {
    return (struct result){.value = value, .errmsg = NULL};
}

result err(const char *errmsg) {
    return (struct result){.value = NULL, .errmsg = errmsg};
}

struct allocator {
    void** bufs;
    size_t cap;
    size_t len;
};

allocator* new_allocator() {
    struct allocator * alct = malloc(sizeof(struct allocator));
    alct->bufs = malloc(sizeof(void*) * 16);
    alct->cap = 16;
    alct->len = 0;
    alct->bufs[0] = NULL;
    return alct;
}

void delete_allocator(allocator* alct) {
    for (size_t i = 0; i < alct->len; i++) {
        free(alct->bufs[i]);
    }
    free(alct->bufs);
    free(alct);
}

void * allocate(allocator* alct, size_t size) {
    assert(size > 0);
    if (alct->len >= alct->cap) {
        alct->cap = alct->cap * 2; // Doubling the capacity
        alct->bufs = realloc(alct->bufs, sizeof(void*) * alct->cap);
    }

    void* ptr = malloc(size); // Allocate requested size
    alct->bufs[alct->len] = ptr; // Store pointer in array
    alct->len++;
    return ptr;
}

char* safe_sprintf(allocator* alct, const char* format, ...) {
    va_list args;
    va_list args_copy;
    int length;
    char* buffer;

    va_start(args, format);
    va_copy(args_copy, args);
    
    length = vsnprintf(NULL, 0, format, args);
    
    if (length < 0) {
        va_end(args);
        va_end(args_copy);
        return NULL;
    }

    buffer = (char*)allocate(alct, length + 1);

    if (buffer == NULL) {
        va_end(args);
        va_end(args_copy);
        return NULL;
    }

    vsprintf(buffer, format, args_copy);

    va_end(args);
    va_end(args_copy);
    
    return buffer;
}