From f6d3090f7fa76eb8e0dbe060b8490cb49c001c39 Mon Sep 17 00:00:00 2001 From: nasr Date: Mon, 5 Oct 2026 22:02:56 +0200 Subject: init: base --- source/base/base_memory.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 source/base/base_memory.h (limited to 'source/base/base_memory.h') diff --git a/source/base/base_memory.h b/source/base/base_memory.h new file mode 100644 index 0000000..eb9ac85 --- /dev/null +++ b/source/base/base_memory.h @@ -0,0 +1,42 @@ +#ifndef BASE_MEMORY_H +#define BASE_MEMORY_H + +//- asan + +// Marks memory region [addr, addr+size) as unaddressable. +// This memory must be previously allocated by the user program. Accessing +// addresses in this region from instrumented code is forbidden until +// this region is unpoisoned. This function is not guaranteed to poison +// the whole region - it may poison only subregion of [addr, addr+size) due +// to ASan alignment restrictions. +// Method is NOT thread-safe in the sense that no two threads can +// (un)poison memory in the same memory region simultaneously. +void __asan_poison_memory_region(void const volatile *addr, size_t size); +// Marks memory region [addr, addr+size) as addressable. +// This memory must be previously allocated by the user program. Accessing +// addresses in this region is allowed until this region is poisoned again. +// This function may unpoison a superregion of [addr, addr+size) due to +// ASan alignment restrictions. +// Method is NOT thread-safe in the sense that no two threads can +// (un)poison memory in the same memory region simultaneously. +void __asan_unpoison_memory_region(void const volatile *addr, size_t size); + +// User code should use macros instead of functions. +#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) +#define ASAN_POISON_MEMORY_REGION(addr, size) __asan_poison_memory_region((addr), (size)) +#define ASAN_UNPOISON_MEMORY_REGION(addr, size) __asan_unpoison_memory_region((addr), (size)) +#else +#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) +#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) +#endif + +//- helper functions + +#define KiB(n) (((u64)(n)) << 10) +#define MiB(n) (((u64)(n)) << 20) +#define GiB(n) (((u64)(n)) << 30) + +internal b32 is_pow(umm x); +internal u64 align(u64 pointer, umm alignment); + +#endif // BASE_MEMORY_H -- cgit v1.3.1