CMR  1.3.0
env_internal.h
Go to the documentation of this file.
1 #ifndef CMR_ENV_INTERNAL_H
2 #define CMR_ENV_INTERNAL_H
3 
4 #include <stdio.h>
5 #include <stdbool.h>
6 #include <stdarg.h>
7 
8 #define CMR_UNUSED(x) (void)(x)
9 
10 #if defined(CMR_DEBUG)
11 
12 static inline
13 void CMRdbgMsg(int indent, const char* format, ...)
14 {
15  va_list args;
16 
17  for (int i = 0; i < indent; ++i)
18  putchar(' ');
19  va_start(args, format);
20  vprintf(format, args);
21  va_end(args);
22  fflush(stdout);
23 }
24 
25 #else /* !CMR_DEBUG */
26 
27 static inline
28 void CMRdbgMsg(int indent, const char* format, ...)
29 {
30  CMR_UNUSED(indent);
31  CMR_UNUSED(format);
32 }
33 /*#define CMRdbgMsg(...) */
34 
35 #endif /* CMR_DEBUG */
36 
37 
38 typedef struct
39 {
40  char* memory;
41  size_t top;
42 } CMR_STACK;
43 
45 {
46  char* errorMessage;
48  FILE* output;
49  bool closeOutput;
50  int verbosity;
51  int numThreads;
53  size_t numStacks;
54  size_t memStacks;
55  size_t currentStack;
57 };
58 
59 #include <cmr/env.h>
60 
68 #define CMRallocStack(cmr, ptr) \
69  _CMRallocStack(cmr, (void**) ptr, sizeof(**ptr))
70 
77 CMR_EXPORT
79  CMR* cmr,
80  void** ptr,
81  size_t size
82 );
83 
88 #define CMRfreeStack(cmr, ptr) \
89  _CMRfreeStack(cmr, (void**) ptr, sizeof(**ptr))
90 
97 CMR_EXPORT
99  CMR* cmr,
100  void** ptr
101 );
102 
107 #define CMRallocStackArray(cmr, ptr, length) \
108  _CMRallocStack(cmr, (void**) ptr, sizeof(**ptr) * (length))
109 
114 #define CMRfreeStackArray(cmr, ptr) \
115  _CMRfreeStack(cmr, (void**) ptr)
116 
117 #if !defined(NDEBUG)
118 
126  CMR* cmr
127 );
128 
129 #else
130 
131 static inline
133  CMR* cmr
134 )
135 {
136 
137 }
138 
139 #endif /* !NDEBUG */
140 
142  CMR* cmr,
143  const char* format, ...
144 );
145 
146 size_t CMRgetStackUsage(
147  CMR* cmr
148 );
149 
150 char* CMRconsistencyMessage(const char* format, ...);
151 
152 #if !defined(NDEBUG)
153 
164 #define CMRconsistencyAssert( call ) \
165  do \
166  { \
167  char* __message = call; \
168  if (__message) \
169  { \
170  fflush(stdout); \
171  fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, __message); \
172  fflush(stderr); \
173  free(__message); \
174  exit(1); \
175  } \
176  } \
177  while (false);
178 
179 #else
180 
181 #define CMRconsistencyAssert( call )
182 
183 #endif
184 
185 #endif /* CMR_ENV_INTERNAL_H */
Basic functionality of the software library.
CMR_ERROR
Type for return codes of library functions.
Definition: env.h:27
size_t CMRgetStackUsage(CMR *cmr)
Definition: env.c:388
char * CMRconsistencyMessage(const char *format,...)
Definition: env.c:401
#define CMR_UNUSED(x)
Definition: env_internal.h:8
void CMRassertStackConsistency(CMR *cmr)
Checks stack protection fields for corruption.
Definition: env.c:328
CMR_EXPORT CMR_ERROR _CMRfreeStack(CMR *cmr, void **ptr)
Carries out the deallocation for CMRfreeStack.
Definition: env.c:273
CMR_EXPORT CMR_ERROR _CMRallocStack(CMR *cmr, void **ptr, size_t size)
Carries out the allocation for CMRallocStack.
Definition: env.c:198
static void CMRdbgMsg(int indent, const char *format,...)
Definition: env_internal.h:28
void CMRraiseErrorMessage(CMR *cmr, const char *format,...)
Definition: env.c:355
Definition: env_internal.h:45
size_t numStacks
Number of allocated stacks in stack array.
Definition: env_internal.h:53
size_t memStacks
Memory for stack array.
Definition: env_internal.h:54
CMR_STACK * stacks
Array of stacks.
Definition: env_internal.h:56
int numThreads
Number of threads to use.
Definition: env_internal.h:51
char * errorMessage
Error message.
Definition: env_internal.h:46
int verbosity
Verbosity level.
Definition: env_internal.h:50
bool closeOutput
Whether to close the output stream at the end.
Definition: env_internal.h:49
size_t currentStack
Index of last used stack.
Definition: env_internal.h:55
FILE * output
Output stream or NULL if silent.
Definition: env_internal.h:48
Definition: env_internal.h:39
size_t top
First used byte.
Definition: env_internal.h:41
char * memory
Raw memory.
Definition: env_internal.h:40