diff options
author | JJ | 2024-05-10 01:00:01 +0000 |
---|---|---|
committer | JJ | 2024-05-10 02:41:52 +0000 |
commit | 073c902a31936c2b53d89245662fb272c9670169 (patch) | |
tree | a8789ed4561dc4c3dde84489a600272cbd5f806b /std/prelude/mem.pk | |
parent | 51c8b349a77a8d8b1b34ce8e03518dad6e3cba00 (diff) |
std: sweeping changes.
- replace interfaces with classes
- replace : with then and do
- solidify memory management as rust-style ownership
- replace unsafe blocks with safe/unsafe attributes
- add magic and copy attributes
- switch Coerce impl from from to to
Diffstat (limited to 'std/prelude/mem.pk')
-rw-r--r-- | std/prelude/mem.pk | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/std/prelude/mem.pk b/std/prelude/mem.pk index 79c524b..5ad3c7a 100644 --- a/std/prelude/mem.pk +++ b/std/prelude/mem.pk @@ -1,35 +1,58 @@ ## std.mem: Unsafe functions for working with raw memory. +## This module is imported by default. ## Raw pointers. Extremely unsafe. ## While they are removed from the memory model by default (and hence can -## cause undefined behavior), pointers must know what *type* they point at. +## cause memory errors), pointers must know what *type* they point at. ## This cuts down on errors somewhat - not much, but somewhat. +@[magic] pub type ptr[T] = addr: uint ## Heap-allocated, memory-safe references. +@[magic] pub type ref[T] -pub type unique[T] +## Heap-allocated, reference-counted references. +## These are a special type - rather than anything handled by the type system +## alone - because there are significant optimizations the compiler can make. +@[magic] +pub type refc[T] + +## Tells the compiler to interpret data of one type directly as data of another. +## Extremely unsafe. Take caution to memory representations when using. +## Compiler magic. The type signature is left here for documentation purposes. +@[unsafe, magic] +pub func cast[T, U](self: T): U ## Sets the sizeof(T) bytes following data + offset * sizeof(T) to T. ## Extremely unsafe. -pub func raw_set[T](data: ptr[T], val: T, offset: uint = 0) = +@[unsafe] +pub func set[T](data: ptr T, val: T, offset: uint = 0) = ... ## Gets the value at data + offset * sizeof(T), and interprets it as type T. ## Extremely unsafe. -pub func raw_get[T](data: ptr[T], offset: uint = 0) = +@[unsafe] +pub func get[T](data: ptr T, offset: uint = 0) = ... ## Returns a pointer offset by offset * sizeof(T). ## Extremely unsafe. -pub func raw_offset[T](data: ptr[T], offset: uint): ptr[T] = +@[unsafe] +pub func offset[T](data: ptr T, offset: uint): ptr T = + ... + +@[unsafe] +pub func malloc[T](size: uint = sizeof(T)): ptr T = ... -pub func malloc[T](size: uint = sizeof(T)): ptr[T] = +@[unsafe] +pub func calloc[T](size: uint = sizeof(T)): ptr T = ... -pub func calloc[T](size: uint = sizeof(T)): ptr[T] = +@[unsafe] +pub func realloc[T](data: ptr T): ptr T = ... -pub func realloc[T](data: ptr[T]): ptr[T] = +@[unsafe] +pub func copy[T](from: ptr T, to: ptr T, length: uint) = ... |