- void *r_ptr;
-
- /* alignment shall be power of 2 */
- assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
- /* alignment shall be at least sizeof(void*) */
- assert( alignment >= sizeof(void*));
-
- if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
- return NULL;
- }
-
-/* no portable aligned realloc */
-#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
- /* glibc doc states one can mixed aligned malloc with realloc */
- r_ptr = realloc( ptr, new_size ); /* fast path */
- /* we simply use `size_t` to cast, since we are only interest in binary AND
- * operator */
- if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) {
- /* this is non-trivial to implement a portable aligned realloc, so use a
- * simple approach where we do not need a function that return the size of an
- * allocated array (eg. _msize on Windows, malloc_size on MacOS,
- * malloc_usable_size on systems with glibc) */
- void *a_ptr = opj_aligned_alloc_n(alignment, new_size);
- if (a_ptr != NULL) {
- memcpy(a_ptr, r_ptr, new_size);
- }
- free( r_ptr );
- r_ptr = a_ptr;
- }
-/* _MSC_VER */
-#elif defined(HAVE__ALIGNED_MALLOC)
- r_ptr = _aligned_realloc( ptr, new_size, alignment );
-#else
- if (ptr == NULL) {
- return opj_aligned_alloc_n(alignment, new_size);
- }
- alignment--;
- {
- void *oldmem;
- OPJ_UINT8 *newmem;
- size_t overhead = alignment + sizeof(void *);
-
- /* let's be extra careful */
- assert(alignment <= (SIZE_MAX - sizeof(void *)));
-
- /* Avoid integer overflow */
- if (new_size > SIZE_MAX - overhead) {
- return NULL;
- }
-
- oldmem = ((void**) ptr)[-1];
- newmem = (OPJ_UINT8*)realloc(oldmem, new_size + overhead);
- if (newmem == NULL) {
- return newmem;
+ void *r_ptr;
+
+ /* alignment shall be power of 2 */
+ assert((alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
+ /* alignment shall be at least sizeof(void*) */
+ assert(alignment >= sizeof(void*));
+
+ if (new_size == 0U) { /* prevent implementation defined behavior of realloc */
+ return NULL;