use 4.3 versions of ppu cross-compilers if we've got them
[debian/gnuradio] / gnuradio-core / src / lib / missing / posix_memalign.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "posix_memalign.h"
28 #include <stdlib.h>
29
30 #ifndef HAVE_POSIX_MEMALIGN
31
32 /* emulate posix_memalign functionality, to some degree */
33
34 #include <errno.h>
35 #include "gr_pagesize.h"
36
37 int posix_memalign
38 (void **memptr, size_t alignment, size_t size)
39 {
40   /* emulate posix_memalign functionality, to some degree */
41
42   /* make sure the return handle is valid; return "bad address" if not valid */
43   if (memptr == 0)
44     return (EFAULT);
45   *memptr = (void*) 0;
46
47   /* make sure 'alignment' is a power of 2
48    * and multiple of sizeof (void*)
49    */
50
51   /* make sure 'alignment' is a multiple of sizeof (void*) */
52   if ((alignment % sizeof (void*)) != 0)
53     return (EINVAL);
54
55   /* make sure 'alignment' is a power of 2 */
56   if ((alignment & (alignment - 1)) != 0)
57     return (EINVAL);
58
59   /* good alignment */
60
61 #if (ALIGNED_MALLOC != 0)
62
63   /* if 'malloc' is known to be aligned, and the desired 'alignment'
64    * matches is <= that provided by 'malloc', then use 'malloc'.  This
65    * works on, e.g., Darwin 8 & 9: for which malloc is 16-byte aligned.
66    */
67   size_t am = (size_t) ALIGNED_MALLOC;
68   if (alignment <= am) {
69     /* make sure ALIGNED_MALLOC is a power of 2, to guarantee that the
70      * alignment is correct (since 'alignment' must be a power of 2).
71      */
72     if ((am & (am - 1)) != 0)
73       return (EINVAL);
74     /* good malloc alignment */
75     *memptr = malloc (size);
76   }
77
78 #endif /* (ALIGNED_MALLOC != 0) */
79 #ifdef HAVE_VALLOC
80
81   if (*memptr == (void*) 0) {
82     /* try valloc if it exists */
83     /* cheap and easy way to make sure alignment is met, so long as it
84      * is <= pagesize () */
85     if (alignment <= (size_t) gr_pagesize ()) {
86       *memptr = valloc (size);
87     }
88   }
89
90 #endif /* HAVE_VALLOC */
91
92 #if (ALIGNED_MALLOC == 0) && !defined (HAVE_VALLOC)
93   /* no posix_memalign, valloc, and malloc isn't known to be aligned
94    * (enough for the input arguments); no idea what to do.
95    */
96
97 #error gnuradio-core/src/libmissing/posix_memalign.cc: Cannot find a way to alloc aligned memory.
98
99 #endif
100
101   /* if the pointer wasn't allocated properly, return that there was
102    * not enough memory to allocate; otherwise, return OK (0).
103    */
104   if (*memptr == (void*) 0)
105     return (ENOMEM);
106   else
107     return (0);
108 };
109
110 #endif /* ! HAVE_POSIX_MEMALIGN */