]> git.wincent.com - WOTest.git/blob - WOMalloc.h
Import WOMalloc functions from WODebug
[WOTest.git] / WOMalloc.h
1 /*
2  * WOMalloc.h
3  * WODebug
4  *
5  * Copyright 2006-2009 Wincent Colaiuta.
6  *
7  */
8
9 #ifndef _STDLIB_H_
10 #include <stdlib.h>
11 #endif
12
13 /*! Wrappers for memory allocation functions which can be used to simulate out-of-memory conditions for testing purposes. There are two ways of simulating failures:
14
15 1. set an environment variable to provoke failures (other vars can be used to control when: always, randomly etc)
16 2. use macros to pass an extra "forced failure" parameter
17
18 Environment variables:
19
20 WOMallocFailAlways set to 1 to always fail, 0 to not force failure
21
22 WOMallocFailRandomly (set to value between 0 and 1 indicating desired frequency of failures; 0 is equivalent to no forced failures, 1 is equivalent to "fail always")
23
24 If set WOMallocFailAlways overrides WOMallocFailRandomly
25
26 */
27
28 #pragma mark -
29 #pragma mark Variadic macros
30
31 /*!
32 \name Variadic macros
33
34  These variadic macros wrap the wrapper functions in such a way that the caller can optionally specify an extra "true" or "false" parameter to force an allocation to fail for testing purposes. One way to do this would be to code in C++ or Objective-C++ and overload the functions so as to provide alternative versions with and without the forced failure flags. An alternative approach has been taken here and that is to use variadic macros and some helper functions which do not require function overloading and can therefore be used in normal C code.
35
36 \startgroup
37 */
38
39 #define WO_MALLOC(size, ...)            WOMallocFailv(size, ## __VAR_ARGS__)
40
41 #define WO_CALLOC(count, size, ...)     WOCallocFailv\
42                                         (count, size, ## __VAR_ARGS__)
43
44 #define WO_VALLOC(size, ...)            WOVallocFailv(size, ## __VAR_ARGS__)
45
46 #define WO_REALLOC(ptr, size, ...)      WOReallocFailv\
47                                         (ptr, size, ## __VAR_ARGS__)
48
49 #define WO_REALLOCF(ptr, size, ...)     WOReallocfFailv\
50                                         (ptr, size, ## __VAR_ARGS__)
51
52 /*! \endgroup */
53
54 #pragma mark -
55 #pragma mark Base wrapper functions
56
57 void * WOMalloc(size_t size);
58
59 void * WOCalloc(size_t count, size_t size);
60
61 void * WOValloc(size_t size);
62
63 void * WORealloc(void *ptr, size_t size);
64
65 void * WOReallocf(void *ptr, size_t size);
66
67 #pragma mark -
68 #pragma mark Wrapper functions with forced failure control
69
70 void * WOMallocFail(size_t size, _Bool fail);
71
72 void * WOCallocFail(size_t count, size_t size, _Bool fail);
73
74 void * WOVallocFail(size_t size, _Bool fail);
75
76 void * WOReallocFail(void *ptr, size_t size, _Bool fail);
77
78 void * WOReallocfFail(void *ptr, size_t size, _Bool fail);
79
80 #pragma mark -
81 #pragma mark Variadic helper function prototypes
82
83 void * WOMallocFailv(size_t size, ...);
84
85 void * WOCallocFailv(size_t count, size_t size, ...);
86
87 void * WOVallocFailv(size_t size, ...);
88
89 void * WOReallocFailv(void *ptr, size_t size, ...);
90
91 void * WOReallocfFailv(void *ptr, size_t size, ...);