~ivantis/armagetronad/sty+ct+ivantis

« back to all changes in this revision

Viewing changes to src/tools/tMemManager.h

  • Committer: ivantis
  • Date: 2008-09-09 21:33:18 UTC
  • Revision ID: ivantis@ivantis.net-20080909213318-k43y6yuq0zd6wbsa
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
*************************************************************************
 
4
 
 
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
 
6
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
 
7
 
 
8
**************************************************************************
 
9
 
 
10
This program is free software; you can redistribute it and/or
 
11
modify it under the terms of the GNU General Public License
 
12
as published by the Free Software Foundation; either version 2
 
13
of the License, or (at your option) any later version.
 
14
 
 
15
This program is distributed in the hope that it will be useful,
 
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
GNU General Public License for more details.
 
19
 
 
20
You should have received a copy of the GNU General Public License
 
21
along with this program; if not, write to the Free Software
 
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
23
  
 
24
***************************************************************************
 
25
 
 
26
*/
 
27
 
 
28
#ifndef ArmageTron_tMemManager_H
 
29
#define ArmageTron_tMemManager_H
 
30
 
 
31
#include "config.h"
 
32
#include <new>
 
33
 
 
34
#ifdef HAVE_STDLIB
 
35
#include <stdlib.h>
 
36
#endif
 
37
 
 
38
#ifdef HAVE_LIBEFENCE
 
39
#include <efence.h>
 
40
#ifdef DONTUSEMEMMANAGER
 
41
#include <efencepp.h>
 
42
#endif
 
43
#endif
 
44
 
 
45
#ifndef _MSC_VER
 
46
#ifndef _cdecl
 
47
#define _cdecl
 
48
#endif
 
49
#endif
 
50
 
 
51
#include <stdlib.h>
 
52
 
 
53
class tMemManBase{
 
54
public:
 
55
    static void  Check();
 
56
    static void  Profile();
 
57
};
 
58
 
 
59
#ifdef WIN32
 
60
#ifdef DEBUG
 
61
//#define DONTUSEMEMMANAGER
 
62
#endif
 
63
#endif
 
64
 
 
65
#ifdef _MSC_VER
 
66
//#define THROW_BADALLOC _THROW1(std::bad_alloc)
 
67
//#define THROW_NOTHING  _THROW0()
 
68
#define THROW_BADALLOC
 
69
#define THROW_NOTHING
 
70
#else
 
71
#define THROW_BADALLOC throw (std::bad_alloc)
 
72
#define THROW_NOTHING  throw ()
 
73
#endif
 
74
 
 
75
 
 
76
#ifndef DONTUSEMEMMANAGER
 
77
 
 
78
#ifndef NO_MALLOC_REPLACEMENT
 
79
 
 
80
// the following include file was found to disable the macros again
 
81
#include <ios>
 
82
 
 
83
// macros replacing C memory management
 
84
#define malloc(SIZE)                static_cast<void *>(tNEW(char)[SIZE])
 
85
#define calloc(ELEMCOUNT, ELEMSIZE) static_cast<void *>(tNEW(char)[(ELEMCOUNT)*(ELEMSIZE)])
 
86
#define free(BASEADR)               delete[] (reinterpret_cast< char* >(BASEADR))
 
87
#define realloc(BASEADR, NEWSIZE)   realloc not defined
 
88
 
 
89
// and other allocating functions
 
90
#define strdup(ADR)  tStrDup(ADR)
 
91
 
 
92
// implementation
 
93
char * tStrDup( char const * s );
 
94
 
 
95
// call the real malloc functions
 
96
void * real_calloc( size_t nmemb, size_t size ); // call calloc
 
97
void * real_malloc( size_t size );               // call malloc
 
98
void   real_free( void * ptr );                  // call free
 
99
void * real_realloc( void * ptr, size_t size );  // call realloc
 
100
char * real_strdup( char const * ptr );          // calls strdup
 
101
void * real_mmove( void * ptr, size_t size );    // take memory allocated by real_malloc or a C library function and move to managed memory
 
102
char * real_strmove( char * ptr );               // take C string allocated by real_malloc or a C library function and move to managed memory
 
103
#endif
 
104
 
 
105
 
 
106
void* _cdecl operator new       (size_t size) THROW_BADALLOC;
 
107
void  _cdecl operator delete   (void *ptr)  THROW_NOTHING;
 
108
void  operator delete   (void *ptr,bool keep) THROW_NOTHING;
 
109
void* operator new      (size_t size,const char *classn,const char *file,int line)  THROW_BADALLOC;
 
110
void  operator delete   (void *ptr,const char *classn, const char *file,int line)  THROW_NOTHING;
 
111
void* operator new[]    (size_t size)  THROW_BADALLOC;
 
112
void  operator delete[]   (void *ptr) THROW_NOTHING;
 
113
void* operator new[]    (size_t size,const char *classn,const char *file,int line)  THROW_BADALLOC;
 
114
void  operator delete[]   (void *ptr,const char *classname,const char *file,int line)  THROW_NOTHING;
 
115
 
 
116
void * operator new(
 
117
    size_t cb,
 
118
    int nBlockUse,
 
119
    const char * szFileName,
 
120
    int nLine
 
121
);
 
122
 
 
123
void operator delete(
 
124
    void * ptr,
 
125
    int nBlockUse,
 
126
    const char * szFileName,
 
127
    int nLine
 
128
);
 
129
 
 
130
#define tNEW(x) new(#x,__FILE__,__LINE__) x
 
131
 
 
132
#define tMEMMANAGER(classname)  public:void *operator new(size_t s){return tMemMan::Alloc(s); }  void operator delete(void *p){  if (p) tMemMan::Dispose(p); }
 
133
#else
 
134
#define tNEW(x) new x
 
135
#define tMEMMANAGER(classname)  
 
136
 
 
137
// just direct to the real malloc functions
 
138
#define real_malloc(SIZE)                malloc(SIZE)
 
139
#define real_calloc(ELEMCOUNT, ELEMSIZE) calloc(ELEMCOUNT, ELEMSIZE)
 
140
#define real_free(BASEADR)               free(BASEADR)
 
141
#define real_realloc(BASEADR, NEWSIZE)   realloc(BASEADR, NEWSIZE)
 
142
#define real_strdup( PTR )               strdup(PTR)
 
143
#define real_mmove( BASEADR, SIZE )      (BASEADR)
 
144
#define real_strmove( BASEADR )          (BASEADR)
 
145
 
 
146
#endif
 
147
 
 
148
#endif