alloc/heap.kur
//! Raw heap allocation and checked size arithmetic.
// alloc::heap -- the one place `malloc`/`realloc`/`free` are declared. Every
// other module that needs raw heap memory goes through
// `HeapAllocator::alloc`/`grow`/`drop`.
//
// `Uninit!` is restricted: only this module can construct or destructure
// one, so no other module can rebuild two owners from one extracted pointer.
// Other modules work with raw addresses through four functions:
// `Uninit!::into_raw` and `Uninit!::into_parts` consume the owner and hand back
// its address (and, for `into_parts`, its extent); `HeapAllocator::adopt_raw`
// and `adopt_raw_sized` turn an address back into an owner and are
// `unsafe fn`, with their preconditions stated on them below.
//
// `Storage::try_alloc`/`try_grow` are the fallible primitives;
// `HeapAllocator::alloc`/`grow` are aborting conveniences built on them.
// The package header (`alloc`'s lib.kur) states the policy for the
// library as a whole.
use package as alloc;
use core::option::Result;
use core::ptr::{null, is_null};
/// C's `malloc`: a block of `size` bytes, or null. Used by `Storage`; not
/// meant for direct use.
extern "C" fn malloc(size: usize) -> *u8;
/// C's `realloc`: the block at `p` resized to `size` bytes, or null with
/// `p` untouched. Used by `Storage`; not meant for direct use.
extern "C" fn realloc(p: *u8, size: usize) -> *u8;
/// C's `free`. Used by `HeapAllocator::drop`; not meant for direct use.
extern "C" fn free(p: *u8);
/// C's `abort`: stops the process at once. Allocation failure, and a size
/// computation that overflows `usize`, are hard stops in the aborting API
/// (`alloc`, `grow`, `checked_add`, `checked_mul`); the rest of the library
/// imports `abort` from here.
extern "C" fn abort() -> !;
/// `a + b`, or `abort` if the sum is not representable.
///
/// Checked by subtraction before adding: `usize::MAX - b` cannot wrap, so
/// `a > usize::MAX - b` detects overflow before a wrapped (too small) size
/// could reach `malloc`. `alloc::arena::has_room` uses the same idiom.
fn checked_add(a: usize, b: usize) -> usize {
let room = usize::MAX - b;
// The `: ()` annotation gives the `if` a concrete type for the diverging
// `abort()` arm.
let g: () = if a > room { unsafe abort() } else { () };
a + b
}
/// `a * b`, or `abort` if the product is not representable.
///
/// Guarded by division against `usize::MAX`, for the same reason: testing
/// `a * b / b != a` would inspect an already-wrapped value. `b == 0usize` is
/// special-cased because the guard divides by it; a zero-sized element or a
/// zero-element request is legitimate and cannot overflow.
fn checked_mul(a: usize, b: usize) -> usize {
let is_zero = b == 0usize;
let limit = if is_zero {
usize::MAX
} else {
usize::MAX / b
};
// See `checked_add` for the `: ()` annotation.
let g: () = if a > limit { unsafe abort() } else { () };
a * b
}
/// The size of the block to ask `malloc` or `realloc` for: `size`, or one
/// byte for none.
fn at_least_one(size: usize) -> usize {
if size == 0usize { 1usize } else { size }
}
/// Allocation failure, as a value. `Storage::try_alloc`/`try_grow` are the
/// only producers. Not linear: an error code is `Copy` data, and the aborting
/// wrappers below drop it.
exposed enum AllocError {
OutOfMemory,
}
/// The fallible allocation primitives, as static methods.
///
/// Zero-field policy types give static allocator methods a nominal receiver.
/// They own no memory; allocation ownership remains exclusively in Uninit!.
struct Storage {}
/// Static allocator methods backed by the host heap. This policy value
/// owns no storage; returned `Uninit!` values hold allocation ownership.
struct HeapAllocator {}
/// Sole ownership of one uninitialized heap block: its base address and its
/// extent. Linear, and restricted to this module.
///
/// `extent` is the size in bytes most recently REQUESTED for the block -- not
/// what the allocator actually reserved (`malloc` may round up), and not an
/// alignment. `alloc`/`try_alloc`/`grow`/`try_grow` record it truthfully;
/// `adopt_raw` cannot and records `0usize` (see there).
struct Uninit! {
ptr: *u8,
extent: usize,
}
impl Uninit! {
/// Consumes the owner and hands back its address. Safe: the caller cannot
/// turn the address back into an `Uninit!` except through `adopt_raw`,
/// which is `unsafe fn` and carries the real obligation.
fn into_raw(self) -> *u8 {
let { ptr: p, extent: n } = self;
p
}
/// `into_raw`, keeping the extent; `alloc::owned::Allocation!::adopt`
/// records both. Safe for the same reason as `into_raw`; the extent is a
/// plain number.
fn into_parts(self) -> (*u8, usize) {
let { ptr: p, extent: n } = self;
(p, n)
}
}
// The fallible allocation primitives. `HeapAllocator::alloc`/`grow` are
// built on these.
//
// `try_grow` can hand the caller's block back on failure because C
// guarantees a failed `realloc` leaves the original block live and unmoved:
// the old address is still the base of a single live allocation with no
// other `Uninit!`, which is exactly `adopt_raw`'s precondition. Linearity
// then forces every caller to deal with the returned owner; ignoring the
// `Err` payload does not compile.
//
// `Storage` and `HeapAllocator` are zero-field policy receivers; neither
// holds storage, and ownership stays with `Uninit!`.
impl Storage {
/// A new uninitialized block of `size` bytes, or the allocation failure.
///
/// The fallible `alloc`. Takes no alignment: the block is whatever
/// `malloc` returns. A block of no bytes (for an empty buffer) is one
/// byte to `malloc`, so it is never null, as
/// `malloc(0)` may be: a block's address goes into a `Ref`, which is
/// never null.
fn try_alloc(size: usize) -> Result[Uninit!, AllocError] {
let raw = unsafe malloc(at_least_one(size));
if is_null(raw) {
Result::Err.(AllocError::OutOfMemory)
} else {
Result::Ok.(Uninit!.{ ptr: raw, extent: size })
}
}
/// `u`'s block resized to `new_size` bytes, or the failure with `u`'s block
/// unchanged.
///
/// The fallible `grow`. The error arm hands the caller's own block back
/// at its original extent -- never a fresh or null one. As in
/// `try_alloc`, no bytes is one byte to `realloc`, which may free the
/// block for none (`realloc(p, 0)`).
fn try_grow(u: Uninit!, new_size: usize) -> Result[Uninit!, (AllocError, Uninit!)] {
let (raw, old_size) = u.into_parts();
let raw2 = unsafe realloc(raw, at_least_one(new_size));
if is_null(raw2) {
Result::Err.((AllocError::OutOfMemory, Uninit!.{ ptr: raw, extent: old_size }))
} else {
Result::Ok.(Uninit!.{ ptr: raw2, extent: new_size })
}
}
}
impl HeapAllocator {
/// A new uninitialized block of `size` bytes, aborting when the allocation
/// fails.
///
/// `Storage::try_alloc`, aborting on failure. Most allocations in the
/// library (arena chunks, hash entries, `Owned!::new`, ...) use this
/// form, so they need no error channel.
///
/// The `Err` arm aborts; its tail still has to be an `Uninit!`. It builds
/// the null `Uninit!` directly rather than through `adopt_raw`, whose
/// precondition (a live allocation) the null pointer does not meet.
/// Constructing the literal in its own module makes no such claim.
fn alloc(size: usize) -> Uninit! {
match Storage::try_alloc(size) {
Ok(u) => u,
Err(e) => {
let g = unsafe abort();
Uninit!.{ ptr: null(), extent: 0usize }
},
}
}
/// `u`'s block resized to `new_size` bytes, aborting when that fails.
///
/// `Storage::try_grow`, aborting on failure. The `Err` arm needs no
/// invented value: `try_grow` hands the caller's block back.
fn grow(u: Uninit!, new_size: usize) -> Uninit! {
match Storage::try_grow(u, new_size) {
Ok(u2) => u2,
Err(pair) => {
let (e, u2) = pair;
let g = unsafe abort();
u2
},
}
}
/// Frees the block `u` owns.
fn drop(u: Uninit!) -> () {
let raw = u.into_raw();
unsafe free(raw)
}
/// Turns an address back into an owner.
///
/// Caller: `p` is the base address of a single live allocation
/// obtained from `malloc`/`realloc` (directly or through this module's
/// `alloc`/`grow`), for which no other `Uninit!` exists -- i.e. an address
/// the caller itself obtained from `Uninit!::into_raw` (or carried along a
/// single-owner path), never a copy, a sub-range, or an address from
/// another allocator.
///
/// Records `extent: 0usize`, meaning "not recorded", which is
/// indistinguishable from a real zero-byte allocation. A caller that
/// still knows the size should use `adopt_raw_sized`.
unsafe fn adopt_raw(p: *u8) -> Uninit! {
Uninit!.{ ptr: p, extent: 0usize }
}
/// `adopt_raw`, for a caller that still knows the block's size, so the
/// extent survives a round trip (as in `alloc::owned::Allocation!::release`).
///
/// Caller: as for `adopt_raw`, and `extent` is the size most recently
/// requested for that block.
unsafe fn adopt_raw_sized(p: *u8, extent: usize) -> Uninit! {
Uninit!.{ ptr: p, extent: extent }
}
}