3
* Copyright 2008,2009 Free Software Foundation, Inc.
5
* This file is part of GNU Radio
7
* GNU Radio is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3, or (at your option)
12
* GNU Radio is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with GNU Radio; see the file COPYING. If not, write to
19
* the Free Software Foundation, Inc., 51 Franklin Street,
20
* Boston, MA 02110-1301, USA.
27
#include "posix_memalign.h"
30
// for Cygwin valloc () prototype
34
#ifndef HAVE_POSIX_MEMALIGN
36
/* emulate posix_memalign functionality, to some degree */
39
#include "gr_pagesize.h"
42
(void **memptr, size_t alignment, size_t size)
44
/* emulate posix_memalign functionality, to some degree */
46
/* make sure the return handle is valid; return "bad address" if not valid */
51
/* make sure 'alignment' is a power of 2
52
* and multiple of sizeof (void*)
55
/* make sure 'alignment' is a multiple of sizeof (void*) */
56
if ((alignment % sizeof (void*)) != 0)
59
/* make sure 'alignment' is a power of 2 */
60
if ((alignment & (alignment - 1)) != 0)
65
#if (ALIGNED_MALLOC != 0)
67
/* if 'malloc' is known to be aligned, and the desired 'alignment'
68
* matches is <= that provided by 'malloc', then use 'malloc'. This
69
* works on, e.g., Darwin 8 & 9: for which malloc is 16-byte aligned.
71
size_t am = (size_t) ALIGNED_MALLOC;
72
if (alignment <= am) {
73
/* make sure ALIGNED_MALLOC is a power of 2, to guarantee that the
74
* alignment is correct (since 'alignment' must be a power of 2).
76
if ((am & (am - 1)) != 0)
78
/* good malloc alignment */
79
*memptr = malloc (size);
82
#endif /* (ALIGNED_MALLOC != 0) */
85
if (*memptr == (void*) 0) {
86
/* try valloc if it exists */
87
/* cheap and easy way to make sure alignment is met, so long as it
88
* is <= pagesize () */
89
if (alignment <= (size_t) gr_pagesize ()) {
90
*memptr = valloc (size);
94
#endif /* HAVE_VALLOC */
96
#if (ALIGNED_MALLOC == 0) && !defined (HAVE_VALLOC)
97
/* no posix_memalign, valloc, and malloc isn't known to be aligned
98
* (enough for the input arguments); no idea what to do.
101
#error gnuradio-core/src/libmissing/posix_memalign.cc: Cannot find a way to alloc aligned memory.
105
/* if the pointer wasn't allocated properly, return that there was
106
* not enough memory to allocate; otherwise, return OK (0).
108
if (*memptr == (void*) 0)
114
#endif /* ! HAVE_POSIX_MEMALIGN */