MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / array_reserve

Function array_reserve

src/simulate/runtime_array.cpp:60–88  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

58 }
59
60 void array_reserve(Context & context, Array & arr, uint64_t newCapacity, uint32_t stride, LineInfo * at) {
61 if ( arr.isLocked() ) context.throw_error_at(at, "can't change capacity of a locked array");
62 if ( arr.capacity >= newCapacity ) return;
63 // Explicit mul_overflow guard: stride is uint32, newCapacity is uint64.
64 // The product can overflow uint64 when newCapacity > UINT64_MAX / stride
65 // (with stride > 0). Panic before passing a wrap-around byte count to
66 // the heap allocator (where it would silently allocate a small block
67 // and the next memcpy would overrun random memory).
68 if ( stride && newCapacity > UINT64_MAX / uint64_t(stride) ) {
69 context.throw_error_at(at, "array_reserve: capacity*stride overflows uint64 [capacity=%llu] [stride=%u]", (unsigned long long)newCapacity, stride);
70 }
71 uint64_t memSize64 = newCapacity * uint64_t(stride);
72 const char * prev_comment = arr.data ? context.heap->get_comment(arr.data) : nullptr;
73 char * newData = nullptr;
74 if ( context.verySafeContext ) {
75 newData = (char *)context.allocate(memSize64, at);
76 if ( newData && arr.data ) {
77 memcpy(newData, arr.data, arr.size*stride);
78 }
79 } else {
80 newData = (char *)context.reallocate(arr.data, arr.capacity*stride, memSize64, at);
81 }
82 context.heap->mark_comment(newData, prev_comment ? prev_comment : "array");
83 if ( newData != arr.data ) {
84 // memcpy(newData, arr.data, arr.capacity);
85 arr.data = newData;
86 }
87 arr.capacity = newCapacity;
88 }
89
90 void array_resize ( Context & context, Array & arr, uint64_t newSize, uint32_t stride, bool zero, LineInfo * at ) {
91 if ( arr.isLocked() ) context.throw_error_at(at, "can't resize locked array");

Callers 6

array_resizeFunction · 0.85
das_array_reserveFunction · 0.85
das_array_reserve_i64Function · 0.85
builtin_array_reserveFunction · 0.85
array_growFunction · 0.85

Calls 4

isLockedMethod · 0.80
throw_error_atMethod · 0.80
allocateMethod · 0.45
reallocateMethod · 0.45

Tested by

no test coverage detected