From f9da8b2676c192e7a1ad68c719b84edf1b52ccb7 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 16 Jun 2011 10:32:52 -0700 Subject: rt: Add an ivec length intrinsic and an ivec reserve function, both untested as of yet --- src/rt/rust_builtin.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/rt/rust_builtin.cpp') diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index 3500743a79e..2437a5c364d 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -576,6 +576,37 @@ get_time(rust_task *task, uint32_t *sec, uint32_t *usec) { } #endif +/** + * Preallocates the exact number of bytes in the given interior vector. + */ +extern "C" void +ivec_reserve(rust_task *task, type_desc *ty, rust_ivec *v, size_t n_elems) +{ + size_t new_alloc = n_elems * ty->size; + if (new_alloc <= v->alloc) + return; // Already big enough. + + rust_ivec_heap *heap_part; + if (v->fill) { + // On stack; spill to heap. + heap_part = (rust_ivec_heap *)task->malloc(new_alloc + + sizeof(size_t)); + heap_part->fill = v->fill; + memcpy(&heap_part->data, v->payload.data, v->fill); + + v->fill = 0; + v->payload.ptr = heap_part; + } else { + // On heap; resize. + heap_part = (rust_ivec_heap *)task->realloc(v->payload.ptr, + new_alloc); + v->payload.ptr = heap_part; + } + + v->alloc = new_alloc; +} + + // // Local Variables: // mode: C++ -- cgit 1.4.1-3-g733a5