root/src/common/malloc.h @ 1

Revision 1, 6.1 kB (checked in by jinshiro, 17 years ago)
Line 
1// Copyright (c) Athena Dev Teams - Licensed under GNU GPL
2// For more information, see LICENCE in the main folder
3
4#ifndef _MALLOC_H_
5#define _MALLOC_H_
6
7#include "../common/cbasetypes.h"
8
9// Q: What are the 'a'-variant allocation functions?
10// A: They allocate memory from the stack, which is automatically
11//    freed when the invoking function returns.
12//    But it's not portable (http://c-faq.com/malloc/alloca.html)
13//    and I have doubts our implementation works.
14//    -> They should NOT be used, period.
15
16#define ALC_MARK __FILE__, __LINE__, __func__
17
18// disable built-in memory manager when using another manager
19#if defined(MEMWATCH) || defined(DMALLOC) || defined(GCOLLECT) || defined(BCHECK)
20#if !defined(NO_MEMMGR)
21#define NO_MEMMGR
22#endif
23#endif
24
25// Use built-in memory manager by default
26#if !defined(NO_MEMMGR) && !defined(USE_MEMMGR)
27#define USE_MEMMGR
28#endif
29
30
31//////////////////////////////////////////////////////////////////////
32// Athena's built-in Memory Manager
33#ifdef USE_MEMMGR
34
35// Enable memory manager logging by default
36#define LOG_MEMMGR
37
38#       define aMalloc(n)               _mmalloc(n,ALC_MARK)
39#       define aMallocA(n)              _mmalloc(n,ALC_MARK)
40#       define aCalloc(m,n)             _mcalloc(m,n,ALC_MARK)
41#       define aCallocA(m,n)    _mcalloc(m,n,ALC_MARK)
42#       define aRealloc(p,n)    _mrealloc(p,n,ALC_MARK)
43#       define aStrdup(p)               _mstrdup(p,ALC_MARK)
44#       define aFree(p)                 _mfree(p,ALC_MARK)
45
46        void* _mmalloc  (size_t size, const char *file, int line, const char *func);
47        void* _mcalloc  (size_t num, size_t size, const char *file, int line, const char *func);
48        void* _mrealloc (void *p, size_t size, const char *file, int line, const char *func);
49        char* _mstrdup  (const char *p, const char *file, int line, const char *func);
50        void  _mfree    (void *p, const char *file, int line, const char *func);
51
52#else
53
54#       define aMalloc(n)               aMalloc_((n),ALC_MARK)
55#       define aMallocA(n)              aMallocA_((n),ALC_MARK)
56#       define aCalloc(m,n)             aCalloc_((m),(n),ALC_MARK)
57#       define aCallocA(m,n)    aCallocA_(m,n,ALC_MARK)
58#       define aRealloc(p,n)    aRealloc_(p,n,ALC_MARK)
59#       define aStrdup(p)               aStrdup_(p,ALC_MARK)
60#       define aFree(p)                 aFree_(p,ALC_MARK)
61
62        void* aMalloc_  (size_t size, const char *file, int line, const char *func);
63        void* aMallocA_ (size_t size, const char *file, int line, const char *func);
64        void* aCalloc_  (size_t num, size_t size, const char *file, int line, const char *func);
65        void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func);
66        void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func);
67        char* aStrdup_  (const char *p, const char *file, int line, const char *func);
68        void  aFree_    (void *p, const char *file, int line, const char *func);
69
70#endif
71
72////////////// Memory Managers //////////////////
73
74#if defined(MEMWATCH)
75
76#       include "memwatch.h"
77#       define MALLOC(n,file,line,func) mwMalloc((n),(file),(line))
78#       define MALLOCA(n,file,line,func)        mwMalloc((n),(file),(line))
79#       define CALLOC(m,n,file,line,func)       mwCalloc((m),(n),(file),(line))
80#       define CALLOCA(m,n,file,line,func)      mwCalloc((m),(n),(file),(line))
81#       define REALLOC(p,n,file,line,func)      mwRealloc((p),(n),(file),(line))
82#       define STRDUP(p,file,line,func) mwStrdup((p),(file),(line))
83#       define FREE(p,file,line,func)           mwFree((p),(file),(line))
84
85#elif defined(DMALLOC)
86
87#       include "dmalloc.h"
88#       define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0)
89#       define MALLOCA(n,file,line,func)        dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0)
90#       define CALLOC(m,n,file,line,func)       dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0)
91#       define CALLOCA(m,n,file,line,func)      dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0)
92#       define REALLOC(p,n,file,line,func)      dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0)
93#       define STRDUP(p,file,line,func) strdup(p)
94#       define FREE(p,file,line,func)           free(p)
95
96#elif defined(GCOLLECT)
97
98#       include "gc.h"
99#       define MALLOC(n,file,line,func) GC_MALLOC(n)
100#       define MALLOCA(n,file,line,func)        GC_MALLOC_ATOMIC(n)
101#       define CALLOC(m,n,file,line,func)       _bcalloc((m),(n))
102#       define CALLOCA(m,n,file,line,func)      _bcallocA((m),(n))
103#       define REALLOC(p,n,file,line,func)      GC_REALLOC((p),(n))
104#       define STRDUP(p,file,line,func) _bstrdup(p)
105#       define FREE(p,file,line,func)           GC_FREE(p)
106
107        void * _bcalloc(size_t, size_t);
108        void * _bcallocA(size_t, size_t);
109        char * _bstrdup(const char *);
110
111#elif defined(BCHECK)
112
113#       define MALLOC(n,file,line,func) malloc(n)
114#       define MALLOCA(n,file,line,func)        malloc(n)
115#       define CALLOC(m,n,file,line,func)       calloc((m),(n))
116#       define CALLOCA(m,n,file,line,func)      calloc((m),(n))
117#       define REALLOC(p,n,file,line,func)      realloc((p),(n))
118#       define STRDUP(p,file,line,func) strdup(p)
119#       define FREE(p,file,line,func)           free(p)
120
121#else
122
123#       define MALLOC(n,file,line,func) malloc(n)
124#       define MALLOCA(n,file,line,func)        malloc(n)
125#       define CALLOC(m,n,file,line,func)       calloc((m),(n))
126#       define CALLOCA(m,n,file,line,func)      calloc((m),(n))
127#       define REALLOC(p,n,file,line,func)      realloc((p),(n))
128#       define STRDUP(p,file,line,func) strdup(p)
129#       define FREE(p,file,line,func)           free(p)
130
131#endif
132
133/////////////// Buffer Creation /////////////////
134// Full credit for this goes to Shinomori [Ajarn]
135
136#ifdef __GNUC__ // GCC has variable length arrays
137
138        #define CREATE_BUFFER(name, type, size) type name[size]
139        #define DELETE_BUFFER(name)
140
141#else // others don't, so we emulate them
142
143        #define CREATE_BUFFER(name, type, size) type *name = (type *) aCalloc (size, sizeof(type))
144        #define DELETE_BUFFER(name) aFree(name)
145
146#endif
147
148////////////// Others //////////////////////////
149// should be merged with any of above later
150#define CREATE(result, type, number) (result) = (type *) aCalloc ((number), sizeof(type))
151
152#define CREATE_A(result, type, number) (result) = (type *) aCallocA ((number), sizeof(type))
153
154#define RECREATE(result, type, number) (result) = (type *) aRealloc ((result), sizeof(type) * (number))
155
156////////////////////////////////////////////////
157
158bool malloc_verify(void* ptr);
159size_t malloc_usage (void);
160void malloc_init (void);
161void malloc_final (void);
162
163#endif /* _MALLOC_H_ */
Note: See TracBrowser for help on using the browser.