File: stackAlloc.cpp

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (35 lines) | stat: -rw-r--r-- 1,417 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
// 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 <emscripten.h>

int main() {
  EM_ASM({
    var size = 128;
    var before;
    before = stackSave();
    var x = stackAlloc(size);
    var y = stackAlloc(size);
    var direction = y > x ? 1 : -1;
    assert(x % 16 == 0, "allocation must have 16-byte alignment");
    assert(x == Math.min(before, before + direction*size), "allocation must return the start of the range allocated");
    var z = stackAlloc(size);
    assert(x != y && y != z && x != z, "allocations must be unique");
    assert((y - x)*(z - y) > 0, "allocations must be in the same direction");
    // no overlaps
    function notInRange(value, begin, end) {
      function errormsg() { return value + " must not be in the range (" + begin + ", " + end + "]"; }
      if (begin < end) assert(!(value >= begin && value < end), errormsg());
      else assert(!(value <= begin && value > end), errormsg());
    }
    notInRange(x, y, y + direction*size);
    notInRange(x, z, z + direction*size);
    notInRange(y, x, x + direction*size);
    notInRange(y, z, z + direction*size);
    notInRange(z, x, x + direction*size);
    notInRange(z, y, y + direction*size);
    out('ok.');
  });
}