Annotation of embedaddon/lighttpd/src/vector.h, revision 1.1
1.1 ! misho 1: #ifndef LI_VECTOR_H
! 2: #define LI_VECTOR_H
! 3: #include "first.h"
! 4:
! 5: #ifndef SIZE_MAX
! 6: # ifdef SIZE_T_MAX
! 7: # define SIZE_MAX SIZE_T_MAX
! 8: # else
! 9: # define SIZE_MAX ((size_t)~0)
! 10: # endif
! 11: #endif
! 12:
! 13: #include <stdlib.h>
! 14: #include <string.h>
! 15:
! 16: static inline size_t vector_align_size(size_t s) {
! 17: size_t a = (s + 16) & ((size_t)~15);
! 18: return (a < s) ? s : a;
! 19: }
! 20:
! 21: void *vector_realloc(void *data, size_t elem_size, size_t size, size_t used);
! 22:
! 23: #define DEFINE_TYPED_VECTOR(name, entry, release) \
! 24: typedef struct vector_ ## name { \
! 25: entry* data; \
! 26: size_t used; \
! 27: size_t size; \
! 28: } vector_ ## name; \
! 29: static inline void vector_ ## name ## _init(vector_ ## name *v) { \
! 30: v->data = NULL; \
! 31: v->used = v->size = 0; \
! 32: } \
! 33: static inline vector_ ## name *vector_ ## name ## _alloc() { \
! 34: vector_ ## name *v = malloc(sizeof(*v)); \
! 35: force_assert(NULL != v); \
! 36: vector_ ## name ## _init(v); \
! 37: return v; \
! 38: } \
! 39: static inline void vector_ ## name ## _clear(vector_ ## name *v) { \
! 40: size_t ndx; \
! 41: vector_ ## name vcopy = *v; \
! 42: vector_ ## name ## _init(v); \
! 43: if (release) for (ndx = 0; ndx < vcopy.used; ++ndx) release(vcopy.data[ndx]); \
! 44: free(vcopy.data); \
! 45: } \
! 46: static inline void vector_ ## name ## _free(vector_ ## name *v) { \
! 47: if (NULL != v) { \
! 48: vector_ ## name ## _clear(v); \
! 49: free(v); \
! 50: } \
! 51: } \
! 52: static inline void vector_ ## name ## _reserve(vector_ ## name *v, size_t p) { \
! 53: force_assert(v->used < SIZE_MAX - p); \
! 54: if (v->size < v->used + p) { \
! 55: v->size = vector_align_size(v->used + p); \
! 56: v->data = vector_realloc(v->data, sizeof(entry), v->size, v->used); \
! 57: } \
! 58: } \
! 59: static inline void vector_ ## name ## _push(vector_ ## name *v, entry e) { \
! 60: vector_ ## name ## _reserve(v, 1); \
! 61: v->data[v->used++] = e; \
! 62: } \
! 63: static inline entry vector_ ## name ## _pop(vector_ ## name *v) { \
! 64: force_assert(v->used > 0); \
! 65: return v->data[--v->used]; \
! 66: } \
! 67: struct vector_ ## name /* expect trailing semicolon */ \
! 68: /* end of DEFINE_TYPED_VECTOR */
! 69:
! 70: #define DEFINE_TYPED_VECTOR_NO_RELEASE(name, entry) \
! 71: DEFINE_TYPED_VECTOR(name, entry, ((void(*)(entry)) NULL)) \
! 72: /* end of DEFINE_TYPED_VECTOR_NO_RELEASE */
! 73:
! 74:
! 75: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>