source: trunk/src/odincrt/malloc.cpp@ 9266

Last change on this file since 9266 was 9266, checked in by sandervl, 23 years ago

include new & delete in memory statistics

File size: 4.2 KB
Line 
1/* $Id: malloc.cpp,v 1.7 2002-09-19 09:38:26 sandervl Exp $ */
2/*
3 * Project Odin Software License can be found in LICENSE.TXT
4 * Memory RTL function wrappers
5 *
6 * Copyright 1999 Sander van Leeuwen
7 *
8 */
9
10//#undef __DEBUG_ALLOC__
11#include <malloc.h>
12#include <umalloc.h>
13#include <os2sel.h>
14
15void * _IMPORT _LNK_CONV _debug_calloc( size_t, size_t, const char *, size_t );
16void _IMPORT _LNK_CONV _debug_free( void *, const char *, size_t );
17void * _IMPORT _LNK_CONV _debug_malloc( size_t, const char *, size_t );
18void * _IMPORT _LNK_CONV _debug_realloc( void *, size_t, const char *, size_t );
19void * _IMPORT _LNK_CONV _debug_umalloc(Heap_t , size_t , const char *,size_t);
20void * _IMPORT _LNK_CONV _debug_ucalloc(Heap_t , size_t, size_t ,const char *,size_t);
21
22#ifdef DEBUG
23unsigned long nrcalls_malloc = 0;
24unsigned long nrcalls_free = 0;
25unsigned long totalmemalloc = 0;
26
27void _LNK_CONV getcrtstat(unsigned long *pnrcalls_malloc,
28 unsigned long *pnrcalls_free,
29 unsigned long *ptotalmemalloc)
30{
31 *pnrcalls_malloc = nrcalls_malloc;
32 *pnrcalls_free = nrcalls_free;
33 *ptotalmemalloc = totalmemalloc;
34}
35
36void __cdecl new_alloc(int size)
37{
38 nrcalls_malloc++;
39 totalmemalloc += size;
40}
41
42void __cdecl delete_free(void *ptr)
43{
44 nrcalls_free++;
45 totalmemalloc -= _msize(ptr);
46}
47
48#endif
49
50void * _LNK_CONV os2calloc( size_t a, size_t b )
51{
52 unsigned short sel = RestoreOS2FS();
53 void *rc;
54
55#ifdef DEBUG
56 totalmemalloc += a*b;
57 nrcalls_malloc++;
58#endif
59 rc = calloc(a,b);
60 SetFS(sel);
61 return rc;
62}
63
64void _LNK_CONV os2free( void *a )
65{
66 unsigned short sel = RestoreOS2FS();
67
68#ifdef DEBUG
69 nrcalls_free++;
70 totalmemalloc -= _msize(a);
71#endif
72 free(a);
73 SetFS(sel);
74}
75
76void * _LNK_CONV os2malloc( size_t a)
77{
78 unsigned short sel = RestoreOS2FS();
79 void *rc;
80
81
82#ifdef DEBUG
83 totalmemalloc += a;
84 nrcalls_malloc++;
85#endif
86 rc = malloc(a);
87 SetFS(sel);
88 return rc;
89}
90
91void * _LNK_CONV os2realloc( void *a, size_t b)
92{
93 unsigned short sel = RestoreOS2FS();
94 void *rc;
95
96#ifdef DEBUG
97 totalmemalloc += (b - _msize(a));
98#endif
99
100 rc = realloc(a, b);
101 SetFS(sel);
102 return rc;
103}
104
105void * _LNK_CONV os2_debug_calloc( size_t a, size_t b, const char *c, size_t d)
106{
107 unsigned short sel = RestoreOS2FS();
108 void *rc;
109
110#ifdef DEBUG
111 totalmemalloc += a*b;
112 nrcalls_malloc++;
113#endif
114
115 rc = _debug_calloc(a,b,c,d);
116 SetFS(sel);
117 return rc;
118}
119
120void _LNK_CONV os2_debug_free( void *a, const char *b, size_t c)
121{
122 unsigned short sel = RestoreOS2FS();
123
124
125#ifdef DEBUG
126 nrcalls_free++;
127 totalmemalloc -= _msize(a);
128#endif
129 _debug_free(a,b,c);
130 SetFS(sel);
131}
132
133void * _LNK_CONV os2_debug_malloc( size_t a, const char *b, size_t c)
134{
135 unsigned short sel = RestoreOS2FS();
136 void *rc;
137
138#ifdef DEBUG
139 totalmemalloc += a;
140 nrcalls_malloc++;
141#endif
142 rc = _debug_calloc(1,a,b,c);
143 SetFS(sel);
144 return rc;
145}
146
147void * _LNK_CONV os2_debug_realloc( void *a, size_t b, const char *c, size_t d)
148{
149 unsigned short sel = RestoreOS2FS();
150 void *rc;
151
152#ifdef DEBUG
153 totalmemalloc += (b - _msize(a));
154#endif
155 rc = _debug_realloc(a,b,c,d);
156 SetFS(sel);
157 return rc;
158}
159
160void * _LNK_CONV os2_umalloc(Heap_t a, size_t b)
161{
162 unsigned short sel = RestoreOS2FS();
163 void *rc;
164
165#ifdef DEBUG
166 totalmemalloc += b;
167 nrcalls_malloc++;
168#endif
169 rc = _umalloc(a,b);
170 SetFS(sel);
171 return rc;
172}
173
174void * _LNK_CONV os2_ucalloc(Heap_t a, size_t b, size_t c)
175{
176 unsigned short sel = RestoreOS2FS();
177 void *rc;
178
179#ifdef DEBUG
180 totalmemalloc += b*c;
181 nrcalls_malloc++;
182#endif
183 rc = _ucalloc(a,b,c);
184 SetFS(sel);
185 return rc;
186}
187
188void * _LNK_CONV os2_debug_umalloc(Heap_t a, size_t b, const char *c, size_t d)
189{
190 unsigned short sel = RestoreOS2FS();
191 void *rc;
192
193#ifdef DEBUG
194 totalmemalloc += b;
195 nrcalls_malloc++;
196#endif
197 rc = _debug_ucalloc(a, 1, b,c,d);
198 SetFS(sel);
199 return rc;
200}
201
202void * _LNK_CONV os2_debug_ucalloc(Heap_t a, size_t b, size_t c, const char *d, size_t e)
203{
204 unsigned short sel = RestoreOS2FS();
205 void *rc;
206
207#ifdef DEBUG
208 totalmemalloc += b*c;
209 nrcalls_malloc++;
210#endif
211 rc = _debug_ucalloc(a,b,c,d,e);
212 SetFS(sel);
213 return rc;
214}
215
Note: See TracBrowser for help on using the repository browser.