Fwd: [Xquartz-dev] ALLOCATE_LOCAL / crashes

Jeremy Huddleston jeremyhu at freedesktop.org
Sat Dec 8 12:29:32 PST 2007

Hi Keith,

We've been having some trouble due to alloca(BIG) in the Xquartz  
server.  Could you comment on the acceptability of something like Greg  
suggests below?  Should we put it inside __APPLE__ ifdefs, or do you  
think it would be good across the board?  Or do you have a better  
solution we should think about?


Begin forwarded message:

From: Greg Parker <gparker at apple.com>
Date: December 7, 2007 15:27:17 PST
To: Developer talk about Xquartz <xquartz-dev at lists.macosforge.org>
Subject: Re: [Xquartz-dev] ALLOCATE_LOCAL / crashes
Reply-To: Developer talk about Xquartz <xquartz- 
dev at lists.macosforge.org>

On Dec 7, 2007, at 1:48 PM, Jeremy Huddleston wrote:
> So... I wonder if we should do this across the board when compiling  
> X.  Other things than the server use Xalloca.h.  I'm thinking I  
> should put it in my standard CFLAGS.  Am I being overly cautious and  
> paranoid, or is that something worth doing?

We should do something like this. It uses alloca() if the allocation  
is small enough, otherwise it uses the fallback allocator (probably  
malloc or Xalloc). The start of the block holds the allocated size so  
DEALLOCATE_LOCAL knows whether to call free.

(16KB might be too small on i386 and/or too big on ppc. There's lots  
of gcc-isms here; it should be protected by __GNUC__ or something. I  
think it works in C++ too. I have only tested it trivially.)


ALLOCATE_LOCAL(size)                                            \
({                                                                  \
        __SIZE_TYPE__ _x_size  
=                                         \
            sizeof(__SIZE_TYPE__) + (__SIZE_TYPE__) 
(size);              \
*_x_p;                                            \
        if (_x_size > ALLOCATE_LOCAL_MAX_ALLOCA)  
{                      \
            _x_p = (__SIZE_TYPE__  
        } else  
{                                                        \
            _x_p = (__SIZE_TYPE__  
*)__builtin_alloca(_x_size);          \
        }                                                               \
        if (_x_p) *_x_p =  
_x_size;                                      \
        (void *)(_x_p +  
1);                                             \

#define DEALLOCATE_LOCAL(ptr)                                         \
    ({                                                                \
        __SIZE_TYPE__ *_x_p = (__SIZE_TYPE__ *)(ptr);                 \
        if (_x_p) {                                                   \
            _x_p = _x_p - 1;                                          \
            __SIZE_TYPE__ _x_size = *_x_p;                            \
            if (_x_size > ALLOCATE_LOCAL_MAX_ALLOCA) {                \
                DEALLOCATE_LOCAL_FALLBACK(_x_p);                      \
            }                                                         \
        }                                                             \

Greg Parker     gparker at apple.com     Runtime Wrangler

Xquartz-dev mailing list
Xquartz-dev at lists.macosforge.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3040 bytes
Desc: not available
Url : http://lists.macosforge.org/pipermail/xquartz-dev/attachments/20071208/72b7b5d3/smime.bin

More information about the Xquartz-dev mailing list