joined 2buddy

This commit is contained in:
dzaima 2021-07-12 00:17:59 +03:00
parent 8f3f9ba114
commit ed691f3dae
6 changed files with 46 additions and 51 deletions

View File

@ -27,7 +27,7 @@ u64* mmX_ctrs[64];
#define PROT PROT_READ|PROT_WRITE|PROT_EXEC #define PROT PROT_READ|PROT_WRITE|PROT_EXEC
#define FLAGS MAP_NORESERVE|MAP_PRIVATE|MAP_ANON|MAP_32BIT #define FLAGS MAP_NORESERVE|MAP_PRIVATE|MAP_ANON|MAP_32BIT
#include "../opt/mm_buddyTemplate.c" #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 BN
#undef BSZ #undef BSZ

View File

@ -4,8 +4,10 @@
u64 currObjCounter; u64 currObjCounter;
#endif #endif
u64 b1_ctrs[64]; u64 mm_ctrs[128];
EmptyValue* b1_buckets[64]; EmptyValue* mm_buckets[128];
#define b1_buckets mm_buckets
#define b1_allocL mm_allocL
#define ALSZ 20 #define ALSZ 20
#define BSZ(X) (1ull<<(X)) #define BSZ(X) (1ull<<(X))
#define MMI(X) X #define MMI(X) X
@ -14,8 +16,8 @@ EmptyValue* b1_buckets[64];
#undef BN #undef BN
#undef BSZ #undef BSZ
u64 b3_ctrs[64]; #define b3_buckets (mm_buckets+64)
EmptyValue* b3_buckets[64]; #define b3_allocL mm_allocL
#define ALSZ 20 #define ALSZ 20
#define BSZ(X) (3ull<<(X)) #define BSZ(X) (3ull<<(X))
#define MMI(X) ((X)|64) #define MMI(X) ((X)|64)
@ -24,6 +26,10 @@ EmptyValue* b3_buckets[64];
#undef BN #undef BN
#undef BSZ #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) { void mm_forHeap(V2v f) {
b1_forHeap(f); b1_forHeap(f);
b3_forHeap(f); b3_forHeap(f);
@ -31,7 +37,7 @@ void mm_forHeap(V2v f) {
u64 mm_heapUsed() { u64 mm_heapUsed() {
u64 r = 0; u64 r = 0;
for (i32 i = 0; i < 64; i++) r+= b1_ctrs[i] * (1ull<<i); for (i32 i = 0; i < 64; i++) r+= mm_ctrs[i ] * (1ull<<i);
for (i32 i = 0; i < 64; i++) r+= b3_ctrs[i] * (3ull<<i); for (i32 i = 0; i < 64; i++) r+= mm_ctrs[i+64] * (3ull<<i);
return r; return r;
} }

View File

@ -12,40 +12,27 @@ struct EmptyValue { // needs set: mmInfo; type=t_empty; next; everything else ca
extern u64 mm_heapAlloc; extern u64 mm_heapAlloc;
extern u64 mm_heapMax; extern u64 mm_heapMax;
extern u64 b1_ctrs[64]; extern u64 mm_ctrs[128];
extern EmptyValue* b1_buckets[64]; extern EmptyValue* mm_buckets[128];
#define BSZ(X) (1ull<<(X)) #define BSZ(X) (((X)&64? 3ull : 1ull)<<(X))
#define BN(X) b1_##X #define BN(X) mm_##X
#include "mm_buddyTemplate.h" #include "mm_buddyTemplate.h"
#undef BN
#undef BSZ
extern u64 b3_ctrs[64];
extern EmptyValue* b3_buckets[64];
#define BSZ(X) (3ull<<(X))
#define BN(X) b3_##X
#include "mm_buddyTemplate.h"
#undef BN
#undef BSZ
#define LOG2(X) ((u8)(64-__builtin_clzl((X)-1ull)))
static void* mm_alloc(usz sz, u8 type) { static void* mm_alloc(usz sz, u8 type) {
assert(sz>=16); assert(sz>=16);
onAlloc(sz, type); u32 log = LOG2(sz);
u8 b1 = 64-__builtin_clzl(sz-1ull); u32 logm2 = log-2;
if (sz <= (3ull<<(b1-2))) return b3_allocL(b1-2, b1-2|64, type); bool b2 = sz <= (3ull<<logm2);
else return b1_allocL(b1, b1, type); return mm_allocL(b2? logm2|64 : log, type);
}
static void mm_free(Value* x) {
if (x->mmInfo&64) b3_free(x);
else b1_free(x);
} }
static u64 mm_round(usz x) { static u64 mm_round(usz x) {
u8 b1 = 64-__builtin_clzl(x-1ull); u8 log = LOG2(x);
u64 s3 = 3ull<<(b1-2); u64 s3 = 3ull<<(log-2);
if (x<=s3) return s3; if (x<=s3) return s3;
return 1ull<<b1; return 1ull<<log;
} }
static u64 mm_size(Value* x) { static u64 mm_size(Value* x) {
u8 m = x->mmInfo; u8 m = x->mmInfo;
@ -53,3 +40,7 @@ static u64 mm_size(Value* x) {
else return 1ull<<(x->mmInfo&63); else return 1ull<<(x->mmInfo&63);
} }
void mm_forHeap(V2v f); void mm_forHeap(V2v f);
#undef BN
#undef BSZ
#undef LOG2

View File

@ -14,28 +14,26 @@ extern u64 mm_heapMax;
extern u64 mm_ctrs[64]; extern u64 mm_ctrs[64];
extern EmptyValue* mm_buckets[64]; extern EmptyValue* mm_buckets[64];
#define BSZ(X) (1ull<<(X)) #define BSZ(X) (1ull<<(X))
#define BN(X) mm_##X #define BN(X) mm_##X
#include "mm_buddyTemplate.h" #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) { static void* mm_alloc(usz sz, u8 type) {
assert(sz>=16); assert(sz>=16);
onAlloc(sz, type); onAlloc(sz, type);
u64 b = BSZI(sz); return mm_allocL(LOG2(sz), type);
Value* r = mm_allocL(b, b, type);
return r;
} }
static u64 mm_round(usz sz) { static u64 mm_round(usz sz) {
return BSZ(BSZI(sz)); return BSZ(LOG2(sz));
} }
static u64 mm_size(Value* x) { static u64 mm_size(Value* x) {
return BSZ(x->mmInfo&63); return BSZ(x->mmInfo&63);
} }
void mm_forHeap(V2v f); void mm_forHeap(V2v f);
#undef BSZI #undef LOG2
#undef BN #undef BN
#undef BSZ #undef BSZ

View File

@ -19,7 +19,7 @@ AllocInfo* al;
u64 alCap; u64 alCap;
u64 alSize; u64 alSize;
static void BN(guaranteeEmpty)(u8 bucket) { static inline void BN(guaranteeEmpty)(u8 bucket) {
u8 cb = bucket; u8 cb = bucket;
EmptyValue* c; EmptyValue* c;
while (true) { while (true) {
@ -63,9 +63,9 @@ static void BN(guaranteeEmpty)(u8 bucket) {
c->next = buckets[cb]; c->next = buckets[cb];
buckets[cb] = c; buckets[cb] = c;
} }
NOINLINE void* BN(allocS)(i64 bucket, i64 info, u8 type) { NOINLINE void* BN(allocS)(i64 bucket, u8 type) {
BN(guaranteeEmpty)(bucket); BN(guaranteeEmpty)(bucket&63);
return BN(allocL)(bucket, info, type); return BN(allocL)(bucket, type);
} }
void BN(forHeap)(V2v f) { void BN(forHeap)(V2v f) {

View File

@ -2,25 +2,25 @@
static void BN(free)(Value* x) { static void BN(free)(Value* x) {
onFree(x); onFree(x);
#ifdef USE_VALGRIND #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->mmInfo, 1);
VALGRIND_MAKE_MEM_DEFINED(&x->type, 1); VALGRIND_MAKE_MEM_DEFINED(&x->type, 1);
#endif #endif
#ifdef DONT_FREE #ifdef DONT_FREE
if (x->type!=t_freed) x->flags = x->type; if (x->type!=t_freed) x->flags = x->type;
#else #else
u8 b = x->mmInfo&63; u8 b = x->mmInfo&127;
BN(ctrs)[x->mmInfo&63]--; BN(ctrs)[b]--;
((EmptyValue*)x)->next = buckets[b]; ((EmptyValue*)x)->next = buckets[b];
buckets[b] = (EmptyValue*)x; buckets[b] = (EmptyValue*)x;
#endif #endif
x->type = t_empty; x->type = t_empty;
} }
NOINLINE void* BN(allocS)(i64 bucket, i64 info, u8 type); NOINLINE void* BN(allocS)(i64 bucket, u8 type);
static void* BN(allocL)(i64 bucket, i64 info, u8 type) { static void* BN(allocL)(i64 bucket, u8 type) {
EmptyValue* x = buckets[bucket]; 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; buckets[bucket] = x->next;
#ifdef USE_VALGRIND #ifdef USE_VALGRIND
VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(bucket)); VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(bucket));
@ -29,8 +29,8 @@ static void* BN(allocL)(i64 bucket, i64 info, u8 type) {
BN(ctrs)[bucket]++; BN(ctrs)[bucket]++;
x->flags = x->extra = x->type = x->mmInfo = 0; x->flags = x->extra = x->type = x->mmInfo = 0;
x->refc = 1; x->refc = 1;
x->mmInfo = info | gc_tagCurr;
x->type = type; x->type = type;
x->mmInfo = bucket | gc_tagCurr;
#if defined(DEBUG) && !defined(DONT_FREE) #if defined(DEBUG) && !defined(DONT_FREE)
u64* p = (u64*)x; u64* p = (u64*)x;
u64* s = p + sizeof(Value)/8; u64* s = p + sizeof(Value)/8;