void cStream::allocateArrays() { if( mArraysAreAllocated ) { #ifdef vs2005 a = (double*)_aligned_realloc( a, N * sizeof(double), 16 ); b = (double*)_aligned_realloc( b, N * sizeof(double), 16 ); c = (double*)_aligned_realloc( c, N * sizeof(double), 16 ); #else free( a ); free( b ); free( c ); #ifdef windowos a = (double*)malloc( N * sizeof(double) ); b = (double*)malloc( N * sizeof(double) ); c = (double*)malloc( N * sizeof(double) ); #else a = (double*)valloc( N * sizeof(double) ); b = (double*)valloc( N * sizeof(double) ); c = (double*)valloc( N * sizeof(double) ); #endif #endif } else { #ifdef vs2005 // allocate memory from heap for arrays a = (double*)_aligned_malloc( N * sizeof(double), 16 ); b = (double*)_aligned_malloc( N * sizeof(double), 16 ); c = (double*)_aligned_malloc( N * sizeof(double), 16 ); bytes = (double*)_aligned_malloc( 4 * sizeof(double), 16 ); #else // obtain clock update frequency: freq = CLOCKS_PER_SEC; // allocate memory from heap for arrays #ifdef windowsos a = (double*)malloc( N * sizeof(double) ); b = (double*)malloc( N * sizeof(double) ); c = (double*)malloc( N * sizeof(double) ); bytes = (double*)_malloc( 4 * sizeof(double) ); #else a = (double*)valloc( N * sizeof(double) ); b = (double*)valloc( N * sizeof(double) ); c = (double*)valloc( N * sizeof(double) ); bytes = (double*)valloc( 4 * sizeof(double) ); #endif #endif } bytes[ eCopy ] = 2 * sizeof(double) * N; bytes[ eScale ] = 2 * sizeof(double) * N; bytes[ eAdd ] = 3 * sizeof(double) * N; bytes[ eTriad ] = 3 * sizeof(double) * N; mArraysAreAllocated = true; } // void cStream::allocateArrays()
//! \brief Reallocates \a sz bytes of memory on a 16 byte boundary, optionally tracking the allocation void* xsAlignedRealloc(void* ptr, size_t sz) { #ifdef TRACK_ALLOCS lastFreeIdx = (lastAlignedFreeIdx + 1) & (TRACK_ALLOCS-1); lastAlignedFrees[lastAlignedFreeIdx] = ptr; ptr = _aligned_realloc(ptr, sz, 16); lastAlignedAllocIdx = (lastAlignedAllocIdx + 1) & (TRACK_ALLOCS-1); lastAlignedAllocs[lastAlignedAllocIdx] = ptr; return ptr; #else return _aligned_realloc(ptr, sz, 16); #endif }
void wf_Bitmap_Decompress(wfContext* wfc, rdpBitmap* bitmap, BYTE* data, int width, int height, int bpp, int length, BOOL compressed, int codecId) { int status; UINT16 size; BYTE* pSrcData; BYTE* pDstData; UINT32 SrcSize; UINT32 SrcFormat; UINT32 bytesPerPixel; bytesPerPixel = (bpp + 7) / 8; size = width * height * 4; if (!bitmap->data) bitmap->data = (BYTE*) _aligned_malloc(size, 16); else bitmap->data = (BYTE*) _aligned_realloc(bitmap->data, size, 16); pSrcData = data; SrcSize = (UINT32) length; pDstData = bitmap->data; if (compressed) { if (bpp < 32) { if (!freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_INTERLEAVED)) return; status = interleaved_decompress(wfc->codecs->interleaved, pSrcData, SrcSize, bpp, &pDstData, PIXEL_FORMAT_XRGB32, width * 4, 0, 0, width, height, NULL); } else { if (!freerdp_client_codecs_prepare(wfc->codecs, FREERDP_CODEC_PLANAR)) return; status = planar_decompress(wfc->codecs->planar, pSrcData, SrcSize, &pDstData, PIXEL_FORMAT_XRGB32, width * 4, 0, 0, width, height, TRUE); } if (status < 0) { WLog_ERR(TAG, "Bitmap Decompression Failed"); return; } } else { SrcFormat = gdi_get_pixel_format(bpp, TRUE); status = freerdp_image_copy(pDstData, PIXEL_FORMAT_XRGB32, width * 4, 0, 0, width, height, pSrcData, SrcFormat, width * bytesPerPixel, 0, 0, NULL); } bitmap->compressed = FALSE; bitmap->length = size; bitmap->bpp = 32; }
void *rax_realloc(void *p, size_t size, boolean needsMemoryAlignment) { //it's actually not that easy to implement an aligned realloc //because we need to know the size of the array pointed to by //the pointer passed as argument //hence I added this boolean flag that should increase programmer //awareness about this issue void *ptr = (void *)NULL; if(needsMemoryAlignment) { assert(0); return (void*)NULL; } else { #ifndef WIN32 ptr = realloc(p, size); #else ptr = _aligned_realloc(p, size, BYTE_ALIGNMENT); #endif } if(ptr == (void*)NULL) { outOfMemory(); assert(0); } return ptr; }
void *av_realloc(void *ptr, size_t size) { #if CONFIG_MEMALIGN_HACK int diff; #endif /* let's disallow possibly ambiguous cases */ if (size > (max_alloc_size - 32)) return NULL; #if CONFIG_MEMALIGN_HACK //FIXME this isn't aligned correctly, though it probably isn't needed if (!ptr) return av_malloc(size); diff = ((char *)ptr)[-1]; av_assert0(diff>0 && diff<=ALIGN); ptr = realloc((char *)ptr - diff, size + diff); if (ptr) ptr = (char *)ptr + diff; return ptr; #elif HAVE_ALIGNED_MALLOC return _aligned_realloc(ptr, size + !size, ALIGN); #else return realloc(ptr, size + !size); #endif }
static void* _memory_reallocate_malloc( void* p, uint64_t size, unsigned int align ) { align = _memory_get_align( align ); #if FOUNDATION_PLATFORM_WINDOWS return _aligned_realloc( p, (size_t)size, align ); #else if( align ) { //No realloc aligned available void* memory = aligned_alloc( align, (size_t)size ); if( !memory ) { log_panicf( ERROR_OUT_OF_MEMORY, "Unable to reallocate memory: %s", system_error_message( 0 ) ); return 0; } if( p ) { size_t prev_size = malloc_usable_size( p ); memcpy( memory, p, ( size < prev_size ) ? size : prev_size ); } return memory; } return realloc( p, (size_t)size ); #endif }
void wf_Bitmap_Decompress(wfContext* wfc, rdpBitmap* bitmap, BYTE* data, int width, int height, int bpp, int length, BOOL compressed, int codec_id) { UINT16 size; size = width * height * (bpp / 8); if (!bitmap->data) bitmap->data = (BYTE*) _aligned_malloc(size, 16); else bitmap->data = (BYTE*) _aligned_realloc(bitmap->data, size, 16); if (compressed) { BOOL status; status = bitmap_decompress(data, bitmap->data, width, height, length, bpp, bpp); if (status != TRUE) { fprintf(stderr, "Bitmap Decompression Failed\n"); } } else { freerdp_image_flip(data, bitmap->data, width, height, bpp); } bitmap->compressed = FALSE; bitmap->length = size; bitmap->bpp = bpp; }
void *jl_gc_managed_realloc(void *d, size_t sz, size_t oldsz, int isaligned) { if (allocd_bytes > collect_interval) jl_gc_collect(); sz = (sz+15) & -16; void *b; #ifdef _P64 b = realloc(d, sz); #elif defined(_OS_WINDOWS_) if (isaligned) b = _aligned_realloc(d, sz, 16); else b = realloc(d, sz); #elif defined(__APPLE__) b = realloc(d, sz); #else // TODO better aligned realloc here b = malloc_a16(sz); if (b != NULL) { memcpy(b, d, oldsz); if (isaligned) free_a16(d); else free(d); } #endif if (b == NULL) jl_throw(jl_memory_exception); allocd_bytes += sz; return b; }
//============================================================================ inline byte* reallocate ( byte* ptr , std::size_t nbytes , std::size_t obytes , std::size_t align ) { byte* result(ptr); if( (obytes < nbytes) || !is_aligned(ptr,align) ) { #if defined(BOOST_MSVC) //======================================================================== // MSVC systems use _aligned_realloc //======================================================================== if( !( result = reinterpret_cast<byte*>(_aligned_realloc(ptr, nbytes, align)) ) ) { BOOST_THROW_EXCEPTION( std::bad_alloc() ); result = 0; } #else //======================================================================== // Other systems allocate/copy/deallocate //======================================================================== byte* tmp = allocate(nbytes, align); std::memcpy(tmp,ptr,obytes); deallocate(ptr,obytes,align); result = tmp; #endif } return result; }
void *__cilkrts_realloc(void *ptr, size_t size) { #ifdef _WIN32 return _aligned_realloc(ptr, size, PREFERRED_ALIGNMENT); #else return realloc(ptr, size); #endif }
void* Aligned_Realloc(void* ptr, size_t size, size_t align) { #ifdef __GNUC__ return posix_memalign(ptr, align, size); #else return _aligned_realloc(ptr, size, align); #endif }
void* tsk_realloc_aligned(void * ptr, tsk_size_t size, tsk_size_t alignment) { #if TSK_UNDER_WINDOWS && !TSK_UNDER_WINDOWS_CE && !TSK_UNDER_WINDOWS_RT return _aligned_realloc(ptr, size, alignment); #else tsk_free_aligned(ptr); return tsk_malloc_aligned(size, alignment); #endif }
bool aligned_reallocate(block &b, size_t n) noexcept { block reallocatedBlock{ _aligned_realloc(b.ptr, n, alignment), n }; if (reallocatedBlock) { b = reallocatedBlock; return true; } return false; }
void *av_realloc(void *ptr, unsigned int size) { /* let's disallow possible ambiguous cases */ if(size > (INT_MAX-16) ) return NULL; #ifndef __GNUC__ return _aligned_realloc(ptr, size,16); #else return __mingw_aligned_realloc(ptr, size,16); #endif }
void FCDECL exaligned_mrealloc (void** p, size_t blocksize, size_t blockalign, size_t memset_start, size_t memset_stop) { void* pp = *p; if (blocksize) { while (blocksize > EXCALCBLOCKSIZE_MAX - blockalign || (pp = _aligned_realloc (*p, blocksize, blockalign)) == NULL) { void (*pvf) (void) = set_exalloc_handler (NULL); set_exalloc_handler (pvf); if (pvf != NULL) { (*pvf)(); continue; } (*exalloc_status.size_handler)(); exalloc_status.nBlocksFailed++; return; } if (((size_t)pp & (blockalign - 1)) != 0) {} if (pp != NULL) { exalloc_status.nBlocksAllocated++; } if (memset_stop > memset_start) { memset ((char*)pp + memset_start, 0, memset_stop - memset_start); } if (exalloc_status.pMinAlloc - 1 >= (char*) pp) { exalloc_status.pMinAlloc = (char*)pp; } if (exalloc_status.pMaxAlloc < (char*)pp + blocksize) { exalloc_status.pMaxAlloc = (char*)pp + blocksize; } } else if (pp) { _aligned_free (pp); pp = NULL; exalloc_status.nBlocksAllocated--; } *p = pp; }
void* FF_aligned_realloc(void *ptr,size_t size,size_t alignment) { if (!ptr) return FF_aligned_malloc(size,alignment); else if (size==0) { FF_aligned_free(ptr); return NULL; } else return _aligned_realloc(ptr,size,alignment); }
N1527MALLOCNOALIASATTR N1527MALLOCPTRATTR void *n1527_aligned_realloc(void *ptr, size_t alignment, size_t size) { #ifdef _MSC_VER return _aligned_realloc(ptr, size, alignment); #else /* Sadly no standard POSIX choice here */ void *temp; size_t oldsize=malloc_usable_size(ptr); if(!(temp=memalign(alignment, size))) return 0; memcpy(temp, ptr, (oldsize<size) ? oldsize : size); free(ptr); return temp; #endif }
void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) { if (0 == _size) { if (NULL != _ptr) { if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) { ::free(_ptr); return NULL; } # if BX_COMPILER_MSVC BX_UNUSED(_file, _line); _aligned_free(_ptr); # else bx::alignedFree(this, _ptr, _align, _file, _line); # endif // BX_ } return NULL; } else if (NULL == _ptr) { if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) { return ::malloc(_size); } # if BX_COMPILER_MSVC BX_UNUSED(_file, _line); return _aligned_malloc(_size, _align); # else return bx::alignedAlloc(this, _size, _align, _file, _line); # endif // BX_ } if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align) { return ::realloc(_ptr, _size); } # if BX_COMPILER_MSVC BX_UNUSED(_file, _line); return _aligned_realloc(_ptr, _size, _align); # else return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line); # endif // BX_ }
void ff::SmallDict::Reserve(size_t newAllocated, bool allowEmptySpace) { size_t oldAllocated = Allocated(); if (newAllocated > oldAllocated) { if (allowEmptySpace) { newAllocated = std::max<size_t>(NearestPowerOfTwo(newAllocated), 4); } size_t byteSize = sizeof(Data) + newAllocated * sizeof(Entry) - sizeof(Entry); _data = (Data *)_aligned_realloc(_data, byteSize, __alignof(Data)); _data->allocated = newAllocated; _data->size = oldAllocated ? _data->size : 0; _data->atomizer = oldAllocated ? _data->atomizer : &ProcessGlobals::Get()->GetStringCache(); } }
static void *a_realloc(void *ptr, size_t size) { #ifdef ALIGNED_MALLOC return _aligned_realloc(ptr, size, ALIGNMENT); #elif ALIGNMENT_HACK long diff; if (!ptr) return a_malloc(size); diff = ((char *)ptr)[-1]; ptr = realloc((char*)ptr - diff, size + diff); if (ptr) ptr = (char *)ptr + diff; return ptr; #else return realloc(ptr, size); #endif }
void CAEBuffer::ReAlloc(const size_t size) { #if defined(TARGET_WINDOWS) m_buffer = (uint8_t*)_aligned_realloc(m_buffer, size, 16); #else uint8_t* tmp = (uint8_t*)_aligned_malloc(size, 16); if (m_buffer) { size_t copy = std::min(size, m_bufferSize); memcpy(tmp, m_buffer, copy); _aligned_free(m_buffer); } m_buffer = tmp; #endif m_bufferSize = size; m_bufferPos = std::min(m_bufferPos, m_bufferSize); }
void allocAlignedRealloc(bool bFree) { void* leaked = _aligned_offset_malloc(64, 16, 1); int* leaked_memory = (int*)_aligned_malloc(64, 16); int* leaked_memory_dbg = (int*)_aligned_malloc_dbg(32, 16, __FILE__, __LINE__); leaked = (int*)_aligned_offset_realloc(leaked, 48, 16, 2); leaked_memory = (int*)_aligned_realloc(leaked_memory, 128, 16); leaked_memory_dbg = (int*)_aligned_realloc_dbg(leaked_memory_dbg, 48, 16, __FILE__, __LINE__); leaked = (int*)_aligned_offset_recalloc(leaked, 1, 52, 16, 2); leaked_memory = (int*)_aligned_recalloc(leaked_memory, 1, 132, 16); leaked_memory_dbg = (int*)_aligned_recalloc_dbg(leaked_memory_dbg, 1, 64, 16, __FILE__, __LINE__); if (bFree) { _aligned_free(leaked); _aligned_free(leaked_memory); _aligned_free_dbg(leaked_memory_dbg); } }
/** * Reallocate memory, with alignment. */ void * _mesa_align_realloc(void *oldBuffer, size_t oldSize, size_t newSize, unsigned long alignment) { #if defined(_WIN32) && defined(_MSC_VER) (void) oldSize; return _aligned_realloc(oldBuffer, newSize, alignment); #else const size_t copySize = (oldSize < newSize) ? oldSize : newSize; void *newBuf = _mesa_align_malloc(newSize, alignment); if (newBuf && oldBuffer && copySize > 0) { memcpy(newBuf, oldBuffer, copySize); } if (oldBuffer) _mesa_align_free(oldBuffer); return newBuf; #endif }
void* MemDefaultAllocator::ReallocAligned(void* pOldPtr, size_t size, size_t alignment, const char* tag, const char* pFile, unsigned int Line) { BEHAVIAC_UNUSED_VAR(alignment); BEHAVIAC_UNUSED_VAR(tag); BEHAVIAC_UNUSED_VAR(pFile); BEHAVIAC_UNUSED_VAR(Line); if (pOldPtr) { #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().UnRegisterPtr(pOldPtr); } } #endif } #if BEHAVIAC_COMPILER_MSVC void* p = _aligned_realloc(pOldPtr, size, alignment); #else void* p = realloc(pOldPtr, size); #endif//BEHAVIAC_COMPILER_MSVC #if BEHAVIAC_DEBUG_MEMORY_STATS if (m_bEnablePtrSizeRegister) { ScopedInt_t scopedInt(GetThreadInt()); if (scopedInt.equal(1)) { GetPtrSizeRegister().RegisterPtrSize(p, size); } } #endif return p; }
void *av_realloc(void *ptr, size_t size) { #if CONFIG_MEMALIGN_HACK int diff; #endif /* let's disallow possibly ambiguous cases */ if (size > (max_alloc_size - 32)) return NULL; #if CONFIG_MEMALIGN_HACK //FIXME this isn't aligned correctly, though it probably isn't needed if (!ptr) return av_malloc(size); diff = ((char *)ptr)[-1]; av_assert0(diff>0 && diff<=ALIGN); ptr = realloc((char *)ptr - diff, size + diff); if (ptr) ptr = (char *)ptr + diff; return ptr; #elif HAVE_ALIGNED_MALLOC return _aligned_realloc(ptr, size + !size, ALIGN); #else #ifdef USE_MEM_STATS if (ptr) mem_cur -= malloc_usable_size(ptr); printf("realloc(%p, %ld)\n", ptr, size); ptr = realloc(ptr, size + !size); if (ptr) { mem_cur += malloc_usable_size(ptr); if (mem_cur > mem_max) { mem_max = mem_cur; printf("mem_max=%d\n", mem_max); } } return ptr; #else return realloc(ptr, size + !size); #endif #endif }
void *av_realloc(void *ptr, size_t size) { #if CONFIG_MEMALIGN_HACK int diff; #endif /* let's disallow possible ambiguous cases */ if(size > (INT_MAX-16) ) return NULL; #if CONFIG_MEMALIGN_HACK //FIXME this isn't aligned correctly, though it probably isn't needed if(!ptr) return av_malloc(size); diff= ((char*)ptr)[-1]; return (char*)realloc((char*)ptr - diff, size + diff) + diff; #elif HAVE_ALIGNED_MALLOC return _aligned_realloc(ptr, size, 32); #else return realloc(ptr, size); #endif }
void* MikMod_realloc(void *data, size_t size) { if (data) { #if defined __MACH__ || defined __QNXNTO__ void *d = realloc(data, size); if(!d) { _mm_errno = MMERR_OUT_OF_MEMORY; if(_mm_errorhandler) { _mm_errorhandler(); } } return d; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) return _aligned_realloc(data, size, ALIGN_STRIDE); #else unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer((char*)newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); }
/** * Reallocate a block of memory with a new size, with the start address * being aligned to a multiple of \p align bytes. * \remarks The returned block of memory must be freed with AlignedFree. * \warning The alignment must be the same as initially passed to * AlignedMalloc. */ void* AlignedRealloc (void* ptr, size_t size, size_t align) { #if !defined(CS_NO_PTMALLOC) void* newPtr = ptmalloc_::ptrealloc (ptr, size); if ((newPtr != ptr) && ((((uintptr_t)newPtr) / align * align) != (uintptr_t)newPtr)) { // Bah, alignment borked. Need to alloc again :( void* newPtrAligned = ptmalloc_::ptmemalign (align, size); memcpy (newPtrAligned, newPtr, size); ptfree (newPtr); newPtr = newPtrAligned; } return newPtr; #elif defined(CS_HAVE__ALIGNED_MALLOC) return _aligned_realloc (ptr, size, align); #else void* orgPtr = *(((void**)ptr) - 1); uintptr_t offsetToData = (uintptr_t)ptr - (uintptr_t)orgPtr; void* newPtr = cs_realloc (orgPtr, size + align + sizeof(void*)); uintptr_t ptrInt = (intptr_t)newPtr; ptrInt = (ptrInt + align + sizeof(void*)) / align * align; uintptr_t newOffsetToData = ptrInt - (uintptr_t)newPtr; if (newOffsetToData != offsetToData) { // Ensure realloced data is aligned again memmove ((void*)(ptrInt), (void*)((uintptr_t)newPtr + offsetToData), size); } *(((void**)ptrInt) - 1) = newPtr; return (void*)ptrInt; #endif }
void* MikMod_realloc(void *data, size_t size) { return realloc(data, size); #if 0 if (data) { #if defined __MACH__ void *d = realloc(data, size); if (d) { return d; } return 0; #elif (defined _WIN32 || defined _WIN64) && !defined(_WIN32_WCE) return _aligned_realloc(data, size, ALIGN_STRIDE); #else unsigned char *newPtr = (unsigned char *)realloc(get_pointer(data), size + ALIGN_STRIDE + sizeof(void*)); return align_pointer(newPtr, ALIGN_STRIDE); #endif } return MikMod_malloc(size); #endif }
void *av_realloc(void *ptr, FF_INTERNAL_MEM_TYPE size) { #if CONFIG_MEMALIGN_HACK int diff; #endif /* let's disallow possible ambiguous cases */ if(size > (INT_MAX-16) ) return NULL; #if CONFIG_MEMALIGN_HACK //FIXME this isn't aligned correctly, though it probably isn't needed if(!ptr) return av_malloc(size); diff= ((char*)ptr)[-1]; return (char*)realloc((char*)ptr - diff, size + diff) + diff; #else // RJVB # if defined(_MSC_VER) || defined(__MINGW32__) return _aligned_realloc(ptr, size, 16); # else return realloc(ptr, size); # endif #endif }