counters instead of allocB

This commit is contained in:
dzaima 2021-07-11 23:07:07 +03:00
parent 1b31c88e4c
commit 2a0b91e7b4
8 changed files with 18 additions and 15 deletions

View File

@ -1,7 +1,5 @@
#include "../core.h"
u64 allocB; // currently allocated number of bytes
#if MM==0
#include "../opt/mm_malloc.c"
#elif MM==1

View File

@ -1,6 +1,3 @@
extern u64 allocB; // currently allocated number of bytes
// memory defs
static void* mm_alloc(usz sz, u8 type);

View File

@ -16,8 +16,7 @@
// separate memory management system for executable code; isn't garbage-collected
EmptyValue* mmX_buckets[64];
u64 mmX_allocB;
#define allocB mmX_allocB
u64* mmX_ctrs[64];
#define BSZ(X) (1ull<<(X))
#define BSZI(X) ((u8)(64-__builtin_clzl((X)-1ull)))
#define MMI(X) X
@ -29,8 +28,6 @@ u64 mmX_allocB;
#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); return mmX_allocL(64-__builtin_clzl(sz-1ull), type); }
#undef allocB
#undef mmX_buckets
#undef BN
#undef BSZ

View File

@ -4,6 +4,7 @@
u64 currObjCounter;
#endif
u64 b1_ctrs[64];
EmptyValue* b1_buckets[64];
#define ALSZ 20
#define BSZ(X) (1ull<<(X))
@ -13,6 +14,7 @@ EmptyValue* b1_buckets[64];
#undef BN
#undef BSZ
u64 b3_ctrs[64];
EmptyValue* b3_buckets[64];
#define ALSZ 20
#define BSZ(X) (3ull<<(X))
@ -28,5 +30,8 @@ void mm_forHeap(V2v f) {
}
u64 mm_heapUsed() {
return allocB;
u64 r = 0;
for (i32 i = 0; i < 64; i++) r+= b1_ctrs[i] * (1ull<<i);
for (i32 i = 0; i < 64; i++) r+= b3_ctrs[i] * (3ull<<i);
return r;
}

View File

@ -12,6 +12,7 @@ struct EmptyValue { // needs set: mmInfo; type=t_empty; next; everything else ca
extern u64 mm_heapAlloc;
extern u64 mm_heapMax;
extern u64 b1_ctrs[64];
extern EmptyValue* b1_buckets[64];
#define BSZ(X) (1ull<<(X))
#define BN(X) b1_##X
@ -19,6 +20,7 @@ extern EmptyValue* b1_buckets[64];
#undef BN
#undef BSZ
extern u64 b3_ctrs[64];
extern EmptyValue* b3_buckets[64];
#define BSZ(X) (3ull<<(X))
#define BN(X) b3_##X

View File

@ -4,15 +4,18 @@
u64 currObjCounter;
#endif
u64 mm_ctrs[64];
EmptyValue* mm_buckets[64];
#define ALSZ 20
#define BSZ(X) (1ull<<(X))
#define MMI(X) X
#define BN(X) mm_##X
#include "mm_buddyTemplate.c"
#undef BN
#undef BSZ
u64 mm_heapUsed() {
return allocB;
u64 r = 0;
for (i32 i = 0; i < 64; i++) r+= mm_ctrs[i]*BSZ(i);
return r;
}
#undef BN
#undef BSZ

View File

@ -12,6 +12,7 @@ struct EmptyValue { // needs set: mmInfo; type=t_empty; next; everything else ca
extern u64 mm_heapAlloc;
extern u64 mm_heapMax;
extern u64 mm_ctrs[64];
extern EmptyValue* mm_buckets[64];
#define BSZ(X) (1ull<<(X))
#define BN(X) mm_##X

View File

@ -10,9 +10,9 @@ static void BN(free)(Value* x) {
if (x->type!=t_freed) x->flags = x->type;
#else
u8 b = x->mmInfo&63;
BN(ctrs)[x->mmInfo&63]--;
((EmptyValue*)x)->next = buckets[b];
buckets[b] = (EmptyValue*)x;
allocB-= BSZ(b);
#endif
x->type = t_empty;
}
@ -26,7 +26,7 @@ static void* BN(allocL)(i64 bucket, u8 type) {
VALGRIND_MAKE_MEM_UNDEFINED(x, BSZ(bucket));
VALGRIND_MAKE_MEM_DEFINED(&x->mmInfo, 1);
#endif
allocB+= BSZ(bucket);
BN(ctrs)[bucket]++;
u8 mmInfo = x->mmInfo;
x->flags = x->extra = x->type = x->mmInfo = 0;
x->refc = 1;