Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
LizBing committed Dec 17, 2022
1 parent bc7161e commit ff2b5bf
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

#include <unistd.h>

AppThrdHandle statics = NULL;
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) {
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);
Expand All @@ -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);
}
Expand All @@ -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));
}
)

Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
Expand Down
6 changes: 3 additions & 3 deletions src/share/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;

Expand Down
40 changes: 5 additions & 35 deletions src/share/appThrd.c
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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));

Expand All @@ -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;
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/share/appThrd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "../os/thread.h"

typedef struct AppThrd AppThrd, *AppThrdHandle;
typedef struct AppThrd AppThrd, *uboa_appThrdHandle;
struct AppThrd {
Page* TLAB;

Expand Down
2 changes: 1 addition & 1 deletion src/share/oop.c
Original file line number Diff line number Diff line change
@@ -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());

Expand Down
95 changes: 95 additions & 0 deletions src/share/operate.c
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion src/share/stdafx.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
42 changes: 30 additions & 12 deletions src/uboa.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <stdio.h>

#ifndef UBOA_GC_SOURCE_
typedef void* AppThrdHandle;
typedef void* uboa_appThrdHandle;
#endif

typedef long off_t;
Expand All @@ -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();
Expand All @@ -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

0 comments on commit ff2b5bf

Please sign in to comment.