MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / grow_memory

Function grow_memory

test/browser/emmalloc_memgrowth.cpp:8–24  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6bool allocHasFailed = false;
7
8void grow_memory() {
9 uint8_t *ptr = (uint8_t*)malloc((size_t)nextAllocationSize);
10 EM_ASM({}, ptr); // Pass ptr out to confuse LLVM that it is used, so it won't optimize it away in -O1 and higher.
11 size_t heapSize = emscripten_get_heap_size();
12 printf("Allocated %zu: %d. Heap size: %zu\n", (size_t)nextAllocationSize, ptr ? 1 : 0, heapSize);
13 if (ptr) {
14 if (!allocHasFailed) {
15 nextAllocationSize *= 2;
16 // Make sure we don't overflow, and also exercise malloc(-1) to gracefully return 0 in ABORTING_MALLOC=0 mode.
17 if (nextAllocationSize > 0xFFFFFFFFULL)
18 nextAllocationSize = 0xFFFFFFFFULL;
19 }
20 } else {
21 nextAllocationSize /= 2;
22 allocHasFailed = true;
23 }
24}
25
26int main() {
27 // Exhaust all available memory.

Callers 1

mainFunction · 0.85

Calls 4

emscripten_get_heap_sizeFunction · 0.85
printfFunction · 0.85
EM_ASMFunction · 0.70
mallocFunction · 0.50

Tested by

no test coverage detected