1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include "altmalloc.h"
alt_allocater_t alt_malloc = malloc;
alt_allocater_t
alt_set_allocater(alt_allocater_t new)
{
alt_allocater_t old;
old = alt_malloc;
alt_malloc = new;
return old;
}
alt_allocater_t alt_malloc_atomic = malloc;
alt_allocater_t
alt_set_atomic_allocater(alt_allocater_t new)
{
alt_allocater_t old;
old = alt_malloc_atomic;
alt_malloc_atomic = new;
return old;
}
alt_reallocater_t alt_realloc = realloc;
alt_reallocater_t
alt_set_reallocater(alt_reallocater_t new)
{
alt_reallocater_t old;
old = alt_realloc;
alt_realloc = new;
return old;
}
alt_freer_t alt_free = free;
alt_freer_t
alt_set_freer(alt_freer_t new)
{
alt_freer_t old;
old = alt_free;
alt_free = new;
return old;
}
int
alt_malloc_vs(ptrdiff_t *p_size, ptrdiff_t n, ptrdiff_t limit, ...)
{
va_list ap;
va_start(ap, limit);
if (n >= *p_size) {
ptrdiff_t newsize;
ptrdiff_t elem_size, isatomic;
void **p_v, *newv;
newsize = (n / 2 + 1) * 3;
if (limit > 0) {
if (n >= limit)
return -1;
if (newsize > limit)
newsize = limit;
}
while ((p_v = va_arg(ap, void **))) {
elem_size = va_arg(ap, ptrdiff_t);
isatomic = va_arg(ap, int);
if (*p_v)
newv = alt_call_realloc(*p_v, elem_size * newsize);
else if (isatomic)
newv = alt_call_malloc_atomic(elem_size * newsize);
else
newv = alt_call_malloc(elem_size * newsize);
if (!newv)
return -1;
memset((char *)newv + elem_size * n, 0, elem_size * (newsize - n));
*p_v = newv;
}
*p_size = newsize;
}
va_end(ap);
return 0;
}
|