memory.h (2168B)
1 #pragma once 2 3 #include <stdbool.h> 4 #include <stdint.h> // IWYU pragma: keep 5 #include <string.h> 6 #include <time.h> // IWYU pragma: keep 7 8 #include "auto/config.h" 9 #include "nvim/macros_defs.h" 10 #include "nvim/memory_defs.h" // IWYU pragma: keep 11 12 /// `malloc()` function signature 13 typedef void *(*MemMalloc)(size_t); 14 15 /// `free()` function signature 16 typedef void (*MemFree)(void *); 17 18 /// `calloc()` function signature 19 typedef void *(*MemCalloc)(size_t, size_t); 20 21 /// `realloc()` function signature 22 typedef void *(*MemRealloc)(void *, size_t); 23 24 #ifdef UNIT_TESTING 25 /// When unit testing: pointer to the `malloc()` function, may be altered 26 extern MemMalloc mem_malloc; 27 28 /// When unit testing: pointer to the `free()` function, may be altered 29 extern MemFree mem_free; 30 31 /// When unit testing: pointer to the `calloc()` function, may be altered 32 extern MemCalloc mem_calloc; 33 34 /// When unit testing: pointer to the `realloc()` function, may be altered 35 extern MemRealloc mem_realloc; 36 #endif 37 38 #ifdef EXITFREE 39 /// Indicates that free_all_mem function was or is running 40 extern bool entered_free_all_mem; 41 #endif 42 43 typedef void *(*MergeSortGetFunc)(void *); 44 typedef void (*MergeSortSetFunc)(void *, void *); 45 typedef int (*MergeSortCompareFunc)(const void *, const void *); 46 47 EXTERN size_t arena_alloc_count INIT( = 0); 48 49 #define kv_fixsize_arena(a, v, s) \ 50 ((v).capacity = (s), \ 51 (v).items = (void *)arena_alloc(a, sizeof((v).items[0]) * (v).capacity, true)) 52 53 #include "memory.h.generated.h" 54 55 #define XFREE_CLEAR(ptr) \ 56 do { \ 57 /* Take the address to avoid double evaluation. #1375 */ \ 58 void **ptr_ = (void **)&(ptr); \ 59 xfree(*ptr_); \ 60 /* coverity[dead-store] */ \ 61 *ptr_ = NULL; \ 62 (void)(*ptr_); \ 63 } while (0) 64 65 #define CLEAR_FIELD(field) memset(&(field), 0, sizeof(field)) 66 #define CLEAR_POINTER(ptr) memset((ptr), 0, sizeof(*(ptr))) 67 68 #ifndef HAVE_STRNLEN 69 # define strnlen xstrnlen // Older versions of SunOS may not have strnlen 70 #endif 71 72 #define STRCPY(d, s) strcpy((char *)(d), (char *)(s)) // NOLINT(runtime/printf) 73 74 // Like strcpy() but allows overlapped source and destination. 75 #define STRMOVE(d, s) memmove((d), (s), strlen(s) + 1)