-#define MEM_VECTOR_PROTO(Towner, Tmem, mem) \
- bool GMQCC_WARN Towner##_##mem##_add(Towner*, Tmem); \
- bool GMQCC_WARN Towner##_##mem##_remove(Towner*, size_t)
-
-#define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem) \
- MEM_VECTOR_PROTO(Towner, Tmem, mem); \
- bool GMQCC_WARN Towner##_##mem##_find(Towner*, Tmem, size_t*); \
- void Towner##_##mem##_clear(Towner*)
-
-#define MEM_VECTOR_MAKE(Twhat, name) \
- Twhat *name; \
- size_t name##_count; \
- size_t name##_alloc
-
-#define MEM_VEC_FUN_ADD(Tself, Twhat, mem) \
-bool GMQCC_WARN Tself##_##mem##_add(Tself *self, Twhat f) \
-{ \
- Twhat *reall; \
- if (self->mem##_count == self->mem##_alloc) { \
- if (!self->mem##_alloc) { \
- self->mem##_alloc = 16; \
- } else { \
- self->mem##_alloc *= 2; \
- } \
- reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc); \
- if (!reall) { \
- return false; \
- } \
- memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
- mem_d(self->mem); \
- self->mem = reall; \
- } \
- self->mem[self->mem##_count++] = f; \
- return true; \
-}
-
-#define MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
-bool GMQCC_WARN Tself##_##mem##_remove(Tself *self, size_t idx) \
-{ \
- size_t i; \
- Twhat *reall; \
- if (idx >= self->mem##_count) { \
- return true; /* huh... */ \
- } \
- for (i = idx; i < self->mem##_count-1; ++i) { \
- self->mem[i] = self->mem[i+1]; \
- } \
- self->mem##_count--; \
- if (self->mem##_count < self->mem##_count/2) { \
- self->mem##_alloc /= 2; \
- reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count); \
- if (!reall) { \
- return false; \
- } \
- memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
- mem_d(self->mem); \
- self->mem = reall; \
- } \
- return true; \
-}
-
-#define MEM_VEC_FUN_FIND(Tself, Twhat, mem) \
-bool GMQCC_WARN Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
-{ \
- size_t i; \
- for (i = 0; i < self->mem##_count; ++i) { \
- if (self->mem[i] == obj) { \
- if (idx) { \
- *idx = i; \
- } \
- return true; \
- } \
- } \
- return false; \
-}
-
-#define MEM_VEC_FUN_APPEND(Tself, Twhat, mem) \
-bool GMQCC_WARN Tself##_##mem##_append(Tself *s, Twhat *p, size_t c) \
-{ \
- Twhat *reall; \
- size_t oldalloc; \
- if (s->mem##_count+c > s->mem##_alloc) { \
- if (!s->mem##_alloc) { \
- s->mem##_alloc = c < 16 ? 16 : c; \
- s->mem = (Twhat*)mem_a(sizeof(Twhat) * s->mem##_alloc); \
- } else { \
- oldalloc = s->mem##_alloc; \
- s->mem##_alloc *= 2; \
- if (s->mem##_count+c >= s->mem##_alloc) { \
- s->mem##_alloc = s->mem##_count+c; \
- } \
- reall = (Twhat*)mem_a(sizeof(Twhat) * s->mem##_alloc); \
- if (!reall) { \
- s->mem##_alloc = oldalloc; \
- return false; \
- } \
- memcpy(reall, s->mem, sizeof(Twhat) * s->mem##_count); \
- mem_d(s->mem); \
- s->mem = reall; \
- } \
- } \
- memcpy(&s->mem[s->mem##_count], p, c*sizeof(*p)); \
- s->mem##_count += c; \
- return true; \
-}
-
-#define MEM_VEC_FUN_RESIZE(Tself, Twhat, mem) \
-bool GMQCC_WARN Tself##_##mem##_resize(Tself *s, size_t c) \
-{ \
- Twhat *reall; \
- if (c > s->mem##_alloc) { \
- reall = (Twhat*)mem_a(sizeof(Twhat) * c); \
- if (!reall) { return false; } \
- memcpy(reall, s->mem, sizeof(Twhat) * s->mem##_count); \
- s->mem##_alloc = c; \
- s->mem##_count = c; \
- mem_d(s->mem); \
- s->mem = reall; \
- return true; \
- } \
- s->mem##_count = c; \
- if (c < (s->mem##_alloc / 2)) { \
- reall = (Twhat*)mem_a(sizeof(Twhat) * c); \
- if (!reall) { return false; } \
- memcpy(reall, s->mem, sizeof(Twhat) * c); \
- mem_d(s->mem); \
- s->mem = reall; \
- s->mem##_alloc = c; \
- } \
- return true; \
-}
-
-#define MEM_VEC_FUN_CLEAR(Tself, mem) \
-void Tself##_##mem##_clear(Tself *self) \
-{ \
- if (!self->mem) \
- return; \
- mem_d((void*) self->mem); \
- self->mem = NULL; \
- self->mem##_count = 0; \
- self->mem##_alloc = 0; \
-}
-
-#define MEM_VECTOR_CLEAR(owner, mem) \
- if ((owner)->mem) \
- mem_d((void*)((owner)->mem)); \
- (owner)->mem = NULL; \
- (owner)->mem##_count = 0; \
- (owner)->mem##_alloc = 0
-
-#define MEM_VECTOR_INIT(owner, mem) \
-{ \
- (owner)->mem = NULL; \
- (owner)->mem##_count = 0; \
- (owner)->mem##_alloc = 0; \
-}
-
-#define MEM_VECTOR_MOVE(from, mem, to, tm) \
-{ \
- (to)->tm = (from)->mem; \
- (to)->tm##_count = (from)->mem##_count; \
- (to)->tm##_alloc = (from)->mem##_alloc; \
- (from)->mem = NULL; \
- (from)->mem##_count = 0; \
- (from)->mem##_alloc = 0; \
-}
-
-#define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
-MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
-MEM_VEC_FUN_ADD(Tself, Twhat, mem)
-
-#define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
-MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
-MEM_VEC_FUN_CLEAR(Tself, mem) \
-MEM_VEC_FUN_FIND(Tself, Twhat, mem)