mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2024-11-27 22:22:17 +00:00
Get rid of realloc
This commit is contained in:
parent
69173876f1
commit
4b6659bfe1
1 changed files with 45 additions and 29 deletions
34
astir.h
34
astir.h
|
@ -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) \
|
||||||
|
@ -38,34 +38,50 @@
|
||||||
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) \
|
||||||
{ \
|
{ \
|
||||||
|
Twhat *reall; \
|
||||||
if (self->mem##_count == self->mem##_alloc) { \
|
if (self->mem##_count == self->mem##_alloc) { \
|
||||||
if (!self->mem##_alloc) \
|
if (!self->mem##_alloc) \
|
||||||
self->mem##_alloc = 16; \
|
self->mem##_alloc = 16; \
|
||||||
else \
|
else \
|
||||||
self->mem##_alloc *= 2; \
|
self->mem##_alloc *= 2; \
|
||||||
self->mem = (Twhat*)realloc(self->mem, \
|
reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc); \
|
||||||
sizeof(Twhat) * self->mem##_alloc); \
|
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; \
|
||||||
} \
|
} \
|
||||||
self->mem[self->mem##_count++] = f; \
|
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; \
|
||||||
|
Twhat *reall; \
|
||||||
if (idx >= self->mem##_count) \
|
if (idx >= self->mem##_count) \
|
||||||
return; \
|
return true; /* huh... */ \
|
||||||
for (i = idx; i < self->mem##_count-1; ++i) \
|
for (i = idx; i < self->mem##_count-1; ++i) \
|
||||||
self->mem[i] = self->mem[i+1]; \
|
self->mem[i] = self->mem[i+1]; \
|
||||||
self->mem##_count--; \
|
self->mem##_count--; \
|
||||||
if (self->mem##_count < self->mem##_count/2) \
|
if (self->mem##_count < self->mem##_count/2) \
|
||||||
{ \
|
{ \
|
||||||
self->mem##_alloc /= 2; \
|
self->mem##_alloc /= 2; \
|
||||||
self->mem = (Twhat*)realloc(self->mem, \
|
reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count); \
|
||||||
self->mem##_alloc * sizeof(Twhat)); \
|
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) \
|
||||||
|
|
Loading…
Reference in a new issue