editor: #undef O after use
[0verkill.git] / error.h
blob4f9918346a620341f0671d27974e525782983ef6
1 /* this code is stolen from the links browser, it was written by Mikulas Patocka */
3 #ifndef __ERROR_H
4 #define __ERROR_H
7 #include "cfg.h"
8 #ifndef WIN32
9 #include "config.h"
10 #else
11 #define inline __inline
12 #endif
13 #include <string.h>
15 #define DUMMY ((void *)-1L)
17 void do_not_optimize_here(void *p);
18 void check_memory_leaks(void);
19 #ifndef WIN32
20 void error(char *, ...);
21 #else
22 #define error(...) while(0);
23 #endif
24 void debug_msg(char *, ...);
25 void int_error(char *, ...);
26 extern int errline;
27 extern char *errfile;
29 #ifdef HAVE_CALLOC
30 #define x_calloc(x) calloc((x), 1)
31 #else
32 static inline void *x_calloc(size_t x)
34 void *p;
35 if ((p = malloc(x))) memset(p, 0, x);
36 return p;
38 #endif
40 #define internal errfile = __FILE__, errline = __LINE__, int_error
41 #define debug errfile = __FILE__, errline = __LINE__, debug_msg
44 #ifdef LEAK_DEBUG
46 extern void *debug_mem_alloc(char *, int, size_t);
47 extern void *debug_mem_calloc(char *, int, size_t);
48 extern void debug_mem_free(char *, int, void *);
49 extern void *debug_mem_realloc(char *, int, void *, size_t);
50 extern void set_mem_comment(void *, char *, int);
52 #define mem_alloc(x) debug_mem_alloc(__FILE__, __LINE__, x)
53 #define mem_calloc(x) debug_mem_calloc(__FILE__, __LINE__, x)
54 #define mem_free(x) debug_mem_free(__FILE__, __LINE__, x)
55 #define mem_realloc(x, y) debug_mem_realloc(__FILE__, __LINE__, x, y)
57 #else
59 static inline void *mem_alloc(size_t size)
61 void *p;
62 if (!size) return DUMMY;
63 if (!(p = malloc(size))) {
64 error("ERROR: out of memory (malloc returned NULL)\n");
65 return NULL;
67 return p;
70 static inline void *mem_calloc(size_t size)
72 void *p;
73 if (!size) return DUMMY;
74 if (!(p = x_calloc(size))) {
75 error("ERROR: out of memory (calloc returned NULL)\n");
76 return NULL;
78 return p;
81 static inline void mem_free(void *p)
83 if (p == DUMMY) return;
84 if (!p) {
85 // internal("mem_free(NULL)");
86 return;
88 free(p);
91 static inline void *mem_realloc(void *p, size_t size)
93 if (p == DUMMY) return mem_alloc(size);
94 if (!p) {
95 // internal("mem_realloc(NULL, %d)", size);
96 return NULL;
98 if (!size) {
99 mem_free(p);
100 return DUMMY;
102 if (!(p = realloc(p, size))) {
103 error("ERROR: out of memory (realloc returned NULL)\n");
104 return NULL;
106 return p;
109 static inline void *debug_mem_alloc(char *f, int l, size_t s) { f=f; l=l; return mem_alloc(s); }
110 static inline void *debug_mem_calloc(char *f, int l, size_t s) { f=f; l=l; return mem_calloc(s); }
111 static inline void debug_mem_free(char *f, int l, void *p) { f=f; l=l; mem_free(p); }
112 static inline void *debug_mem_realloc(char *f, int l, void *p, size_t s) { f=f; l=l; return mem_realloc(p, s); }
113 static inline void set_mem_comment(void *p, char *c, int l) {p=p; c=c; l=l;}
115 #endif
119 #endif /* __ERROR_H */