0798860fa89a625aad4bfbee1c9cfffd88fd70bc
[satune.git] / src / mymemory.h
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      version 2 as published by the Free Software Foundation.
8  */
9
10 /** @file mymemory.h
11  *  @brief Memory allocation functions.
12  */
13
14 #ifndef CSAT_MY_MEMORY_H
15 #define CSAT_MY_MEMORY_H
16 #include <limits.h>
17 #include <stddef.h>
18 #include <stdlib.h>
19
20 #include "config.h"
21
22 //#define SATCHECK_CONFIG
23
24 /*
25    void * ourmalloc(size_t size);
26    void ourfree(void *ptr);
27    void * ourcalloc(size_t count, size_t size);
28    void * ourrealloc(void *ptr, size_t size);
29  */
30
31 #ifdef SATCHECK_CONFIG
32 void *model_malloc(size_t size);
33 void model_free(void *ptr);
34 void *model_calloc(size_t count, size_t size);
35 void *model_realloc(void *ptr, size_t size);
36
37
38 #define ourmalloc model_malloc
39 #define ourfree model_free
40 #define ourrealloc model_realloc
41 #define ourcalloc model_calloc
42
43 #else
44 static inline void *ourmalloc(size_t size) { return malloc(size); }
45 static inline void ourfree(void *ptr) { free(ptr); }
46 static inline void *ourcalloc(size_t count, size_t size) { return calloc(count, size); }
47 static inline void *ourrealloc(void *ptr, size_t size) { return realloc(ptr, size); }
48 #endif
49
50 #define CMEMALLOC                           \
51         void *operator new(size_t size) {       \
52                 return ourmalloc(size);                \
53         }                                                  \
54         void operator delete(void *p, size_t size) {       \
55                 ourfree(p);                                      \
56         }                                                  \
57         void *operator new[](size_t size) {               \
58                 return ourmalloc(size);                          \
59         }                                                    \
60         void operator delete[](void *p, size_t size) {       \
61                 ourfree(p);                                        \
62         }                                                                     \
63         void *operator new(size_t size, void *p) {                                                                                                                      /* placement new */ \
64                 return p;                                                           \
65         }
66
67 #endif/* _MY_MEMORY_H */