[utils] Adding realloc checks to vector implementation

This commit is contained in:
Al
2016-08-05 23:02:52 -04:00
parent 14c35b35c6
commit 161f18575d

View File

@@ -1,6 +1,8 @@
#ifndef VECTOR_H
#define VECTOR_H
#include <stdio.h>
#define DEFAULT_VECTOR_SIZE 8
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
@@ -25,9 +27,6 @@ static inline void _aligned_free(void *p)
#define MIE_ALIGN(x) __attribute__((aligned(x)))
#endif
#define CONST_128D(var, val) \
MIE_ALIGN(16) static const double var[2] = {(val), (val)}
// Based kvec.h, dynamic vectors of any type
#define __VECTOR_BASE(name, type) typedef struct { size_t n, m; type *a; } name; \
static inline name *name##_new_size(size_t size) { \
@@ -51,28 +50,35 @@ static inline void _aligned_free(void *p)
array->m = size; \
return array; \
} \
static inline void name##_resize(name *array, size_t size) { \
if (size <= array->m) return; \
static inline bool name##_resize(name *array, size_t size) { \
if (size <= array->m) return true; \
type *ptr = realloc(array->a, sizeof(type) * size); \
if (ptr == NULL) return; \
if (ptr == NULL) return false; \
array->a = ptr; \
array->m = size; \
return true; \
} \
static inline void name##_push(name *array, type value) { \
if (array->n == array->m) { \
size_t size = array->m ? array->m << 1 : 2; \
type *ptr = realloc(array->a, sizeof(type) * size); \
if (ptr == NULL) return; \
if (ptr == NULL) { \
fprintf(stderr, "realloc failed during " #name "_push\n"); \
exit(EXIT_FAILURE); \
} \
array->a = ptr; \
array->m = size; \
} \
array->a[array->n++] = value; \
} \
static inline void name##_extend(name *array, name *other) { \
static inline bool name##_extend(name *array, name *other) { \
bool ret = false; \
size_t new_size = array->n + other->n; \
if (new_size > array->m) name##_resize(array, new_size); \
if (new_size > array->m) ret = name##_resize(array, new_size); \
if (!ret) return false; \
memcpy(array->a + array->n, other->a, other->n * sizeof(type)); \
array->n = new_size; \
return ret; \
} \
static inline void name##_pop(name *array) { \
if (array->n > 0) array->n--; \
@@ -80,14 +86,17 @@ static inline void _aligned_free(void *p)
static inline void name##_clear(name *array) { \
array->n = 0; \
} \
static inline void name##_copy(name *dst, name *src, size_t n) { \
if (dst->m < n) name##_resize(dst, n); \
static inline bool name##_copy(name *dst, name *src, size_t n) { \
bool ret = true; \
if (dst->m < n) ret = name##_resize(dst, n); \
if (!ret) return false; \
memcpy(dst->a, src->a, n * sizeof(type)); \
dst->n = n; \
return ret; \
} \
static inline name *name##_new_copy(name *vector, size_t n) { \
name *cpy = name##_new_size(n); \
name##_copy(cpy, vector, n); \
if (!name##_copy(cpy, vector, n)) return NULL; \
return cpy; \
}
@@ -103,7 +112,7 @@ static inline void _aligned_free(void *p)
static inline void name##_destroy(name *array) { \
if (array == NULL) return; \
if (array->a != NULL) { \
for (int i = 0; i < array->n; i++) { \
for (size_t i = 0; i < array->n; i++) { \
free_func(array->a[i]); \
} \
} \
@@ -119,7 +128,4 @@ static inline void _aligned_free(void *p)
__VECTOR_BASE(name, type) \
__VECTOR_DESTROY_FREE_DATA(name, type, free_func)
#endif