0
|
1 /* Copyright (C) 2004, 2009 Free Software Foundation, Inc.
|
|
2
|
|
3 This file is part of GCC.
|
|
4
|
|
5 GCC is free software; you can redistribute it and/or modify
|
|
6 it under the terms of the GNU General Public License as published by
|
|
7 the Free Software Foundation; either version 3, or (at your option)
|
|
8 any later version.
|
|
9
|
|
10 GCC is distributed in the hope that it will be useful,
|
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 GNU General Public License for more details.
|
|
14
|
|
15 Under Section 7 of GPL version 3, you are granted additional
|
|
16 permissions described in the GCC Runtime Library Exception, version
|
|
17 3.1, as published by the Free Software Foundation.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License and
|
|
20 a copy of the GCC Runtime Library Exception along with this program;
|
|
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
22 <http://www.gnu.org/licenses/>. */
|
|
23
|
|
24 #ifndef _MM_MALLOC_H_INCLUDED
|
|
25 #define _MM_MALLOC_H_INCLUDED
|
|
26
|
|
27 #include <stdlib.h>
|
|
28 #include <errno.h>
|
|
29
|
|
30 static __inline__ void*
|
|
31 _mm_malloc (size_t size, size_t align)
|
|
32 {
|
|
33 void * malloc_ptr;
|
|
34 void * aligned_ptr;
|
|
35
|
|
36 /* Error if align is not a power of two. */
|
|
37 if (align & (align - 1))
|
|
38 {
|
|
39 errno = EINVAL;
|
|
40 return ((void*) 0);
|
|
41 }
|
|
42
|
|
43 if (size == 0)
|
|
44 return ((void *) 0);
|
|
45
|
|
46 /* Assume malloc'd pointer is aligned at least to sizeof (void*).
|
|
47 If necessary, add another sizeof (void*) to store the value
|
|
48 returned by malloc. Effectively this enforces a minimum alignment
|
|
49 of sizeof double. */
|
|
50 if (align < 2 * sizeof (void *))
|
|
51 align = 2 * sizeof (void *);
|
|
52
|
|
53 malloc_ptr = malloc (size + align);
|
|
54 if (!malloc_ptr)
|
|
55 return ((void *) 0);
|
|
56
|
|
57 /* Align We have at least sizeof (void *) space below malloc'd ptr. */
|
|
58 aligned_ptr = (void *) (((size_t) malloc_ptr + align)
|
|
59 & ~((size_t) (align) - 1));
|
|
60
|
|
61 /* Store the original pointer just before p. */
|
|
62 ((void **) aligned_ptr) [-1] = malloc_ptr;
|
|
63
|
|
64 return aligned_ptr;
|
|
65 }
|
|
66
|
|
67 static __inline__ void
|
|
68 _mm_free (void * aligned_ptr)
|
|
69 {
|
|
70 if (aligned_ptr)
|
|
71 free (((void **) aligned_ptr) [-1]);
|
|
72 }
|
|
73
|
|
74 #endif /* _MM_MALLOC_H_INCLUDED */
|