Loading...
Searching...
No Matches
Allocator.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#ifndef ULTRALIGHT_ALLOCATOR_H
9#define ULTRALIGHT_ALLOCATOR_H
10
11#include <stddef.h>
12#include <stdbool.h>
13#include <Ultralight/Exports.h>
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19///
20/// User-defined allocator interface.
21///
22/// @pre This API is only available in the Pro edition when the UL_ENABLE_ALLOCATOR_OVERRIDE
23/// build option is enabled.
24///
25/// The library uses this to allocate memory. You can override the default allocator functions
26/// by setting the ulAllocator object with your own functions.
27///
28/// This should be done before calling any other library functions.
29///
30/// @see ulAllocator
31///
32typedef struct {
33 //
34 // Allocate a block of memory of at least |bytes| size.
35 //
36 void* (*malloc)(size_t bytes);
37
38 //
39 // Reallocate a block of memory to at least |bytes| size.
40 //
41 void* (*realloc)(void* address, size_t bytes);
42
43 //
44 // Free a block of memory allocated with malloc or realloc.
45 //
46 void (*free)(void* address);
47
48 //
49 // Allocate a block of memory of at least |bytes| size, aligned to |alignment|.
50 //
51 void* (*aligned_malloc)(size_t bytes, size_t alignment);
52
53 //
54 // Reallocate a block of memory to at least |bytes| size, aligned to |alignment|.
55 //
56 void* (*aligned_realloc)(void* address, size_t bytes, size_t alignment);
57
58 //
59 // Free a block of memory allocated with aligned_malloc or aligned_realloc.
60 //
61 void (*aligned_free)(void* address);
62
63 //
64 // Get the size of the memory block that backs the allocation at |address|. The memory
65 // block size is always at least as large as the allocation it backs, and may be larger.
66 // * Windows equivalent: _msize
67 // * POSIX equivalent: malloc_size
68 //
69 size_t (*get_size_estimate)(void* address);
70
72
73///
74/// Get the allocator interface object for the library.
75///
76/// @pre This API is only available in the Pro edition when the UL_ENABLE_ALLOCATOR_OVERRIDE
77/// build option is enabled.
78///
79/// The C functions set in this object will be used for allocating memory inside the library
80/// when the `UL_ENABLE_ALLOCATOR_OVERRIDE` build option is enabled.
81///
82/// Default functions are already set for all of these but you can override them with your own.
83///
84/// Platform specific notes:
85/// * __Windows__: The default functions use `HeapAlloc` / `HeapReAlloc` / `HeapFree`.
86///
88
89#ifdef _WIN32
90///
91/// Get the handle to the private heap used by the library.
92///
93/// This is the handle returned by `HeapCreate()`, you should destroy it after unloading the library
94/// by calling `HeapDestroy()`.
95///
96/// This is only valid if the UL_ENABLE_ALLOCATOR_OVERRIDE build option is enabled and the default
97/// functions are set in the ulAllocator object.
98///
99UCExport void* ulGetHeapHandle();
100#endif
101
102#ifdef __cplusplus
103}
104#endif
105
106#endif // ULTRALIGHT_ALLOCATOR_H
UCExport ULAllocator ulAllocator
Get the allocator interface object for the library.
#define UCExport
Definition Exports.h:44
User-defined allocator interface.
Definition Allocator.h:32