Get rid of realloc

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-04-27 11:41:12 +02:00
parent 69173876f1
commit 4b6659bfe1

74
astir.h
View file

@ -24,8 +24,8 @@
#define ASTIR_COMMON_H__ #define ASTIR_COMMON_H__
#define MEM_VECTOR_PROTO(Towner, Tmem, mem) \ #define MEM_VECTOR_PROTO(Towner, Tmem, mem) \
void Towner##_##mem##_add(Towner*, Tmem); \ bool Towner##_##mem##_add(Towner*, Tmem); \
void Towner##_##mem##_remove(Towner*, size_t); bool Towner##_##mem##_remove(Towner*, size_t);
#define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem) \ #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem) \
MEM_VECTOR_PROTO(Towner, Tmem, mem) \ MEM_VECTOR_PROTO(Towner, Tmem, mem) \
@ -37,35 +37,51 @@
size_t name##_count; \ size_t name##_count; \
size_t name##_alloc size_t name##_alloc
#define _MEM_VEC_FUN_ADD(Tself, Twhat, mem) \ #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem) \
void Tself##_##mem##_add(Tself *self, Twhat f) \ bool Tself##_##mem##_add(Tself *self, Twhat f) \
{ \ { \
if (self->mem##_count == self->mem##_alloc) { \ Twhat *reall; \
if (!self->mem##_alloc) \ if (self->mem##_count == self->mem##_alloc) { \
self->mem##_alloc = 16; \ if (!self->mem##_alloc) \
else \ self->mem##_alloc = 16; \
self->mem##_alloc *= 2; \ else \
self->mem = (Twhat*)realloc(self->mem, \ self->mem##_alloc *= 2; \
sizeof(Twhat) * self->mem##_alloc); \ reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc); \
} \ if (!reall) { \
self->mem[self->mem##_count++] = f; \ MEM_VECTOR_CLEAR(self, mem); \
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) \ #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem) \
void Tself##_##mem##_remove(Tself *self, size_t idx) \ bool Tself##_##mem##_remove(Tself *self, size_t idx) \
{ \ { \
size_t i; \ size_t i; \
if (idx >= self->mem##_count) \ Twhat *reall; \
return; \ if (idx >= self->mem##_count) \
for (i = idx; i < self->mem##_count-1; ++i) \ return true; /* huh... */ \
self->mem[i] = self->mem[i+1]; \ for (i = idx; i < self->mem##_count-1; ++i) \
self->mem##_count--; \ self->mem[i] = self->mem[i+1]; \
if (self->mem##_count < self->mem##_count/2) \ self->mem##_count--; \
{ \ if (self->mem##_count < self->mem##_count/2) \
self->mem##_alloc /= 2; \ { \
self->mem = (Twhat*)realloc(self->mem, \ self->mem##_alloc /= 2; \
self->mem##_alloc * sizeof(Twhat)); \ reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count); \
} \ if (!reall) { \
MEM_VECTOR_CLEAR(self, mem); \
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) \ #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem) \