valloc() bug on the Mac OS X 10.5

How many of you use the valloc() call?
Did you notice any problem with the valloc() on the Mac OS X 10.5? Recently I noticed one in serious way. I wrote a plugin program which was invoked by a its host program periodically.
Its structure looks like this :

A host program calls “do_some()” of the plugin.

do_some()
{
    prepare_something_when_invoked_at_first();
    really_do_something();
}

really_do_something()
{
    // For data set A
    if( this is first of every 4 calls )
    {
        dataBuffer = valloc() // A
    }
    else
        dataBuffer = realloc( for the amount of data you need );

    copy data into dataBuffer // B

    // For data set B
    dataBuffer = realloc();
    copy data into dataBuffer

    // For data set C
    dataBuffer = realloc();
    copy data into dataBuffer

    // For data set D
    dataBuffer = realloc();
    copy data into dataBuffer

    if( this is 4th call of every 4 calls )
    {
        store the data in dataBuffer into a disk
        release the dataBuffer and delete it.
    }
}

Although A is successfully called, sometimes the code at B complains that the memory space, dataBuffer, is destroyed but accessed, and crashes. When I put an NSLog() or printf() between them, it doesn’t crash anymore. Right after such output function is added, it doesn’t crash and behaves as intended. So, I guess the compiler or something reorders the machine code somehow wrongly, and it makes the difference.

I solved this problem by replacing the valloc() with a malloc(). But then the performance of saving to disk will become slower.
I reported this problem to the Apple Inc.

If you notice problems like this, let’s share.

5 responses to this post.

  1. Hi!

    I wonder what does the valloc() function have to do with performance.

    As far as I know, valloc() merely does “aligning with the system’s page size” in addition to what malloc() does.

    Is there something that valloc() does “secretly”? I’m just curious. :-)

    Reply

  2. Posted by jongampark on October 13, 2008 at 5:42 AM

    It is not secret. It returns memory space aligned with the page size.
    Try searching “Paging” or other terms which are related.

    Reply

  3. Oh, yeah, I got it. If you use valloc(), it certainly lowers the probability of page-in/out. Thanks, I should have given more time to thinking the “true” meaning of aligned with “page size” :-)

    Reply

  4. Posted by Vishal Singh on February 24, 2009 at 5:01 AM

    The valloc() function has the same effect as malloc(), except that the allocated memory is aligned to a page boundary.

    Pointers returned by valloc() may be passed to free() but not to realloc().
    http://www.mkssoftware.com/docs/man3/valloc.3.asp

    Reply

    • Posted by jongampark on February 24, 2009 at 9:59 AM

      Hello, Vishal.
      Thank you for posting your commenting with the link. However, the link you put is not for the Mac OS X.
      This has been long debate regarding to Unix model. There are two variant of Unix. One is System V and the other is BSD. Although different Unix shares same library functions, it has been very well known that things are not really identical. The most notorious example is BSD socket calls like select(). There are many functions which have the same prototypes but work differently on different Unix. Even with Unices that have the same parent, things can be different from Unix to Unix.

      Apple’s MAN page doesn’t say that the realloc() doesn’t work with the valloc(). Probably the text in the link you posted can be true for the Mac OS X also, but there is no such thing mentioned in their own MAN page.

      Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: