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