1 /**
2 * Aften: A/52 audio encoder
3 * Copyright (c) 2007 Justin Ruggles
4 * Copyright (c) 2007 Prakash Punnoor <prakash@punnoor.de>
5 *
6 * Uses a modified version of memalign hack from FFmpeg
7 * Copyright (c) 2002 Fabrice Bellard.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file mem.h
26 * Memory-related functions
27 */
28
29 #ifndef MEM_H
30 #define MEM_H
31
32 #include "common.h"
33
34 #ifdef HAVE_MM_MALLOC
35
36 #define aligned_malloc(X) _mm_malloc(X,16)
37
38 #define aligned_free(X) _mm_free(X)
39
40 #else
41 #ifdef HAVE_POSIX_MEMALIGN
42
43 #define _XOPEN_SOURCE 600
44 #include <stdlib.h>
45
46 static inline void *
aligned_malloc(size_t size)47 aligned_malloc(size_t size)
48 {
49 void *mem;
50 if (posix_memalign(&mem, 16, size))
51 return NULL;
52 return mem;
53 }
54
55 #define aligned_free(X) free(X)
56
57 #else
58
59 static inline void*
aligned_malloc(long size)60 aligned_malloc(long size)
61 {
62 void *mem;
63 long diff;
64 mem = malloc(size+32);
65 if(!mem)
66 return mem;
67 diff = ((long)mem & 15) - 32;
68 mem -= diff;
69 ((int *)mem)[-1] = (int)diff;
70 return mem;
71 }
72
73 static inline void
aligned_free(void * ptr)74 aligned_free(void *ptr)
75 {
76 if(ptr)
77 free(ptr + ((int*)ptr)[-1]);
78 }
79
80 #endif /* HAVE_POSIX_MEMALIGN */
81
82 #endif /* HAVE_MM_MALLOC */
83
84 #endif /* MEM_H */
85