File: test_mem_growth.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (34 lines) | stat: -rw-r--r-- 898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <assert.h>
#include <stdio.h>

#include <emscripten/emmalloc.h>
#include <emscripten/html5.h>
#include <emscripten/heap.h>

#ifdef __wasm64__
#define MAX_HEAP (4*1024*1024*1024ll)
#else
// We don't allow the full 4Gb on wasm32 since that size would wrap
// back to zero.  See getHeapMax in library.js.
#define MAX_HEAP (4*1024*1024*1024ll - 65536)
#endif

int main() {
  size_t prevheapsize = 0;
  int count = 0;
  while (1) {
    size_t heap_size = emscripten_get_heap_size();
    if (prevheapsize != heap_size) {
      printf("Heap size: %zu inc: %zu count: %d\n", heap_size, heap_size - prevheapsize, count);
    }

    prevheapsize = heap_size;
    void *ptr = malloc(16*1024*1024);
    if (!ptr) {
      printf("Cannot malloc anymore. Final heap size: %zu\n", emscripten_get_heap_size());
      assert(emscripten_get_heap_size() == MAX_HEAP);
      return 0;
    }
    count++;
  }
}