| 1 | /** @file
|
|---|
| 2 | * Memory RTL function wrappers for GCC.
|
|---|
| 3 | *
|
|---|
| 4 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <odin.h>
|
|---|
| 8 | #include <os2sel.h>
|
|---|
| 9 |
|
|---|
| 10 | #include <memory.h>
|
|---|
| 11 |
|
|---|
| 12 | #ifdef DEBUG
|
|---|
| 13 | unsigned long nrcalls_malloc = 0;
|
|---|
| 14 | unsigned long nrcalls_free = 0;
|
|---|
| 15 | unsigned long totalmemalloc = 0;
|
|---|
| 16 |
|
|---|
| 17 | // currently, it's a dumb stub
|
|---|
| 18 | void SYSTEM getcrtstat(unsigned long *pnrcalls_malloc,
|
|---|
| 19 | unsigned long *pnrcalls_free,
|
|---|
| 20 | unsigned long *ptotalmemalloc)
|
|---|
| 21 | {
|
|---|
| 22 | *pnrcalls_malloc = nrcalls_malloc;
|
|---|
| 23 | *pnrcalls_free = nrcalls_free;
|
|---|
| 24 | *ptotalmemalloc = totalmemalloc;
|
|---|
| 25 | }
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | extern "C"
|
|---|
| 29 | {
|
|---|
| 30 |
|
|---|
| 31 | // these are GCC kLIBC exports
|
|---|
| 32 | void *_std_malloc(size_t sz);
|
|---|
| 33 | void *_std_realloc(void *ptr, size_t sz);
|
|---|
| 34 | void *_std_calloc(size_t cnt, size_t sz);
|
|---|
| 35 | void _std_free(void *ptr);
|
|---|
| 36 |
|
|---|
| 37 | void *malloc(size_t sz)
|
|---|
| 38 | {
|
|---|
| 39 | unsigned short sel = RestoreOS2FS();
|
|---|
| 40 | void *ptr = _std_malloc(sz);
|
|---|
| 41 | SetFS(sel);
|
|---|
| 42 | return ptr;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | void *realloc(void *ptr, size_t sz)
|
|---|
| 46 | {
|
|---|
| 47 | unsigned short sel = RestoreOS2FS();
|
|---|
| 48 | void *newPtr = _std_realloc(ptr, sz);
|
|---|
| 49 | SetFS(sel);
|
|---|
| 50 | return newPtr;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void *calloc(size_t cnt, size_t sz)
|
|---|
| 54 | {
|
|---|
| 55 | unsigned short sel = RestoreOS2FS();
|
|---|
| 56 | void *ptr = _std_calloc(cnt, sz);
|
|---|
| 57 | SetFS(sel);
|
|---|
| 58 | return ptr;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | void free(void *ptr)
|
|---|
| 62 | {
|
|---|
| 63 | unsigned short sel = RestoreOS2FS();
|
|---|
| 64 | _std_free(ptr);
|
|---|
| 65 | SetFS(sel);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | }
|
|---|