From 5804c5d345e96e324c69916b608630b449fe85e1 Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 28 Jun 2026 14:03:58 +0100 Subject: [PATCH] Add function to check memory op support for allocators --- VERSION | 2 +- src/base/mem/allocator/mem_allocator.c | 45 +++++++++++++++++++++++++- src/base/mem/allocator/mem_allocator.h | 11 +++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 38f77a6..7ec1d6d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.0.1 +2.1.0 diff --git a/src/base/mem/allocator/mem_allocator.c b/src/base/mem/allocator/mem_allocator.c index 216d863..1fd317c 100644 --- a/src/base/mem/allocator/mem_allocator.c +++ b/src/base/mem/allocator/mem_allocator.c @@ -7,29 +7,72 @@ void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size) { wpDebugAssert(allocator != NULL && (allocator->alloc) != NULL, "`allocator` and `allocator->alloc` should not be NULL"); + + if (wpMemAllocatorOpSupported(allocator, WP_MEM_OP_ALLOC)) { + return NULL; + } + return allocator->alloc(size, allocator->obj); } void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment) { wpDebugAssert(allocator != NULL && (allocator->alloc_aligned) != NULL, "`allocator` and `allocator->alloc_aligned` should not be NULL"); + + if (wpMemAllocatorOpSupported(allocator, WP_MEM_OP_ALLOC_ALIGNED)) { + return NULL; + } + return allocator->alloc_aligned(size, alignment, allocator->obj); } void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size) { wpDebugAssert(allocator != NULL && (allocator->realloc) != NULL, "`allocator` and `allocator->realloc` should not be NULL"); + + if (wpMemAllocatorOpSupported(allocator, WP_MEM_OP_REALLOC)) { + return NULL; + } + return allocator->realloc(ptr, old_size, new_size, allocator->obj); } void *wpMemAllocatorReallocAligned(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size, u64 alignment) { wpDebugAssert(allocator != NULL && (allocator->realloc_aligned) != NULL, "`allocator` and `allocator->realloc_aligned` should not be NULL"); + + if (wpMemAllocatorOpSupported(allocator, WP_MEM_OP_REALLOC_ALIGNED)) { + return NULL; + } + return allocator->realloc_aligned(ptr, old_size, new_size, alignment, allocator->obj); } void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size) { - if (!allocator || !(allocator->free)) { + wpDebugAssert(allocator != NULL && (allocator->free) != NULL, "`allocator` and `allocator->free` should not be NULL"); + + if (wpMemAllocatorOpSupported(allocator, WP_MEM_OP_FREE)) { return; } allocator->free(ptr, size, allocator->obj); } + +b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) { + wpDebugAssert(allocator != NULL, "`allocator` should not be NULL"); + + switch (op) { + case WP_MEM_OP_ALLOC: + return allocator->alloc != NULL; + case WP_MEM_OP_ALLOC_ALIGNED: + return allocator->alloc_aligned != NULL; + case WP_MEM_OP_REALLOC: + return allocator->realloc != NULL; + case WP_MEM_OP_REALLOC_ALIGNED: + return allocator->realloc_aligned != NULL; + case WP_MEM_OP_FREE: + return allocator->free != NULL; + default: + break; + } + + return false; +} diff --git a/src/base/mem/allocator/mem_allocator.h b/src/base/mem/allocator/mem_allocator.h index ac3940f..ea6e36f 100644 --- a/src/base/mem/allocator/mem_allocator.h +++ b/src/base/mem/allocator/mem_allocator.h @@ -11,6 +11,16 @@ BEGIN_C_LINKAGE #endif // !WP_PLATFORM_CPP +typedef enum { + WP_MEM_OP_ALLOC, + WP_MEM_OP_ALLOC_ALIGNED, + WP_MEM_OP_REALLOC, + WP_MEM_OP_REALLOC_ALIGNED, + WP_MEM_OP_FREE, + + COUNT_MEM_OPS +} WpMemOp; + typedef void *(WpMemAllocFunc)(u64 size, void *alloc_obj); typedef void *(WpMemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj); typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj); @@ -42,6 +52,7 @@ void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_siz void *wpMemAllocatorReallocAligned(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size, u64 alignment); void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size); +b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op); #ifdef WP_PLATFORM_CPP END_C_LINKAGE