Hello!

On Wednesday 06 August 2008 06:09:15 Adam Jiang wrote:
> I am trying to port an application from glibc to uclibc. But I can't find
> the peer of the 'malloc_usable_size' function in uclibc included in my
> toolchain. Should I find an alternative way to implement function or is
> there any possible to get this by using the latest uclibc?

There's no such thing as malloc_usable_size in uClibc.
There even isn't a man page for it on my Debian testing.

> Please give me some advice on this, I will appreciate.

Track the allocated size by yourself.

Or you can go in the uClibc source code and add the necessary support code.

Or you can install a wrapper to malloc in your own application, that would
do smthg like:

void *malloc(size_t size)
{
    static void (*real_malloc)(size_t) = NULL;
    void my_ptr;
    size_t my_size;

    /* The real malloc */
    if( real_malloc == NULL ) {
        char *err;
        dlerror();
        real_malloc = dlsym( RTLD_NEXT, "malloc" );
        err = dlerror();
        if( err != NULL ) {
            fprintf( stderr, "%s\n", err );
            exit( -1 );
        }
        if( real_malloc == malloc ) {
            fprintf( stderr, "Could not find the real malloc\n" );
            exit( -2 );
        }
    }

    /* add a few bytes to store the allocated memory and a magic */
    my_size = sizeof(int) + sizeof(size_t) + size;

    my_ptr = real_malloc( my_size );

    if( my_ptr == NULL ) {
        return NULL;
    }

    *(int*)my_ptr = (int)0xDEADBEEF;
    *(size_t*)( my_ptr + sizeof(int) ) = size;

    return ( my_ptr + sizeof(int) + sizeof(size_t) );
}

size_t malloc_usable_size( void* ptr )
{
    if( *(int*)( ptr - sizeof(size_t) - sizeof(int) ) != 0xDEADBEEF ) {
        fprintf( stderr, "Chunk at %p not from my malloc!\n", ptr );
        exit( -3 );
    }
    return *(size_t*)( ptr - sizeof(size_t) );
}

Of course, you will also have to overload free, realloc, and all functions
that work with allocated memory. Note that the above code may not compile,
it was typed directly in the mail editor!

Regards,
Yann E. MORIN.

-- 
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +0/33 662376056 | Software  Designer | \ / CAMPAIGN     |   ^                |
| --==< °_° >==-- °------------.-------:  X  AGAINST      |  /e\  There is no  |
| http://ymorin.is-a-geek.org/ | * _ * | / \ HTML MAIL    |  """  conspiracy.  |
°------------------------------°-------°------------------°--------------------°

_______________________________________________
uClibc mailing list
uClibc@uclibc.org
http://busybox.net/cgi-bin/mailman/listinfo/uclibc

Reply via email to