cleanup header file and move to implementation
[openjpeg.git] / src / lib / openjp2 / opj_malloc.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2015, Mathieu Malaterre <mathieu.malaterre@gmail.com>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #define OPJ_SKIP_POISON
32 #include "opj_malloc.h"
33 #include <stdlib.h>
34
35 static inline void *opj_aligned_alloc(size_t alignment, size_t size)
36 {
37 #ifndef HAVE_ALIGNED_ALLOC
38   /* older linux */
39 #ifdef HAVE_MEMALIGN
40   assert( size % alignment == 0 );
41   return memalign( alignment, size );
42 #endif /* HAVE_MEMALIGN */
43
44 /* _MSC_VER */
45 #ifdef HAVE__ALIGNED_MALLOC
46   return _aligned_malloc( alignment, size );
47 #endif /* HAVE__ALIGNED_MALLOC */
48
49 /* MacOSX / clang */
50 #if defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_MEMALIGN)
51   void* ptr;
52   if (posix_memalign (&ptr, alignment, size))
53   {
54     ptr = NULL;
55   }
56   return ptr;
57 #endif /* HAVE_POSIX_MEMALIGN */
58
59 #else /* HAVE_ALIGNED_ALLOC */
60   return aligned_alloc( alignment, size );
61 #endif /* HAVE_ALIGNED_ALLOC */
62 /* TODO: _mm_malloc(x,y) */
63 }
64
65 void * opj_malloc(size_t size)
66 {
67   return malloc(size);
68 }
69
70 void * opj_calloc(size_t numOfElements, size_t sizeOfElements)
71 {
72   return calloc(numOfElements, sizeOfElements);
73 }
74
75 void *opj_aligned_malloc(size_t size)
76 {
77   return opj_aligned_alloc(size,16);
78 }
79
80 void opj_aligned_free(void* ptr)
81 {
82 #ifdef HAVE__ALIGNED_MALLOC
83   _aligned_free( ptr );
84 #else
85   free( ptr );
86 #endif
87 }
88
89 void * opj_realloc(void * m, size_t s)
90 {
91   return realloc(m,s);
92 }
93 void opj_free(void * m)
94 {
95   free(m);
96 }