| 1 | #pragma once |
| 2 | |
| 3 | #include <uacpi/internal/types.h> |
| 4 | #include <uacpi/internal/helpers.h> |
| 5 | #include <uacpi/platform/libc.h> |
| 6 | #include <uacpi/platform/config.h> |
| 7 | #include <uacpi/kernel_api.h> |
| 8 | |
| 9 | #ifdef UACPI_USE_BUILTIN_STRING |
| 10 | |
| 11 | #ifndef uacpi_memcpy |
| 12 | void *uacpi_memcpy(void *dest, const void *src, uacpi_size count); |
| 13 | #endif |
| 14 | |
| 15 | #ifndef uacpi_memmove |
| 16 | void *uacpi_memmove(void *dest, const void *src, uacpi_size count); |
| 17 | #endif |
| 18 | |
| 19 | #ifndef uacpi_memset |
| 20 | void *uacpi_memset(void *dest, uacpi_i32 ch, uacpi_size count); |
| 21 | #endif |
| 22 | |
| 23 | #ifndef uacpi_memcmp |
| 24 | uacpi_i32 uacpi_memcmp(const void *lhs, const void *rhs, uacpi_size count); |
| 25 | #endif |
| 26 | |
| 27 | #else |
| 28 | |
| 29 | #ifndef uacpi_memcpy |
| 30 | #ifdef UACPI_COMPILER_HAS_BUILTIN_MEMCPY |
| 31 | #define uacpi_memcpy __builtin_memcpy |
| 32 | #else |
| 33 | extern void *memcpy(void *dest, const void *src, uacpi_size count); |
| 34 | #define uacpi_memcpy memcpy |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #ifndef uacpi_memmove |
| 39 | #ifdef UACPI_COMPILER_HAS_BUILTIN_MEMMOVE |
| 40 | #define uacpi_memmove __builtin_memmove |
| 41 | #else |
| 42 | extern void *memmove(void *dest, const void *src, uacpi_size count); |
| 43 | #define uacpi_memmove memmove |
| 44 | #endif |
| 45 | #endif |
| 46 | |
| 47 | #ifndef uacpi_memset |
| 48 | #ifdef UACPI_COMPILER_HAS_BUILTIN_MEMSET |
| 49 | #define uacpi_memset __builtin_memset |
| 50 | #else |
| 51 | extern void *memset(void *dest, int ch, uacpi_size count); |
| 52 | #define uacpi_memset memset |
| 53 | #endif |
| 54 | #endif |
| 55 | |
| 56 | #ifndef uacpi_memcmp |
| 57 | #ifdef UACPI_COMPILER_HAS_BUILTIN_MEMCMP |
| 58 | #define uacpi_memcmp __builtin_memcmp |
| 59 | #else |
| 60 | extern int memcmp(const void *lhs, const void *rhs, uacpi_size count); |
| 61 | #define uacpi_memcmp memcmp |
| 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | #endif |
| 66 | |
| 67 | #ifndef uacpi_strlen |
| 68 | uacpi_size uacpi_strlen(const uacpi_char *str); |
| 69 | #endif |
| 70 | |
| 71 | #ifndef uacpi_strnlen |
| 72 | uacpi_size uacpi_strnlen(const uacpi_char *str, uacpi_size max); |
| 73 | #endif |
| 74 | |
| 75 | #ifndef uacpi_strcmp |
| 76 | uacpi_i32 uacpi_strcmp(const uacpi_char *lhs, const uacpi_char *rhs); |
| 77 | #endif |
| 78 | |
| 79 | #ifndef uacpi_snprintf |
| 80 | UACPI_PRINTF_DECL(3, 4) |
| 81 | uacpi_i32 uacpi_snprintf( |
| 82 | uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, ... |
| 83 | ); |
| 84 | #endif |
| 85 | |
| 86 | #ifndef uacpi_vsnprintf |
| 87 | uacpi_i32 uacpi_vsnprintf( |
| 88 | uacpi_char *buffer, uacpi_size capacity, const uacpi_char *fmt, |
| 89 | uacpi_va_list vlist |
| 90 | ); |
| 91 | #endif |
| 92 | |
| 93 | #ifdef UACPI_SIZED_FREES |
| 94 | #define uacpi_free(mem, size) uacpi_kernel_free(mem, size) |
| 95 | #else |
| 96 | #define uacpi_free(mem, _) uacpi_kernel_free(mem) |
| 97 | #endif |
| 98 | |
| 99 | #define uacpi_memzero(ptr, size) uacpi_memset(ptr, 0, size) |
| 100 | |
| 101 | #define UACPI_COMPARE(x, y, op) ((x) op (y) ? (x) : (y)) |
| 102 | #define UACPI_MIN(x, y) UACPI_COMPARE(x, y, <) |
| 103 | #define UACPI_MAX(x, y) UACPI_COMPARE(x, y, >) |
| 104 | |
| 105 | #define UACPI_ALIGN_UP_MASK(x, mask) (((x) + (mask)) & ~(mask)) |
| 106 | #define UACPI_ALIGN_UP(x, val, type) UACPI_ALIGN_UP_MASK(x, (type)(val) - 1) |
| 107 | |
| 108 | #define UACPI_ALIGN_DOWN_MASK(x, mask) ((x) & ~(mask)) |
| 109 | #define UACPI_ALIGN_DOWN(x, val, type) UACPI_ALIGN_DOWN_MASK(x, (type)(val) - 1) |
| 110 | |
| 111 | #define UACPI_IS_ALIGNED_MASK(x, mask) (((x) & (mask)) == 0) |
| 112 | #define UACPI_IS_ALIGNED(x, val, type) UACPI_IS_ALIGNED_MASK(x, (type)(val) - 1) |
| 113 | |
| 114 | #define UACPI_IS_POWER_OF_TWO(x, type) UACPI_IS_ALIGNED(x, x, type) |
| 115 | |
| 116 | void uacpi_memcpy_zerout(void *dst, const void *src, |
| 117 | uacpi_size dst_size, uacpi_size src_size); |
| 118 | |
| 119 | // Returns the one-based bit location of LSb or 0 |
| 120 | uacpi_u8 uacpi_bit_scan_forward(uacpi_u64); |
| 121 | |
| 122 | // Returns the one-based bit location of MSb or 0 |
| 123 | uacpi_u8 uacpi_bit_scan_backward(uacpi_u64); |
| 124 | |
| 125 | #ifndef UACPI_NATIVE_ALLOC_ZEROED |
| 126 | void *uacpi_builtin_alloc_zeroed(uacpi_size size); |
| 127 | #define uacpi_kernel_alloc_zeroed uacpi_builtin_alloc_zeroed |
| 128 | #endif |
| 129 | |