1+ /*
2+ CoreFoundation Allocators
3+
4+ Copyright: Copyright © 2024-2025, Kitsunebi Games EMV
5+ License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6+ Authors: Luna Nielsen
7+ */
8+ module corefoundation.cfallocator ;
9+ import corefoundation.cfstring;
10+ import corefoundation.core;
11+
12+ /**
13+ CFAllocator is an opaque type that allocates and deallocates memory for you.
14+ You never have to allocate, reallocate, or deallocate memory directly for Core Foundation objects—and rarely should you.
15+ You pass CFAllocator objects into functions that create objects; these functions have “Create” embedded in their names,
16+ for example, CFStringCreateWithPascalString. The creation functions use the allocators to allocate memory for the objects they create.
17+ */
18+ alias CFAllocatorRef = CFSubType! (" CFAllocator" );
19+
20+ extern (C ) @nogc nothrow :
21+
22+ /**
23+ A prototype for a function callback that allocates memory of a requested size.
24+ */
25+ alias CFAllocatorAllocateCallBack = const (void )* function (CFIndex allocSize, CFOptionFlags hint, void * info);
26+
27+ /**
28+ A prototype for a function callback that provides a description of the specified data.
29+ */
30+ alias CFAllocatorCopyDescriptionCallBack = CFStringRef function (const (void )* info);
31+
32+ /**
33+ A prototype for a function callback that deallocates a block of memory.
34+ */
35+ alias CFAllocatorDeallocateCallBack = const (void )* function (void * ptr, void * info);
36+
37+ /**
38+ A prototype for a function callback that gives the size of memory likely to be allocated, given a certain request.
39+ */
40+ alias CFAllocatorPreferredSizeCallBack = CFIndex function (CFIndex size, CFOptionFlags hint, void * info);
41+
42+ /**
43+ A prototype for a function callback that reallocates memory of a requested size for an existing block of memory.
44+ */
45+ alias CFAllocatorReallocateCallBack = const (void )* function (void * ptr, CFIndex newsize, CFOptionFlags hint, void * info);
46+
47+ /**
48+ A prototype for a function callback that releases the given data.
49+ */
50+ alias CFAllocatorReleaseCallBack = const (void )* function (const (void )* info);
51+
52+ /**
53+ A prototype for a function callback that retains the given data.
54+ */
55+ alias CFAllocatorRetainCallBack = const (void )* function (const (void )* info);
56+
57+ /**
58+ A structure that defines the context or operating environment for an allocator (CFAllocator) object.
59+ Every Core Foundation allocator object must have a context defined for it.
60+ */
61+ struct CFAllocatorContext {
62+
63+ /**
64+ Assign the version number of the allocator.
65+
66+ Currently the only valid value is 0.
67+ */
68+ CFIndex version_;
69+
70+ /**
71+ An untyped pointer to program-defined data. Allocate memory for this data and assign a pointer to it.
72+ This data is often control information for the allocator.
73+
74+ You may assign [null].
75+ */
76+ void * info;
77+
78+ /**
79+ A prototype for a function callback that retains the data pointed to by the info field.
80+ In implementing this function, retain the data you have defined for the allocator context in this field.
81+ (This might make sense only if the data is a Core Foundation object.)
82+
83+ You may set this function pointer to [null].
84+ */
85+ CFAllocatorRetainCallBack retain;
86+
87+ /**
88+ A prototype for a function callback that releases the data pointed to by the info field.
89+ In implementing this function, release (or free) the data you have defined for the allocator context.
90+
91+ You may set this function pointer to [null], but doing so may result in memory leaks.
92+ */
93+ CFAllocatorReleaseCallBack release;
94+
95+ /**
96+ A prototype for a function callback that provides a description of the data pointed to by the info field.
97+ In implementing this function, return a reference to a CFString object that describes your allocator,
98+ particularly some characteristics of your program-defined data.
99+
100+ You may set this function pointer to [null], in which case Core Foundation will provide a rudimentary description.
101+ */
102+ CFAllocatorCopyDescriptionCallBack copyDescription;
103+
104+ /**
105+ A prototype for a function callback that allocates memory of a requested size.
106+ In implementing this function, allocate a block of memory of at least size bytes and return a pointer to the start of the block.
107+ The hint argument is a bitfield that you should currently not use (that is, assign 0).
108+ The size parameter should always be greater than 0.
109+ If it is not, or if problems in allocation occur, return [null].
110+
111+ You may set this function pointer to [null].
112+ */
113+ CFAllocatorAllocateCallBack allocate;
114+
115+ /**
116+ A prototype for a function callback that reallocates memory of a requested size for an existing block of memory.
117+
118+ You may set this function pointer to [null], in which case the CFAllocatorReallocate(_:_:) function has no effect.
119+ */
120+ CFAllocatorReallocateCallBack reallocate;
121+
122+ /**
123+ A prototype for a function callback that deallocates a given block of memory.
124+ In implementing this function, make the block of memory pointed to by ptr available for subsequent
125+ reuse by the allocator but unavailable for continued use by the program.
126+ The ptr parameter cannot be NULL and if the ptr parameter is not a block of memory that has been
127+ previously allocated by the allocator, the results are undefined; abnormal program termination can occur.
128+
129+ You may set this function pointer to [null], in which case the CFAllocatorDeallocate(_:_:) function has no effect.
130+ */
131+ CFAllocatorDeallocateCallBack deallocate;
132+
133+ /**
134+ A prototype for a function callback that determines whether there is enough free memory to satisfy a request.
135+ In implementing this function, return the actual size the allocator is likely to allocate given a request for a block of memory of size size.
136+
137+ The hint argument is a bitfield that you should currently not use.
138+ */
139+ CFAllocatorPreferredSizeCallBack preferredSize;
140+ }
141+
142+ /**
143+ Returns the type identifier for the CFAllocator opaque type.
144+ */
145+ extern CFTypeID CFAllocatorGetTypeID();
146+
147+ /**
148+ CFAllocatorSetDefault() sets the allocator that is used in the current
149+ thread whenever NULL is specified as an allocator argument. This means
150+ that most, if not all allocations will go through this allocator. It
151+ also means that any allocator set as the default needs to be ready to
152+ deal with arbitrary memory allocation requests; in addition, the size
153+ and number of requests will change between releases.
154+
155+ An allocator set as the default will never be released, even if later
156+ another allocator replaces it as the default. Not only is it impractical
157+ for it to be released (as there might be caches created under the covers
158+ that refer to the allocator), in general it's also safer and more
159+ efficient to keep it around.
160+
161+ If you wish to use a custom allocator in a context, it's best to provide
162+ it as the argument to the various creation functions rather than setting
163+ it as the default. Setting the default allocator is not encouraged.
164+
165+ If you do set an allocator as the default, either do it for all time in
166+ your app, or do it in a nested fashion (by restoring the previous allocator
167+ when you exit your context). The latter might be appropriate for plug-ins
168+ or libraries that wish to set the default allocator.
169+ */
170+ extern void CFAllocatorSetDefault(CFAllocatorRef allocator);
171+
172+ /**
173+ Gets the default allocator
174+ */
175+ extern CFAllocatorRef CFAllocatorGetDefault();
176+
177+ /**
178+ Creates an allocator object.
179+ */
180+ extern CFAllocatorRef CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContext * context);
181+
182+ /**
183+ Allocates memory using the specified allocator.
184+ */
185+ extern void * CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint);
186+
187+ /**
188+ Reallocates memory using the specified allocator.
189+ */
190+ extern void * CFAllocatorReallocate(CFAllocatorRef allocator, void * ptr, CFIndex newsize, CFOptionFlags hint);
191+
192+ /**
193+ Deallocates a block of memory with a given allocator.
194+ */
195+ extern void CFAllocatorDeallocate(CFAllocatorRef allocator, void * ptr);
196+
197+ /**
198+ Obtains the number of bytes likely to be allocated upon a specific request.
199+ */
200+ extern CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint);
201+
202+ /**
203+ Obtains the context of the specified allocator or of the default allocator.
204+ */
205+ extern void CFAllocatorGetContext(CFAllocatorRef allocator, CFAllocatorContext * context);
206+
207+ /**
208+ Returns the allocator used to allocate a Core Foundation object.
209+
210+ Params:
211+ cfObject = The object to get the allocator for.
212+
213+ Returns:
214+ The allocator that owns the object.
215+ */
216+ extern CFAllocatorRef CFGetAllocator(CFTypeRef cfObject);
0 commit comments