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

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

Added wrappers for new & delete operators

File size: 4.1 KB
Line 
1/* $Id: malloc.cpp,v 1.9 2003-01-23 12:33:04 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#define ORIGINAL_VAC_FUNCTIONS
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
36#endif
37
38void * _LNK_CONV CRTWRAP(calloc)( size_t a, size_t b )
39{
40 unsigned short sel = RestoreOS2FS();
41 void *rc;
42
43#ifdef DEBUG
44 totalmemalloc += a*b;
45 nrcalls_malloc++;
46#endif
47 rc = calloc(a,b);
48 SetFS(sel);
49 return rc;
50}
51
52void _LNK_CONV CRTWRAP(free)( void *a )
53{
54 unsigned short sel = RestoreOS2FS();
55
56#ifdef DEBUG
57 nrcalls_free++;
58 totalmemalloc -= _msize(a);
59#endif
60 free(a);
61 SetFS(sel);
62}
63
64void * _LNK_CONV CRTWRAP(malloc)( size_t a)
65{
66 unsigned short sel = RestoreOS2FS();
67 void *rc;
68
69
70#ifdef DEBUG
71 totalmemalloc += a;
72 nrcalls_malloc++;
73#endif
74 rc = malloc(a);
75 SetFS(sel);
76 return rc;
77}
78
79void * _LNK_CONV CRTWRAP(realloc)( void *a, size_t b)
80{
81 unsigned short sel = RestoreOS2FS();
82 void *rc;
83
84#ifdef DEBUG
85 totalmemalloc += (b - _msize(a));
86#endif
87
88 rc = realloc(a, b);
89 SetFS(sel);
90 return rc;
91}
92
93void * _LNK_CONV CRTWRAP(_debug_calloc)( size_t a, size_t b, const char *c, size_t d)
94{
95 unsigned short sel = RestoreOS2FS();
96 void *rc;
97
98#ifdef DEBUG
99 totalmemalloc += a*b;
100 nrcalls_malloc++;
101#endif
102
103 rc = _debug_calloc(a,b,c,d);
104 SetFS(sel);
105 return rc;
106}
107
108void _LNK_CONV CRTWRAP(_debug_free)( void *a, const char *b, size_t c)
109{
110 unsigned short sel = RestoreOS2FS();
111
112
113#ifdef DEBUG
114 nrcalls_free++;
115 totalmemalloc -= _msize(a);
116#endif
117 _debug_free(a,b,c);
118 SetFS(sel);
119}
120
121void * _LNK_CONV CRTWRAP(_debug_malloc)( size_t a, const char *b, size_t c)
122{
123 unsigned short sel = RestoreOS2FS();
124 void *rc;
125
126#ifdef DEBUG
127 totalmemalloc += a;
128 nrcalls_malloc++;
129#endif
130 rc = _debug_calloc(1,a,b,c);
131 SetFS(sel);
132 return rc;
133}
134
135void * _LNK_CONV CRTWRAP(_debug_realloc)( void *a, size_t b, const char *c, size_t d)
136{
137 unsigned short sel = RestoreOS2FS();
138 void *rc;
139
140#ifdef DEBUG
141 totalmemalloc += (b - _msize(a));
142#endif
143 rc = _debug_realloc(a,b,c,d);
144 SetFS(sel);
145 return rc;
146}
147
148void * _LNK_CONV CRTWRAP(_umalloc)(Heap_t a, size_t b)
149{
150 unsigned short sel = RestoreOS2FS();
151 void *rc;
152
153#ifdef DEBUG
154 totalmemalloc += b;
155 nrcalls_malloc++;
156#endif
157 rc = _umalloc(a,b);
158 SetFS(sel);
159 return rc;
160}
161
162void * _LNK_CONV CRTWRAP(_ucalloc)(Heap_t a, size_t b, size_t c)
163{
164 unsigned short sel = RestoreOS2FS();
165 void *rc;
166
167#ifdef DEBUG
168 totalmemalloc += b*c;
169 nrcalls_malloc++;
170#endif
171 rc = _ucalloc(a,b,c);
172 SetFS(sel);
173 return rc;
174}
175
176void * _LNK_CONV CRTWRAP(_debug_umalloc)(Heap_t a, size_t b, const char *c, size_t d)
177{
178 unsigned short sel = RestoreOS2FS();
179 void *rc;
180
181#ifdef DEBUG
182 totalmemalloc += b;
183 nrcalls_malloc++;
184#endif
185 rc = _debug_ucalloc(a, 1, b,c,d);
186 SetFS(sel);
187 return rc;
188}
189
190void * _LNK_CONV CRTWRAP(_debug_ucalloc)(Heap_t a, size_t b, size_t c, const char *d, size_t e)
191{
192 unsigned short sel = RestoreOS2FS();
193 void *rc;
194
195#ifdef DEBUG
196 totalmemalloc += b*c;
197 nrcalls_malloc++;
198#endif
199 rc = _debug_ucalloc(a,b,c,d,e);
200 SetFS(sel);
201 return rc;
202}
203
Note: See TracBrowser for help on using the repository browser.