Skip to content

Commit 6df4649

Browse files
authored
Merge pull request #228 from nature-lang/feature/string_vec_map_set_to_value
feat: string/vec/map/set to value
2 parents d8d165e + 2412308 commit 6df4649

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1586
-1077
lines changed

runtime/allocator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ void *gc_malloc(uint64_t rhash) {
14821482
bitmap_to_str(RTDATA(rtype->malloc_gc_bits_offset),
14831483
calc_gc_bits_size(rtype->heap_size, POINTER_SIZE)));
14841484

1485-
void *result = rti_gc_malloc(rtype->heap_size, rtype);
1485+
void *result = rti_gc_malloc(rtype->gc_heap_size, rtype);
14861486
DEBUGF("[gc_malloc] size %lu, value %p", rtype->heap_size, result);
14871487
return result;
14881488
}

runtime/builtin.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ static void print_arg(n_union_t *arg) {
120120
}
121121

122122

123-
void print(n_vec_t *args) {
123+
void print(n_vec_t args) {
124124
// any_trans 将 int 转换成了堆中的一段数据,并将堆里面的其实地址返回了回去
125-
// 所以 args->data 是一个堆里面的地址,其指向的堆内存区域是 [any_start_ptr1, any_start_ptr2m, ...]
126-
addr_t base = (addr_t) args->data;// 把 data 中存储的值赋值给 p
127-
uint64_t element_size = args->element_size;
125+
// 所以 args.data 是一个堆里面的地址,其指向的堆内存区域是 [any_start_ptr1, any_start_ptr2m, ...]
126+
addr_t base = (addr_t) args.data;// 把 data 中存储的值赋值给 p
127+
uint64_t element_size = args.element_size;
128128

129-
DEBUGF("[runtime.print] args vec=%p, len=%lu, data=%p, element_size=%lu", args, args->length, (void *) base,
129+
DEBUGF("[runtime.print] args len=%lu, data=%p, element_size=%lu", args.length, (void *) base,
130130
element_size);
131131

132-
for (int i = 0; i < args->length; ++i) {
132+
for (int i = 0; i < args.length; ++i) {
133133
addr_t p = base + (i * element_size);
134134

135135
// 将 p 中存储的地址赋值给 a, 此时 a 中存储的是一个堆中的地址,其结构是 memory_any_t
@@ -142,16 +142,16 @@ void print(n_vec_t *args) {
142142
}
143143
}
144144

145-
void println(n_vec_t *args) {
145+
void println(n_vec_t args) {
146146
// any_trans 将 int 转换成了堆中的一段数据,并将堆里面的其实地址返回了回去
147-
// 所以 args->data 是一个堆里面的地址,其指向的堆内存区域是 [any_start_ptr1, any_start_ptr2m, ...]
148-
addr_t base = (addr_t) args->data;// 把 data 中存储的值赋值给 p
149-
uint64_t element_size = args->element_size;
147+
// 所以 args.data 是一个堆里面的地址,其指向的堆内存区域是 [any_start_ptr1, any_start_ptr2m, ...]
148+
addr_t base = (addr_t) args.data;// 把 data 中存储的值赋值给 p
149+
uint64_t element_size = args.element_size;
150150

151-
DEBUGF("[runtime.println] args vec=%p, len=%lu, data=%p, element_size=%lu", args, args->length, (void *) base,
151+
DEBUGF("[runtime.println] args len=%lu, data=%p, element_size=%lu", args.length, (void *) base,
152152
element_size);
153153

154-
for (int i = 0; i < args->length; ++i) {
154+
for (int i = 0; i < args.length; ++i) {
155155
addr_t p = base + (i * element_size);
156156

157157
// 将 p 中存储的地址赋值给 a, 此时 a 中存储的是一个堆中的地址,其结构是 memory_any_t
@@ -162,7 +162,7 @@ void println(n_vec_t *args) {
162162

163163
print_arg(union_arg);
164164

165-
if (i < (args->length - 1)) {
165+
if (i < (args.length - 1)) {
166166
VOID write(STDOUT_FILENO, space, 1);
167167
}
168168
}

runtime/builtin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include "utils/type.h"
55

6-
void print(n_vec_t *args);
6+
void print(n_vec_t args);
77

8-
void println(n_vec_t *args);
8+
void println(n_vec_t args);
99

1010
#endif //NATURE_SRC_LIR_NATIVE_BUILTIN_H_

runtime/collector.c

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@ static bool sweep_span(mcentral_t *central, mspan_t *span) {
187187
allocated_bytes -= span->obj_size;
188188
has_freed = true;
189189

190-
DEBUGF("[sweep_span] will sweep, span_base=%p obj_addr=%p", span->base, (void *) (span->base + i * span->obj_size));
190+
DEBUGF("[sweep_span] will sweep, span_base=%p obj_addr=%p", span->base,
191+
(void *) (span->base + i * span->obj_size));
191192
// memset((void *) (span->base + i * span->obj_size), 0, span->obj_size);
192193
} else {
193-
DEBUGF("[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d",
194-
span->base,
195-
(void *) (span->base + i * span->obj_size), bitmap_test(span->alloc_bits, i),
196-
bitmap_test(span->gcmark_bits, i));
194+
DEBUGF(
195+
"[sweep_span] will sweep, span_base=%p, obj_addr=%p, not calc allocated_bytes, alloc_bit=%d, gcmark_bit=%d",
196+
span->base,
197+
(void *) (span->base + i * span->obj_size), bitmap_test(span->alloc_bits, i),
198+
bitmap_test(span->gcmark_bits, i));
197199
}
198200
}
199201

@@ -330,11 +332,11 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
330332
addr_t sp_value = (addr_t) co->aco.reg[ACO_REG_IDX_SP];
331333

332334
DEBUGF(
333-
"[runtime_gc.scan_stack] start, p_index=%d(%lu), p_status=%d, co=%p, co_status=%d, co_stack_size=%zu, save_stack=%p(%zu), "
334-
"bp_value=%p, sp_value=%p, share_stack.base=%p",
335-
p->index, (uint64_t) p->thread_id, p->status, co, co->status, co->aco.save_stack.valid_sz,
336-
co->aco.save_stack.ptr,
337-
co->aco.save_stack.sz, bp_value, sp_value, co->aco.share_stack->align_retptr);
335+
"[runtime_gc.scan_stack] start, p_index=%d(%lu), p_status=%d, co=%p, co_status=%d, co_stack_size=%zu, save_stack=%p(%zu), "
336+
"bp_value=%p, sp_value=%p, share_stack.base=%p",
337+
p->index, (uint64_t) p->thread_id, p->status, co, co->status, co->aco.save_stack.valid_sz,
338+
co->aco.save_stack.ptr,
339+
co->aco.save_stack.sz, bp_value, sp_value, co->aco.share_stack->align_retptr);
338340

339341
// save_stack 也是通过 gc 申请,即使是 gc_work 也需要标记一下
340342
assert(p->gc_work_finished < memory->gc_count && "gc work finished, cannot insert to gc worklist");
@@ -352,8 +354,8 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
352354
insert_gc_worklist(worklist, co->error);
353355
}
354356

355-
if (co->traces) {
356-
insert_gc_worklist(worklist, co->traces);
357+
if (co->traces.data && span_of((addr_t) co->traces.data)) {
358+
insert_gc_worklist(worklist, co->traces.data);
357359
}
358360

359361
if (co->flag & FLAG(CO_FLAG_RTFN)) {
@@ -408,7 +410,9 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
408410

409411
if (ret_addr == assist_preempt_yield_ret_addr) {
410412
found_assist = true;
411-
DEBUGF("[runtime_gc.scan_stack] find assist_preempt_yield_ret_addr %p, conservative treatment will be carried out", (void *) assist_preempt_yield_ret_addr);
413+
DEBUGF(
414+
"[runtime_gc.scan_stack] find assist_preempt_yield_ret_addr %p, conservative treatment will be carried out",
415+
(void *) assist_preempt_yield_ret_addr);
412416
break;
413417
}
414418
// check prev value is nature fn
@@ -489,8 +493,9 @@ static void scan_stack(n_processor_t *p, coroutine_t *co) {
489493

490494
fndef_t *fn = find_fn(ret_addr, p);
491495
if (!fn) {
492-
DEBUGF("[runtime_gc.scan_stack] fn not found by ret_addr, return_addr=%p, share_stack_frame_bp=%p, bp_offset = %ld",
493-
(void *) ret_addr, (void *) share_stack_frame_bp, share_stack_frame_bp - sp_value);
496+
DEBUGF(
497+
"[runtime_gc.scan_stack] fn not found by ret_addr, return_addr=%p, share_stack_frame_bp=%p, bp_offset = %ld",
498+
(void *) ret_addr, (void *) share_stack_frame_bp, share_stack_frame_bp - sp_value);
494499

495500
// next and continue
496501
addr_t bp_offset = share_stack_frame_bp - sp_value;
@@ -591,11 +596,12 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
591596
}
592597

593598
bitmap_set(span->gcmark_bits, obj_index);
594-
DEBUGF("[runtime_gc.handle_gc_ptr] addr=%p, span=%p, span_base=%p, obj_index=%lu marked, test=%d, obj_size=%d, spanclass_has_ptr=%d",
595-
(void *) addr,
596-
span,
597-
(void *) span->base,
598-
obj_index, bitmap_test(span->gcmark_bits, obj_index), span->obj_size, spanclass_has_ptr(span->spanclass));
599+
DEBUGF(
600+
"[runtime_gc.handle_gc_ptr] addr=%p, span=%p, span_base=%p, obj_index=%lu marked, test=%d, obj_size=%d, spanclass_has_ptr=%d",
601+
(void *) addr,
602+
span,
603+
(void *) span->base,
604+
obj_index, bitmap_test(span->gcmark_bits, obj_index), span->obj_size, spanclass_has_ptr(span->spanclass));
599605

600606
mutex_unlock(&span->gcmark_locker);
601607

@@ -621,9 +627,9 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
621627
addr_t value = fetch_addr_value(temp_addr);
622628

623629
DEBUGF(
624-
"[handle_gc_ptr] addr is ptr,base=%p cursor=%p cursor_value=%p, obj_size=%ld, bit_index=%lu, in_heap=%d",
625-
(void *) addr,
626-
(void *) temp_addr, (void *) value, span->obj_size, bit_index, in_heap(value));
630+
"[handle_gc_ptr] addr is ptr,base=%p cursor=%p cursor_value=%p, obj_size=%ld, bit_index=%lu, in_heap=%d",
631+
(void *) addr,
632+
(void *) temp_addr, (void *) value, span->obj_size, bit_index, in_heap(value));
627633

628634
if (span_of(value)) {
629635
// assert(span_of(heap_addr) && "heap_addr not belong active span");
@@ -640,9 +646,9 @@ static void handle_gc_ptr(n_processor_t *p, addr_t addr) {
640646
}
641647
} else {
642648
DEBUGF(
643-
"[handle_gc_ptr] addr not ptr,base=%p cursor=%p cursor_value(int)=%p, obj_size=%ld, bit_index=%lu",
644-
(void *) addr,
645-
(void *) temp_addr, fetch_int_value(temp_addr, POINTER_SIZE), span->obj_size, bit_index);
649+
"[handle_gc_ptr] addr not ptr,base=%p cursor=%p cursor_value(int)=%p, obj_size=%ld, bit_index=%lu",
650+
(void *) addr,
651+
(void *) temp_addr, fetch_int_value(temp_addr, POINTER_SIZE), span->obj_size, bit_index);
646652
}
647653
}
648654
}
@@ -703,10 +709,10 @@ static void gc_work() {
703709
current = current->succ;
704710

705711
DEBUGF(
706-
"[runtime_gc.gc_work] will scan_stack p_index=%d, co=%p, status=%d, is_main=%d, gc_black=%lu/gc_count=%lu, aco=%p",
707-
share_p->index,
708-
wait_co, wait_co->status, wait_co->main, wait_co->gc_black, memory->gc_count,
709-
&wait_co->aco);
712+
"[runtime_gc.gc_work] will scan_stack p_index=%d, co=%p, status=%d, is_main=%d, gc_black=%lu/gc_count=%lu, aco=%p",
713+
share_p->index,
714+
wait_co, wait_co->status, wait_co->main, wait_co->gc_black, memory->gc_count,
715+
&wait_co->aco);
710716

711717
if (wait_co->status == CO_STATUS_DEAD) {
712718
DEBUGF("[runtime_gc.gc_work] co=%p, main=%d, status=dead, will remove",
@@ -820,7 +826,9 @@ static void scan_pool() {
820826
n_processor_t *p = processor_index[0];
821827
n_string_t *value;
822828
sc_map_foreach_value(&const_str_pool, value) {
823-
rt_linked_fixalloc_push(&p->gc_worklist, value);
829+
if (value && span_of((addr_t) value->data)) {
830+
rt_linked_fixalloc_push(&p->gc_worklist, value->data);
831+
}
824832
}
825833
}
826834

@@ -859,7 +867,8 @@ static void scan_global() {
859867
gc_bits = (uint8_t *) &rtype->gc_bits;
860868
}
861869

862-
if (bitmap_test(gc_bits, index)) { // need gc
870+
if (bitmap_test(gc_bits, index)) {
871+
// need gc
863872
addr_t addr = fetch_addr_value(current);
864873
if (span_of(addr)) {
865874
DEBUGF("[runtime.scan_global] name=%s, kind=%s, base=%p(%p), index=%d, addr=%p need gc",
@@ -1010,8 +1019,10 @@ void runtime_gc() {
10101019
next_gc_bytes = heap_live + (heap_live * GC_PERCENT / 100);
10111020

10121021
gc_stage = GC_STAGE_OFF;
1013-
DEBUGF("[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, before=%ldKB, current=%ldKB, cleanup=%ldKB, alloc_total=%ldKB, remove_to_sys=%ldKB, used=%ldKB, next_gc=%ldKB",
1014-
before / 1024,
1015-
allocated_bytes / 1024,
1016-
(before - allocated_bytes) / 1024, allocated_total_bytes / 1024, remove_total_bytes / 1024, (allocated_total_bytes - remove_total_bytes) / 1024, next_gc_bytes / 1024);
1022+
DEBUGF(
1023+
"[runtime_gc] gc stage: GC_OFF, gc_barrier_stop, before=%ldKB, current=%ldKB, cleanup=%ldKB, alloc_total=%ldKB, remove_to_sys=%ldKB, used=%ldKB, next_gc=%ldKB",
1024+
before / 1024,
1025+
allocated_bytes / 1024,
1026+
(before - allocated_bytes) / 1024, allocated_total_bytes / 1024, remove_total_bytes / 1024,
1027+
(allocated_total_bytes - remove_total_bytes) / 1024, next_gc_bytes / 1024);
10171028
}

runtime/linkco.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ linkco_t *rti_acquire_linkco() {
2525

2626
// 全局借取失败,需要自己 new 一个
2727
if (p->linkco_count == 0) {
28-
linkco_t *linkco = rti_gc_malloc(linkco_rtype.heap_size, &linkco_rtype);
28+
linkco_t *linkco = rti_gc_malloc(linkco_rtype.gc_heap_size, &linkco_rtype);
2929
DEBUGF("[rti_acquire_linkco] p: %d gc_malloc linkco %p", p->index, linkco);
3030
// p->linkco_cache[p->linkco_count++] = linkco;
3131
rti_write_barrier_ptr(&p->linkco_cache[p->linkco_count++], linkco, false);

runtime/memory.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ void register_const_str_pool() {
101101
continue;
102102
}
103103

104-
n_string_t *str = rt_string_new(s.base);
105-
sc_map_put_sv(&const_str_pool, (char *) s.base, str);
104+
n_string_t str = rt_string_new(s.base);
105+
n_string_t *pool_copy = malloc(sizeof(n_string_t));
106+
if (pool_copy == NULL) {
107+
continue;
108+
}
109+
*pool_copy = str;
110+
sc_map_put_sv(&const_str_pool, (char *) s.base, pool_copy);
106111
// TDEBUGF("[register_const_str_pool] str = %p, str %s", str, (char *) s.base);
107112
}
108113
};

runtime/nutils/array.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
static inline n_array_t *rti_array_new(rtype_t *element_rtype, uint64_t length) {
1010
assert(element_rtype && "element_rtype is null");
11-
assert(element_rtype->heap_size > 0 && "element_rtype size is zero");
11+
assert(element_rtype->gc_heap_size > 0 && "element_rtype size is zero");
1212

1313
DEBUGF("[rti_array_new] ele_sz=%lu(rtype_stack_size=%lu),ele_kind=%s(n_gc=%d),len=%lu", element_rtype->heap_size,
1414
element_rtype->storage_size, type_kind_str[element_rtype->kind],
@@ -18,7 +18,7 @@ static inline n_array_t *rti_array_new(rtype_t *element_rtype, uint64_t length)
1818
rtype_t rtype = rti_rtype_array(element_rtype, length);
1919

2020
// - 基于 rtype 进行 malloc 的申请调用, 这里进行的是堆内存申请,所以需要的就是其在在堆内存中占用的空间大小
21-
void *addr = rti_gc_malloc(rtype.heap_size, &rtype);
21+
void *addr = rti_gc_malloc(rtype.gc_heap_size, &rtype);
2222
DEBUGF(
2323
"[rti_array_new] success, base=%p, element_rtype.size=%lu, element_rtype.kind=%s(last_ptr=%d), "
2424
"array_rtype_size=%lu(length=%lu),rtype_kind=%s, rtype_last_ptr=%d",

runtime/nutils/dns.h

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include "runtime/processor.h"
44
#include <uv.h>
55

6+
typedef struct {
7+
n_string_t host;
8+
n_vec_t ips;
9+
} dns_ctx_t;
10+
611
static inline void on_dns_resolved_cb(uv_getaddrinfo_t *req, int status, struct addrinfo *res) {
712
coroutine_t *co = req->data;
813
DEBUGF("[on_dns_resolved_cb] co: %p, status: %d", co, status);
@@ -14,7 +19,8 @@ static inline void on_dns_resolved_cb(uv_getaddrinfo_t *req, int status, struct
1419
}
1520

1621
// co->data = res; // addr info need use uv_freeaddrinfo free
17-
n_vec_t *ips = co->data;
22+
dns_ctx_t *ctx = co->data;
23+
n_vec_t *ips = &ctx->ips;
1824
struct addrinfo *current = res;
1925
while (current != NULL) {
2026
if (current->ai_family == AF_INET) {
@@ -23,15 +29,15 @@ static inline void on_dns_resolved_cb(uv_getaddrinfo_t *req, int status, struct
2329

2430
uv_ip4_name(addr_in, addr, 16);
2531

26-
n_string_t *ip = rt_string_new((n_anyptr_t) addr);
32+
n_string_t ip = rt_string_new((n_anyptr_t) addr);
2733
rt_vec_push(ips, string_rtype.hash, &ip);
2834
} else if (current->ai_family == AF_INET6) {
2935
char addr6[INET6_ADDRSTRLEN] = {'\0'};
3036

3137
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *) current->ai_addr;
3238
uv_ip6_name(addr_in6, addr6, sizeof(addr6));
3339

34-
n_string_t *ip = rt_string_new((n_anyptr_t) addr6);
40+
n_string_t ip = rt_string_new((n_anyptr_t) addr6);
3541
rt_vec_push(ips, string_rtype.hash, &ip);
3642
}
3743
current = current->ai_next;
@@ -41,35 +47,42 @@ static inline void on_dns_resolved_cb(uv_getaddrinfo_t *req, int status, struct
4147
co_ready(co);
4248
}
4349

44-
void uv_async_getaddrinfo_register(uv_getaddrinfo_t *req, n_string_t *host) {
45-
DEBUGF("[uv_async_getaddrinfo_register] host is %s, co=%p", (char *) rt_string_ref(host), req->data);
50+
void uv_async_getaddrinfo_register(uv_getaddrinfo_t *req, dns_ctx_t *ctx) {
51+
DEBUGF("[uv_async_getaddrinfo_register] host is %s, co=%p", (char *) rt_string_ref(&ctx->host), req->data);
4652
struct addrinfo hints;
4753
memset(&hints, 0, sizeof(hints));
4854
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
4955
hints.ai_socktype = SOCK_STREAM; // TCP
5056

51-
int result = uv_getaddrinfo(&global_loop, req, on_dns_resolved_cb, rt_string_ref(host), NULL, &hints);
57+
int result = uv_getaddrinfo(&global_loop, req, on_dns_resolved_cb, rt_string_ref(&ctx->host), NULL, &hints);
5258
if (result) {
5359
DEBUGF("[uv_async_getaddrinfo_register] uv_getaddrinfo failed: %s, co=%p", uv_strerror(result), req->data);
54-
rti_co_throw(req->data, tlsprintf("resolve %s failed: %s", rt_string_ref(host), uv_strerror(result)), false);
60+
rti_co_throw(req->data, tlsprintf("resolve %s failed: %s", rt_string_ref(&ctx->host), uv_strerror(result)), false);
5561
co_ready(req->data);
5662
return;
5763
}
5864
}
5965

60-
void rt_uv_dns_lookup(n_string_t *host, n_vec_t *ips) {
66+
n_vec_t rt_uv_dns_lookup(n_string_t host) {
6167
n_processor_t *p = processor_get();
6268
coroutine_t *co = coroutine_get();
6369

64-
DEBUGF("[rt_uv_dns_lookup] start, host is %s, co=%p", (char *) rt_string_ref(host), co);
70+
DEBUGF("[rt_uv_dns_lookup] start, host is %s, co=%p", (char *) rt_string_ref(&host), co);
71+
72+
dns_ctx_t *ctx = malloc(sizeof(dns_ctx_t));
73+
ctx->host = host;
74+
ctx->ips = rt_vec_cap(vec_rtype.hash, string_rtype.hash, 0);
6575

6676
uv_getaddrinfo_t *req = malloc(sizeof(uv_getaddrinfo_t));
6777
req->data = co;
68-
co->data = ips;
69-
70-
global_waiting_send(uv_async_getaddrinfo_register, req, host, 0);
78+
co->data = ctx;
7179

80+
global_waiting_send(uv_async_getaddrinfo_register, req, ctx, 0);
7281
free(req);
82+
83+
n_vec_t result = ctx->ips;
84+
free(ctx);
85+
return result;
7386
}
7487

7588
#endif //NATURE_RUNTIME_NUTILS_DNS_H_

0 commit comments

Comments
 (0)