Browse Source

[core] use ck_assert() in vector.[ch]

master
Glenn Strauss 3 months ago
parent
commit
699209103c
  1. 4
      src/vector.c
  2. 8
      src/vector.h

4
src/vector.c

@ -12,9 +12,9 @@ void *vector_malloc(size_t sz) { return malloc(sz); }
void *vector_realloc(void *data, size_t elem_size, size_t size, size_t used) {
const size_t total_size = elem_size * size;
const size_t used_size = elem_size * used;
force_assert(size <= SIZE_MAX / elem_size);
ck_assert(size <= SIZE_MAX / elem_size);
data = realloc(data, total_size);
force_assert(NULL != data);
ck_assert(NULL != data);
/* clear new memory */
memset(((char*)data) + used_size, 0, total_size - used_size);

8
src/vector.h

@ -2,7 +2,7 @@
#define LI_VECTOR_H
#include "first.h"
#include "buffer.h" /* force_assert() */
#include "ck.h" /* ck_assert() */
static inline size_t vector_align_size(size_t s) {
size_t a = (s + 15) & ~(size_t)15uL;
@ -31,7 +31,7 @@ void *vector_realloc(void *data, size_t elem_size, size_t size, size_t used);
__attribute_returns_nonnull__ \
static inline vector_ ## name *vector_ ## name ## _alloc() { \
vector_ ## name *v = vector_malloc(sizeof(*v)); \
force_assert(NULL != v); \
ck_assert(NULL != v); \
vector_ ## name ## _init(v); \
return v; \
} \
@ -48,7 +48,7 @@ void *vector_realloc(void *data, size_t elem_size, size_t size, size_t used);
} \
static inline void vector_ ## name ## _reserve(vector_ ## name *v, size_t p) { \
if (v->size - v->used < p) { \
force_assert(v->used < SIZE_MAX - p); \
ck_assert(v->used < SIZE_MAX - p); \
v->size = vector_align_size(v->used + p); \
v->data = vector_realloc(v->data, sizeof(entry), v->size, v->used); \
} \
@ -58,7 +58,7 @@ void *vector_realloc(void *data, size_t elem_size, size_t size, size_t used);
v->data[v->used++] = e; \
} \
static inline entry vector_ ## name ## _pop(vector_ ## name *v) { \
force_assert(v->used > 0); \
ck_assert(v->used > 0); \
return v->data[--v->used]; \
} \
struct vector_ ## name /* expect trailing semicolon */ \

Loading…
Cancel
Save