2 module std.experimental.allocator.mallocator;
3 import std.experimental.allocator.common;
10 @system unittest { testAllocator!(() => Mallocator.instance); }
13 The alignment is a static constant equal to $(D platformAlignment), which
14 ensures proper alignment for any D data type.
16 enum uint alignment = platformAlignment;
19 Standard allocator methods per the semantics defined above. The
20 $(D deallocate) and $(D reallocate) methods are $(D @system) because they
21 may move memory around, leaving dangling pointers in user code. Somewhat
22 paradoxically, $(D malloc) is $(D @safe) but that's only useful to safe
23 programs that can afford to leak memory allocated.
25 @trusted @nogc nothrow
26 void[] allocate(size_t bytes) shared
28 import core.stdc.stdlib : malloc;
29 if (!bytes) return null;
30 auto p = malloc(bytes);
31 return p ? p[0 .. bytes] : null;
36 bool deallocate(void[] b) shared
38 import core.stdc.stdlib : free;
45 bool reallocate(ref void[] b, size_t s) shared
47 import core.stdc.stdlib : realloc;
50 // fuzzy area in the C standard, see http://goo.gl/ZpWeSE
51 // so just deallocate and nullify the pointer
56 auto p = cast(ubyte*) realloc(b.ptr, s);
63 Returns the global instance of this allocator type. The C heap allocator is
64 thread-safe, therefore all of its methods and `it` itself are
67 static shared Mallocator instance;
74 auto buffer = Mallocator.instance.allocate(1024 * 1024 * 4);
75 scope(exit) Mallocator.instance.deallocate(buffer);
86 p = cast(int*) A.instance.allocate(int.sizeof);
87 scope(exit) A.instance.deallocate(p[0 .. int.sizeof]);
99 import std.experimental.allocator : make;
101 p = A.instance.make!Object();
110 // DMD Win 32 bit, DigitalMars C standard library misses the _aligned_xxx
111 // functions family (snn.lib)
112 version (CRuntime_DigitalMars)
114 // Helper to cast the infos written before the aligned pointer
115 // this header keeps track of the size (required to realloc) and of
116 // the base ptr (required to free).
117 private struct AlignInfo
123 static AlignInfo* opCall(void* ptr)
125 return cast(AlignInfo*) (ptr - AlignInfo.sizeof);
130 private void* _aligned_malloc(size_t size, size_t alignment)
132 import core.stdc.stdlib : malloc;
133 size_t offset = alignment + size_t.sizeof * 2 - 1;
136 void* basePtr = malloc(size + offset);
137 if (!basePtr) return null;
139 // get aligned location within the chunk
140 void* alignedPtr = cast(void**)((cast(size_t)(basePtr) + offset)
143 // write the header before the aligned pointer
144 AlignInfo* head = AlignInfo(alignedPtr);
145 head.basePtr = basePtr;
152 private void* _aligned_realloc(void* ptr, size_t size, size_t alignment)
154 import core.stdc.stdlib : free;
155 import core.stdc.string : memcpy;
157 if (!ptr) return _aligned_malloc(size, alignment);
159 // gets the header from the exising pointer
160 AlignInfo* head = AlignInfo(ptr);
162 // gets a new aligned pointer
163 void* alignedPtr = _aligned_malloc(size, alignment);
166 //to https://msdn.microsoft.com/en-us/library/ms235462.aspx
167 //see Return value: in this case the original block is unchanged
172 memcpy(alignedPtr, ptr, head.size);
179 private void _aligned_free(void *ptr)
181 import core.stdc.stdlib : free;
183 AlignInfo* head = AlignInfo(ptr);
188 // DMD Win 64 bit, uses microsoft standard C library which implements them
191 @nogc nothrow private extern(C) void* _aligned_malloc(size_t, size_t);
192 @nogc nothrow private extern(C) void _aligned_free(void *memblock);
193 @nogc nothrow private extern(C) void* _aligned_realloc(void *, size_t, size_t);
198 Aligned allocator using OS-specific primitives, under a uniform API.
200 struct AlignedMallocator
202 @system unittest { testAllocator!(() => typeof(this).instance); }
205 The default alignment is $(D platformAlignment).
207 enum uint alignment = platformAlignment;
210 Forwards to $(D alignedAllocate(bytes, platformAlignment)).
212 @trusted @nogc nothrow
213 void[] allocate(size_t bytes) shared
215 if (!bytes) return null;
216 return alignedAllocate(bytes, alignment);
220 Uses $(HTTP man7.org/linux/man-pages/man3/posix_memalign.3.html,
221 $(D posix_memalign)) on Posix and
222 $(HTTP msdn.microsoft.com/en-us/library/8z34s9c6(v=vs.80).aspx,
223 $(D __aligned_malloc)) on Windows.
226 @trusted @nogc nothrow
227 void[] alignedAllocate(size_t bytes, uint a) shared
229 import core.stdc.errno : ENOMEM, EINVAL;
230 import core.sys.posix.stdlib : posix_memalign;
231 assert(a.isGoodDynamicAlignment);
233 auto code = posix_memalign(&result, a, bytes);
237 else if (code == EINVAL)
239 assert(0, "AlignedMallocator.alignment is not a power of two "
240 ~"multiple of (void*).sizeof, according to posix_memalign!");
243 assert(0, "posix_memalign returned an unknown code!");
246 return result[0 .. bytes];
248 else version (Windows)
249 @trusted @nogc nothrow
250 void[] alignedAllocate(size_t bytes, uint a) shared
252 auto result = _aligned_malloc(bytes, a);
253 return result ? result[0 .. bytes] : null;
255 else static assert(0);
258 Calls $(D free(b.ptr)) on Posix and
259 $(HTTP msdn.microsoft.com/en-US/library/17b5h8td(v=vs.80).aspx,
260 $(D __aligned_free(b.ptr))) on Windows.
263 @system @nogc nothrow
264 bool deallocate(void[] b) shared
266 import core.stdc.stdlib : free;
270 else version (Windows)
271 @system @nogc nothrow
272 bool deallocate(void[] b) shared
274 _aligned_free(b.ptr);
277 else static assert(0);
280 On Posix, forwards to $(D realloc). On Windows, forwards to
281 $(D alignedReallocate(b, newSize, platformAlignment)).
284 @system @nogc nothrow
285 bool reallocate(ref void[] b, size_t newSize) shared
287 return Mallocator.instance.reallocate(b, newSize);
290 @system @nogc nothrow
291 bool reallocate(ref void[] b, size_t newSize) shared
293 return alignedReallocate(b, newSize, alignment);
297 On Posix, uses $(D alignedAllocate) and copies data around because there is
298 no realloc for aligned memory. On Windows, calls
299 $(HTTP msdn.microsoft.com/en-US/library/y69db7sx(v=vs.80).aspx,
300 $(D __aligned_realloc(b.ptr, newSize, a))).
303 @system @nogc nothrow
304 bool alignedReallocate(ref void[] b, size_t s, uint a) shared
312 auto p = cast(ubyte*) _aligned_realloc(b.ptr, s, a);
313 if (!p) return false;
319 Returns the global instance of this allocator type. The C heap allocator is
320 thread-safe, therefore all of its methods and `instance` itself are
323 static shared AlignedMallocator instance;
330 auto buffer = AlignedMallocator.instance.alignedAllocate(1024 * 1024 * 4,
332 scope(exit) AlignedMallocator.instance.deallocate(buffer);
336 version (unittest) version (CRuntime_DigitalMars)
338 size_t addr(ref void* ptr) { return cast(size_t) ptr; }
340 version (CRuntime_DigitalMars)
346 m = _aligned_malloc(16, 0x10);
349 assert((m.addr & 0xF) == 0);
353 m = _aligned_malloc(16, 0x100);
356 assert((m.addr & 0xFF) == 0);
360 m = _aligned_malloc(16, 0x1000);
363 assert((m.addr & 0xFFF) == 0);
367 m = _aligned_malloc(16, 0x10);
370 assert((cast(size_t) m & 0xF) == 0);
371 m = _aligned_realloc(m, 32, 0x10000);
372 if (m) assert((m.addr & 0xFFFF) == 0);
376 m = _aligned_malloc(8, 0x10);
379 *cast(ulong*) m = 0X01234567_89ABCDEF;
380 m = _aligned_realloc(m, 0x800, 0x1000);
381 if (m) assert(*cast(ulong*) m == 0X01234567_89ABCDEF);