From ed691f3dae826ccccd141c3f0f02a38f79e114a0 Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 12 Jul 2021 00:17:59 +0300 Subject: [PATCH] joined 2buddy --- src/jit/nvm_x86_64.c | 2 +- src/opt/mm_2buddy.c | 18 +++++++++++------ src/opt/mm_2buddy.h | 41 +++++++++++++++----------------------- src/opt/mm_buddy.h | 14 ++++++------- src/opt/mm_buddyTemplate.c | 8 ++++---- src/opt/mm_buddyTemplate.h | 14 ++++++------- 6 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/jit/nvm_x86_64.c b/src/jit/nvm_x86_64.c index ac7f745b..1e110e8a 100644 --- a/src/jit/nvm_x86_64.c +++ b/src/jit/nvm_x86_64.c @@ -27,7 +27,7 @@ u64* mmX_ctrs[64]; #define PROT PROT_READ|PROT_WRITE|PROT_EXEC #define FLAGS MAP_NORESERVE|MAP_PRIVATE|MAP_ANON|MAP_32BIT #include "../opt/mm_buddyTemplate.c" -static void* mmX_allocN(usz sz, u8 type) { assert(sz>=16); u64 b = 64-__builtin_clzl(sz-1ull); return mmX_allocL(b, b, type); } +static void* mmX_allocN(usz sz, u8 type) { assert(sz>=16); return mmX_allocL(64-__builtin_clzl(sz-1ull), type); } #undef BN #undef BSZ diff --git a/src/opt/mm_2buddy.c b/src/opt/mm_2buddy.c index a7a102a0..529b999a 100644 --- a/src/opt/mm_2buddy.c +++ b/src/opt/mm_2buddy.c @@ -4,8 +4,10 @@ u64 currObjCounter; #endif -u64 b1_ctrs[64]; -EmptyValue* b1_buckets[64]; +u64 mm_ctrs[128]; +EmptyValue* mm_buckets[128]; +#define b1_buckets mm_buckets +#define b1_allocL mm_allocL #define ALSZ 20 #define BSZ(X) (1ull<<(X)) #define MMI(X) X @@ -14,8 +16,8 @@ EmptyValue* b1_buckets[64]; #undef BN #undef BSZ -u64 b3_ctrs[64]; -EmptyValue* b3_buckets[64]; +#define b3_buckets (mm_buckets+64) +#define b3_allocL mm_allocL #define ALSZ 20 #define BSZ(X) (3ull<<(X)) #define MMI(X) ((X)|64) @@ -24,6 +26,10 @@ EmptyValue* b3_buckets[64]; #undef BN #undef BSZ +NOINLINE void* mm_allocS(i64 bucket, u8 type) { + return bucket&64? b3_allocS(bucket, type) : b1_allocS(bucket, type); +} + void mm_forHeap(V2v f) { b1_forHeap(f); b3_forHeap(f); @@ -31,7 +37,7 @@ void mm_forHeap(V2v f) { u64 mm_heapUsed() { u64 r = 0; - for (i32 i = 0; i < 64; i++) r+= b1_ctrs[i] * (1ull<=16); - onAlloc(sz, type); - u8 b1 = 64-__builtin_clzl(sz-1ull); - if (sz <= (3ull<<(b1-2))) return b3_allocL(b1-2, b1-2|64, type); - else return b1_allocL(b1, b1, type); -} -static void mm_free(Value* x) { - if (x->mmInfo&64) b3_free(x); - else b1_free(x); + u32 log = LOG2(sz); + u32 logm2 = log-2; + bool b2 = sz <= (3ull<mmInfo; @@ -53,3 +40,7 @@ static u64 mm_size(Value* x) { else return 1ull<<(x->mmInfo&63); } void mm_forHeap(V2v f); + +#undef BN +#undef BSZ +#undef LOG2 \ No newline at end of file diff --git a/src/opt/mm_buddy.h b/src/opt/mm_buddy.h index 13ae5c07..91f56136 100644 --- a/src/opt/mm_buddy.h +++ b/src/opt/mm_buddy.h @@ -14,28 +14,26 @@ extern u64 mm_heapMax; extern u64 mm_ctrs[64]; extern EmptyValue* mm_buckets[64]; -#define BSZ(X) (1ull<<(X)) -#define BN(X) mm_##X +#define BSZ(X) (1ull<<(X)) +#define BN(X) mm_##X #include "mm_buddyTemplate.h" -#define BSZI(X) ((u8)(64-__builtin_clzl((X)-1ull))) +#define LOG2(X) ((u8)(64-__builtin_clzl((X)-1ull))) static void* mm_alloc(usz sz, u8 type) { assert(sz>=16); onAlloc(sz, type); - u64 b = BSZI(sz); - Value* r = mm_allocL(b, b, type); - return r; + return mm_allocL(LOG2(sz), type); } static u64 mm_round(usz sz) { - return BSZ(BSZI(sz)); + return BSZ(LOG2(sz)); } static u64 mm_size(Value* x) { return BSZ(x->mmInfo&63); } void mm_forHeap(V2v f); -#undef BSZI +#undef LOG2 #undef BN #undef BSZ diff --git a/src/opt/mm_buddyTemplate.c b/src/opt/mm_buddyTemplate.c index 5569b4df..f581d45f 100644 --- a/src/opt/mm_buddyTemplate.c +++ b/src/opt/mm_buddyTemplate.c @@ -19,7 +19,7 @@ AllocInfo* al; u64 alCap; u64 alSize; -static void BN(guaranteeEmpty)(u8 bucket) { +static inline void BN(guaranteeEmpty)(u8 bucket) { u8 cb = bucket; EmptyValue* c; while (true) { @@ -63,9 +63,9 @@ static void BN(guaranteeEmpty)(u8 bucket) { c->next = buckets[cb]; buckets[cb] = c; } -NOINLINE void* BN(allocS)(i64 bucket, i64 info, u8 type) { - BN(guaranteeEmpty)(bucket); - return BN(allocL)(bucket, info, type); +NOINLINE void* BN(allocS)(i64 bucket, u8 type) { + BN(guaranteeEmpty)(bucket&63); + return BN(allocL)(bucket, type); } void BN(forHeap)(V2v f) { diff --git a/src/opt/mm_buddyTemplate.h b/src/opt/mm_buddyTemplate.h index 8f262e87..0e1c9987 100644 --- a/src/opt/mm_buddyTemplate.h +++ b/src/opt/mm_buddyTemplate.h @@ -2,25 +2,25 @@ static void BN(free)(Value* x) { onFree(x); #ifdef USE_VALGRIND - VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(x->mmInfo&63)); + VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(x->mmInfo&127)); VALGRIND_MAKE_MEM_DEFINED(&x->mmInfo, 1); VALGRIND_MAKE_MEM_DEFINED(&x->type, 1); #endif #ifdef DONT_FREE if (x->type!=t_freed) x->flags = x->type; #else - u8 b = x->mmInfo&63; - BN(ctrs)[x->mmInfo&63]--; + u8 b = x->mmInfo&127; + BN(ctrs)[b]--; ((EmptyValue*)x)->next = buckets[b]; buckets[b] = (EmptyValue*)x; #endif x->type = t_empty; } -NOINLINE void* BN(allocS)(i64 bucket, i64 info, u8 type); -static void* BN(allocL)(i64 bucket, i64 info, u8 type) { +NOINLINE void* BN(allocS)(i64 bucket, u8 type); +static void* BN(allocL)(i64 bucket, u8 type) { EmptyValue* x = buckets[bucket]; - if (RARE(x==NULL)) return BN(allocS)(bucket, info, type); + if (RARE(x==NULL)) return BN(allocS)(bucket, type); buckets[bucket] = x->next; #ifdef USE_VALGRIND VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(bucket)); @@ -29,8 +29,8 @@ static void* BN(allocL)(i64 bucket, i64 info, u8 type) { BN(ctrs)[bucket]++; x->flags = x->extra = x->type = x->mmInfo = 0; x->refc = 1; - x->mmInfo = info | gc_tagCurr; x->type = type; + x->mmInfo = bucket | gc_tagCurr; #if defined(DEBUG) && !defined(DONT_FREE) u64* p = (u64*)x; u64* s = p + sizeof(Value)/8;