]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | /* Copyright (C) 2004-2024 Free Software Foundation, Inc. |
f6bc51cb L |
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 | |
748086b7 | 7 | the Free Software Foundation; either version 3, or (at your option) |
f6bc51cb L |
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 | ||
748086b7 JJ |
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/>. */ | |
f6bc51cb L |
23 | |
24 | #ifndef _MM_MALLOC_H_INCLUDED | |
25 | #define _MM_MALLOC_H_INCLUDED | |
26 | ||
27 | #include <stdlib.h> | |
1460c0bb | 28 | #if __STDC_HOSTED__ |
f6bc51cb | 29 | #include <errno.h> |
1460c0bb | 30 | #endif |
f6bc51cb | 31 | |
b5fd0b71 JJ |
32 | static __inline__ void * |
33 | _mm_malloc (size_t __size, size_t __align) | |
f6bc51cb | 34 | { |
b5fd0b71 JJ |
35 | void * __malloc_ptr; |
36 | void * __aligned_ptr; | |
f6bc51cb L |
37 | |
38 | /* Error if align is not a power of two. */ | |
b5fd0b71 | 39 | if (__align & (__align - 1)) |
f6bc51cb | 40 | { |
1460c0bb | 41 | #if __STDC_HOSTED__ |
f6bc51cb | 42 | errno = EINVAL; |
1460c0bb | 43 | #endif |
b5fd0b71 | 44 | return ((void *) 0); |
f6bc51cb L |
45 | } |
46 | ||
b5fd0b71 | 47 | if (__size == 0) |
f6bc51cb L |
48 | return ((void *) 0); |
49 | ||
50 | /* Assume malloc'd pointer is aligned at least to sizeof (void*). | |
c112cf2b | 51 | If necessary, add another sizeof (void*) to store the value |
f6bc51cb L |
52 | returned by malloc. Effectively this enforces a minimum alignment |
53 | of sizeof double. */ | |
b5fd0b71 JJ |
54 | if (__align < 2 * sizeof (void *)) |
55 | __align = 2 * sizeof (void *); | |
f6bc51cb | 56 | |
b5fd0b71 JJ |
57 | __malloc_ptr = malloc (__size + __align); |
58 | if (!__malloc_ptr) | |
f6bc51cb L |
59 | return ((void *) 0); |
60 | ||
61 | /* Align We have at least sizeof (void *) space below malloc'd ptr. */ | |
b5fd0b71 JJ |
62 | __aligned_ptr = (void *) (((size_t) __malloc_ptr + __align) |
63 | & ~((size_t) (__align) - 1)); | |
f6bc51cb L |
64 | |
65 | /* Store the original pointer just before p. */ | |
b5fd0b71 | 66 | ((void **) __aligned_ptr)[-1] = __malloc_ptr; |
f6bc51cb | 67 | |
b5fd0b71 | 68 | return __aligned_ptr; |
f6bc51cb L |
69 | } |
70 | ||
71 | static __inline__ void | |
b5fd0b71 | 72 | _mm_free (void *__aligned_ptr) |
f6bc51cb | 73 | { |
b5fd0b71 JJ |
74 | if (__aligned_ptr) |
75 | free (((void **) __aligned_ptr)[-1]); | |
f6bc51cb L |
76 | } |
77 | ||
78 | #endif /* _MM_MALLOC_H_INCLUDED */ |