Skip to content

Commit 14567c5

Browse files
committed
Factor vector reserve code in runtime into its own function
1 parent eb4661f commit 14567c5

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

src/rt/rust_builtin.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,7 @@ str_from_vec(rust_task *task, rust_vec **vp)
225225
extern "C" CDECL void
226226
vec_reserve_shared(rust_task* task, type_desc* ty, rust_vec** vp,
227227
size_t n_elts) {
228-
size_t new_sz = n_elts * ty->size;
229-
if (new_sz > (*vp)->alloc) {
230-
size_t new_alloc = next_power_of_two(new_sz);
231-
*vp = (rust_vec*)task->kernel->realloc(*vp, new_alloc +
232-
sizeof(rust_vec));
233-
(*vp)->alloc = new_alloc;
234-
}
228+
reserve_vec(task, vp, n_elts * ty->size);
235229
}
236230

237231
/**

src/rt/rust_upcall.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,28 +347,17 @@ upcall_get_type_desc(rust_task *task,
347347
extern "C" CDECL void
348348
upcall_vec_grow(rust_task* task, rust_vec** vp, size_t new_sz) {
349349
LOG_UPCALL_ENTRY(task);
350-
// FIXME factor this into a utility function
351-
if (new_sz > (*vp)->alloc) {
352-
size_t new_alloc = next_power_of_two(new_sz);
353-
*vp = (rust_vec*)task->kernel->realloc(*vp, new_alloc +
354-
sizeof(rust_vec));
355-
(*vp)->alloc = new_alloc;
356-
}
350+
reserve_vec(task, vp, new_sz);
357351
(*vp)->fill = new_sz;
358352
}
359353

360354
extern "C" CDECL void
361355
upcall_vec_push(rust_task* task, rust_vec** vp, type_desc* elt_ty,
362356
void* elt) {
363357
LOG_UPCALL_ENTRY(task);
358+
size_t new_sz = (*vp)->fill + elt_ty->size;
359+
reserve_vec(task, vp, new_sz);
364360
rust_vec* v = *vp;
365-
size_t new_sz = v->fill + elt_ty->size;
366-
if (new_sz > v->alloc) {
367-
size_t new_alloc = next_power_of_two(new_sz);
368-
*vp = v = (rust_vec*)task->kernel->realloc(v, new_alloc +
369-
sizeof(rust_vec));
370-
v->alloc = new_alloc;
371-
}
372361
copy_elements(task, elt_ty, &v->data[0] + v->fill, elt, elt_ty->size);
373362
v->fill += elt_ty->size;
374363
}

src/rt/rust_util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ inline size_t vec_size(size_t elems) {
209209
return sizeof(rust_vec) + sizeof(T) * elems;
210210
}
211211

212+
inline void reserve_vec(rust_task* task, rust_vec** vpp, size_t size) {
213+
if (size > (*vpp)->alloc) {
214+
size_t new_alloc = next_power_of_two(size);
215+
*vpp = (rust_vec*)task->kernel->realloc(*vpp, new_alloc +
216+
sizeof(rust_vec));
217+
(*vpp)->alloc = new_alloc;
218+
}
219+
}
220+
212221
//
213222
// Local Variables:
214223
// mode: C++

0 commit comments

Comments
 (0)