summaryrefslogtreecommitdiffstats
path: root/mbbsd/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'mbbsd/alloc.c')
-rw-r--r--mbbsd/alloc.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/mbbsd/alloc.c b/mbbsd/alloc.c
index 06b84177..de676ce4 100644
--- a/mbbsd/alloc.c
+++ b/mbbsd/alloc.c
@@ -56,6 +56,9 @@ static void REGPARM(1) *do_mmap(size_t size) {
static __alloc_t* __small_mem[8];
+static int smallalloc[8];
+static int smallalloc_max[8];
+
#define __SMALL_NR(i) (MEM_BLOCK_SIZE/(i))
#define __MIN_SMALL_SIZE __SMALL_NR(256) /* 16 / 32 */
@@ -88,6 +91,17 @@ static void REGPARM(2) __small_free(void*_ptr,size_t _size) {
ptr->next=__small_mem[idx];
__small_mem[idx]=ptr;
+
+ smallalloc[idx]--;
+
+ if (MEM_BLOCK_SIZE == PAGE_SIZE &&
+ smallalloc[idx] == 0 &&
+ smallalloc_max[idx] < __SMALL_NR(size)) {
+ __alloc_t* p = __small_mem[idx];
+ __alloc_t* ph = p - (size_t)p%PAGE_SIZE;
+ munmap(ph, MEM_BLOCK_SIZE);
+ __small_mem[idx] = 0;
+ }
}
static void* REGPARM(1) __small_malloc(size_t _size) {
@@ -119,6 +133,10 @@ static void* REGPARM(1) __small_malloc(size_t _size) {
__small_mem[idx]=ptr->next;
ptr->next=0;
+ smallalloc[idx]++;
+ if(smallalloc[idx] > smallalloc_max[idx])
+ smallalloc_max[idx] = smallalloc[idx];
+
return ptr;
}