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.
7 * Copyright (c) 2015, Mathieu Malaterre <mathieu.malaterre@gmail.com>
8 * Copyright (c) 2015, Matthieu Darbois
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
32 #define OPJ_SKIP_POISON
33 #include "opj_includes.h"
35 static INLINE void *opj_aligned_alloc_n(size_t alignment, size_t size)
39 /* alignment shall be power of 2 */
40 assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
42 if (size == 0U) { /* prevent implementation defined behavior of realloc */
46 #if defined(HAVE_POSIX_MEMALIGN)
47 /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote:
48 * This function was introduced in POSIX 1003.1d. Although this function is
49 * superseded by aligned_alloc, it is more portable to older POSIX systems
50 * that do not support ISO C11. */
51 if (posix_memalign (&ptr, alignment, size))
56 #elif defined(HAVE_MEMALIGN)
57 ptr = memalign( alignment, size );
59 #elif defined(HAVE__ALIGNED_MALLOC)
60 ptr = _aligned_malloc(size, alignment);
62 /* TODO: _mm_malloc(x,y) */
63 #error missing aligned alloc function
67 static INLINE void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t new_size)
71 /* alignment shall be power of 2 */
72 assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
74 if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
78 /* no portable aligned realloc */
79 #if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
80 /* glibc doc states one can mixed aligned malloc with realloc */
81 r_ptr = realloc( ptr, new_size ); /* fast path */
82 /* we simply use `size_t` to cast, since we are only interest in binary AND
84 if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) {
85 /* this is non-trivial to implement a portable aligned realloc, so use a
86 * simple approach where we do not need a function that return the size of an
87 * allocated array (eg. _msize on Windows, malloc_size on MacOS,
88 * malloc_usable_size on systems with glibc) */
89 void *a_ptr = opj_aligned_alloc_n(alignment, new_size);
91 memcpy(a_ptr, r_ptr, new_size);
97 #elif defined(HAVE__ALIGNED_MALLOC)
98 r_ptr = _aligned_realloc( ptr, new_size, alignment );
100 /* TODO: _mm_malloc(x,y) */
101 #error missing aligned realloc function
105 void * opj_malloc(size_t size)
107 if (size == 0U) { /* prevent implementation defined behavior of realloc */
112 void * opj_calloc(size_t num, size_t size)
114 if (size == 0U) { /* prevent implementation defined behavior of realloc */
117 /* according to C89 standard, num == 0 shall return a valid pointer */
118 return calloc(num, size);
121 void *opj_aligned_malloc(size_t size)
123 return opj_aligned_alloc_n(16U, size);
125 void * opj_aligned_realloc(void *ptr, size_t size)
127 return opj_aligned_realloc_n(ptr, 16U, size);
130 void opj_aligned_free(void* ptr)
132 #ifdef HAVE__ALIGNED_MALLOC
133 _aligned_free( ptr );
139 void * opj_realloc(void *ptr, size_t new_size)
141 if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
144 return realloc(ptr, new_size);
146 void opj_free(void *ptr)