www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10823] New: Aligned malloc functions for C heap

http://d.puremagic.com/issues/show_bug.cgi?id=10823

           Summary: Aligned malloc functions for C heap
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-08-15 10:35:18 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 15 2013