Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
#! @file allocator.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the `allocator` trait, which hands out blocks of memory and takes
#! them back, and the functions containers acquire, dispose of, and resize their
#! memory through: each uses the allocator it is given, or the program's heap
#! where it is given none. Each block is asked for with its size and its
#! alignment, and an allocator must outlive every block it gave.
module core;
#! Defines what all allocators should be able to do.
#!
#! Allocators are simply a structured mechanism for being able to manage memory
#! in a way that is clear and simple, as an arena, a pool, or the program's heap
#! does.
#!
#! A block is given back with the size it was asked for with, so an allocator
#! need not keep the size of each block itself.
pub trait allocator {
#! The purpose of acquire is to provide you with a block of memory from the
#! backing data store that the allocator is managing.
#! @param this The allocator to acquire from.
#! @param size The number of bytes the block holds.
#! @param align What the address of its first byte is a multiple of, a power
#! of two no wider than `max_align`.
#! @returns The block if it could be acquired; otherwise `null`.
#! @remarks The block may hold anything at first.
def acquire(&mut this, size: u64, align: u64): *mut u8;
#! The purpose of dispose is to reclaim the block of memory that was allocated
#! to you, freeing it in the backing data store that the allocator is
#! managing.
#! @param this The allocator to dispose to.
#! @param addr The block of memory to dispose.
#! @param size The number of bytes it was acquired or last resized with.
def dispose(&mut this, addr: *mut u8, size: u64): void;
#! The purpose of resize is to provide you with a block of memory of another
#! size, holding what a block it handed out holds, as far as both reach.
#!
#! It will allocate the new block, copy the data of the old block to the newly
#! allocated one, then release the old block in most cases. The allocator
#! implementation has discretion in how it actually accomplishes this, and may
#! grow or shrink the block where it is.
#! @param this The allocator to resize the allocation.
#! @param addr The block to resize, or `null` to acquire one.
#! @param old The number of bytes it was acquired or last resized with, or
#! 0 where `addr` is `null`.
#! @param size The number of bytes the new block holds.
#! @param align What the address of its first byte is a multiple of, as it
#! was acquired with.
#! @returns The new block if it could be acquired; otherwise `null`, leaving
#! the old block as it was.
#! @remarks What the new block holds past what it keeps may be anything.
def resize(&mut this, addr: *mut u8, old: u64, size: u64, align: u64): *mut u8;
}
#! Acquires memory for a number of elements, from an allocator or the heap.
#!
#! When the allocator is present, its `acquire` gives the block. Otherwise the
#! program's heap gives it, as `raw_allocate` does, uncleared.
#! @tparam T The type of the elements the memory holds.
#! @param alloc The optional allocator to acquire from.
#! @param count How many elements the memory holds.
#! @returns The block, as a pointer to its first element.
#! @remarks The block is zeroed where `T` is `closeable`, so that assigning an
#! element into it closes nothing; otherwise it may hold anything at
#! first. An allocator that gives no memory stops the program, as
#! `raw_allocate` does where the system gives none, so what is given is
#! never `null` where any memory was asked for.
pub def acquire<T>(alloc: maybe<&mut allocator>, count: u64): *mut T {
if const T is closeable {
return acquire_zeroed::<T>(alloc, count);
}
val wanted := bytes_of::<T>(count);
val block := match alloc {
some(held) => held.acquire(wanted, align_of::<T>()),
none => raw_allocate(wanted)
};
if block == null && wanted != 0 {
exhausted(wanted);
}
return block as *mut T;
}
#! Takes zeroed memory for a number of elements, from an allocator or, where
#! none is given, from the program's heap, which clears only memory it used
#! before, as `raw_allocate_zeroed` does.
#! @tparam T The type of the elements.
#! @param alloc The optional allocator to take it from.
#! @param count How many elements.
#! @returns The first element, zeroed.
#! @remarks An allocator that gives no memory stops the program, as `acquire`
#! does.
pub def acquire_zeroed<T>(alloc: maybe<&mut allocator>, count: u64): *mut T {
val wanted := bytes_of::<T>(count);
val block := match alloc {
some(held) => held.acquire(wanted, align_of::<T>()),
none => raw_allocate_zeroed(wanted)
};
if block == null && wanted != 0 {
exhausted(wanted);
}
if alloc is some(_) {
memset(block, 0, wanted);
}
return block as *mut T;
}
#! Gives memory back to the allocator it came from, or to the heap.
#!
#! When the allocator is present, its `dispose` takes the block. Otherwise
#! `raw_release` gives it back to the program's heap.
#! @tparam T The type of the elements the memory holds.
#! @param alloc The optional allocator the memory came from.
#! @param addr The first element of the block to give back, or `null`, which
#! gives back nothing.
#! @param count How many elements it was acquired or last resized for.
#! @remarks The block must have come from the same allocator, or from the heap
#! where none is given. Where it did not, what happens is up to the
#! allocator, which may stop the program.
pub def dispose<T>(alloc: maybe<&mut allocator>, addr: *mut T, count: u64): void {
if addr == null {
return;
}
match alloc {
some(held) => held.dispose(addr as *mut u8, bytes_of::<T>(count));
none => raw_release(addr as *mut u8);
}
}
#! Resizes memory for a number of elements, from an allocator or the heap.
#!
#! When the allocator is present, its `resize` gives the new block. Otherwise
#! `raw_resize` gives it, from the program's heap, or `acquire` does where there
#! is no block yet. Either keeps what the old block held, as far as the new one
#! reaches, and gives the old block back.
#! @tparam T The type of the elements the memory holds.
#! @param alloc The optional allocator the memory came from.
#! @param addr The first element of the block to resize, which may be `null`
#! to acquire a new one.
#! @param old How many elements it was acquired or last resized for, or 0
#! where `addr` is `null`.
#! @param count How many elements the new block holds.
#! @returns The new block, as a pointer to its first element.
#! @remarks The new block is zeroed past what was kept where `T` is
#! `closeable`, as `acquire` zeroes it. The block must have come from
#! the same allocator, or from the heap where none is given. An
#! allocator that gives no memory stops the program, as `acquire` does.
pub def resize<T>(alloc: maybe<&mut allocator>, addr: *mut T, old: u64, count: u64): *mut T {
# The old count is one the memory was acquired for, as `dispose` counts it.
val had := addr == null ? 0 : old * size_of::<T>();
val wanted := bytes_of::<T>(count);
val block := match alloc {
some(held) => held.resize(addr as *mut u8, had, wanted, align_of::<T>()),
none => addr == null ? raw_allocate(wanted) : raw_resize(addr as *mut u8, wanted)
};
if block == null && wanted != 0 {
exhausted(wanted);
}
if const T is closeable {
if wanted > had {
memset(block + had, 0, wanted - had);
}
}
return block as *mut T;
}