Skip to content

Commit

Permalink
std: Add type generic memory allocation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kukrimate committed Jan 27, 2023
1 parent 2579f2f commit 7f43a1c
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
45 changes: 45 additions & 0 deletions mpc_std/mem.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@

import libc

struct AlignOfHelper<T> (byte: Uint8, val: T)
struct SizeOfHelper<T> (byte: Uint8, val: T)

// Yields the minimum alignment of T in bytes
function align_of<T>(dummy: *T) -> Uintn {
let ptr: *AlignOfHelper<T> = nil;
(&(*ptr).val) as <Uintn>
}

// Yields the size of T in bytes
function size_of<T>(dummy: *T) -> Uintn {
let ptr: *SizeOfHelper<T> = nil;
(&(*ptr).val) as <Uintn>
}

// Allocate memory to hold one instance of T
function allocate<T>() -> *mut T {
libc::malloc(size_of(nil as <*T>)) as <*mut T>
}

// Allocate memory to hold n instance of T
function allocate_contiguous<T>(n: Uintn) -> *mut T {
libc::malloc(size_of(nil as <*T>) * n) as <*mut T>
}

// Resize a (potentially empty) memory block to hold n instances of T
function reallocate_contiguous<T>(ptr: *mut T, n: Uintn) -> *mut T {
libc::realloc(ptr as <*mut libc::Void>, size_of(nil as <*T>) * n) as <*mut T>
}

// Deallocate a memory block
function deallocate<T>(ptr: *mut T) {
libc::free(ptr as <*mut libc::Void>)
}

// Yield a pointer to the i-th element in a memory block holding instances of T
function ptr_off<T>(ptr: *mut T, i: Uintn) -> *mut T {
(ptr as <Uintn> + i * size_of(nil as <*T>)) as <*mut T>
}

// Yield the number of instances of T held in the memory block between a and b
function ptr_diff<T>(a: *mut T, b: *mut T) -> Uintn {
(b as <Uintn> - a as <Uintn>) / size_of(nil as <*T>)
}

function zero_bytes<T>(ptr: *mut T, n: Uintn) {
libc::memset(ptr as <*mut libc::Void>, 0, n);
}
Expand Down
28 changes: 28 additions & 0 deletions mpc_std/opt.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2022-2023, Mate Kukri
* SPDX-License-Identifier: GPL-2.0-only
*
* Description: Maybe monad
*/

import prog

enum Option<T>(
Some (val: T),
None
)

function some<T>(val: T) -> Option<T> {
Option::Some(val)
}

function none<T>() -> Option<T> {
Option::None
}

function unwrap<T>(o: Option<T>) -> T {
match o {
s: Some => s.val,
None => prog::panic(c"Tried to unwrap None\n")
}
}
21 changes: 21 additions & 0 deletions mpc_std/prog.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2022-2023, Mate Kukri
* SPDX-License-Identifier: GPL-2.0-only
*
* Description: Program utilities
*/

import libc

// Exit the program with exit code `code`
function exit<T>(code: Int32) -> T {
libc::exit(code);
exit(code)
}

// Panic the program with message `msg`
function panic<T>(msg: *Int8) -> T {
libc::fprintf(libc::stderr, c"%s", msg);
libc::abort();
panic(msg)
}

0 comments on commit 7f43a1c

Please sign in to comment.