Skip to content

Commit 4257273

Browse files
committed
Make TLSF always align to 8 bytes, see #15
1 parent f754b24 commit 4257273

File tree

11 files changed

+2088
-2080
lines changed

11 files changed

+2088
-2080
lines changed

std/assembly/allocator/arena.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/////////////// A simple yet effective Arena Memory Allocator /////////////////
2-
3-
// Provides a `reset_memory` function to reset the heap to its initial state. A
4-
// user has to make sure that there are no more references to cleared memory
5-
// afterwards. Always aligns to 8 bytes.
1+
/**
2+
* @file Arena Memory Allocator
3+
*
4+
* Provides a `reset_memory` function to reset the heap to its initial state. A user has to make
5+
* sure that there are no more references to cleared memory afterwards. Always aligns to 8 bytes.
6+
*/
67

78
const ALIGN_LOG2: usize = 3;
89
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;

std/assembly/allocator/emscripten.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
///////////////////////// Emscripten Memory Allocator //////////////////////////
2-
3-
// Uses Emscripten's exported _malloc and _free implementations, i.e., when
4-
// linking with Emscripten-compiled programs that already provide these.
5-
// Differs from 'system' in that their names are prefixed with an underscore.
1+
/**
2+
* @file Emscripten Memory Allocator
3+
*
4+
* Uses Emscripten's exported _malloc and _free implementations, i.e., when linking with
5+
* Emscripten-compiled programs that already provide these. Differs from 'system' in that their
6+
* names are prefixed with an underscore.
7+
*/
68

79
declare function _malloc(size: usize): usize;
810
declare function _free(ptr: usize): void;

std/assembly/allocator/none.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @file Memory Allocator Stub
3+
*/
4+
15
export function allocate_memory(size: usize): usize {
26
throw new Error("not supported");
37
}

std/assembly/allocator/system.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
/////////////////////////// System Memory Allocator ////////////////////////////
2-
3-
// Uses the environment's malloc and free implementations, i.e., when linking
4-
// with other C-like programs that already provide these.
1+
/**
2+
* @file System Memory Allocator
3+
*
4+
* Uses the environment's malloc and free implementations, i.e., when linking with other C-like
5+
* programs that already provide these.
6+
*/
57

68
declare function malloc(size: usize): usize;
79
declare function free(ptr: usize): void;

std/assembly/allocator/tlsf.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
////////////// TLSF (Two-Level Segregate Fit) Memory Allocator ////////////////
1+
/**
2+
* @file Two-Level Segregate Fit Memory Allocator
3+
*
4+
* A general purpose dynamic memory allocator specifically designed to meet real-time requirements.
5+
* Always aligns to 8 bytes.
6+
*/
27

38
// ╒══════════════ Block size interpretation (32-bit) ═════════════╕
49
// 3 2 1
@@ -8,7 +13,7 @@
813
// └───────────────────────────────────────────────┴─────────╨─────┘
914
// FL: first level, SL: second level, AL: alignment, SB: small block
1015

11-
const AL_BITS: u32 = sizeof<usize>() == sizeof<u32>() ? 2 : 3;
16+
const AL_BITS: u32 = 3; // always align to 8 bytes
1217
const AL_SIZE: usize = 1 << <usize>AL_BITS;
1318
const AL_MASK: usize = AL_SIZE - 1;
1419

@@ -46,8 +51,6 @@ const LEFT_FREE: usize = 1 << 1;
4651
/** Mask to obtain all tags. */
4752
const TAGS: usize = FREE | LEFT_FREE;
4853

49-
assert(AL_BITS >= 2); // alignment must be large enough to store all tags
50-
5154
/** Block structure. */
5255
@unmanaged
5356
class Block {
@@ -56,15 +59,15 @@ class Block {
5659
info: usize;
5760

5861
/** End offset of the {@link Block#info} field. User data starts here. */
59-
static readonly INFO: usize = sizeof<usize>();
62+
static readonly INFO: usize = (sizeof<usize>() + AL_MASK) & ~AL_MASK;
6063

6164
/** Previous free block, if any. Only valid if free. */
6265
prev: Block | null;
6366
/** Next free block, if any. Only valid if free. */
6467
next: Block | null;
6568

6669
/** Minimum size of a block, excluding {@link Block#info}. */
67-
static readonly MIN_SIZE: usize = 3 * sizeof<usize>(); // prev + next + jump
70+
static readonly MIN_SIZE: usize = (3 * sizeof<usize>() + AL_MASK) & ~AL_MASK;// prev + next + jump
6871

6972
/** Maximum size of a used block, excluding {@link Block#info}. */
7073
static readonly MAX_SIZE: usize = 1 << (FL_BITS + SB_BITS);
@@ -445,7 +448,7 @@ export function allocate_memory(size: usize): usize {
445448
root.setHead(fl, sl, null);
446449
}
447450
}
448-
root.addMemory(rootOffset + Root.SIZE, current_memory() << 16);
451+
root.addMemory((rootOffset + Root.SIZE + AL_MASK) & ~AL_MASK, current_memory() << 16);
449452
}
450453

451454
// search for a suitable block

tests/allocators/buddy/assembly/buddy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @file Buddy Memory Allocator
3+
*/
4+
15
/*
26
Copyright 2018 Evan Wallace
37

0 commit comments

Comments
 (0)