From 55d07b6cb0460fae63c0cfb6730dba462ba2c560 Mon Sep 17 00:00:00 2001 From: Nidin Vinayakan <01@01alchemist.com> Date: Thu, 28 Feb 2019 15:21:57 +0100 Subject: [PATCH 1/2] Atomic memory allocator Atomic allocator for shared memory --- std/assembly/allocator/atomic.ts | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 std/assembly/allocator/atomic.ts diff --git a/std/assembly/allocator/atomic.ts b/std/assembly/allocator/atomic.ts new file mode 100644 index 0000000000..a218aaf809 --- /dev/null +++ b/std/assembly/allocator/atomic.ts @@ -0,0 +1,49 @@ +import { AL_MASK, MAX_SIZE_32 } from "../internal/allocator"; + +var SATRT_OFFSET: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK; +var OFFSET_PTR: usize = SATRT_OFFSET; +var TOP = (HEAP_BASE + 8 + AL_MASK) & ~AL_MASK; +store(OFFSET_PTR, TOP); + +@global export function __allocator_get_offset(): usize { + return Atomic.load(OFFSET_PTR); +} + +@global export function __allocator_set_offset(oldOffset: usize, newOffset: usize): usize { + return Atomic.cmpxchg(OFFSET_PTR, oldOffset, newOffset); +} + +@global export function __memory_allocate(size: usize): usize { + if (size) { + if (size > MAX_SIZE_32) unreachable(); + let currentOffset: usize; + let top: usize; + do { + currentOffset = __allocator_get_offset(); + top = (currentOffset + size + AL_MASK) & ~AL_MASK; + let pagesBefore = memory.size(); + if (top > (pagesBefore) << 16) { + let pagesNeeded = ((top - currentOffset + 0xffff) & ~0xffff) >>> 16; + let pagesWanted = max(pagesBefore, pagesNeeded); // double memory + if (memory.grow(pagesWanted) < 0) { + if (memory.grow(pagesNeeded) < 0) { + unreachable(); // out of memory + } + } + } + } while ( + Atomic.cmpxchg(OFFSET_PTR, currentOffset, top) != currentOffset + ); + + return currentOffset; + } + return 0; +} + +@global export function __memory_free(ptr: usize): void { + //TODO: Not implemented +} + +@global export function __memory_reset(): void { + Atomic.store(OFFSET_PTR, SATRT_OFFSET); +} From 182c9b94d5d053b092239b044be9afe2ae27816c Mon Sep 17 00:00:00 2001 From: Nidin Vinayakan Date: Thu, 28 Feb 2019 15:30:58 +0100 Subject: [PATCH 2/2] Update atomic.ts --- std/assembly/allocator/atomic.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/std/assembly/allocator/atomic.ts b/std/assembly/allocator/atomic.ts index a218aaf809..ee9d620e62 100644 --- a/std/assembly/allocator/atomic.ts +++ b/std/assembly/allocator/atomic.ts @@ -6,11 +6,11 @@ var TOP = (HEAP_BASE + 8 + AL_MASK) & ~AL_MASK; store(OFFSET_PTR, TOP); @global export function __allocator_get_offset(): usize { - return Atomic.load(OFFSET_PTR); + return atomic.load(OFFSET_PTR); } @global export function __allocator_set_offset(oldOffset: usize, newOffset: usize): usize { - return Atomic.cmpxchg(OFFSET_PTR, oldOffset, newOffset); + return atomic.cmpxchg(OFFSET_PTR, oldOffset, newOffset); } @global export function __memory_allocate(size: usize): usize { @@ -32,7 +32,7 @@ store(OFFSET_PTR, TOP); } } } while ( - Atomic.cmpxchg(OFFSET_PTR, currentOffset, top) != currentOffset + atomic.cmpxchg(OFFSET_PTR, currentOffset, top) != currentOffset ); return currentOffset; @@ -45,5 +45,5 @@ store(OFFSET_PTR, TOP); } @global export function __memory_reset(): void { - Atomic.store(OFFSET_PTR, SATRT_OFFSET); + atomic.store(OFFSET_PTR, SATRT_OFFSET); }