diff --git a/README.md b/README.md index b81490b..d1d161d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A pauseless garbage collector in C. ## 为什么要用C做? C是一门非常优秀的语言,它的语法简单,编译速度快,运行速度快,内存管理简单。 -## 基本操作接口(v0.0.0dev2) +## 基本操作接口(v0.0.0dev2, v0.0.0dev3的自己估计看看就懂了吧) ### 1. 垃圾回收器的初始化 void uboa_init(int nGCWorkers, size_t xmx, long timerInterval_min, bool enableRuleProactive, const char* logFilePath); diff --git a/demo/test.c b/demo/test.c index 12ab589..ecaee23 100644 --- a/demo/test.c +++ b/demo/test.c @@ -5,7 +5,7 @@ #include -AppThrdHandle statics = NULL; +uboa_appThrdHandle statics = NULL; uboa_reference oop = NULL; const size_t length = 15; @@ -13,7 +13,7 @@ const off_t numOff = sizeof(off_t) * length; off_t* map = NULL; void foo(long num) { - AppThrdHandle hdl = uboa_createAppThrdHandle(alloca(1024)); + uboa_appThrdHandle hdl = uboa_createAppThrdHandle(alloca(1024)); uboa_reference ref[length]; for(int i = 0; i < length; ++i) ref[i] = uboa_pushReference(hdl); @@ -27,21 +27,20 @@ void foo(long num) { else uboa_new(hdl, ref[i], 1l << 24, oop); - uboa_store(ref[i], numOff, 114514); + uboa_storeInt32(ref[i], numOff, 114514); } ) - for(int a = 0; a < 1008600; ++a) { uboa_safeRegion( for(int i = 0; i < length; ++i) { - assert(114514 == uboa_load(ref[i], numOff)); + assert(114514 == uboa_loadInt32(ref[i], numOff)); for(int j = 0; j < length; ++j) { uboa_reference tmp = uboa_pushReference(hdl); uboa_new(hdl, tmp, 128, uboa_null()); - uboa_store(tmp, numOff, 114514); + uboa_storeInt32(tmp, numOff, 114514); uboa_popReferences(hdl, 1); } @@ -52,7 +51,7 @@ void foo(long num) { uboa_safeRegion( for(int i = 0; i < length; ++i) { uboa_loadReference(ref[i], ref[i], map[i]); - assert(114514 == uboa_load(ref[i], numOff)); + assert(114514 == uboa_loadInt32(ref[i], numOff)); } ) diff --git a/makefile b/makefile index 651b778..05ba52a 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ out = build/lib/libuboa.a macro = \ -DBUILD_DATE="\"12/17/2022, Saturday\"" \ - -DGC_VERSION="\"0.0.0.dev2\"" \ + -DGC_VERSION="\"0.0.0.dev3\"" \ -DCOPYRIGHT="\"Copyright\(c\) 2022, Lizbing. All rights reserved.\"" \ -DGC_PRODUCT_NAME="\"Uboa Garbage Collector\"" \ -DFEEDBACK_EMAIL="\"feedback@relight.team\"" diff --git a/src/share/allocator.c b/src/share/allocator.c index f41b8e9..6176928 100644 --- a/src/share/allocator.c +++ b/src/share/allocator.c @@ -38,7 +38,7 @@ infile_inline(size_t align(size_t s, int alignment)) { return s - n + alignment; } -infile_inline(void* edenAllocSmall(AppThrdHandle hdl, size_t s, Page** src)) { +infile_inline(void* edenAllocSmall(uboa_appThrdHandle hdl, size_t s, Page** src)) { Page* p = hdl->TLAB; void* ret = Page_alloc(p, s); @@ -118,7 +118,7 @@ inline void* memAlloc(size_t s) { return survAllocSmall(align(s, alignmentSmall), NULL); } -void uboa_new(AppThrdHandle hdl, uboa_reference r, size_t s, uboa_reference oopRef) { +void uboa_new(uboa_appThrdHandle hdl, uboa_reference r, size_t s, uboa_reference oopRef) { uboa_newArray(hdl, r, s, 1, oopRef); } @@ -129,7 +129,7 @@ inline void Object_init(Object* this, Page* p) { LargeObject_push(this); } -void uboa_newArray(AppThrdHandle hdl, uboa_reference r, size_t s, size_t count, uboa_reference oopRef) { +void uboa_newArray(uboa_appThrdHandle hdl, uboa_reference r, size_t s, size_t count, uboa_reference oopRef) { Object* ptr = NULL; Page* src = NULL; diff --git a/src/share/appThrd.c b/src/share/appThrd.c index ca18c6f..4975ef1 100644 --- a/src/share/appThrd.c +++ b/src/share/appThrd.c @@ -1,7 +1,5 @@ #include "appThrd.h" -#include "barrier.h" -static const intptr_t null = 0; static atomic_flag sl = ATOMIC_FLAG_INIT; static AppThrd* head = NULL; @@ -34,7 +32,7 @@ void forEachRoot(void(*func)(AppThrd*)) { } } -AppThrdHandle uboa_createAppThrdHandle(void* stack) { +uboa_appThrdHandle uboa_createAppThrdHandle(void* stack) { AppThrd* at = stack; memset(at, 0, sizeof(AppThrd)); @@ -51,7 +49,7 @@ AppThrdHandle uboa_createAppThrdHandle(void* stack) { return at; } -void uboa_destroyAppThrdHandle(AppThrdHandle hdl) { +void uboa_destroyAppThrdHandle(uboa_appThrdHandle hdl) { while(atomic_flag_test_and_set(&sl)); AppThrd* prev = hdl->prev, *next = hdl->next; @@ -68,40 +66,12 @@ void uboa_destroyAppThrdHandle(AppThrdHandle hdl) { while(atomic_flag_test_and_set(&hdl->flag)); } -uboa_reference uboa_null() { - return &null; -} - -uboa_reference uboa_pushReference(AppThrdHandle hdl) { +uboa_reference uboa_pushReference(uboa_appThrdHandle hdl) { uboa_reference ref = hdl->stack + hdl->size++; *ref = NULL; return ref; } -void uboa_popReferences(AppThrdHandle hdl, size_t n) { +void uboa_popReferences(uboa_appThrdHandle hdl, size_t n) { hdl->size -= n; -} - -void uboa_assign(uboa_reference dst, uboa_reference src) { - *dst = loadValueBarrier(src); -} - -bool uboa_isNull(uboa_reference r) { - return !*r; -} - -intptr_t uboa_load(uboa_reference r, off_t off) { - return *(intptr_t*)(((Object*)loadValueBarrier(r))->data + off); -} - -void uboa_store(uboa_reference r, off_t off, intptr_t v) { - *(intptr_t*)(((Object*)loadValueBarrier(r))->data + off) = v; -} - -void uboa_loadReference(uboa_reference dst, uboa_reference src, off_t off) { - uboa_assign(dst, ((Object*)loadValueBarrier(src))->data + off); -} - -void uboa_storeReference(uboa_reference dst, off_t off, uboa_reference src) { - *(uboa_reference)(((Object*)loadValueBarrier(dst))->data + off) = *src; -} +} \ No newline at end of file diff --git a/src/share/appThrd.h b/src/share/appThrd.h index 546c3ba..490ea21 100644 --- a/src/share/appThrd.h +++ b/src/share/appThrd.h @@ -6,7 +6,7 @@ #include "../os/thread.h" -typedef struct AppThrd AppThrd, *AppThrdHandle; +typedef struct AppThrd AppThrd, *uboa_appThrdHandle; struct AppThrd { Page* TLAB; diff --git a/src/share/oop.c b/src/share/oop.c index f41c5fd..0f4389e 100644 --- a/src/share/oop.c +++ b/src/share/oop.c @@ -1,7 +1,7 @@ #include "oop.h" #include "object.h" -void uboa_new_Oop(AppThrdHandle hdl, uboa_reference r, size_t s, off_t* m) { +void uboa_new_Oop(uboa_appThrdHandle hdl, uboa_reference r, size_t s, off_t* m) { size_t size = sizeof(off_t) * s; uboa_new(hdl, r, sizeof(OopDec) + size, uboa_null()); diff --git a/src/share/operate.c b/src/share/operate.c new file mode 100644 index 0000000..379d3d3 --- /dev/null +++ b/src/share/operate.c @@ -0,0 +1,95 @@ +#include "barrier.h" + +static const intptr_t null = 0; + +uboa_reference uboa_null() { + return &null; +} + +void uboa_assign(uboa_reference dst, uboa_reference src) { + *dst = loadValueBarrier(src); +} + +bool uboa_isNull(uboa_reference r) { + return !*r; +} + +void uboa_loadReference(uboa_reference dst, uboa_reference src, off_t off) { + uboa_assign(dst, ((Object*)loadValueBarrier(src))->data + off); +} + +void uboa_storeReference(uboa_reference dst, off_t off, uboa_reference src) { + *(uboa_reference)(((Object*)loadValueBarrier(dst))->data + off) = *src; +} + +int8_t uboa_loadInt8(uboa_reference r, off_t off) { + return *(int8_t*)(((Object*)loadValueBarrier(r))->data + off); +} + +int16_t uboa_loadInt16(uboa_reference r, off_t off) { + return *(int16_t*)(((Object*)loadValueBarrier(r))->data + off); +} + +int32_t uboa_loadInt32(uboa_reference r, off_t off) { + return *(int32_t*)(((Object*)loadValueBarrier(r))->data + off); +} + +int64_t uboa_loadInt64(uboa_reference r, off_t off) { + return *(int64_t*)(((Object*)loadValueBarrier(r))->data + off); +} + +float uboa_loadFloat(uboa_reference r, off_t off) { + return *(float*)(((Object*)loadValueBarrier(r))->data + off); +} + +double uboa_loadDouble(uboa_reference r, off_t off) { + return *(double*)(((Object*)loadValueBarrier(r))->data + off); +} + +void uboa_load(void* dst, uboa_reference src, off_t off, size_t n) { + memcpy(dst, ((Object*)loadValueBarrier(src))->data + off, n); +} + +void uboa_storeInt8(uboa_reference r, off_t off, int8_t v) { + *(int8_t*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_storeInt16(uboa_reference r, off_t off, int16_t v) { + *(int16_t*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_storeInt32(uboa_reference r, off_t off, int32_t v) { + *(int32_t*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_storeInt64(uboa_reference r, off_t off, int64_t v) { + *(int64_t*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_storeFloat(uboa_reference r, off_t off, float v) { + *(float*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_storeDouble(uboa_reference r, off_t off, double v) { + *(double*)(((Object*)loadValueBarrier(r))->data + off) = v; +} + +void uboa_store(uboa_reference dst, off_t off, void* src, size_t n) { + memcpy(((Object*)loadValueBarrier(dst))->data + off, src, n); +} + +void uboa_memset(uboa_reference r, off_t off, int8_t c, size_t n) { + memset(((Object*)loadValueBarrier(r))->data + off, c, n); +} + +void uboa_memcpy(uboa_reference dst, off_t dstOff, uboa_reference src, off_t srcOff, size_t n) { + memcpy(((Object*)loadValueBarrier(dst))->data + dstOff, ((Object*)loadValueBarrier(src))->data + srcOff, n); +} + +void uboa_memmove(uboa_reference dst, off_t dstOff, uboa_reference src, off_t srcOff, size_t n) { + memmove(((Object*)loadValueBarrier(dst))->data + dstOff, ((Object*)loadValueBarrier(src))->data + srcOff, n); +} + +int uboa_memcmp(uboa_reference r1, off_t off1, uboa_reference r2, off_t off2, size_t n) { + return memcmp(((Object*)loadValueBarrier(r1))->data + off1, ((Object*)loadValueBarrier(r2))->data + off2, n); +} \ No newline at end of file diff --git a/src/share/stdafx.h b/src/share/stdafx.h index 7401730..6c4cec7 100644 --- a/src/share/stdafx.h +++ b/src/share/stdafx.h @@ -1,7 +1,7 @@ #ifndef UBOA_GC_STDAFX_ #define UBOA_GC_STDAFX_ -typedef struct AppThrd AppThrd, *AppThrdHandle; +typedef struct AppThrd AppThrd, *uboa_appThrdHandle; #define UBOA_GC_SOURCE_ #include "../uboa.h" diff --git a/src/uboa.h b/src/uboa.h index afd71e7..4244c82 100644 --- a/src/uboa.h +++ b/src/uboa.h @@ -7,7 +7,7 @@ #include #ifndef UBOA_GC_SOURCE_ -typedef void* AppThrdHandle; +typedef void* uboa_appThrdHandle; #endif typedef long off_t; @@ -20,6 +20,12 @@ const char* uboa_version(); void uboa_init(int nGCWorkers, size_t xmx, long timerInterval_min, bool enableRuleProactive, const char* logFilePath); +uboa_appThrdHandle uboa_createAppThrdHandle(void* stack); +void uboa_destroyAppThrdHandle(uboa_appThrdHandle); + +uboa_reference uboa_pushReference(uboa_appThrdHandle); +void uboa_popReferences(uboa_appThrdHandle, size_t); + // following APIs should be used in safe region void uboa_enterSafeRegion(); void uboa_exitSafeRegion(); @@ -30,23 +36,35 @@ void uboa_exitSafeRegion(); void uboa_gc(const char* reason); -AppThrdHandle uboa_createAppThrdHandle(void* stack); -void uboa_destroyAppThrdHandle(AppThrdHandle); - -uboa_reference uboa_pushReference(AppThrdHandle); -void uboa_popReferences(AppThrdHandle, size_t); - -void uboa_new_Oop(AppThrdHandle, uboa_reference, size_t, off_t*); -void uboa_new(AppThrdHandle, uboa_reference, size_t, uboa_reference oopRef); -void uboa_newArray(AppThrdHandle, uboa_reference, size_t, size_t count, uboa_reference oopRef); +void uboa_new_Oop(uboa_appThrdHandle, uboa_reference, size_t, off_t*); +void uboa_new(uboa_appThrdHandle, uboa_reference, size_t, uboa_reference oopRef); +void uboa_newArray(uboa_appThrdHandle, uboa_reference, size_t, size_t count, uboa_reference oopRef); uboa_reference uboa_null(); void uboa_assign(uboa_reference dst, uboa_reference src); bool uboa_isNull(uboa_reference); -intptr_t uboa_load(uboa_reference, off_t); -void uboa_store(uboa_reference, off_t, intptr_t); +int8_t uboa_loadInt8(uboa_reference, off_t); +int16_t uboa_loadInt16(uboa_reference, off_t); +int32_t uboa_loadInt32(uboa_reference, off_t); +int64_t uboa_loadInt64(uboa_reference, off_t); +float uboa_loadFloat(uboa_reference, off_t); +double uboa_loadDouble(uboa_reference, off_t); void uboa_loadReference(uboa_reference dst, uboa_reference src, off_t); +void uboa_load(void*, uboa_reference, off_t, size_t); + +void uboa_storeInt8(uboa_reference, off_t, int8_t); +void uboa_storeInt16(uboa_reference, off_t, int16_t); +void uboa_storeInt32(uboa_reference, off_t, int32_t); +void uboa_storeInt64(uboa_reference, off_t, int64_t); +void uboa_storeFloat(uboa_reference, off_t, float); +void uboa_storeDouble(uboa_reference, off_t, double); void uboa_storeReference(uboa_reference dst, off_t, uboa_reference src); +void uboa_store(uboa_reference, off_t, void*, size_t); + +void uboa_memset(uboa_reference, off_t, int8_t, size_t); +void uboa_memcpy(uboa_reference dst, off_t dstOff, uboa_reference src, off_t srcOff, size_t); +void uboa_memmove(uboa_reference dst, off_t dstOff, uboa_reference src, off_t srcOff, size_t); +int uboa_memcmp(uboa_reference r1, off_t off1, uboa_reference r2, off_t off2, size_t); #endif \ No newline at end of file