diff --git a/modules/src/malloc/impl.h b/modules/src/malloc/impl.h index 05791a69b..62762397b 100644 --- a/modules/src/malloc/impl.h +++ b/modules/src/malloc/impl.h @@ -12,6 +12,18 @@ #if ALIGNMENT != 4 && ALIGNMENT != 8 && ALIGNMENT != 16 ALIGNMENT must be 4, 8, or 16 #endif +#if MIN_SIZE % ALIGNMENT +ALIGNMENT must be a dividor of MIN_SIZE +#endif +#if ALIGNMENT == 4 +#define LOG_ALIGNMENT 2 +#endif +#if ALIGNMENT == 8 +#define LOG_ALIGNMENT 3 +#endif +#if ALIGNMENT == 16 +#define LOG_ALIGNMENT 4 +#endif #define align(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1)) union _inf { diff --git a/modules/src/malloc/mal.c b/modules/src/malloc/mal.c index c4cab133c..568d69e88 100644 --- a/modules/src/malloc/mal.c +++ b/modules/src/malloc/mal.c @@ -28,6 +28,7 @@ extern char *SBRK(); #ifdef STORE #define MAX_STORE 32 +#define MAX_SZ_IN_STORE (MAX_STORE*ALIGNMENT) private do_free(), sell_out(); privatedata mallink *store[MAX_STORE]; #endif STORE @@ -44,9 +45,9 @@ malloc(n) } if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n); #ifdef STORE - if (n <= MAX_STORE*MIN_SIZE) { + if (n <= MAX_SZ_IN_STORE) { /* look in the store first */ - register mallink **stp = &store[(n >> LOG_MIN_SIZE) - 1]; + register mallink **stp = &store[(n >> LOG_ALIGNMENT) - 1]; if (ml = *stp) { *stp = log_next_of(ml); @@ -165,9 +166,9 @@ free(addr) if (free_of(ml) || in_store(ml)) return; /* user frees free block */ - if (size_of(ml) <= MAX_STORE*MIN_SIZE) { + if (size_of(ml) <= MAX_SZ_IN_STORE) { /* return to store */ - mallink **stp = &store[(size_of(ml) >> LOG_MIN_SIZE) - 1]; + mallink **stp = &store[(size_of(ml) >> LOG_ALIGNMENT) - 1]; set_log_next(ml, *stp); *stp = ml; @@ -253,7 +254,7 @@ realloc(addr, n) if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n); #ifdef STORE if (in_store(ml)) { - register mallink *stp = store[(size_of(ml) >> LOG_MIN_SIZE) - 1]; + register mallink *stp = store[(size_of(ml) >> LOG_ALIGNMENT) - 1]; mallink *stp1 = 0; while (ml != stp) { stp1 = stp;