Fix: did not work with ALIGNMENT < MIN_SIZE

This commit is contained in:
ceriel 1990-03-21 16:45:39 +00:00
parent 28d6834ae7
commit a932246479
2 changed files with 18 additions and 5 deletions

View file

@ -12,6 +12,18 @@
#if ALIGNMENT != 4 && ALIGNMENT != 8 && ALIGNMENT != 16 #if ALIGNMENT != 4 && ALIGNMENT != 8 && ALIGNMENT != 16
ALIGNMENT must be 4, 8, or 16 ALIGNMENT must be 4, 8, or 16
#endif #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)) #define align(n) (((n) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
union _inf { union _inf {

View file

@ -28,6 +28,7 @@
extern char *SBRK(); extern char *SBRK();
#ifdef STORE #ifdef STORE
#define MAX_STORE 32 #define MAX_STORE 32
#define MAX_SZ_IN_STORE (MAX_STORE*ALIGNMENT)
private do_free(), sell_out(); private do_free(), sell_out();
privatedata mallink *store[MAX_STORE]; privatedata mallink *store[MAX_STORE];
#endif STORE #endif STORE
@ -44,9 +45,9 @@ malloc(n)
} }
if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n); if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n);
#ifdef STORE #ifdef STORE
if (n <= MAX_STORE*MIN_SIZE) { if (n <= MAX_SZ_IN_STORE) {
/* look in the store first */ /* 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) { if (ml = *stp) {
*stp = log_next_of(ml); *stp = log_next_of(ml);
@ -165,9 +166,9 @@ free(addr)
if (free_of(ml) || in_store(ml)) if (free_of(ml) || in_store(ml))
return; /* user frees free block */ return; /* user frees free block */
if (size_of(ml) <= MAX_STORE*MIN_SIZE) { if (size_of(ml) <= MAX_SZ_IN_STORE) {
/* return to 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); set_log_next(ml, *stp);
*stp = ml; *stp = ml;
@ -253,7 +254,7 @@ realloc(addr, n)
if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n); if (n < MIN_SIZE) n = align(MIN_SIZE); else n = align(n);
#ifdef STORE #ifdef STORE
if (in_store(ml)) { 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; mallink *stp1 = 0;
while (ml != stp) { while (ml != stp) {
stp1 = stp; stp1 = stp;