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