Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
static void*
_memory_allocate_malloc(hash_t context, size_t size, unsigned  int align, unsigned int hint) {
	void* block;
	FOUNDATION_UNUSED(context);
	align = _memory_get_align(align);
	block = _memory_allocate_malloc_raw(size, align, hint);
	if (block && (hint & MEMORY_ZERO_INITIALIZED))
		memset(block, 0, (size_t)size);
	return block;
}
Ejemplo n.º 3
0
void* memory_allocate( uint64_t size, unsigned int align, memory_hint_t hint )
{
	void* p;
	if( ( hint == MEMORY_TEMPORARY ) && _memory_temporary.storage && ( size + align < _memory_temporary.maxchunk ) )
	{
		align = _memory_get_align( align );
		p = _memory_align_pointer( _atomic_allocate_linear( size + align ), align );
	}
	else
	{		
		p = _memsys.allocate( memory_context(), size, align, hint );
	}
	_memory_track( p, size );
	return p;
}
Ejemplo n.º 4
0
void* memory_allocate( uint64_t context, uint64_t size, unsigned int align, int hint )
{
	void* p;
	if( ( hint & MEMORY_TEMPORARY ) && _memory_temporary.storage && ( size + align < _memory_temporary.maxchunk ) )
	{
		align = _memory_get_align( align );
		if( align < FOUNDATION_SIZE_POINTER )
			align = FOUNDATION_SIZE_POINTER;
		p = _memory_align_pointer( _atomic_allocate_linear( size + align ), align );
		FOUNDATION_ASSERT( !( (uintptr_t)p & 1 ) );
		memset( p, 0, (size_t)size );
	}
	else
	{
		p = _memory_system.allocate( context ? context : memory_context(), size, align, hint );
	}
	_memory_track( p, size );
	return p;
}
Ejemplo n.º 5
0
static void* _memory_allocate_malloc( uint16_t context, uint64_t size, unsigned int align, memory_hint_t hint )
{
	align = _memory_get_align( align );
#if FOUNDATION_PLATFORM_WINDOWS
	return _aligned_malloc( (size_t)size, align );
#elif FOUNDATION_PLATFORM_POSIX && !FOUNDATION_PLATFORM_ANDROID
	void* memory = 0;
	if( !align )
		return malloc( (size_t)size );
	/*int result = posix_memalign( &memory, align, (size_t)size );	
	if( result || !memory )
		log_panicf( ERROR_OUT_OF_MEMORY, "Unable to allocate memory: %s", system_error_message( 0 ) );
	return ( result == 0 ) ? memory : 0;*/
	memory = aligned_alloc( align, (size_t)size );
	if( !memory )
		log_panicf( ERROR_OUT_OF_MEMORY, "Unable to allocate memory: %s", system_error_message( 0 ) );
	return memory;
#else
	void* memory = malloc( size + align );
	memory = _memory_align_pointer( memory, align );
	return memory;
#endif
}
Ejemplo n.º 6
0
static void*
_memory_reallocate_malloc(void* p, size_t size, unsigned  int align, size_t oldsize) {
#if ( FOUNDATION_SIZE_POINTER == 4 ) && FOUNDATION_PLATFORM_WINDOWS
	FOUNDATION_UNUSED(oldsize);
	align = _memory_get_align(align);
#  if BUILD_ENABLE_MEMORY_GUARD
	if (p) {
		p = _memory_guard_verify(p);
		p = _aligned_realloc(p, (size_t)size + FOUNDATION_MAX_ALIGN * 3, align);
	}
	else {
		p = _aligned_malloc((size_t)size + FOUNDATION_MAX_ALIGN * 3, align);
	}
	if (p)
		p = _memory_guard_initialize(p, (size_t)size);
	return p;
#  else
	return _aligned_realloc(p, (size_t)size, align);
#  endif
#else
	void* memory;
	void* raw_p;

	align = _memory_get_align(align);

	memory = p;
#  if BUILD_ENABLE_MEMORY_GUARD
	if (memory)
		memory = _memory_guard_verify(memory);
#  endif
	raw_p = memory ? *((void**)memory - 1) : nullptr;
	memory = nullptr;

#if FOUNDATION_PLATFORM_WINDOWS
	if (raw_p && !((uintptr_t)raw_p & 1)) {
		size_t padding = (align > FOUNDATION_SIZE_POINTER ? align : FOUNDATION_SIZE_POINTER);
#  if BUILD_ENABLE_MEMORY_GUARD
		size_t extra_padding = FOUNDATION_MAX_ALIGN * 3;
#  else
		size_t extra_padding = 0;
#  endif
		void* raw_memory = _aligned_realloc(raw_p, size + padding + extra_padding, align ? align : 8);
		if (raw_memory) {
			memory = pointer_offset(raw_memory, padding);
			*((void**)memory - 1) = raw_memory;
#  if BUILD_ENABLE_MEMORY_GUARD
			memory = _memory_guard_initialize(memory, size);
#  endif
		}
	}
	else {
#  if FOUNDATION_SIZE_POINTER == 4
		memory = _memory_allocate_malloc_raw(size, align, 0U);
#  else
		memory = _memory_allocate_malloc_raw(size, align,
		                                     (raw_p && ((uintptr_t)raw_p < 0xFFFFFFFFULL)) ?
		                                       MEMORY_32BIT_ADDRESS : 0U);
#  endif
		if (p && memory && oldsize)
			memcpy(memory, p, (size < oldsize) ? size : oldsize);
		_memory_deallocate_malloc(p);
	}

#else //!FOUNDATION_PLATFORM_WINDOWS

//If we're on ARM the realloc can return a 16-bit aligned address, causing raw pointer store to SIGILL
//Realigning does not work since the realloc memory copy preserve cannot be done properly. Revert to normal alloc-and-copy
//Same with alignment, since we cannot guarantee that the returned memory block offset from start of actual memory block
//is the same in the reallocated block as the original block, we need to alloc-and-copy to get alignment
//Memory guard introduces implicit alignments as well so alloc-and-copy for that
#if !FOUNDATION_ARCH_ARM && !FOUNDATION_ARCH_ARM_64 && !BUILD_ENABLE_MEMORY_GUARD
	if (!align && raw_p && !((uintptr_t)raw_p & 1)) {
		void* raw_memory = realloc(raw_p, (size_t)size + FOUNDATION_SIZE_POINTER);
		if (raw_memory) {
			*(void**)raw_memory = raw_memory;
			memory = pointer_offset(raw_memory, FOUNDATION_SIZE_POINTER);
		}
	}
	else
#endif
	{
#  if FOUNDATION_SIZE_POINTER == 4
#    if !BUILD_ENABLE_LOG
		FOUNDATION_UNUSED(raw_p);
#    endif
		memory = _memory_allocate_malloc_raw(size, align, 0U);
#  else
		memory = _memory_allocate_malloc_raw(size, align,
		                                     (raw_p && ((uintptr_t)raw_p < 0xFFFFFFFFULL)) ?
		                                       MEMORY_32BIT_ADDRESS : 0U);
#  endif
		if (p && memory && oldsize)
			memcpy(memory, p, (size < oldsize) ? (size_t)size : (size_t)oldsize);
		_memory_deallocate_malloc(p);
	}

#endif

	if (!memory) {
		string_const_t errmsg = system_error_message(0);
		log_panicf(HASH_MEMORY, ERROR_OUT_OF_MEMORY,
		           STRING_CONST("Unable to reallocate memory (%" PRIsize " -> %" PRIsize " @ 0x%" PRIfixPTR ", raw 0x%" PRIfixPTR "): %.*s"),
		           oldsize, size, (uintptr_t)p, (uintptr_t)raw_p, STRING_FORMAT(errmsg));
	}

	return memory;
#endif
}
Ejemplo n.º 7
0
static FOUNDATION_CONSTCALL FOUNDATION_FORCEINLINE int unsigned
_memory_get_align_forced(unsigned int align) {
	align = _memory_get_align(align);
	return align > FOUNDATION_SIZE_POINTER ? align : FOUNDATION_SIZE_POINTER;
}