File: test_memorygrowth_linear_step.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 (57 lines) | stat: -rw-r--r-- 1,597 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * Copyright 2018 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "emscripten/heap.h"

int get_memory_size() {
  return emscripten_get_heap_size();
}

int main() {

  // INITIAL_MEMORY=64Mb
  // STACK_SIZE=1Mb
  // MAXIMUM_MEMORY=130Mb
  // MEMORY_GROWTH_LINEAR_STEP=1Mb

  // Because the stack is 1Mb, the first increase will take place
  // in i = 63, which attempts to grow the heap to 64Mb + 1Mb of stack > 64Mb INITIAL_MEMORY
  // We should get exactly to 130MB at i == 128

  const int MB = 1024 * 1024;
  const int totalMemory = get_memory_size();
  int printedOnce = 0;

  for (int i = 0; 1; i++) {

    printf("%d %d %d\n", i, get_memory_size(), get_memory_size() / MB);
    volatile long sink = (long)malloc(MB);

    if (!sink) {
      printf("failed at %d %d %d\n", i, get_memory_size(), get_memory_size() / MB);
      // i ==  63 > growth to 65MB failed
      // i == 128 > growth to 130MB failed
      // i == 129 > growth to 131MB failed
      assert(i >= 128);
      break;
    }

    if (get_memory_size() > totalMemory && !printedOnce) {
      printf("memory growth started at %d %d %d\n", i, get_memory_size(), get_memory_size() / MB);
      printedOnce = 1;
    }

    assert(i < 130); // the wasm mem max limit, we must not get there
  }

  printf("grew memory ok.\n");
}