#! @file mem.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains what a program manages memory with itself: the sizes and alignments #! of types, allocating, resizing, and releasing blocks, reaching through #! pointers, and copying, filling, and comparing bytes. #! #! A block is the program's until it releases it, and nothing keeps one alive: #! a pointer or a reference into one is good while the block is, and using one #! past that is the program's mistake. Owners such as `box`, `rc`, `arc`, and #! the containers release what they allocate where they close. import core::swar; module core; #! The number of bytes a value of a type takes in memory, which the compiler #! gives. #! @tparam T The type to measure. #! @returns Its size in bytes, known while compiling. @intrinsic pub def size_of(): u64; #! The alignment a value of a type needs in memory, which the compiler gives. #! @tparam T The type to measure. #! @returns Its alignment in bytes, known while compiling. @intrinsic pub def align_of(): u64; #! The widest alignment any type needs, which `raw_allocate` aligns every block #! to. pub const max_align := 16u; #! Allocates a block of bytes from the program's heap, as C's `malloc` does, #! without clearing it, for what is written before it is read. #! @param size The number of bytes to allocate. #! @returns The first byte of the block, aligned to `max_align`, which may hold #! anything at first. #! @remarks Where the system gives no memory, the program stops, so this never #! gives `null` where any memory was asked for. Reading a byte before #! writing it is the program's mistake, which `run --checks` stops at. @intrinsic pub def raw_allocate(size: u64): *mut u8; #! Allocates a block of zeroed bytes from the program's heap, as C's `calloc` #! does. Memory the system gives fresh is zero already, so only a block used #! before is cleared. #! @param size The number of bytes to allocate. #! @returns The first byte of the block, aligned to `max_align`. #! @remarks Where the system gives no memory, the program stops, as #! `raw_allocate` does. @intrinsic pub def raw_allocate_zeroed(size: u64): *mut u8; # Allocates a block a value of a type is written into: zeroed where the type # closes, since writing it closes what the block held, and as it is otherwise. def allocate_for(size: u64): *mut u8 { if const T is closeable { return raw_allocate_zeroed(size); } return raw_allocate(size); } #! Releases a block allocated by `raw_allocate`, `raw_allocate_zeroed`, or #! `raw_resize`. #! @param pointer The first byte of the block, or `null`, which does nothing. #! @remarks Releasing a block twice stops the program. @intrinsic pub def raw_release(pointer: *mut u8): void; #! Moves an allocated block to one of another size, keeping as many bytes as #! fit, then releases the old one. #! @param pointer The first byte of the block, or `null` to allocate a new #! one as `raw_allocate` does. #! @param size The number of bytes the new block holds. #! @returns The first byte of the new block, which may hold anything past what #! was kept. It may be the old block, where that is large enough #! already. #! @remarks Where the system gives no memory, the program stops, as #! `raw_allocate` does. @intrinsic pub def raw_resize(pointer: *mut u8, size: u64): *mut u8; #! A reference that reads what a pointer points at. #! #! A pointer is an address with no members of its own, so this reaches them, as #! `through(node).next` does, the same as `(*node).next`. #! @tparam T The type pointed at. #! @param pointer The pointer to read through. #! @returns A reference that only reads what the pointer points at. #! @remarks A null pointer stops the program. pub def through(pointer: *T): &T { return &pointer[0]; } #! A reference that writes what a pointer points at, as `through(node).next = #! null` does. #! @tparam T The type pointed at. #! @param pointer The pointer to write through. #! @returns A reference that writes what the pointer points at. #! @remarks A null pointer stops the program. pub def through(pointer: *mut T): &mut T { return &mut pointer[0]; } #! A pointer that writes what a reference refers to, as `through` gives a #! reference at what a pointer points at, as `address(node)` is `&mut node as #! *mut node`. Given a place, a call lends it, as `address(x)` does a `var x`. #! @tparam T The type referred to. #! @param value The reference. #! @returns A pointer at what the reference refers to, which keeps nothing alive. pub def address(value: &mut T): *mut T { return value as *mut T; } #! A pointer that reads what a reference refers to, as `address(x)` is of a #! `val x`. #! @tparam T The type referred to. #! @param value The reference. #! @returns A pointer at what the reference refers to, which keeps nothing alive. pub def address(value: &T): *T { return value as *T; } # The bytes a number of elements take, or more than can be allocated when that # overflows, so the allocation stops the program instead of being too small. #! The bytes a number of elements of a type take. #! @tparam T The type of the elements. #! @param count How many elements. #! @returns Their size in bytes, or the largest `u64` where that overflows, so #! that allocating it stops the program rather than giving too small a #! block. pub def bytes_of(count: u64): u64 { val size := size_of::(); if size != 0 && count > 0xFFFFFFFFFFFFFFFF / size { return 0xFFFFFFFFFFFFFFFF; } return count * size; } #! Rounds an address up to the next multiple of an alignment. #! @param addr The address to align. #! @param align The alignment, which is a power of two. #! @returns The address, or the next past it that is a multiple of the #! alignment. pub def align(addr: u64, align: u64): u64 { return (addr + (align - 1)) & ~(align - 1); } #! Copies bytes from one buffer into another. #! @param dst The first byte to copy to. #! @param src The first byte to copy from. #! @param len How many bytes to copy. #! @returns The destination, `dst`. #! @remarks The buffers may overlap. Either pointer being `null` copies nothing. pub def memcpy(dst: *mut u8, src: *u8, len: u64): *mut u8 { if dst == null || src == null { return dst; } if dst == src || len == 0 { return dst; } if len >= 8 { return (dst as u64) - (src as u64) >= len ? copyfwd(dst, src, len) : copybwd(dst, src, len); } if len >= 4 { val head := *(src as *u32); val tail := *((src + len - 4) as *u32); *(dst as *mut u32) = head; *((dst + len - 4) as *mut u32) = tail; return dst; } if len >= 2 { val head := *(src as *u16); val tail := *((src + len - 2) as *u16); *(dst as *mut u16) = head; *((dst + len - 2) as *mut u16) = tail; return dst; } *dst = *src; return dst; } #! Sets every byte of a buffer to one value. #! @param dst The first byte to set. #! @param data The value to set each byte to. #! @param len How many bytes to set. #! @returns The destination, `dst`. #! @remarks The bytes are set a word at a time, each word the byte repeated, and #! the last word ends where the buffer does, over what the words before #! it set, so no byte is set alone. A buffer shorter than a word is set #! by two halves of one, which overlap the same way. A `null` #! destination sets nothing. pub def memset(dst: *mut u8, data: u8, len: u64): *mut u8 { if dst == null { return dst; } if len == 0 { return dst; } val end := dst + len; # Where vectors are held in registers, 16 bytes at a time, the last ending # where the buffer does. if len >= 32 && lanes_held() { val pattern := u8x16::splat(data); var at := dst; val last := end - 16; while at < last { lanes_apply::(at, (&pattern._lanes) as *u8, null, null); at = at + 16; } lanes_apply::(last, (&pattern._lanes) as *u8, null, null); return dst; } if len >= 8 { val word := (data as u64) * 0x0101010101010101; var each := dst as *mut u64; val last := each + (len >> 3); while each < last { *each++ = word; } *((end - 8) as *mut u64) = word; return dst; } if len >= 4 { val half := (data as u32) * 0x01010101; *(dst as *mut u32) = half; *((end - 4) as *mut u32) = half; return dst; } if len >= 2 { val pair := (data as u16) * 0x0101; *(dst as *mut u16) = pair; *((end - 2) as *mut u16) = pair; return dst; } *dst = data; return dst; } #! Compares the bytes of two buffers, in order, as unsigned numbers. #! @param bpa The first byte of the first buffer. #! @param bpb The first byte of the second buffer. #! @param len How many bytes to compare. #! @retval -1 Where the first buffer orders before the second. #! @retval 0 Where their bytes are the same. #! @retval 1 Where the first buffer orders after the second. #! @remarks Where either is `null`, the pointers themselves are compared. pub def memcmp(bpa: *u8, bpb: *u8, len: u64): i64 { if bpa == null || bpb == null { if bpa < bpb { return -1; } if bpa > bpb { return 1; } return 0; } # Whole words first, then what is left a byte at a time, and the word that # differs a byte at a time to say which way. Fewer than eight bytes that are the # same are found so with two words of half or a quarter as many, which may overlap. var p := bpa; var q := bpb; var i := len; # Where vectors are held in registers, 16 bytes at a time, the first that # differs found from the lanes that do. while i >= 16 && lanes_held() { var a: u8x16; var b: u8x16; lanes_apply::((&mut a._lanes) as *mut u8, p, null, null); lanes_apply::((&mut b._lanes) as *mut u8, q, null, null); val same := a.equal(&b).bits(); if same != 0xFFFF { val k := trailing_zeros(~same) as u64; return p[k] < q[k] ? -1 : 1; } p = p + 16; q = q + 16; i = i - 16; } while i >= 8 && *(p as *u64) == *(q as *u64) { p = p + 8; q = q + 8; i = i - 8; } if i >= 4 && i < 8 && *(p as *u32) == *(q as *u32) && *((p + i - 4) as *u32) == *((q + i - 4) as *u32) { return 0; } if i >= 2 && i < 4 && *(p as *u16) == *(q as *u16) && *((p + i - 2) as *u16) == *((q + i - 2) as *u16) { return 0; } while i > 0 { val a := *p++; val b := *q++; if a != b { return a < b ? -1 : 1; } i--; } return 0; } # The first place from `from` up to `end` where the bytes hold one byte, or # `end` where none does: sixteen at a time where vectors are held in registers, # the first found from the bits of those that are it, then eight at a time as # words, and the last few as one word read without passing the end. def find_byte(data: *u8, from: u64, end: u64, byte: u8): u64 { var at := from; if lanes_held() { val wanted := u8x16::splat(byte); while at + 16 <= end { var chunk: u8x16; lanes_apply::((&mut chunk._lanes) as *mut u8, data + at, null, null); val found := chunk.equal(&wanted).bits(); if found != 0 { return at + trailing_zeros(found) as u64; } at += 16; } } val wanted := swar::repeat(byte); while at + 8 <= end { val word: swar::bytes8 = swar::load(data + at) ^ wanted; val found := word.zero(); return at + found.first() if found.any(); at += 8; } if at < end { val word: swar::bytes8 = swar::load_last(data + at, end - at) ^ wanted; val found: swar::mask8 = word.zero() & swar::mask8::leading(end - at); return at + found.first() if found.any(); } return end; } # The last place from `from` up to `end` where the bytes hold one byte, or # `end` where none does: eight at a time as words from the end back, and the # first few as one word read without passing their beginning. def find_last_byte(data: *u8, from: u64, end: u64, byte: u8): u64 { val wanted := swar::repeat(byte); var at := end; while at >= from + 8 { val word: swar::bytes8 = swar::load(data + at - 8) ^ wanted; val found := word.zero(); return at - 8 + found.last() if found.any(); at -= 8; } if at > from { val word: swar::bytes8 = swar::load_last(data + from, at - from) ^ wanted; val found: swar::mask8 = word.zero() & swar::mask8::leading(at - from); return from + found.last() if found.any(); } return end; } # How many of the bytes from `from` up to `end` are one byte: eight at a time # as words, and the last few as one word read without passing the end. def count_byte(data: *u8, from: u64, end: u64, byte: u8): u64 { val wanted := swar::repeat(byte); var count: u64 = 0; var at := from; while at + 8 <= end { val word: swar::bytes8 = swar::load(data + at) ^ wanted; count += word.zero().count(); at += 8; } if at < end { val word: swar::bytes8 = swar::load_last(data + at, end - at) ^ wanted; val found: swar::mask8 = word.zero() & swar::mask8::leading(end - at); count += found.count(); } return count; } # Copies at least a word from the first byte up, a word at a time, each read before # it is written, so a destination below the source never writes over what is left # to read. The last word ends where the buffers do, read before anything is written. def copyfwd(dst: *mut u8, src: *u8, len: u64): *mut u8 { # Where vectors are held in registers, 16 bytes at a time, the same way. if len >= 32 && lanes_held() { var last: u8x16; lanes_apply::((&mut last._lanes) as *mut u8, src + len - 16, null, null); var to := dst; var from := src; val end := dst + len - 16; while to < end { lanes_apply::(to, from, null, null); to = to + 16; from = from + 16; } lanes_apply::(end, (&last._lanes) as *u8, null, null); return dst; } val tail := *((src + len - 8) as *u64); var dw := dst as *mut u64; var sw := src as *u64; val last := dw + (len >> 3); while dw < last { *dw++ = *sw++; } *((dst + len - 8) as *mut u64) = tail; return dst; } # Copies at least a word from the last byte down, so a destination above the source # never writes over what is left. The first word begins where the buffers do, read # before anything is written. def copybwd(dst: *mut u8, src: *u8, len: u64): *mut u8 { if len >= 32 && lanes_held() { var first: u8x16; lanes_apply::((&mut first._lanes) as *mut u8, src, null, null); var to := dst + len; var from := src + len; while to > dst + 16 { to = to - 16; from = from - 16; lanes_apply::(to, from, null, null); } lanes_apply::(dst, (&first._lanes) as *u8, null, null); return dst; } val head := *(src as *u64); var dw := (dst + len) as *mut u64; var sw := (src + len) as *u64; val first := dw - (len >> 3); while dw > first { *--dw = *--sw; } *(dst as *mut u64) = head; return dst; }