-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrtbl.c
More file actions
36 lines (32 loc) · 945 Bytes
/
strtbl.c
File metadata and controls
36 lines (32 loc) · 945 Bytes
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
#include "strtbl.h"
#include <stdlib.h>
#include <string.h>
StrTbl* strtbl_create(void) {
StrTbl* strtab = (StrTbl*)malloc(sizeof (StrTbl));
if (!strtab) return NULL;
strtab->stack = NULL;
strtab->curr = NULL;
strtab->end = NULL;
if ((!strtbl_grow(strtab, KLSTRTAB_EXTRA))) {
free(strtab);
return NULL;
}
return strtab;
}
void strtbl_delete(StrTbl* strtab) {
assert(strtab != NULL && strtab->stack != NULL);
free(strtab->stack);
free(strtab);
}
char* strtbl_grow(StrTbl* strtab, size_t extra) {
size_t expectedcap = strtbl_size(strtab) + extra;
size_t newcap = 2 * strtbl_capacity(strtab);
if (newcap < expectedcap) newcap = expectedcap;
char* newstk = (char*)realloc(strtab->stack, sizeof (char) * newcap);
if ((!newstk)) return NULL;
size_t curroffset = strtbl_size(strtab);
strtab->stack = newstk;
strtab->curr = newstk + curroffset;
strtab->end = newstk + newcap;
return strtab->curr;
}