Skip to content

Commit 4c0d41c

Browse files
committedJun 14, 2012
Add a malloc_dyn upcall for dynamically sized allocations on the shared heap.
1 parent 31f4b63 commit 4c0d41c

File tree

7 files changed

+102
-39
lines changed

7 files changed

+102
-39
lines changed
 

‎src/rt/boxed_region.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
#include "rust_globals.h"
33
#include "rust_task.h"
44
#include "rust_env.h"
5+
#include "rust_util.h"
56

67
// #define DUMP_BOXED_REGION
78

8-
rust_opaque_box *boxed_region::malloc(type_desc *td) {
9-
size_t header_size = sizeof(rust_opaque_box);
10-
size_t body_size = td->size;
11-
size_t body_align = td->align;
12-
size_t total_size = align_to(header_size, body_align) + body_size;
9+
rust_opaque_box *boxed_region::malloc(type_desc *td, size_t body_size) {
10+
size_t total_size = get_box_size(body_size, td->align);
1311
rust_opaque_box *box =
1412
(rust_opaque_box*)backing_region->malloc(total_size, "@");
1513
box->td = td;
@@ -22,14 +20,14 @@ rust_opaque_box *boxed_region::malloc(type_desc *td) {
2220
LOG(rust_get_current_task(), box,
2321
"@malloc()=%p with td %p, size %lu==%lu+%lu, "
2422
"align %lu, prev %p, next %p\n",
25-
box, td, total_size, header_size, body_size, body_align,
26-
box->prev, box->next);
23+
box, td, total_size, sizeof(rust_opaque_box), body_size,
24+
td->align, box->prev, box->next);
2725

2826
return box;
2927
}
3028

31-
rust_opaque_box *boxed_region::calloc(type_desc *td) {
32-
rust_opaque_box *box = malloc(td);
29+
rust_opaque_box *boxed_region::calloc(type_desc *td, size_t body_size) {
30+
rust_opaque_box *box = malloc(td, body_size);
3331
memset(box_body(box), 0, td->size);
3432
return box;
3533
}
@@ -62,3 +60,13 @@ void boxed_region::free(rust_opaque_box *box) {
6260

6361
backing_region->free(box);
6462
}
63+
64+
//
65+
// Local Variables:
66+
// mode: C++
67+
// fill-column: 78;
68+
// indent-tabs-mode: nil
69+
// c-basic-offset: 4
70+
// buffer-file-coding-system: utf-8-unix
71+
// End:
72+
//

‎src/rt/boxed_region.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ class boxed_region {
3434

3535
rust_opaque_box *first_live_alloc() { return live_allocs; }
3636

37-
rust_opaque_box *malloc(type_desc *td);
38-
rust_opaque_box *calloc(type_desc *td);
37+
rust_opaque_box *malloc(type_desc *td, size_t body_size);
38+
rust_opaque_box *calloc(type_desc *td, size_t body_size);
3939
void free(rust_opaque_box *box);
4040
};
4141

4242
#endif /* BOXED_REGION_H */
43+
44+
//
45+
// Local Variables:
46+
// mode: C++
47+
// fill-column: 78;
48+
// indent-tabs-mode: nil
49+
// c-basic-offset: 4
50+
// buffer-file-coding-system: utf-8-unix
51+
// End:
52+
//

‎src/rt/rust_kernel.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ rust_kernel::malloc(size_t size, const char *tag) {
6262
return _region.malloc(size, tag);
6363
}
6464

65+
void *
66+
rust_kernel::calloc(size_t size, const char *tag) {
67+
return _region.calloc(size, tag);
68+
}
69+
6570
void *
6671
rust_kernel::realloc(void *mem, size_t size) {
6772
return _region.realloc(mem, size);

‎src/rt/rust_kernel.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class rust_kernel {
113113
void fatal(char const *fmt, ...);
114114

115115
void *malloc(size_t size, const char *tag);
116+
void *calloc(size_t size, const char *tag);
116117
void *realloc(void *mem, size_t size);
117118
void free(void *mem);
118119
memory_region *region() { return &_region; }
@@ -165,3 +166,13 @@ template <typename T> struct kernel_owned {
165166
};
166167

167168
#endif /* RUST_KERNEL_H */
169+
170+
//
171+
// Local Variables:
172+
// mode: C++
173+
// fill-column: 78;
174+
// indent-tabs-mode: nil
175+
// c-basic-offset: 4
176+
// buffer-file-coding-system: utf-8-unix
177+
// End:
178+
//

‎src/rt/rust_upcall.cpp

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,15 @@ exchange_malloc(rust_task *task, type_desc *td, uintptr_t size) {
144144

145145
LOG(task, mem, "upcall exchange malloc(0x%" PRIxPTR ")", td);
146146

147-
// Copied from boxed_region
148-
size_t header_size = sizeof(rust_opaque_box);
149-
size_t body_size = size;
150-
size_t body_align = td->align;
151-
// FIXME: This alignment calculation is suspicious. Is it right?
152-
size_t total_size = align_to(header_size, body_align) + body_size;
153-
154-
void *p = task->kernel->malloc(total_size, "exchange malloc");
147+
size_t total_size = get_box_size(size, td->align);
148+
void *p = task->kernel->calloc(total_size, "exchange malloc");
155149

156150
rust_opaque_box *header = static_cast<rust_opaque_box*>(p);
157151
header->ref_count = -1; // This is not ref counted
158152
header->td = td;
159153
header->prev = 0;
160154
header->next = 0;
161155

162-
memset(&header[1], '\0', body_size);
163-
164156
return (uintptr_t)header;
165157
}
166158

@@ -174,8 +166,7 @@ upcall_s_exchange_malloc(s_exchange_malloc_args *args) {
174166
rust_task *task = rust_get_current_task();
175167
LOG_UPCALL_ENTRY(task);
176168

177-
uintptr_t retval = exchange_malloc(task, args->td, args->td->size);
178-
args->retval = retval;
169+
args->retval = exchange_malloc(task, args->td, args->td->size);
179170
}
180171

181172
extern "C" CDECL uintptr_t
@@ -196,8 +187,7 @@ upcall_s_exchange_malloc_dyn(s_exchange_malloc_dyn_args *args) {
196187
rust_task *task = rust_get_current_task();
197188
LOG_UPCALL_ENTRY(task);
198189

199-
uintptr_t retval = exchange_malloc(task, args->td, args->size);
200-
args->retval = retval;
190+
args->retval = exchange_malloc(task, args->td, args->size);
201191
}
202192

203193
extern "C" CDECL uintptr_t
@@ -228,31 +218,37 @@ upcall_exchange_free(void *ptr) {
228218
* Allocate an object in the task-local heap.
229219
*/
230220

231-
struct s_malloc_args {
232-
uintptr_t retval;
233-
type_desc *td;
234-
};
235-
236-
extern "C" CDECL void
237-
upcall_s_malloc(s_malloc_args *args) {
238-
rust_task *task = rust_get_current_task();
239-
LOG_UPCALL_ENTRY(task);
240-
241-
LOG(task, mem, "upcall malloc(0x%" PRIxPTR ")", args->td);
221+
extern "C" CDECL uintptr_t
222+
shared_malloc(rust_task *task, type_desc *td, uintptr_t size) {
223+
LOG(task, mem, "upcall malloc(0x%" PRIxPTR ")", td);
242224

243225
cc::maybe_cc(task);
244226

245227
// FIXME--does this have to be calloc?
246-
rust_opaque_box *box = task->boxed.calloc(args->td);
228+
rust_opaque_box *box = task->boxed.calloc(td, size);
247229
void *body = box_body(box);
248230

249231
debug::maybe_track_origin(task, box);
250232

251233
LOG(task, mem,
252234
"upcall malloc(0x%" PRIxPTR ") = box 0x%" PRIxPTR
253235
" with body 0x%" PRIxPTR,
254-
args->td, (uintptr_t)box, (uintptr_t)body);
255-
args->retval = (uintptr_t) box;
236+
td, (uintptr_t)box, (uintptr_t)body);
237+
238+
return (uintptr_t)box;
239+
}
240+
241+
struct s_malloc_args {
242+
uintptr_t retval;
243+
type_desc *td;
244+
};
245+
246+
extern "C" CDECL void
247+
upcall_s_malloc(s_malloc_args *args) {
248+
rust_task *task = rust_get_current_task();
249+
LOG_UPCALL_ENTRY(task);
250+
251+
args->retval = shared_malloc(task, args->td, args->td->size);
256252
}
257253

258254
extern "C" CDECL uintptr_t
@@ -262,6 +258,28 @@ upcall_malloc(type_desc *td) {
262258
return args.retval;
263259
}
264260

261+
struct s_malloc_dyn_args {
262+
uintptr_t retval;
263+
type_desc *td;
264+
uintptr_t size;
265+
};
266+
267+
extern "C" CDECL void
268+
upcall_s_malloc_dyn(s_malloc_dyn_args *args) {
269+
rust_task *task = rust_get_current_task();
270+
LOG_UPCALL_ENTRY(task);
271+
272+
args->retval = shared_malloc(task, args->td, args->size);
273+
}
274+
275+
extern "C" CDECL uintptr_t
276+
upcall_malloc_dyn(type_desc *td, uintptr_t size) {
277+
s_malloc_dyn_args args = {0, td, size};
278+
UPCALL_SWITCH_STACK(&args, upcall_s_malloc_dyn);
279+
return args.retval;
280+
}
281+
282+
265283
/**********************************************************************
266284
* Called whenever an object in the task-local heap is freed.
267285
*/

‎src/rt/rust_util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ make_str_vec(rust_kernel* kernel, size_t nstrs, char **strs) {
104104
return v;
105105
}
106106

107+
inline size_t get_box_size(size_t body_size, size_t body_align) {
108+
size_t header_size = sizeof(rust_opaque_box);
109+
// FIXME: This alignment calculation is suspicious. Is it right?
110+
size_t total_size = align_to(header_size, body_align) + body_size;
111+
return total_size;
112+
}
113+
107114
// Initialization helpers for ISAAC RNG
108115

109116
inline void isaac_seed(rust_kernel* kernel, uint8_t* dest)

‎src/rustc/back/upcall.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type upcalls =
1111
{_fail: ValueRef,
1212
trace: ValueRef,
1313
malloc: ValueRef,
14+
malloc_dyn: ValueRef,
1415
free: ValueRef,
1516
exchange_malloc: ValueRef,
1617
exchange_malloc_dyn: ValueRef,
@@ -58,6 +59,9 @@ fn declare_upcalls(targ_cfg: @session::config,
5859
int_t]),
5960
malloc:
6061
nothrow(d("malloc", [T_ptr(tydesc_type)],
62+
malloc_dyn:
63+
nothrow(d("malloc_dyn",
64+
[T_ptr(tydesc_type), int_t],
6165
T_ptr(T_i8()))),
6266
free:
6367
nothrow(dv("free", [T_ptr(T_i8())])),

0 commit comments

Comments
 (0)
Please sign in to comment.