Skip to content

Commit

Permalink
FEAT: added possibility to keep a track of a Rebol series in a handle…
Browse files Browse the repository at this point in the history
…'s context (guarding this series from GC as long as the handle is referenced)
  • Loading branch information
Oldes committed Dec 6, 2022
1 parent 931ddfb commit 9c128dc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions make/rebol3.nest
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ temp: %make/tmp/
stack-size: 4194304 ;= 4MB (4 * 1024 * 1024)
optimize: 2

version: 3.10.1
version: 3.10.2

target-windows: [
os: windows
Expand Down Expand Up @@ -691,7 +691,7 @@ include-view: [
;- native tests:
include-test-extension: [
host-files: %os/host-ext-test.c
config: TEST_EXTENSIONS ;@@TODO: rename!
define: TEST_EXTENSIONS ;@@TODO: rename!
]

;- native experimantal code:
Expand Down
2 changes: 1 addition & 1 deletion src/core/c-handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
hob = (REBHOB*)Make_Node(HOB_POOL);
hob->data = MAKE_MEM(size);
hob->index = idx;
hob->flags = 0;
hob->flags = HANDLE_CONTEXT;
hob->sym = sym;
CLEAR(hob->data, size);
USE_HOB(hob);
Expand Down
9 changes: 8 additions & 1 deletion src/core/m-gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@

#include "sys-core.h"
#include "reb-evtypes.h"
//#include "stdio.h"

#ifdef REB_API
extern REBOL_HOST_LIB *Host_Lib;
Expand Down Expand Up @@ -284,6 +285,7 @@ static void Mark_Series(REBSER *series, REBCNT depth);
REBCNT len;
REBSER *ser;
REBVAL *val;
REBHOB *hob;

ASSERT(series != 0, RP_NULL_MARK_SERIES);

Expand Down Expand Up @@ -318,8 +320,13 @@ static void Mark_Series(REBSER *series, REBCNT depth);
break;
case REB_HANDLE:
if (IS_CONTEXT_HANDLE(val)) {
//printf("marked hob: %p %p\n", VAL_HANDLE_CTX(val), val);
hob = VAL_HANDLE_CTX(val);
//printf("marked hob: %p %p\n", hob, val);
MARK_HANDLE_CONTEXT(val);
if (hob->series) {
//puts("marked hob's series");
MARK_SERIES(hob->series);
}
}
else if (IS_SERIES_HANDLE(val) && !HANDLE_GET_FLAG(val, HANDLE_RELEASABLE)) {
//printf("markserhandle %0xh val: %0xh %s \n", (void*)val, VAL_HANDLE(val), VAL_HANDLE_NAME(val));
Expand Down
5 changes: 3 additions & 2 deletions src/include/sys-value.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
** REBOL [R3] Language Interpreter and Run-time Environment
**
** Copyright 2012 REBOL Technologies
** Copyright 2012-2021 Rebol Open Source Contributors
** Copyright 2012-2022 Rebol Open Source Contributors
** REBOL is a trademark of REBOL Technologies
**
** Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -1091,10 +1091,11 @@ typedef struct Reb_Handle_Spec {
} REBHSP;

typedef struct Reb_Handle_Context {
REBYTE *data;
REBYTE *data; // Pointer to raw data
REBCNT sym; // Index of the word's symbol. Used as a handle's type!
REBFLG flags:16; // Handle_Flags (HANDLE_CONTEXT_MARKED and HANDLE_CONTEXT_USED)
REBCNT index:16; // Index into Reb_Handle_Spec value
REBSER *series; // Optional pointer to Rebol series, which may be marked by GC
} REBHOB;

typedef struct Reb_Handle {
Expand Down
22 changes: 16 additions & 6 deletions src/os/host-ext-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ char *RX_Spec =
"vec0: command [{return vector size in bytes} v [vector!]]\n"
"vec1: command [{return vector size in bytes (from object)} o [object!]]\n"
"blk1: command [{print type ids of all values in a block} b [block!]]\n"
"hob1: command [{creates XTEST handle}]"
"hob1: command [{creates XTEST handle} bin [binary!]]"
"hob2: command [{prints XTEST handle's data} hndl [handle!]]"
"str0: command [{return a constructed string}]"
"echo: command [{return the input value} value]"
Expand All @@ -90,7 +90,7 @@ char *RX_Spec =
"i: make image! 2x2\n"
"xtest: does [\n"
"foreach blk [\n"
"[x: hob1]"
"[x: hob1 #{0102}]"
"[hob2 x]"
"[h: hndl1]\n"
"[hndl2 h]\n"
Expand Down Expand Up @@ -266,7 +266,7 @@ RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
case 11: //command [{creates a handle}]"
{
RXA_HANDLE(frm, 1) = (void*)42;
RXA_HANDLE_TYPE(frm, 1) = AS_WORD("xtest");
RXA_HANDLE_TYPE(frm, 1) = AS_WORD("xtest_plain");
RXA_TYPE(frm, 1) = RXT_HANDLE;
}
break;
Expand Down Expand Up @@ -315,13 +315,20 @@ RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
return RXR_UNSET;
}
break;
case 16: //command [{creates XTEST handle}]"
case 16: //command [{creates XTEST handle} bin [binary!]]"
{
REBHOB *hob = RL_MAKE_HANDLE_CONTEXT(Handle_XTest);
REBSER *bin = RXA_SERIES(frm, 1);
XTEST* data = (XTEST*)hob->data;

if (SERIES_REST(bin) < 1) {
RL_EXPAND_SERIES(bin, SERIES_TAIL(bin), 1);
}
hob->series = bin;

printf("data=> id: %u num: %i\n", data->id, data->num);
data->id = 1;
data->num = -42;
data->num = SERIES_TAIL(bin);
printf("data=> id: %u num: %i\n", data->id, data->num);

RXA_HANDLE(frm, 1) = hob;
Expand All @@ -335,7 +342,9 @@ RXIEXT int RX_Call(int cmd, RXIFRM *frm, void *ctx) {
REBHOB* hob = RXA_HANDLE(frm, 1);
if (hob->sym == Handle_XTest) {
XTEST* data = (XTEST*)hob->data;
printf("data=> id: %u num: %i\n", data->id, data->num);
REBSER *bin = hob->series;
SERIES_DATA(bin)[0] = SERIES_DATA(bin)[0] + 1;
printf("data=> id: %u num: %i b: %i\n", data->id, data->num, (u8)SERIES_DATA(bin)[0]);
RXA_INT64(frm, 1) = data->num;
RXA_TYPE(frm, 1) = RXT_INTEGER;
}
Expand Down Expand Up @@ -374,6 +383,7 @@ void* releaseXTestContext(void* ctx) {
printf("data=> id: %u num: %i\n", data->id, data->num);
CLEARS(data);
printf("data=> id: %u num: %i\n", data->id, data->num);
return NULL;
}

void Init_Ext_Test(void)
Expand Down

0 comments on commit 9c128dc

Please sign in to comment.