CMR  1.3.0
env.h
Go to the documentation of this file.
1 #ifndef CMR_ENV_H
2 #define CMR_ENV_H
3 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include <cmr/config.h>
17 #include <cmr/export.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <assert.h>
22 
23 /* Macro for intended non-use of variables. */
24 #define CMR_UNUSED(x) (void)(x)
25 
26 
31 typedef enum
32 {
33  CMR_OKAY = 0,
41 } CMR_ERROR;
42 
47 #define CMR_CALL(call) \
48  do \
49  { \
50  CMR_ERROR _cmr_error = call; \
51  if (_cmr_error) \
52  { \
53  if (_cmr_error == CMR_ERROR_INPUT) \
54  fprintf(stderr, "User input error"); \
55  else if (_cmr_error == CMR_ERROR_OUTPUT) \
56  fprintf(stderr, "Error when writing user output"); \
57  else if (_cmr_error == CMR_ERROR_MEMORY) \
58  fprintf(stderr, "Memory (re)allocation failed"); \
59  else if (_cmr_error == CMR_ERROR_INVALID) \
60  fprintf(stderr, "Invalid input"); \
61  else if (_cmr_error == CMR_ERROR_TIMEOUT) \
62  fprintf(stderr, "Time limit exceeded"); \
63  else if (_cmr_error == CMR_ERROR_OVERFLOW) \
64  fprintf(stderr, "Integer overflow"); \
65  else if (_cmr_error == CMR_ERROR_STRUCTURE) \
66  fprintf(stderr, "Invalid matrix structure"); \
67  else \
68  fprintf(stderr, "Unknown error"); \
69  fprintf(stderr, " in %s:%d.\n", __FILE__, __LINE__); \
70  return _cmr_error; \
71  } \
72  } while (false)
73 
74 struct CMR_ENVIRONMENT;
75 
82 typedef struct CMR_ENVIRONMENT CMR;
83 
90 CMR_EXPORT
92  CMR** pcmr
93 );
94 
99 CMR_EXPORT
101  CMR** pcmr
102 );
103 
111 #define CMRallocBlock(cmr, ptr) \
112  _CMRallocBlock(cmr, (void**) ptr, sizeof(**ptr))
113 
120 CMR_EXPORT
121 CMR_ERROR _CMRallocBlock(CMR* cmr, void** ptr, size_t size);
122 
127 #define CMRfreeBlock(cmr, ptr) \
128  _CMRfreeBlock(cmr, (void**) ptr, sizeof(**ptr))
129 
136 CMR_EXPORT
137 CMR_ERROR _CMRfreeBlock(CMR* cmr, void** ptr, size_t size);
138 
147 #define CMRallocBlockArray(cmr, ptr, length) \
148  _CMRallocBlockArray(cmr, (void**) ptr, sizeof(**ptr), length)
149 
156 CMR_EXPORT
157 CMR_ERROR _CMRallocBlockArray(CMR* cmr, void** ptr, size_t size, size_t length);
158 
166 #define CMRreallocBlockArray(cmr, ptr, length) \
167  _CMRreallocBlockArray(cmr, (void**) ptr, sizeof(**ptr), length)
168 
175 CMR_EXPORT
176 CMR_ERROR _CMRreallocBlockArray(CMR* cmr, void** ptr, size_t size, size_t length);
177 
186 #define CMRduplicateBlockArray(cmr, ptr, length, source) \
187  _CMRduplicateBlockArray(cmr, (void**) ptr, sizeof(**ptr), length, source)
188 
195 CMR_EXPORT
196 CMR_ERROR _CMRduplicateBlockArray(CMR* cmr, void** ptr, size_t size, size_t length, void* source);
197 
204 #define CMRfreeBlockArray(cmr, ptr) \
205  _CMRfreeBlockArray(cmr, (void**) ptr)
206 
207 CMR_EXPORT
208 CMR_ERROR _CMRfreeBlockArray(CMR* cmr, void** ptr);
209 
214 CMR_EXPORT
215 char* CMRgetErrorMessage(
216  CMR* cmr
217 );
218 
223 CMR_EXPORT
225  CMR* cmr
226 );
227 
228 
229 #ifdef __cplusplus
230 }
231 #endif
232 
233 #endif /* CMR_ENV_H */
234 
CMR_EXPORT CMR_ERROR _CMRfreeBlock(CMR *cmr, void **ptr, size_t size)
Carries out the deallocation for CMRfreeBlock.
Definition: env.c:94
CMR_EXPORT CMR_ERROR CMRfreeEnvironment(CMR **pcmr)
Frees a CMR environment.
Definition: env.c:60
CMR_EXPORT void CMRclearErrorMessage(CMR *cmr)
Clears the error message.
Definition: env.c:402
CMR_EXPORT char * CMRgetErrorMessage(CMR *cmr)
Returns the error message (or NULL if no error with a message occured).
Definition: env.c:397
CMR_EXPORT CMR_ERROR _CMRreallocBlockArray(CMR *cmr, void **ptr, size_t size, size_t length)
Carries out the reallocation for CMRreallocBlockArray.
Definition: env.c:121
CMR_EXPORT CMR_ERROR _CMRallocBlock(CMR *cmr, void **ptr, size_t size)
Carries out the allocation for CMRallocBlock.
Definition: env.c:82
CMR_EXPORT CMR_ERROR _CMRallocBlockArray(CMR *cmr, void **ptr, size_t size, size_t length)
Carries out the allocation for CMRallocBlockArray.
Definition: env.c:108
CMR_EXPORT CMR_ERROR CMRcreateEnvironment(CMR **pcmr)
Allocates and initializes a default CMR environment.
Definition: env.c:20
CMR_ERROR
Type for return codes of library functions.
Definition: env.h:32
@ CMR_ERROR_OVERFLOW
Definition: env.h:38
@ CMR_ERROR_TIMEOUT
Definition: env.h:39
@ CMR_ERROR_MEMORY
Definition: env.h:36
@ CMR_ERROR_INVALID
Definition: env.h:37
@ CMR_OKAY
Definition: env.h:33
@ CMR_ERROR_INPUT
Definition: env.h:34
@ CMR_ERROR_OUTPUT
Definition: env.h:35
@ CMR_ERROR_STRUCTURE
Definition: env.h:40
CMR_EXPORT CMR_ERROR _CMRduplicateBlockArray(CMR *cmr, void **ptr, size_t size, size_t length, void *source)
Carries out the duplication for CMRduplicateBlockArray.
Definition: env.c:132
CMR_EXPORT CMR_ERROR _CMRfreeBlockArray(CMR *cmr, void **ptr)
Definition: env.c:146
Definition: env_internal.h:45