This commit is contained in:
dcode 2019-04-18 13:08:49 +02:00
parent 18c3f0c555
commit 2b0a165e7f
2 changed files with 13 additions and 4 deletions

View File

@ -12,6 +12,9 @@ Interface
Dynamically allocates a chunk of memory of at least the specified size and returns its address. Dynamically allocates a chunk of memory of at least the specified size and returns its address.
Alignment is guaranteed to be 16 bytes to fit up to v128 values naturally. Alignment is guaranteed to be 16 bytes to fit up to v128 values naturally.
* **__rt_reallocate**(ref: `usize`, size: `usize`): `usize`<br />
Dynamically changes the size of a chunk of memory, possibly moving it to a new address.
* **__rt_free**(ref: `usize`): `void`<br /> * **__rt_free**(ref: `usize`): `void`<br />
Frees a dynamically allocated chunk of memory by its address. Frees a dynamically allocated chunk of memory by its address.

View File

@ -2,7 +2,7 @@ import { AL_MASK, CommonBlock } from "./common";
// @ts-ignore: decorator // @ts-ignore: decorator
@inline @inline
const BLOCK_OVERHEAD = offsetof<CommonBlock>(); const BLOCK_OVERHEAD = (offsetof<CommonBlock>() + AL_MASK) & ~AL_MASK;
// @ts-ignore: decorator // @ts-ignore: decorator
@inline @inline
@ -43,9 +43,15 @@ function __rt_allocate(size: usize, id: u32): usize {
@unsafe @global @unsafe @global
function __rt_reallocate(ref: usize, size: usize): usize { function __rt_reallocate(ref: usize, size: usize): usize {
var block = changetype<CommonBlock>(ref - BLOCK_OVERHEAD); var block = changetype<CommonBlock>(ref - BLOCK_OVERHEAD);
var newRef = __rt_allocate(size, block.rtId); var oldSize = <usize>block.rtSize;
memory.copy(newRef, ref, block.rtSize); if (size > oldSize) {
return newRef; let newRef = __rt_allocate(size, block.rtId);
memory.copy(newRef, ref, oldSize);
ref = newRef;
} else {
block.rtSize = size;
}
return ref;
} }
// @ts-ignore: decorator // @ts-ignore: decorator