minor tweaks to the code
[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 "opj_config_private.h"
34 #include <stdlib.h>
35 #include <string.h>
36 #include <inttypes.h>
37 #include <assert.h>
38
39 static inline void *opj_aligned_alloc_n(size_t alignment, size_t size)
40 {
41 #if defined(HAVE_POSIX_MEMALIGN)
42   /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote:
43    * This function was introduced in POSIX 1003.1d. Although this function is
44    * superseded by aligned_alloc, it is more portable to older POSIX systems
45    * that do not support ISO C11.  */
46   void* ptr;
47   if (posix_memalign (&ptr, alignment, size))
48   {
49     ptr = NULL;
50   }
51   return ptr;
52   /* older linux */
53 #elif defined(HAVE_MEMALIGN)
54   assert( size & (alignment - 1u) == 0 );
55   return memalign( alignment, size );
56 /* _MSC_VER */
57 #elif defined(HAVE__ALIGNED_MALLOC)
58   return _aligned_malloc( alignment, size );
59 #else
60 /* TODO: _mm_malloc(x,y) */
61 #error missing aligned alloc function
62 #endif
63 }
64 static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size)
65 {
66   void *a_ptr;
67 /* no portable aligned realloc */
68 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
69   /* glibc doc states one can mixed aligned malloc with realloc */
70   void *r_ptr = realloc( ptr, size );
71   assert( alignment > 0 );
72   /* fast path */
73   /* we simply use `size_t` to cast, since we are only interest in binary AND
74    * operator */
75   if( ((size_t)r_ptr & (alignment - 1u)) == 0 )
76     return r_ptr;
77   /* this is non-trivial to implement a portable aligned realloc, so use a
78    * simple approach where we do not need a function that return the size of an
79    * allocated array (eg. _msize on Windows, malloc_size on MacOS,
80    * malloc_usable_size on systems with glibc) */
81   a_ptr = opj_aligned_alloc_n(alignment, size);
82   /* memory may overlap, do not use memcpy */
83   memmove(a_ptr, r_ptr, size);
84   free( r_ptr );
85   return a_ptr;
86 /* _MSC_VER */
87 #elif defined(HAVE__ALIGNED_MALLOC)
88   return _aligned_realloc( ptr, size, alignment );
89 #else
90 /* TODO: _mm_malloc(x,y) */
91 #error missing aligned realloc function
92 #endif
93 }
94 void * opj_malloc(size_t size)
95 {
96   return malloc(size);
97 }
98 void * opj_calloc(size_t numOfElements, size_t sizeOfElements)
99 {
100   return calloc(numOfElements, sizeOfElements);
101 }
102
103 void *opj_aligned_malloc(size_t size)
104 {
105   return opj_aligned_alloc_n(16u,size);
106 }
107 void * opj_aligned_realloc(void *ptr, size_t size)
108 {
109   return opj_aligned_realloc_n(ptr,16u,size);
110 }
111
112 void opj_aligned_free(void* ptr)
113 {
114 #ifdef HAVE__ALIGNED_MALLOC
115   _aligned_free( ptr );
116 #else
117   free( ptr );
118 #endif
119 }
120
121 void * opj_realloc(void * m, size_t s)
122 {
123   return realloc(m,s);
124 }
125 void opj_free(void * m)
126 {
127   free(m);
128 }