HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_StackAlloc.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_StackAlloc.h ( UT Library, C++)
7  *
8  * COMMENTS: Wrapper for alloca.
9  */
10 
11 #ifndef __UT_StackAlloc__
12 #define __UT_StackAlloc__
13 
14 #include "UT_Assert.h"
15 
16 #ifdef WIN32
17  #include <malloc.h>
18 #else
19  #if defined(LINUX) || defined(MBSD)
20  #include <stdlib.h>
21  #else
22  #include <alloca.h>
23  #endif
24 #endif
25 
26 // The maximum size of an object that we would allocate on the stack,
27 // in bytes
28 #define UT_STACK_LIMIT 131072
29 
30 // Sentinel values to be stored in the first 4 bytes of the stack data
31 #define UT_STACK_ALLOCA 100
32 #define UT_STACK_MALLOC 200
33 
34 #define UTstackAlloc(size) (void *)(&((size < UT_STACK_LIMIT) ? \
35  (*((int32 *)::alloca(size+2*sizeof(int32))) = UT_STACK_ALLOCA) : \
36  (*((int32 *)::malloc(size+2*sizeof(int32))) = UT_STACK_MALLOC)) + 2)
37 
38 #define UTstackFree(ptr) \
39  { \
40  UT_ASSERT_P(*((int32 *)ptr - 2) == UT_STACK_MALLOC || \
41  *((int32 *)ptr - 2) == UT_STACK_ALLOCA); \
42  if (*((int32 *)ptr - 2) == UT_STACK_MALLOC) \
43  ::free(((int32 *)ptr - 2)); \
44  }
45 
46 #endif