From 4bbdc2cd4175eefbcdef2b6216740460cd1b4725 Mon Sep 17 00:00:00 2001 From: LizBing Date: Thu, 22 Dec 2022 23:48:08 +0800 Subject: [PATCH] main --- .gitignore | 4 +- README.md | 5 +-- demo/test.c | 86 ------------------------------------------- makefile | 4 +- src/share/allocator.c | 10 +++-- src/share/appThrd.c | 14 +++++-- src/share/appThrd.h | 5 ++- src/share/mark.c | 9 ++--- src/share/object.h | 3 +- src/share/oop.c | 19 ---------- src/share/oop.h | 20 ---------- src/share/operate.c | 15 ++++++-- src/uboa.h | 28 +++++++++----- 13 files changed, 61 insertions(+), 161 deletions(-) delete mode 100644 demo/test.c delete mode 100644 src/share/oop.c delete mode 100644 src/share/oop.h diff --git a/.gitignore b/.gitignore index b818852..9fdf61e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .vscode build -debug \ No newline at end of file +debug +plan +demo \ No newline at end of file diff --git a/README.md b/README.md index a9c9f24..ad08225 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,9 @@ ## 为什么要做这个? 单纯是对这门技术感兴趣,花自己空闲时间研究了八个月,做出来给众多C/C++同好开发使用。 -## 为什么要用C做? -C是一门非常优秀的语言,它的语法简单,编译速度快,运行速度快,内存管理简单。 +## 说好的C++支持我不做了,反正也没人用。这个0.0.0dev5版本我连测试都没做就挂上来了,没什么动力,最近还得了新冠太累了,往后吧。 -## 基本操作接口(v0.0.0dev2, v0.0.0dev3的自己估计看看就懂了吧) +## 基本操作接口(v0.0.0dev2, 更新的版本的看接口名估计就懂了吧,dev版本我都不再更新接口说明了) ### 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 deleted file mode 100644 index f9149dc..0000000 --- a/demo/test.c +++ /dev/null @@ -1,86 +0,0 @@ -#include "../src/uboa.h" -#include -#include -#include - -#include - -uboa_appThrdHandle statics = NULL; -uboa_reference oop = NULL; - -const size_t length = 15; -const off_t numOff = sizeof(off_t) * length; -off_t* map = NULL; - -void foo(long num) { - uboa_safeRegion( - uboa_appThrdHandle hdl = uboa_createAppThrdHandle(alloca(1024)); - uboa_reference ref[length]; - for(int i = 0; i < length; ++i) - ref[i] = uboa_pushReference(hdl); - ) - - uboa_safeRegion( - for(int i = 0; i < length; ++i) { - if(num < 8) - uboa_new(hdl, ref[i], 4096, oop); - else if(num < 10) - uboa_new(hdl, ref[i], 1 << 19, oop); - else - uboa_new(hdl, ref[i], 1l << 24, oop); - - 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_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_storeInt32(tmp, numOff, 114514); - - uboa_popReferences(hdl, 1); - } - } - ) - } - - uboa_safeRegion( - for(int i = 0; i < length; ++i) { - uboa_loadReference(ref[i], ref[i], map[i]); - assert(114514 == uboa_loadInt32(ref[i], numOff)); - } - - uboa_destroyAppThrdHandle(hdl); - ) -} - -int main() { - uboa_init(12, (1l << 36) + (1l << 35), 0, false, "./log.txt"); - - map = alloca(sizeof(off_t) * length); - for(int i = 0; i < length; ++i) - map[i] = i * 8; - - uboa_safeRegion( - statics = uboa_createAppThrdHandle(alloca(1024)); - oop = uboa_pushReference(statics); - uboa_new_Oop(statics, oop, length, map); - ) - - for(int i = 0; i < 12; ++i) { - pthread_t t = { 0 }; - pthread_create(&t, NULL, foo, (long)i); - pthread_detach(t); - } - - sleep(120); - printf("Done\n"); - - return 0; -} \ No newline at end of file diff --git a/makefile b/makefile index 3330485..1287893 100644 --- a/makefile +++ b/makefile @@ -3,8 +3,8 @@ std = -std=c11 out = build/lib/libuboa.a macro = \ - -DBUILD_DATE="\"12/19/2022, Monday\"" \ - -DGC_VERSION="\"0.0.0.dev4\"" \ + -DBUILD_DATE="\"12/12/2022, Tursday\"" \ + -DGC_VERSION="\"0.0.0.dev5\"" \ -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 9613919..fb2b782 100644 --- a/src/share/allocator.c +++ b/src/share/allocator.c @@ -126,8 +126,8 @@ inline void* memAlloc(size_t s) { return survAllocSmall(align(s, alignmentSmall), NULL); } -void uboa_new(uboa_appThrdHandle hdl, uboa_reference r, size_t s, uboa_reference oopRef) { - uboa_newArray(hdl, r, s, 1, oopRef); +void uboa_new(uboa_appThrdHandle hdl, uboa_reference r, size_t s, Oop oop) { + uboa_newArray(hdl, r, s, 1, oop); } inline void Object_init(Object* this, Page* p) { @@ -137,7 +137,9 @@ inline void Object_init(Object* this, Page* p) { LargeObject_push(this); } -void uboa_newArray(uboa_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, Oop oop) { + uboa_assert(!hdl->free, "memory allocation should be done in bound region."); + Object* ptr = NULL; Page* src = NULL; @@ -156,7 +158,7 @@ void uboa_newArray(uboa_appThrdHandle hdl, uboa_reference r, size_t s, size_t co recordAllocation(size); Object_init(ptr, src); - uboa_assign(&ptr->oopObj, oopRef); + ptr->oop = oop; ptr->count = count; ptr->size = size; ptr->unit = s; diff --git a/src/share/appThrd.c b/src/share/appThrd.c index d825431..90a8f08 100644 --- a/src/share/appThrd.c +++ b/src/share/appThrd.c @@ -31,12 +31,12 @@ void forEachRoot(void(*func)(AppThrd*)) { n = iter; iter = n->next; - atomic_flag_test_and_set(&n->flag); + atomic_flag_test_and_set(&n->lock); atomic_flag_clear(&sl); func(n); - atomic_flag_clear(&n->flag); + atomic_flag_clear(&n->lock); } } @@ -71,7 +71,7 @@ void uboa_destroyAppThrdHandle(uboa_appThrdHandle hdl) { atomic_flag_clear(&sl); - while(atomic_flag_test_and_set(&hdl->flag)); + while(atomic_flag_test_and_set(&hdl->lock)); } uboa_reference uboa_pushReference(uboa_appThrdHandle hdl) { @@ -82,4 +82,12 @@ uboa_reference uboa_pushReference(uboa_appThrdHandle hdl) { void uboa_popReferences(uboa_appThrdHandle hdl, size_t n) { hdl->size -= n; +} + +void uboa_enterRiskRegion(uboa_appThrdHandle hdl) { + hdl->free = true; +} + +void uboa_exitRiskRegion(uboa_appThrdHandle hdl) { + hdl->free = false; } \ No newline at end of file diff --git a/src/share/appThrd.h b/src/share/appThrd.h index 5326feb..e25c6e6 100644 --- a/src/share/appThrd.h +++ b/src/share/appThrd.h @@ -19,11 +19,12 @@ typedef struct AppThrd AppThrd, *uboa_appThrdHandle; struct AppThrd { _Atomic(Page*) TLAB; - atomic_flag flag; + bool free; + atomic_flag lock; AppThrd* prev, *next; atomic_size_t size; - atomic_intptr_t stack[0]; + uboa_pointer stack[0]; }; void forEachRoot(void(*func)(AppThrd*)); diff --git a/src/share/mark.c b/src/share/mark.c index 0996a4b..57c46dc 100644 --- a/src/share/mark.c +++ b/src/share/mark.c @@ -121,15 +121,12 @@ void worker_markFollowings() { Object* n = NULL; while((n = Following_pop())) { bool trigger = false; - if(n->oopObj) { - mark(&n->oopObj); - Oop oop = ((Object*)n->oopObj)->data; - + if(n->oop) { for(long i = 0; i < n->count; ++i) { void* base = n->data + n->unit * i; - for(long j = 0; j < oop->size; ++j) - if(mark(base + oop->map[j])) + for(long j = 0; j < n->oop->size; ++j) + if(mark(base + n->oop->map[j])) trigger = true; } } diff --git a/src/share/object.h b/src/share/object.h index a7dfcb7..84a32ed 100644 --- a/src/share/object.h +++ b/src/share/object.h @@ -11,7 +11,6 @@ #define UBOA_GC_OBJECT_ #include "page.h" -#include "oop.h" struct Object { ListNode nodeFollowing, nodeLiveSet; @@ -19,7 +18,7 @@ struct Object { Page* page; - atomic_intptr_t oopObj; + Oop oop; size_t size, unit, count; byte data[0]; diff --git a/src/share/oop.c b/src/share/oop.c deleted file mode 100644 index 218b626..0000000 --- a/src/share/oop.c +++ /dev/null @@ -1,19 +0,0 @@ -/** - * @file oop.c - * @author Lizbing (lizbing@relight.com) - * - * @copyright Copyright (c) 2022. All rights reserved. - * - */ - -#include "oop.h" -#include "object.h" - -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()); - - Oop oop = ((Object*)*r)->data; - oop->size = s; - memcpy(oop->map, m, size); -} \ No newline at end of file diff --git a/src/share/oop.h b/src/share/oop.h deleted file mode 100644 index 7e61235..0000000 --- a/src/share/oop.h +++ /dev/null @@ -1,20 +0,0 @@ -/** - * @file oop.h - * @author Lizbing (lizbing@relight.com) - * @brief OOP maps for objects - * - * @copyright Copyright (c) 2022. All rights reserved. - * - */ - -#ifndef UBOA_GC_OOP_ -#define UBOA_GC_OOP_ - -#include "stdafx.h" - -typedef struct { - size_t size; - off_t map[0]; -} OopDec, *Oop; - -#endif \ No newline at end of file diff --git a/src/share/operate.c b/src/share/operate.c index 336c5b1..241de48 100644 --- a/src/share/operate.c +++ b/src/share/operate.c @@ -7,11 +7,11 @@ * */ +#include "appThrd.h" #include "barrier.h" -static const intptr_t null = 0; - -uboa_reference uboa_null() { +uboa_reference uboa_nullref() { + static const uboa_pointer null = 0; return &null; } @@ -23,6 +23,15 @@ bool uboa_isNull(uboa_reference r) { return !*r; } +void* uboa_castToObject(uboa_appThrdHandle hdl, uboa_reference r) { + uboa_assert(hdl->free, "called 'uboa_getObject' in bound region."); + return loadValueBarrier(r)->data; +} + +uboa_pointer uboa_castToPointer(void* p) { + return p - sizeof(Object); +} + void uboa_loadReference(uboa_reference dst, uboa_reference src, off_t off) { uboa_assign(dst, loadValueBarrier(src)->data + off); } diff --git a/src/uboa.h b/src/uboa.h index 689be42..c9a8189 100644 --- a/src/uboa.h +++ b/src/uboa.h @@ -1,7 +1,6 @@ /** * @file uboa.h * @author Lizbing (lizbing@relight.team) - * @brief * @version 0.0.0dev5 * @date 2022-12-20 * @@ -15,13 +14,20 @@ #include #include #include +#include #ifndef UBOA_GC_SOURCE_ typedef void* uboa_appThrdHandle; #endif typedef long off_t; -typedef atomic_intptr_t* uboa_reference; +typedef atomic_intptr_t* uboa_reference, uboa_pointer; +#define uboa_weakReference(ptr) (&(ptr)) + +typedef struct { + size_t size; + off_t* map; +} OopDec, *Oop; const char* uboa_copyright(); const char* uboa_name(); @@ -38,22 +44,24 @@ void uboa_popReferences(uboa_appThrdHandle, size_t); void uboa_enterSafeRegion(); void uboa_exitSafeRegion(); -#define uboa_safeRegion(...) \ - uboa_enterSafeRegion(); \ - __VA_ARGS__ \ - uboa_exitSafeRegion(); +void uboa_enterRiskRegion(uboa_appThrdHandle); +void uboa_exitRiskRegion(uboa_appThrdHandle); void uboa_gc(const char* reason); -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); +void uboa_new(uboa_appThrdHandle, uboa_reference, size_t, Oop oop); +void uboa_newArray(uboa_appThrdHandle, uboa_reference, size_t, size_t count, Oop oop); + +#define uboa_nullptr ((uboa_pointer)0) +uboa_reference uboa_nullref(); -uboa_reference uboa_null(); void uboa_assign(uboa_reference dst, uboa_reference src); bool uboa_isNull(uboa_reference); bool uboa_equal(uboa_reference, uboa_reference); +void* uboa_castToObject(uboa_appThrdHandle, uboa_reference); +uboa_pointer uboa_castToPointer(void*); + int8_t uboa_loadInt8(uboa_reference, off_t); int16_t uboa_loadInt16(uboa_reference, off_t); int32_t uboa_loadInt32(uboa_reference, off_t);