Branch data Line data Source code
1 : : // Copyright 2011-2021 David Robillard <d@drobilla.net> 2 : : // SPDX-License-Identifier: ISC 3 : : 4 : : #include <zix/allocator.h> 5 : : 6 : : #include "zix_config.h" 7 : : 8 : : #include <zix/attributes.h> 9 : : 10 : : #ifdef _WIN32 11 : : # include <malloc.h> 12 : : # include <windows.h> 13 : : #endif 14 : : 15 : : #include <stdlib.h> 16 : : 17 : : ZIX_MALLOC_FUNC static void* 18 : 440 : zix_default_malloc(ZixAllocator* const allocator, const size_t size) 19 : : { 20 : : (void)allocator; 21 : 440 : return malloc(size); 22 : : } 23 : : 24 : : ZIX_MALLOC_FUNC static void* 25 : 603626 : zix_default_calloc(ZixAllocator* const allocator, 26 : : const size_t nmemb, 27 : : const size_t size) 28 : : { 29 : : (void)allocator; 30 : 603626 : return calloc(nmemb, size); 31 : : } 32 : : 33 : : static void* 34 : 34 : zix_default_realloc(ZixAllocator* const allocator, 35 : : void* const ptr, 36 : : const size_t size) 37 : : { 38 : : (void)allocator; 39 : 34 : return realloc(ptr, size); 40 : : } 41 : : 42 : : static void 43 : 603001 : zix_default_free(ZixAllocator* const allocator, void* const ptr) 44 : : { 45 : : (void)allocator; 46 : 603001 : free(ptr); 47 : 603001 : } 48 : : 49 : : ZIX_MALLOC_FUNC static void* 50 : 6923 : zix_default_aligned_alloc(ZixAllocator* const allocator, 51 : : const size_t alignment, 52 : : const size_t size) 53 : : { 54 : : (void)allocator; 55 : : 56 : : #ifdef _WIN32 57 : : return _aligned_malloc(size, alignment); 58 : : #elif USE_POSIX_MEMALIGN 59 : 6923 : void* ptr = NULL; 60 [ + - ]: 6923 : return posix_memalign(&ptr, alignment, size) ? NULL : ptr; 61 : : #else 62 : : (void)alignment; 63 : : (void)size; 64 : : return NULL; 65 : : #endif 66 : : } 67 : : 68 : : static void 69 : 7026 : zix_default_aligned_free(ZixAllocator* const allocator, void* const ptr) 70 : : { 71 : : (void)allocator; 72 : : 73 : : #ifdef _WIN32 74 : : _aligned_free(ptr); 75 : : #else 76 : 7026 : free(ptr); 77 : : #endif 78 : 7026 : } 79 : : 80 : : ZixAllocator* 81 : 1221289 : zix_default_allocator(void) 82 : : { 83 : : static ZixAllocator default_allocator = { 84 : : zix_default_malloc, 85 : : zix_default_calloc, 86 : : zix_default_realloc, 87 : : zix_default_free, 88 : : zix_default_aligned_alloc, 89 : : zix_default_aligned_free, 90 : : }; 91 : : 92 : 1221289 : return &default_allocator; 93 : : }