1: 
  2: /* Copyright (c) 2000 Digital Mars      */
  3: /* All Rights Reserved                  */
  4: 
  5: #include <stdio.h>
  6: #include <stdlib.h>
  7: #include <string.h>
  8: 
  9: #if linux || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun&&__SVR4
 10: #include "../root/rmem.h"
 11: #else
 12: #include "rmem.h"
 13: #endif
 14: 
 15: /* This implementation of the storage allocator uses the standard C allocation package.
 16:  */
 17: 
 18: Mem mem;
 19: 
 20: void Mem::init()
 21: {
 22: }
 23: 
 24: char *Mem::strdup(const char *s)
 25: {
 26:     char *p;
 27: 
 28:     if (s)
 29:     {
 30:         p = ::strdup(s);
warning C4996: 'strdup': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strdup. See online help for details. c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\string.h(238) : see declaration of 'strdup'
31: if (p) 32: return p; 33: error(); 34: } 35: return NULL; 36: } 37: 38: void *Mem::malloc(size_t size) 39: { void *p; 40: 41: if (!size) 42: p = NULL; 43: else 44: { 45: p = ::malloc(size); 46: if (!p) 47: error(); 48: } 49: return p; 50: } 51: 52: void *Mem::calloc(size_t size, size_t n) 53: { void *p; 54: 55: if (!size || !n) 56: p = NULL; 57: else 58: { 59: p = ::calloc(size, n); 60: if (!p) 61: error(); 62: } 63: return p; 64: } 65: 66: void *Mem::realloc(void *p, size_t size) 67: { 68: if (!size) 69: { if (p) 70: { ::free(p); 71: p = NULL; 72: } 73: } 74: else if (!p) 75: { 76: p = ::malloc(size); 77: if (!p) 78: error(); 79: } 80: else 81: { 82: p = ::realloc(p, size);
warning C6308: 'realloc' might return null pointer: assigning null pointer to 'p', which is passed as an argument to 'realloc', will cause the original memory block to be leaked
83: if (!p) 84: error(); 85: } 86: return p; 87: } 88: 89: void Mem::free(void *p) 90: { 91: if (p) 92: ::free(p); 93: } 94: 95: void *Mem::mallocdup(void *o, size_t size) 96: { void *p; 97: 98: if (!size) 99: p = NULL; 100: else 101: { 102: p = ::malloc(size); 103: if (!p) 104: error(); 105: else 106: memcpy(p,o,size); 107: } 108: return p; 109: } 110: 111: void Mem::error() 112: { 113: printf("Error: out of memory\n"); 114: exit(EXIT_FAILURE); 115: } 116: 117: void Mem::fullcollect() 118: { 119: } 120: 121: void Mem::mark(void *pointer) 122: { 123: (void) pointer; // necessary for VC /W4 124: } 125: 126: /* =================================================== */ 127: 128: void * operator new(size_t m_size) 129: { 130: void *p = malloc(m_size); 131: if (p) 132: return p; 133: printf("Error: out of memory\n"); 134: exit(EXIT_FAILURE); 135: return p; 136: } 137: 138: void operator delete(void *p) 139: { 140: free(p); 141: } 142: 143: 144: