void*   realloc(void *p, uint64_t size)
{
	void *ptr;
	struct boundary_tag *tag;
	int real_size;

	if ( size == 0 )
	{
		free( p );
		return NULL;
	}
	if ( p == NULL ) return malloc( size );

	liballoc_lock();		// lockit
	tag = (struct boundary_tag*)((unsigned int)p - sizeof( struct boundary_tag ));
	real_size = tag->size;
	liballoc_unlock();

	if ( real_size > size ) real_size = size;

	ptr = malloc( size );
	liballoc_memcpy( ptr, p, real_size );
	free( p );

	return ptr;
}
Exemple #2
0
void*   realloc(void *p, size_t size)
{
    void *ptr;
    struct liballoc_minor *min;
    int real_size;

    if ( size == 0 )
    {
        free( p );
        return NULL;
    }
    if ( p == NULL ) return malloc( size );

    if ( liballoc_lock != NULL ) liballoc_lock();		// lockit
    min = (struct liballoc_minor*)((unsigned int)p - sizeof( struct liballoc_minor ));
    real_size = min->size;
    if ( liballoc_unlock != NULL ) liballoc_unlock();

    if ( real_size > size ) real_size = size;

    ptr = malloc( size );
    liballoc_memcpy( ptr, p, real_size );
    free( p );

    return ptr;
}
Exemple #3
0
void*   PREFIX(realloc)(void *p, size_t size)
{
	void *ptr;
	struct liballoc_minor *min;
	unsigned int real_size;

	// Honour the case of size == 0 => free old and return NULL
	if ( size == 0 )
	{
		PREFIX(free)( p );
		return NULL;
	}

	// In the case of a NULL pointer, return a simple malloc.
	if ( p == NULL ) return PREFIX(malloc)( size );

	// Unalign the pointer if required.
	ptr = p;
	UNALIGN(ptr);

	liballoc_lock();		// lockit

		min = (struct liballoc_minor*)((uintptr_t)ptr - sizeof( struct liballoc_minor ));

		// Ensure it is a valid structure.
		if ( min->magic != LIBALLOC_MAGIC )
		{
			l_errorCount += 1;

			// Check for overrun errors. For all bytes of LIBALLOC_MAGIC
			if (
				((min->magic & 0xFFFFFF) == (LIBALLOC_MAGIC & 0xFFFFFF)) ||
				((min->magic & 0xFFFF) == (LIBALLOC_MAGIC & 0xFFFF)) ||
				((min->magic & 0xFF) == (LIBALLOC_MAGIC & 0xFF))
			   )
			{
				l_possibleOverruns += 1;
				#if defined DEBUG || defined INFO
				serial_printf( "liballoc: ERROR: Possible 1-3 byte overrun for magic %x != %x\n",
									min->magic,
									LIBALLOC_MAGIC );
				FLUSH();
				#endif
			}


			if ( min->magic == LIBALLOC_DEAD )
			{
				#if defined DEBUG || defined INFO
				serial_printf( "liballoc: ERROR: multiple PREFIX(free)() attempt on %x from %x.\n",
										ptr,
										__builtin_return_address(0) );
				FLUSH();
				#endif
			}
			else
			{
				#if defined DEBUG || defined INFO
				serial_printf( "liballoc: ERROR: Bad PREFIX(free)( %x ) called from %x\n",
									ptr,
									__builtin_return_address(0) );
				FLUSH();
				#endif
			}

			// being lied to...
			liballoc_unlock();		// release the lock
			return NULL;
		}

		// Definitely a memory block.

		real_size = min->req_size;

		if ( real_size >= size )
		{
			min->req_size = size;
			liballoc_unlock();
			return p;
		}

	liballoc_unlock();

	// If we got here then we're reallocating to a block bigger than us.
	ptr = PREFIX(malloc)( size );					// We need to allocate new memory
	liballoc_memcpy( ptr, p, real_size );
	PREFIX(free)( p );

	return ptr;
}