Handle allocation failures during parsing
This commit is contained in:
parent
ff97a09343
commit
3dde0a6f39
10 changed files with 288 additions and 122 deletions
|
|
@ -19,13 +19,23 @@ typedef struct {
|
|||
|
||||
static inline Vector vector_new(size_t element_size, size_t capacity) {
|
||||
Vector result;
|
||||
result.contents = ts_calloc(capacity, element_size);
|
||||
result.size = 0;
|
||||
result.capacity = capacity;
|
||||
result.element_size = element_size;
|
||||
|
||||
if (capacity > 0) {
|
||||
result.contents = ts_calloc(capacity, element_size);
|
||||
if (!result.contents)
|
||||
result.element_size = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline bool vector_valid(Vector *self) {
|
||||
return self->element_size > 0;
|
||||
}
|
||||
|
||||
static inline void vector_delete(Vector *self) {
|
||||
ts_free(self->contents);
|
||||
}
|
||||
|
|
@ -53,17 +63,20 @@ static inline void vector_erase(Vector *self, size_t index) {
|
|||
self->size--;
|
||||
}
|
||||
|
||||
static inline void vector_push(Vector *self, void *entry) {
|
||||
static inline bool vector_push(Vector *self, void *entry) {
|
||||
if (self->size == self->capacity) {
|
||||
self->capacity += 4;
|
||||
self->contents =
|
||||
ts_realloc(self->contents, self->capacity * self->element_size);
|
||||
void *new_contents = ts_realloc(self->contents, self->capacity * self->element_size);
|
||||
if (!new_contents)
|
||||
return false;
|
||||
self->contents = new_contents;
|
||||
}
|
||||
|
||||
char *contents = (char *)self->contents;
|
||||
memcpy(contents + (self->size * self->element_size), (char *)entry,
|
||||
self->element_size);
|
||||
self->size++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void vector_reverse(Vector *self) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue