[Xquartz-dev] ALLOCATE_LOCAL / crashes

Greg Parker gparker at apple.com
Fri Dec 7 15:27:17 PST 2007


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 at apple.com     Runtime Wrangler




More information about the Xquartz-dev mailing list