Implement a mechanism to realloc array buffers; Trap when trying to allocate more than max size; Test allocators in CI

This commit is contained in:
dcodeIO
2018-04-08 00:43:38 +02:00
parent dcc0e284fb
commit 9731958738
41 changed files with 2911 additions and 2024 deletions

View File

@ -7,15 +7,15 @@
* @module std/assembly/allocator/arena
*//***/
import { AL_MASK } from "../internal/allocator";
import { AL_MASK, MAX_SIZE_32 } from "../internal/allocator";
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
var offset: usize = startOffset;
@global
export function allocate_memory(size: usize): usize {
const MAX_SIZE: usize = 1 << 30;
if (size && size < MAX_SIZE) {
if (size) {
if (size > MAX_SIZE_32) unreachable();
let ptr = offset;
let newPtr = (ptr + size + AL_MASK) & ~AL_MASK;
let pagesBefore = current_memory();

View File

@ -347,9 +347,7 @@ export function allocate_memory(request: usize): usize {
* a hard-coded limit on the maximum allocation size because of the way this
* allocator works.
*/
if (request > MAX_ALLOC - HEADER_SIZE) {
return 0;
}
if (request > MAX_ALLOC - HEADER_SIZE) unreachable();
/*
* Initialize our global state if this is the first call to "malloc". At the

View File

@ -457,7 +457,8 @@ export function allocate_memory(size: usize): usize {
// search for a suitable block
var data: usize = 0;
if (size && size <= Block.MAX_SIZE) {
if (size) {
if (size > Block.MAX_SIZE) unreachable();
// 32-bit MAX_SIZE is 1 << 30 and itself aligned, hence the following can't overflow MAX_SIZE
size = max<usize>((size + AL_MASK) & ~AL_MASK, Block.MIN_SIZE);