From 14567c5eb05b6dd7ba9706a605c8d3ee8c5d8742 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Mon, 29 Aug 2011 22:35:29 +0200 Subject: [PATCH] Factor vector reserve code in runtime into its own function --- src/rt/rust_builtin.cpp | 8 +------- src/rt/rust_upcall.cpp | 17 +++-------------- src/rt/rust_util.h | 9 +++++++++ 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp index a8929677aff..049fe71384e 100644 --- a/src/rt/rust_builtin.cpp +++ b/src/rt/rust_builtin.cpp @@ -225,13 +225,7 @@ str_from_vec(rust_task *task, rust_vec **vp) extern "C" CDECL void vec_reserve_shared(rust_task* task, type_desc* ty, rust_vec** vp, size_t n_elts) { - size_t new_sz = n_elts * ty->size; - if (new_sz > (*vp)->alloc) { - size_t new_alloc = next_power_of_two(new_sz); - *vp = (rust_vec*)task->kernel->realloc(*vp, new_alloc + - sizeof(rust_vec)); - (*vp)->alloc = new_alloc; - } + reserve_vec(task, vp, n_elts * ty->size); } /** diff --git a/src/rt/rust_upcall.cpp b/src/rt/rust_upcall.cpp index c94e993e7e6..9f453d49046 100644 --- a/src/rt/rust_upcall.cpp +++ b/src/rt/rust_upcall.cpp @@ -347,13 +347,7 @@ upcall_get_type_desc(rust_task *task, extern "C" CDECL void upcall_vec_grow(rust_task* task, rust_vec** vp, size_t new_sz) { LOG_UPCALL_ENTRY(task); - // FIXME factor this into a utility function - if (new_sz > (*vp)->alloc) { - size_t new_alloc = next_power_of_two(new_sz); - *vp = (rust_vec*)task->kernel->realloc(*vp, new_alloc + - sizeof(rust_vec)); - (*vp)->alloc = new_alloc; - } + reserve_vec(task, vp, new_sz); (*vp)->fill = new_sz; } @@ -361,14 +355,9 @@ extern "C" CDECL void upcall_vec_push(rust_task* task, rust_vec** vp, type_desc* elt_ty, void* elt) { LOG_UPCALL_ENTRY(task); + size_t new_sz = (*vp)->fill + elt_ty->size; + reserve_vec(task, vp, new_sz); rust_vec* v = *vp; - size_t new_sz = v->fill + elt_ty->size; - if (new_sz > v->alloc) { - size_t new_alloc = next_power_of_two(new_sz); - *vp = v = (rust_vec*)task->kernel->realloc(v, new_alloc + - sizeof(rust_vec)); - v->alloc = new_alloc; - } copy_elements(task, elt_ty, &v->data[0] + v->fill, elt, elt_ty->size); v->fill += elt_ty->size; } diff --git a/src/rt/rust_util.h b/src/rt/rust_util.h index 10cdbce6109..7118572393f 100644 --- a/src/rt/rust_util.h +++ b/src/rt/rust_util.h @@ -209,6 +209,15 @@ inline size_t vec_size(size_t elems) { return sizeof(rust_vec) + sizeof(T) * elems; } +inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) { + if (size > (*vpp)->alloc) { + size_t new_alloc = next_power_of_two(size); + *vpp = (rust_vec*)task->kernel->realloc(*vpp, new_alloc + + sizeof(rust_vec)); + (*vpp)->alloc = new_alloc; + } +} + // // Local Variables: // mode: C++