~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to gnuradio-core/src/lib/missing/posix_memalign.cc

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
29
#ifdef HAVE_MALLOC_H
 
30
// for Cygwin valloc () prototype
 
31
#include <malloc.h>
 
32
#endif
 
33
 
 
34
#ifndef HAVE_POSIX_MEMALIGN
 
35
 
 
36
/* emulate posix_memalign functionality, to some degree */
 
37
 
 
38
#include <errno.h>
 
39
#include "gr_pagesize.h"
 
40
 
 
41
int posix_memalign
 
42
(void **memptr, size_t alignment, size_t size)
 
43
{
 
44
  /* emulate posix_memalign functionality, to some degree */
 
45
 
 
46
  /* make sure the return handle is valid; return "bad address" if not valid */
 
47
  if (memptr == 0)
 
48
    return (EFAULT);
 
49
  *memptr = (void*) 0;
 
50
 
 
51
  /* make sure 'alignment' is a power of 2
 
52
   * and multiple of sizeof (void*)
 
53
   */
 
54
 
 
55
  /* make sure 'alignment' is a multiple of sizeof (void*) */
 
56
  if ((alignment % sizeof (void*)) != 0)
 
57
    return (EINVAL);
 
58
 
 
59
  /* make sure 'alignment' is a power of 2 */
 
60
  if ((alignment & (alignment - 1)) != 0)
 
61
    return (EINVAL);
 
62
 
 
63
  /* good alignment */
 
64
 
 
65
#if (ALIGNED_MALLOC != 0)
 
66
 
 
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.
 
70
   */
 
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).
 
75
     */
 
76
    if ((am & (am - 1)) != 0)
 
77
      return (EINVAL);
 
78
    /* good malloc alignment */
 
79
    *memptr = malloc (size);
 
80
  }
 
81
 
 
82
#endif /* (ALIGNED_MALLOC != 0) */
 
83
#ifdef HAVE_VALLOC
 
84
 
 
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);
 
91
    }
 
92
  }
 
93
 
 
94
#endif /* HAVE_VALLOC */
 
95
 
 
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.
 
99
   */
 
100
 
 
101
#error gnuradio-core/src/libmissing/posix_memalign.cc: Cannot find a way to alloc aligned memory.
 
102
 
 
103
#endif
 
104
 
 
105
  /* if the pointer wasn't allocated properly, return that there was
 
106
   * not enough memory to allocate; otherwise, return OK (0).
 
107
   */
 
108
  if (*memptr == (void*) 0)
 
109
    return (ENOMEM);
 
110
  else
 
111
    return (0);
 
112
};
 
113
 
 
114
#endif /* ! HAVE_POSIX_MEMALIGN */