function SAFE_HEAP_ACCESS(dest, type, store, ignore, storeValue) {
39
39
//if (dest === A_NUMBER) Module.print ([dest, type, store, ignore, storeValue] + ' ' + new Error().stack); // Something like this may be useful, in debugging
40
40
41
assert(dest >= STACK_ROOT, 'segmentation fault: null pointer, or below normal memory');
41
assert(dest > 0, 'segmentation fault');
42
42
43
43
#if USE_TYPED_ARRAYS
44
44
// When using typed arrays, reads over the top of TOTAL_MEMORY will fail silently, so we must
45
45
// correct that by growing TOTAL_MEMORY as needed. Without typed arrays, memory is a normal
46
46
// JS array so it will work (potentially slowly, depending on the engine).
47
assert(ignore || dest < STATICTOP);
48
assert(ignore || STATICTOP <= TOTAL_MEMORY);
47
assert(ignore || dest < Math.max(DYNAMICTOP, STATICTOP));
48
assert(ignore || DYNAMICTOP <= TOTAL_MEMORY);
49
49
#endif
50
50
51
51
#if USE_TYPED_ARRAYS == 2
231
231
var setjmpLabels = {};
232
232
#endif
233
233
234
var ABORT = false;
234
var ABORT = false; // whether we are quitting the application. no code should run after this. set in exit() and abort()
235
235
236
236
var undef = 0;
237
237
// tempInt is used for 32-bit signed values or smaller. tempBigInt is used
417
417
var ALLOC_NORMAL = 0; // Tries to use _malloc()
418
418
var ALLOC_STACK = 1; // Lives for the duration of the current function call
419
419
var ALLOC_STATIC = 2; // Cannot be freed
420
var ALLOC_NONE = 3; // Do not allocate
420
var ALLOC_DYNAMIC = 3; // Cannot be freed except through sbrk
421
var ALLOC_NONE = 4; // Do not allocate
421
422
Module['ALLOC_NORMAL'] = ALLOC_NORMAL;
422
423
Module['ALLOC_STACK'] = ALLOC_STACK;
423
424
Module['ALLOC_STATIC'] = ALLOC_STATIC;
425
Module['ALLOC_DYNAMIC'] = ALLOC_DYNAMIC;
424
426
Module['ALLOC_NONE'] = ALLOC_NONE;
425
427
426
428
// allocate(): This is for internal use. You can use it yourself as well, but the interface
var HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64;
574
580
#endif
575
581
576
var STACK_ROOT, STACKTOP, STACK_MAX;
577
var STATICTOP;
582
var STATIC_BASE = 0, STATICTOP = 0, staticSealed = false; // static area
583
var STACK_BASE = 0, STACKTOP = 0, STACK_MAX = 0; // stack area
584
var DYNAMIC_BASE = 0, DYNAMICTOP = 0; // dynamic area handled by sbrk
585
578
586
#if USE_TYPED_ARRAYS
579
587
function enlargeMemory() {
580
588
#if ALLOW_MEMORY_GROWTH == 0
584
592
abort('Cannot enlarge memory arrays in asm.js. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value, or (2) set Module.TOTAL_MEMORY before the program runs.');
585
593
#endif
586
594
#else
587
// TOTAL_MEMORY is the current size of the actual array, and STATICTOP is the new top.
595
// TOTAL_MEMORY is the current size of the actual array, and DYNAMICTOP is the new top.
588
596
#if ASSERTIONS
589
Module.printErr('Warning: Enlarging memory arrays, this is not fast, and ALLOW_MEMORY_GROWTH is not fully tested with all optimizations on! ' + [STATICTOP, TOTAL_MEMORY]); // We perform safe elimination instead of elimination in this mode, but if you see this error, try to disable it and other optimizations entirely
590
assert(STATICTOP >= TOTAL_MEMORY);
597
Module.printErr('Warning: Enlarging memory arrays, this is not fast, and ALLOW_MEMORY_GROWTH is not fully tested with all optimizations on! ' + [DYNAMICTOP, TOTAL_MEMORY]); // We perform safe elimination instead of elimination in this mode, but if you see this error, try to disable it and other optimizations entirely
598
assert(DYNAMICTOP >= TOTAL_MEMORY);
591
599
assert(TOTAL_MEMORY > 4); // So the loop below will not be infinite
592
600
#endif
593
while (TOTAL_MEMORY <= STATICTOP) { // Simple heuristic. Override enlargeMemory() if your program has something more optimal for it
601
while (TOTAL_MEMORY <= DYNAMICTOP) { // Simple heuristic. Override enlargeMemory() if your program has something more optimal for it
594
602
TOTAL_MEMORY = alignMemoryPage(2*TOTAL_MEMORY);
595
603
}
596
604
assert(TOTAL_MEMORY <= Math.pow(2, 30)); // 2^30==1GB is a practical maximum - 2^31 is already close to possible negative numbers etc.
680
688
Module['HEAPF64'] = HEAPF64;
681
689
#endif
682
690
683
STACK_ROOT = STACKTOP = Runtime.alignMemory(1);
684
STACK_MAX = TOTAL_STACK; // we lose a little stack here, but TOTAL_STACK is nice and round so use that as the max
685
686
#if USE_TYPED_ARRAYS == 2
687
var tempDoublePtr = Runtime.alignMemory(allocate(12, 'i8', ALLOC_STACK), 8);
688
assert(tempDoublePtr % 8 == 0);
689
function copyTempFloat(ptr) { // functions, because inlining this code increases code size too much
690
HEAP8[tempDoublePtr] = HEAP8[ptr];
691
HEAP8[tempDoublePtr+1] = HEAP8[ptr+1];
692
HEAP8[tempDoublePtr+2] = HEAP8[ptr+2];
693
HEAP8[tempDoublePtr+3] = HEAP8[ptr+3];
694
}
695
function copyTempDouble(ptr) {
696
HEAP8[tempDoublePtr] = HEAP8[ptr];
697
HEAP8[tempDoublePtr+1] = HEAP8[ptr+1];
698
HEAP8[tempDoublePtr+2] = HEAP8[ptr+2];
699
HEAP8[tempDoublePtr+3] = HEAP8[ptr+3];
700
HEAP8[tempDoublePtr+4] = HEAP8[ptr+4];
701
HEAP8[tempDoublePtr+5] = HEAP8[ptr+5];
702
HEAP8[tempDoublePtr+6] = HEAP8[ptr+6];
703
HEAP8[tempDoublePtr+7] = HEAP8[ptr+7];
704
}
705
#endif
706
707
STATICTOP = STACK_MAX;
708
assert(STATICTOP < TOTAL_MEMORY); // Stack must fit in TOTAL_MEMORY; allocations from here on may enlarge TOTAL_MEMORY
709
710
var nullString = allocate(intArrayFromString('(null)'), 'i8', ALLOC_STACK);
711
712
691
function callRuntimeCallbacks(callbacks) {
713
692
while(callbacks.length > 0) {
714
693
var callback = callbacks.shift();
802
781
{{{ reSign }}}
803
782
804
783
#if PRECISE_I32_MUL
805
if (!Math.imul) Math.imul = function(a, b) {
784
if (!Math['imul']) Math['imul'] = function(a, b) {
806
785
var ah = a >>> 16;
807
786
var al = a & 0xffff;
808
787
var bh = b >>> 16;
810
789
return (al*bl + ((ah*bl + al*bh) << 16))|0;
811
790
};
812
791
#else
813
Math.imul = function(a, b) {
792
Math['imul'] = function(a, b) {
814
793
return (a*b)|0; // fast but imprecise
815
794
};
816
795
#endif
834
813
if (id) {
835
814
assert(!runDependencyTracking[id]);
836
815
runDependencyTracking[id] = 1;
816
#if ASSERTIONS
837
817
if (runDependencyWatcher === null && typeof setInterval !== 'undefined') {
838
818
// Check for missing dependencies every few seconds
839
819
runDependencyWatcher = setInterval(function() {
848
828
if (shown) {
849
829
Module.printErr('(end of list)');
850
830
}
851
}, 6000);
831
}, 10000);
852
832
}
833
#endif
853
834
} else {
854
835
Module.printErr('warning: run dependency added without ID');