# Aligned memory and posix\_memalign

**URL:** <https://www.ruby-forum.com/t/aligned-memory-and-posix-memalign/204142>\
**Category:** GNU Radio\
**Created:** [March 6, 2011, 3:50pm UTC](https://www.ruby-forum.com/t/aligned-memory-and-posix-memalign/204142 "2011-03-06T15:50:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 6, 2011, 3:50pm UTC](https://www.ruby-forum.com/t/aligned-memory-and-posix-memalign/204142/1 "2011-03-06T15:50:19Z")

</div>

A worrisome (to me) issue with the volk library is the use of  
posix\_memalign() in the qa tests. The worry is:

- posix\_memalign() will never work on Windows with Microsoft’s runtime  
library \*

That means that code that uses posix\_memalign won’t work on Windows with  
MinGW or Microsoft native compilers. (The problem is not that memory  
alignment is difficult; it is simply that there is no memory-aligned  
version  
of malloc() in the Microsoft API that works with free().)

At present, posix\_memalign is used in GNU Radio in machine-dependent  
code  
(gcell, altivec, and arm7) and in the volk qa tests. If we can replace  
it  
in volk and avoid its spread, we can continue to use GNU Radio on  
Windows.

One option is to use something like malloc16Align/free16Align (in  
malloc16.{h,c}) in gnuradio-core. Can we structure the code to make  
this  
available in volk, and anywhere else it is needed? We can add a version  
to  
do more general alignment if needed. Would it help to wrap aligned  
buffers  
in a C++ class to make it easier to catch errors (such as using free()  
on a  
malloc16Align() buffer)?

Note that simply enhancing our own missing/posix\_memalign function using  
\_mingw\_aligned\_malloc() or Microsofts’s \_aligned\_malloc() won’t work  
because  
memory allocated with those cannot be released with free().

Trying not to cause too much trouble,

– Don W.

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [March 6, 2011, 6:12pm UTC](https://www.ruby-forum.com/t/aligned-memory-and-posix-memalign/204142/2 "2011-03-06T18:12:17Z")

</div>

On Sun, Mar 6, 2011 at 9:48 AM, Don W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Note that simply enhancing our own missing/posix\_memalign function using  
> \_mingw\_aligned\_malloc() or Microsofts’s \_aligned\_malloc() won’t work because  
> memory allocated with those cannot be released with free().
> 
> Trying not to cause too much trouble,
> 
> – Don W.

Thanks for bringing this up. I’ll look into it and we’ll try to figure  
out a  
general solution for all platforms involved.

Tom

---

<div class="post-metadata">

**Author:** ![walkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@walkins](https://www.ruby-forum.com/u/walkins)\
**Post date:** [March 9, 2011, 1:45am UTC](https://www.ruby-forum.com/t/aligned-memory-and-posix-memalign/204142/3 "2011-03-09T01:45:46Z")

</div>

here is my fix attempt:  
[http://gnuradio.org/cgit/jblum.git/commit/?h=wip/volk/remove\_posix\_memalign](http://gnuradio.org/cgit/jblum.git/commit/?h=wip/volk/remove_posix_memalign)

-josh
