1 /* 2 * Copyright 2020 Axel Davy <davyaxel0@gmail.com> 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * on the rights to use, copy, modify, merge, publish, distribute, sub 8 * license, and/or sell copies of the Software, and to permit persons to whom 9 * the Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 22 23 #ifndef _NINE_MEMORY_HELPER_H_ 24 #define _NINE_MEMORY_HELPER_H_ 25 26 27 struct NineDevice9; 28 29 struct nine_allocator; 30 struct nine_allocation; 31 32 /* Note: None of these functions are thread safe, thus the worker thread is disallowed 33 * to call any of them. Only exception is nine_free_worker reserved for it. */ 34 35 struct nine_allocation * 36 nine_allocate(struct nine_allocator *allocator, unsigned size); 37 38 /* Note: Suballocations MUST be freed before their parent */ 39 void nine_free(struct nine_allocator *allocator, struct nine_allocation *allocation); 40 void nine_free_worker(struct nine_allocator *allocator, struct nine_allocation *allocation); 41 42 void *nine_get_pointer(struct nine_allocator *allocator, struct nine_allocation *allocation); 43 44 /* We don't need the pointer anymore, but we are likely to need it again soon */ 45 void nine_pointer_weakrelease(struct nine_allocator *allocator, struct nine_allocation *allocation); 46 47 /* We don't need the pointer anymore, probably for a long time */ 48 void nine_pointer_strongrelease(struct nine_allocator *allocator, struct nine_allocation *allocation); 49 50 /* You can strong release when counter becomes 0. 51 * Once a counter is used for a given allocation, the same must keep being used */ 52 void nine_pointer_delayedstrongrelease(struct nine_allocator *allocator, 53 struct nine_allocation *allocation, 54 unsigned *counter); 55 56 /* Note: It is disallowed to release a suballocation before its parent. 57 * It is disallowed to suballocate on a suballocation. */ 58 struct nine_allocation * 59 nine_suballocate(struct nine_allocator* allocator, struct nine_allocation *allocation, int offset); 60 61 /* Won't be freed - but at least we can use the same interface */ 62 struct nine_allocation * 63 nine_wrap_external_pointer(struct nine_allocator* allocator, void* data); 64 65 66 /* memfd_virtualsizelimit: Limit for the virtual memory usage (in MB) 67 * above which memfd files are unmapped (to reduce virtual memory usage). 68 * If negative, disables memfd usage. */ 69 struct nine_allocator * 70 nine_allocator_create(struct NineDevice9 *device, int memfd_virtualsizelimit); 71 72 void 73 nine_allocator_destroy(struct nine_allocator *allocator); 74 75 #endif /* _NINE_MEMORY_HELPER_H_ */ 76