Skip to content

Commit ae8ea4a

Browse files
committed
rt: Add constructors and destructors for stacks
1 parent b98df86 commit ae8ea4a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/rt/rust_stack.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef RUST_STACK_H
2+
#define RUST_STACK_H
3+
14
struct stk_seg {
25
stk_seg *prev;
36
stk_seg *next;
@@ -10,6 +13,19 @@ struct stk_seg {
1013
uint8_t data[];
1114
};
1215

16+
template <class T>
17+
stk_seg *
18+
create_stack(T allocer, size_t sz) {
19+
size_t total_sz = sizeof(stk_seg) + sz;
20+
return (stk_seg *)allocer->malloc(total_sz, "stack");
21+
}
22+
23+
template <class T>
24+
void
25+
destroy_stack(T allocer, stk_seg *stk) {
26+
allocer->free(stk);
27+
}
28+
1329
void
1430
config_valgrind_stack(stk_seg *stk);
1531

@@ -21,3 +37,5 @@ add_stack_canary(stk_seg *stk);
2137

2238
void
2339
check_stack_canary(stk_seg *stk);
40+
41+
#endif /* RUST_STACK_H */

src/rt/rust_task.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ void
538538
rust_task::free_stack(stk_seg *stk) {
539539
LOGPTR(thread, "freeing stk segment", (uintptr_t)stk);
540540
total_stack_sz -= user_stack_size(stk);
541-
free(stk);
541+
destroy_stack(this, stk);
542542
}
543543

544544
void
@@ -581,8 +581,8 @@ rust_task::new_stack(size_t requested_sz) {
581581
fail();
582582
}
583583

584-
size_t sz = sizeof(stk_seg) + rust_stk_sz + RED_ZONE_SIZE;
585-
stk_seg *new_stk = (stk_seg *)malloc(sz, "stack");
584+
size_t sz = rust_stk_sz + RED_ZONE_SIZE;
585+
stk_seg *new_stk = create_stack(this, sz);
586586
LOGPTR(thread, "new stk", (uintptr_t)new_stk);
587587
memset(new_stk, 0, sizeof(stk_seg));
588588
add_stack_canary(new_stk);

0 commit comments

Comments
 (0)