Bug 10823 – Aligned malloc functions for C heap

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-15T10:35:00Z
Last change time
2016-04-05T16:53:51Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2013-08-15T10:35:18Z
In some low-level D programs you allocate memory from the C heap, so I suggest to add to Phobos three simple functions similar to the C standard functions that allocate and free memory aligned to the given number of bytes: void* alignedMalloc(in size_t size, in size_t alignment) nothrow; void* alignedCalloc(in size_t num, in size_t size, in size_t alignment) nothrow; void alignedFree(void* ptr) nothrow; The alignedFree doesn't need the alignment value because the allocating function write such value at the start of the memory chunk, just before where the given pointer points. An alternative safer API for alignedCalloc is, this, that turns it into a template: T* alignedCalloc(T)(in size_t num, in size_t alignment) nothrow; Such functions are meant to be used mostly for arrays in SIMD code, that needs data aligned to 16 bytes (SSE 3), 32 bytes (AVX), 64 bytes (Intel MIC), or more. They avoid the D programmer to write such functions over and over in D projects, that can also contain bugs. A standard library is meant to contain similar small functions that are sometimes useful.
Comment #1 by jack — 2016-04-05T16:53:51Z
This is satisfied by std.experimental.allocator.mallocator : AlignedMallocator