File: test_4GB.cpp

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 (38 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (4)
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
35
36
37
38
#include <assert.h>
#include <emscripten.h>
#include <stdio.h>
#include <vector>

int main() {
  const int CHUNK_SIZE = 100 * 1024 * 1024;
  const int NUM_CHUNKS = 31; // total allocation will be over 3GB

  std::vector<std::vector<char>> chunks;
  chunks.resize(NUM_CHUNKS);

  puts("allocating");

  for (int i = 0; i < NUM_CHUNKS; i++) {
    printf("alloc %d\n", i);
    chunks[i].resize(CHUNK_SIZE);
  }

  puts("testing");

  for (int i = 0; i < NUM_CHUNKS; i++) {
    printf("test %d\n", i);
    chunks[i][i] = i;
    int fromJS = EM_ASM_INT({
      return HEAP8[$0];
    }, &chunks[i][i]);
    printf("wrote %d in C, read %d from JS\n", i, fromJS);
    EM_ASM_INT({
      HEAP8[$0] = 2 * $1;
    }, &chunks[i][i], i);
    int fromC = chunks[i][i];
    printf("wrote %d in JS, read %d from C\n", 2 * i, fromC);
  }

  puts("success");
}