Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/browser/test_4GB_fail.cpp
4150 views
1
#include <assert.h>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
6
void* allocation;
7
8
int main() {
9
const int MB = 1024 * 1024;
10
const int CHUNK = 512 * MB;
11
long num_allocations;
12
for (num_allocations = 0;; num_allocations++) {
13
allocation = malloc(CHUNK);
14
if (!allocation) {
15
puts("failed to allocate any more");
16
break;
17
}
18
printf("%ld: Allocated %d MB, total so far: %ld MB\n",
19
num_allocations, CHUNK / MB,
20
(num_allocations * CHUNK) / MB);
21
printf(" (writing to make sure, to %p)\n", allocation);
22
memset(allocation, 42, CHUNK);
23
}
24
// We should have allocated at least 2GB.
25
assert(num_allocations >= 4);
26
// We should have allocated less than 4GB (we can't get to exactly 4GB
27
// since we started with some small amount, and then add 512MB chunks).
28
assert(num_allocations < 8);
29
puts("success");
30
}
31
32