From 4757dff52c83dcceed119aefb225c5b3e751ae5f Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Mon, 5 Sep 2005 21:10:31 +0000 Subject: [PATCH] Add malloc_top() that try to allocate memory at top of memory --- second/bank.c | 22 ++++++++++++++++++++++ second/bank.h | 1 + 2 files changed, 23 insertions(+) diff --git a/second/bank.c b/second/bank.c index 3424fdb..d795323 100644 --- a/second/bank.c +++ b/second/bank.c @@ -304,6 +304,28 @@ void *malloc_contiguous(size_t size) return contiguous; } +void *malloc_top(size_t size) +{ + void *top; + void* tmp; + long bubble; + + bubble = bank_mem_avail() - size; + do { + tmp = malloc_contiguous(bubble); + if (tmp) + { + top = malloc(size); + free(tmp); + if (top) + return top; + } + bubble -= 1024 * 1024; /* decrease of 1 MB */ + } while (bubble > 0); + + return NULL; +} + #ifdef BANK_DUMP void bank_dump() { diff --git a/second/bank.h b/second/bank.h index eb9de81..97f8a8d 100644 --- a/second/bank.h +++ b/second/bank.h @@ -31,3 +31,4 @@ extern int logical2physical(unsigned long logical, unsigned long *physical); extern int physical2logical(unsigned long physical, unsigned long *logical); extern int check_full_in_bank(unsigned long start, unsigned long size); extern void *malloc_contiguous(size_t size); +extern void *malloc_top(size_t size);