Generic aligned malloc 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  * Copyright (c) 2015, Matthieu Darbois
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #define OPJ_SKIP_POISON
33 #include "opj_includes.h"
34
35 #ifndef SIZE_MAX
36 #define SIZE_MAX ((size_t) -1)
37 #endif
38
39 static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size)
40 {
41   void* ptr;
42
43   /* alignment shall be power of 2 */
44   assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
45
46   if (size == 0U) { /* prevent implementation defined behavior of realloc */
47     return NULL;
48   }
49
50 #if defined(HAVE_POSIX_MEMALIGN)
51   /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote:
52    * This function was introduced in POSIX 1003.1d. Although this function is
53    * superseded by aligned_alloc, it is more portable to older POSIX systems
54    * that do not support ISO C11.  */
55   if (posix_memalign (&ptr, alignment, size))
56   {
57     ptr = NULL;
58   }
59   /* older linux */
60 #elif defined(HAVE_MEMALIGN)
61   ptr = memalign( alignment, size );
62 /* _MSC_VER */
63 #elif defined(HAVE__ALIGNED_MALLOC)
64   ptr = _aligned_malloc(size, alignment);
65 #else
66   /*
67    * Generic aligned malloc implementation.
68    * Uses ptrdiff_t for the integer manipulation of the pointer, as
69    * uintptr_t is not available in C89.
70    */
71   {
72     ptrdiff_t mask;
73     void *mem;
74
75     /* Room for padding and extra pointer stored in front of allocated area */
76     size_t overhead = (alignment - 1) + sizeof(void *);
77
78     /* Avoid integer overflow */
79     if (size > SIZE_MAX - overhead)
80       return NULL;
81
82     mem = malloc(size + overhead);
83     if (!mem)
84       return mem;
85
86     mask = ~(ptrdiff_t)(alignment - 1);
87     ptr = (void *) ((ptrdiff_t) (mem + overhead) & mask);
88     ((void**) ptr)[-1] = mem;
89   }
90 #endif
91   return ptr;
92 }
93 static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t new_size)
94 {
95   void *r_ptr;
96
97   /* alignment shall be power of 2 */
98   assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
99
100   if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
101     return NULL;
102   }
103
104 /* no portable aligned realloc */
105 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
106   /* glibc doc states one can mixed aligned malloc with realloc */
107   r_ptr = realloc( ptr, new_size ); /* fast path */
108   /* we simply use `size_t` to cast, since we are only interest in binary AND
109    * operator */
110   if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) {
111     /* this is non-trivial to implement a portable aligned realloc, so use a
112      * simple approach where we do not need a function that return the size of an
113      * allocated array (eg. _msize on Windows, malloc_size on MacOS,
114      * malloc_usable_size on systems with glibc) */
115     void *a_ptr = opj_aligned_alloc_n(alignment, new_size);
116     if (a_ptr != NULL) {
117       memcpy(a_ptr, r_ptr, new_size);
118     }
119     free( r_ptr );
120     r_ptr = a_ptr;
121   }
122 /* _MSC_VER */
123 #elif defined(HAVE__ALIGNED_MALLOC)
124   r_ptr = _aligned_realloc( ptr, new_size, alignment );
125 #else
126   {
127     void *oldmem, *newmem;
128     size_t overhead = (alignment - 1) + sizeof(void *);
129     
130     if (new_size == 0) {
131       my_aligned_free(ptr);
132       return NULL;
133     }
134
135     /* Avoid integer overflow */
136     if (new_size > SIZE_MAX - overhead)
137       return NULL;
138
139     oldmem = ((void**) ptr)[-1];
140     newmem = realloc(oldmem, new_size + overhead);
141     if (!newmem)
142       return newmem;
143
144     if (newmem == oldmem) {
145       r_ptr = ptr;
146     }
147     else {
148       ptrdiff_t old_offset, new_offset;
149       ptrdiff_t mask;
150
151       /* realloc created a new copy, realign the copied memory block */
152       old_offset = (char *) ptr - (char *) oldmem;
153
154       mask = ~(ptrdiff_t)(alignment - 1);
155       r_ptr = (void *) ((ptrdiff_t) (newmem + overhead) & mask);
156
157       new_offset = (char *) r_ptr - (char *) newmem;
158
159       if (new_offset != old_offset) {
160         memmove((char *) newmem + new_offset, (char *) newmem + old_offset,
161                       new_size);
162       }
163       ((void**) r_ptr)[-1] = newmem;
164     }
165   }
166 #endif
167         return r_ptr;
168 }
169 void * opj_malloc(size_t size)
170 {
171   if (size == 0U) { /* prevent implementation defined behavior of realloc */
172     return NULL;
173   }
174   return malloc(size);
175 }
176 void * opj_calloc(size_t num, size_t size)
177 {
178   if (size == 0U) { /* prevent implementation defined behavior of realloc */
179     return NULL;
180   }
181   /* according to C89 standard, num == 0 shall return a valid pointer */
182   return calloc(num, size);
183 }
184
185 void *opj_aligned_malloc(size_t size)
186 {
187   return opj_aligned_alloc_n(16U, size);
188 }
189 void * opj_aligned_realloc(void *ptr, size_t size)
190 {
191   return opj_aligned_realloc_n(ptr, 16U, size);
192 }
193
194 void opj_aligned_free(void* ptr)
195 {
196 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
197   free( ptr );
198 #elif defined(HAVE__ALIGNED_MALLOC)
199   _aligned_free( ptr );
200 #else
201   /* Generic implementation has malloced pointer stored in front of used area */
202   if (ptr)
203     free(((void**) ptr)[-1]);
204 #endif
205 }
206
207 void * opj_realloc(void *ptr, size_t new_size)
208 {
209   if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
210     return NULL;
211   }
212   return realloc(ptr, new_size);
213 }
214 void opj_free(void *ptr)
215 {
216   free(ptr);
217 }