123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #pragma once
- #include <nall/algorithm.hpp>
- #include <nall/stdint.hpp>
- namespace nall::memory {
- template<typename T = uint8_t> auto allocate(uint size) -> T*;
- template<typename T = uint8_t> auto allocate(uint size, const T& value) -> T*;
- template<typename T = uint8_t> auto resize(void* target, uint size) -> T*;
- auto free(void* target) -> void;
- template<typename T = uint8_t> auto compare(const void* target, uint capacity, const void* source, uint size) -> int;
- template<typename T = uint8_t> auto compare(const void* target, const void* source, uint size) -> int;
- template<typename T = uint8_t> auto icompare(const void* target, uint capacity, const void* source, uint size) -> int;
- template<typename T = uint8_t> auto icompare(const void* target, const void* source, uint size) -> int;
- template<typename T = uint8_t> auto copy(void* target, uint capacity, const void* source, uint size) -> T*;
- template<typename T = uint8_t> auto copy(void* target, const void* source, uint size) -> T*;
- template<typename T = uint8_t> auto move(void* target, uint capacity, const void* source, uint size) -> T*;
- template<typename T = uint8_t> auto move(void* target, const void* source, uint size) -> T*;
- template<typename T = uint8_t> auto fill(void* target, uint capacity, const T& value = {}) -> T*;
- template<typename T> auto assign(T* target) -> void {}
- template<typename T, typename U, typename... P> auto assign(T* target, const U& value, P&&... p) -> void;
- template<uint size, typename T = uint64_t> auto readl(const void* source) -> T;
- template<uint size, typename T = uint64_t> auto readm(const void* source) -> T;
- template<uint size, typename T = uint64_t> auto writel(void* target, T data) -> void;
- template<uint size, typename T = uint64_t> auto writem(void* target, T data) -> void;
- }
- namespace nall::memory {
- //implementation notes:
- //memcmp, memcpy, memmove have terrible performance on small block sizes (FreeBSD 10.0-amd64)
- //as this library is used extensively by nall/string, and most strings tend to be small,
- //this library hand-codes these functions instead. surprisingly, it's a substantial speedup
- template<typename T> auto allocate(uint size) -> T* {
- return (T*)malloc(size * sizeof(T));
- }
- template<typename T> auto allocate(uint size, const T& value) -> T* {
- auto result = allocate<T>(size);
- if(result) fill<T>(result, size, value);
- return result;
- }
- template<typename T> auto resize(void* target, uint size) -> T* {
- return (T*)realloc(target, size * sizeof(T));
- }
- inline auto free(void* target) -> void {
- ::free(target);
- }
- template<typename T> auto compare(const void* target, uint capacity, const void* source, uint size) -> int {
- auto t = (uint8_t*)target;
- auto s = (uint8_t*)source;
- auto l = min(capacity, size) * sizeof(T);
- while(l--) {
- auto x = *t++;
- auto y = *s++;
- if(x != y) return x - y;
- }
- if(capacity == size) return 0;
- return -(capacity < size);
- }
- template<typename T> auto compare(const void* target, const void* source, uint size) -> int {
- return compare<T>(target, size, source, size);
- }
- template<typename T> auto icompare(const void* target, uint capacity, const void* source, uint size) -> int {
- auto t = (uint8_t*)target;
- auto s = (uint8_t*)source;
- auto l = min(capacity, size) * sizeof(T);
- while(l--) {
- auto x = *t++;
- auto y = *s++;
- if(x - 'A' < 26) x += 32;
- if(y - 'A' < 26) y += 32;
- if(x != y) return x - y;
- }
- return -(capacity < size);
- }
- template<typename T> auto icompare(const void* target, const void* source, uint size) -> int {
- return icompare<T>(target, size, source, size);
- }
- template<typename T> auto copy(void* target, uint capacity, const void* source, uint size) -> T* {
- auto t = (uint8_t*)target;
- auto s = (uint8_t*)source;
- auto l = min(capacity, size) * sizeof(T);
- while(l--) *t++ = *s++;
- return (T*)target;
- }
- template<typename T> auto copy(void* target, const void* source, uint size) -> T* {
- return copy<T>(target, size, source, size);
- }
- template<typename T> auto move(void* target, uint capacity, const void* source, uint size) -> T* {
- auto t = (uint8_t*)target;
- auto s = (uint8_t*)source;
- auto l = min(capacity, size) * sizeof(T);
- if(t < s) {
- while(l--) *t++ = *s++;
- } else {
- t += l;
- s += l;
- while(l--) *--t = *--s;
- }
- return (T*)target;
- }
- template<typename T> auto move(void* target, const void* source, uint size) -> T* {
- return move<T>(target, size, source, size);
- }
- template<typename T> auto fill(void* target, uint capacity, const T& value) -> T* {
- auto t = (T*)target;
- while(capacity--) *t++ = value;
- return (T*)target;
- }
- template<typename T, typename U, typename... P> auto assign(T* target, const U& value, P&&... p) -> void {
- *target++ = value;
- assign(target, forward<P>(p)...);
- }
- template<uint size, typename T> auto readl(const void* source) -> T {
- auto p = (const uint8_t*)source;
- T data = 0;
- for(uint n = 0; n < size; n++) data |= T(*p++) << n * 8;
- return data;
- }
- template<uint size, typename T> auto readm(const void* source) -> T {
- auto p = (const uint8_t*)source;
- T data = 0;
- for(int n = size - 1; n >= 0; n--) data |= T(*p++) << n * 8;
- return data;
- }
- template<uint size, typename T> auto writel(void* target, T data) -> void {
- auto p = (uint8_t*)target;
- for(uint n = 0; n < size; n++) *p++ = data >> n * 8;
- }
- template<uint size, typename T> auto writem(void* target, T data) -> void {
- auto p = (uint8_t*)target;
- for(int n = size - 1; n >= 0; n--) *p++ = data >> n * 8;
- }
- }
|