Skip to content

Commit 8769409

Browse files
olsonjefferybrson
authored andcommitted
rt: adding rust_uv_* binding for kernel malloc and free'ing :/
I need these in the context of doing various malloc/free operations for libuv structs that need to live in the heap, because of API workflow (there's no stack to put them in). This has cropped up several times when impl'ing the high-level API for things like timers, but I've decided to take the plunge and use this approach for the net::tcp module. Technically, this can be avoided by spawning a new task that contains the needed memory structures on its stack and then having it block for the duration of the time we need that memory to be valid (this is what I did in std::timer). Exposing this API provides a much lower overhead way to address the issue, albeit with safety concerns. The main mitigation policy should be to use malloc/free with libuv handles only when the handles, are then associated with a resource or class-with-dtor. So we have a finite lifetime for the object and can gaurantee a free(), barring a runtime crash (in which case you have bigger problems!)
1 parent 733881d commit 8769409

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/rt/rust_uv.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,13 @@ rust_uv_get_kernel_global_chan_ptr() {
450450
LOG(task, stdlib,"global loop val: %lu", (unsigned long int)*result);
451451
return result;
452452
}
453+
454+
extern "C" void*
455+
rust_uv_current_kernel_malloc(size_t size) {
456+
return current_kernel_malloc(size, "rust_uv_current_kernel_malloc");
457+
}
458+
459+
extern "C" void*
460+
rust_uv_current_kernel_free(void* mem) {
461+
return current_kernel_free(mem);
462+
}

src/rt/rustrt.def.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ rust_uv_set_data_for_req
143143
rust_uv_get_base_from_buf
144144
rust_uv_get_len_from_buf
145145
rust_uv_get_kernel_global_chan_ptr
146+
rust_uv_current_kernel_malloc
147+
rust_uv_current_kernel_free
146148
rust_dbg_lock_create
147149
rust_dbg_lock_destroy
148150
rust_dbg_lock_lock

0 commit comments

Comments
 (0)