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