mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 11:11:43 +00:00
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:
@ -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();
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user