1 krisbash 1.1 #ifndef _pal_memory_h
2 #define _pal_memory_h
3
4 #include "nits/base/nits.h"
5
6 PAL_BEGIN_EXTERNC
7
8 /* Defining FASTER macro disables malloc injection
9 to reduce overhead for manual performance benchmarking.
10 It is not defined durring normal builds.
11 */
12 #if !defined(FASTER) && !defined(USE_ALLOCATOR)
13
14 PAL_INLINE void *malloc_Injected(
15 size_t length,
16 NitsCallSite cs)
17 {
18 if (NitsShouldFault(cs, NitsAutomatic))
19 return NULL;
20
21 return SystemMalloc(length);
22 krisbash 1.1 }
23
24 #ifdef PAL_Malloc
25 # undef PAL_Malloc
26 #endif
27 #define PAL_Malloc(n) malloc_Injected(n, NitsHere())
28
29 PAL_INLINE void *calloc_Injected(
30 size_t num,
31 size_t size,
32 NitsCallSite cs)
33 {
34 if (NitsShouldFault(cs, NitsAutomatic))
35 return NULL;
36
37 return SystemCalloc(num, size);
38 }
39
40 #ifdef PAL_Calloc
41 # undef PAL_Calloc
42 #endif
43 krisbash 1.1 #define PAL_Calloc(n, s) calloc_Injected(n, s, NitsHere())
44
45 PAL_INLINE void *realloc_Injected(
46 _In_ void *originalBuffer,
47 size_t length,
48 NitsCallSite cs)
49 {
50 if (NitsShouldFault(cs, NitsAutomatic))
51 return NULL;
52
53 return SystemRealloc(originalBuffer, length);
54 }
55
56 #ifdef PAL_Realloc
57 # undef PAL_Realloc
58 #endif
59 #define PAL_Realloc(p, n) realloc_Injected(p, n, NitsHere())
60
61 #endif
62
63 /*
64 krisbash 1.1 // Required for PAL_Malloca
65 //
66 // (FUTURE) Defines a stack buffer for PAL_Malloca. On platforms that don't
67 // support 'alloca', reserves some space for PAL_Malloca to allocate
68 // from before falling back to malloc.
69 PAL_MALLOCA_STACK(initialSize)
70
71 // attempts to allocate from the stack via 'alloca' if available,
72 // otherwise from malloc. Must be freed by PAL_Freea.
73 void* PAL_Malloca(size_t sz)
74
75 // free an Alloca pointer. p may be null.
76 void PAL_Freea(void *p)
77 */
78 #if defined(_MSC_VER) && !defined(PAL_NO_ALLOCA)
79 #include <malloc.h>
80 #define PAL_MALLOCA_STACK(initialSize) char _Pal_Malloca_Buffer[(initialSize)>0]
81 #define PAL_Malloca(sz) (&_Pal_Malloca_Buffer, NitsShouldFault(NitsHere(), NitsAutomatic) ? NULL : _malloca((sz)))
82 #define PAL_Freea(p) _freea((p))
83 #elif defined(__GNUC__) && !defined(PAL_NO_ALLOCA)
84 #include <alloca.h>
85 krisbash 1.1 #define PAL_MALLOCA_STACK(initialSize) char _Pal_Malloca_Buffer[(initialSize)>0]
86 #define PAL_Malloca(sz) (&_Pal_Malloca_Buffer, NitsShouldFault(NitsHere(), NitsAutomatic) ? NULL : alloca((sz)))
87 #define PAL_Freea(p)
88 #else
89 /* TODO/FIXME: add bookkeeping to support allocating from a fixed sized stack
90 buffer, failing over to malloc when it runs out. */
91 #define PAL_MALLOCA_STACK(initialSize) char _Pal_Malloca_Buffer[(initialSize)>0]
92 #define PAL_Malloca(sz) (&_Pal_Malloca_Stack, PAL_Malloc((sz)))
93 #define PAL_Freea(p) PAL_Free((p))
94 #endif
95
96 PAL_END_EXTERNC
97
98 #endif /* _pal_memory_h */
|