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? Thanks, Jeremy Begin forwarded message: From: Greg Parker <gparker@apple.com> Date: December 7, 2007 15:27:17 PST To: Developer talk about Xquartz <xquartz-dev@lists.macosforge.org> Subject: Re: [Xquartz-dev] ALLOCATE_LOCAL / crashes Reply-To: Developer talk about Xquartz <xquartz- dev@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.) #ifndef ALLOCATE_LOCAL_MAX_ALLOCA #define ALLOCATE_LOCAL_MAX_ALLOCA (16*1024) #endif #define ALLOCATE_LOCAL(size) \ ({ \ __SIZE_TYPE__ _x_size = \ sizeof(__SIZE_TYPE__) + (__SIZE_TYPE__) (size); \ __SIZE_TYPE__ *_x_p; \ if (_x_size > ALLOCATE_LOCAL_MAX_ALLOCA) { \ _x_p = (__SIZE_TYPE__ *)ALLOCATE_LOCAL_FALLBACK(_x_size); \ } 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@apple.com Runtime Wrangler _______________________________________________ Xquartz-dev mailing list Xquartz-dev@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo/xquartz-dev