1 /* 2 * Copyright 2020 Axel Davy <[email protected]> 3 * SPDX-License-Identifier: MIT 4 */ 5 6 #ifndef _NINE_MEMORY_HELPER_H_ 7 #define _NINE_MEMORY_HELPER_H_ 8 9 10 struct NineDevice9; 11 12 struct nine_allocator; 13 struct nine_allocation; 14 15 /* Note: None of these functions are thread safe, thus the worker thread is disallowed 16 * to call any of them. Only exception is nine_free_worker reserved for it. */ 17 18 struct nine_allocation * 19 nine_allocate(struct nine_allocator *allocator, unsigned size); 20 21 /* Note: Suballocations MUST be freed before their parent */ 22 void nine_free(struct nine_allocator *allocator, struct nine_allocation *allocation); 23 void nine_free_worker(struct nine_allocator *allocator, struct nine_allocation *allocation); 24 25 void *nine_get_pointer(struct nine_allocator *allocator, struct nine_allocation *allocation); 26 27 /* We don't need the pointer anymore, but we are likely to need it again soon */ 28 void nine_pointer_weakrelease(struct nine_allocator *allocator, struct nine_allocation *allocation); 29 30 /* We don't need the pointer anymore, probably for a long time */ 31 void nine_pointer_strongrelease(struct nine_allocator *allocator, struct nine_allocation *allocation); 32 33 /* You can strong release when counter becomes 0. 34 * Once a counter is used for a given allocation, the same must keep being used */ 35 void nine_pointer_delayedstrongrelease(struct nine_allocator *allocator, 36 struct nine_allocation *allocation, 37 unsigned *counter); 38 39 /* Note: It is disallowed to release a suballocation before its parent. 40 * It is disallowed to suballocate on a suballocation. */ 41 struct nine_allocation * 42 nine_suballocate(struct nine_allocator* allocator, struct nine_allocation *allocation, int offset); 43 44 /* Won't be freed - but at least we can use the same interface */ 45 struct nine_allocation * 46 nine_wrap_external_pointer(struct nine_allocator* allocator, void* data); 47 48 49 /* memfd_virtualsizelimit: Limit for the virtual memory usage (in MB) 50 * above which memfd files are unmapped (to reduce virtual memory usage). 51 * If negative, disables memfd usage. */ 52 struct nine_allocator * 53 nine_allocator_create(struct NineDevice9 *device, int memfd_virtualsizelimit); 54 55 void 56 nine_allocator_destroy(struct nine_allocator *allocator); 57 58 #endif /* _NINE_MEMORY_HELPER_H_ */ 59