Memory.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "stdh.h"
  13. #include <Engine/Base/Memory.h>
  14. #include <Engine/Base/Translation.h>
  15. #include <Engine/Base/ErrorReporting.h>
  16. #include <new.h>
  17. extern FLOAT _bCheckAllAllocations = FALSE;
  18. /*
  19. * Declarations for setting up the 'new_handler'.
  20. */
  21. _CRTIMP int __cdecl _query_new_mode( void );
  22. _CRTIMP int __cdecl _set_new_mode( int );
  23. #ifndef _PNH_DEFINED
  24. typedef int (__cdecl * _PNH)( size_t );
  25. #define _PNH_DEFINED
  26. #endif
  27. /*_CRTIMP _PNH __cdecl _query_new_handler( void );
  28. _CRTIMP _PNH __cdecl _set_new_handler( _PNH );*/
  29. #ifndef NDEBUG
  30. // include this for debug version of operator new
  31. #pragma comment (lib, "msvcrtd.lib")
  32. #endif
  33. // include user32 library (because of message box)
  34. #pragma comment (lib, "user32.lib")
  35. // if not enough memory
  36. int NewHandler(size_t size)
  37. {
  38. // terminate program
  39. FatalError(TRANS("Not enough memory (%d bytes needed)!"), size);
  40. return 0;
  41. }
  42. /* Static class used for initializing memory handlers. */
  43. static class CMemHandlerInit {
  44. public:
  45. // constructor
  46. CMemHandlerInit(void);
  47. } MemHandlerInit;
  48. CMemHandlerInit::CMemHandlerInit(void)
  49. {
  50. // set our not-enough-memory handler
  51. _set_new_handler(NewHandler);
  52. // make malloc use that handler
  53. _set_new_mode(1);
  54. }
  55. #undef AllocMemory
  56. void *AllocMemory( SLONG memsize )
  57. {
  58. void *pmem;
  59. ASSERTMSG(memsize>0, "AllocMemory: Block size is less or equal zero.");
  60. if (_bCheckAllAllocations) {
  61. _CrtCheckMemory();
  62. }
  63. pmem = malloc( memsize);
  64. // memory handler asures no null results here?!
  65. if (pmem==NULL) {
  66. _CrtCheckMemory();
  67. FatalError(TRANS("Not enough memory (%d bytes needed)!"), memsize);
  68. }
  69. return pmem;
  70. }
  71. #ifndef NDEBUG
  72. void *_debug_AllocMemory( SLONG memsize, int iType, const char *strFile, int iLine)
  73. {
  74. void *pmem;
  75. ASSERTMSG(memsize>0, "AllocMemory: Block size is less or equal zero.");
  76. if (_bCheckAllAllocations) {
  77. _CrtCheckMemory();
  78. }
  79. pmem = _malloc_dbg( memsize, iType, strFile, iLine);
  80. // memory handler asures no null results here?!
  81. if (pmem==NULL) {
  82. _CrtCheckMemory();
  83. FatalError(TRANS("Not enough memory (%d bytes needed)!"), memsize);
  84. }
  85. return pmem;
  86. }
  87. #endif
  88. void *AllocMemoryAligned( SLONG memsize, SLONG slAlignPow2)
  89. {
  90. ULONG ulMem = (ULONG)AllocMemory(memsize+slAlignPow2*2);
  91. ULONG ulMemAligned = ((ulMem+slAlignPow2-1) & ~(slAlignPow2-1)) + slAlignPow2;
  92. ((ULONG *)ulMemAligned)[-1] = ulMem;
  93. return (void*)ulMemAligned;
  94. }
  95. void FreeMemoryAligned( void *memory)
  96. {
  97. FreeMemory((void*) ( ( (ULONG*)memory )[-1] ) );
  98. }
  99. void FreeMemory( void *memory )
  100. {
  101. ASSERTMSG(memory!=NULL, "FreeMemory: NULL pointer input.");
  102. free( (char *)memory);
  103. }
  104. void ResizeMemory( void **ppv, SLONG slSize )
  105. {
  106. if (_bCheckAllAllocations) {
  107. _CrtCheckMemory();
  108. }
  109. void *pv = realloc(*ppv, slSize);
  110. // memory handler asures no null results here?!
  111. if (pv==NULL) {
  112. _CrtCheckMemory();
  113. FatalError(TRANS("Not enough memory (%d bytes needed)!"), slSize);
  114. }
  115. *ppv = pv;
  116. }
  117. void GrowMemory( void **ppv, SLONG newSize )
  118. {
  119. ResizeMemory(ppv, newSize);
  120. }
  121. void ShrinkMemory( void **ppv, SLONG newSize )
  122. {
  123. ResizeMemory(ppv, newSize);
  124. }
  125. /*
  126. * Allocate a copy of a string. - fatal error if not enough memory.
  127. */
  128. char *StringDuplicate(const char *strOriginal) {
  129. // get the size
  130. SLONG slSize = strlen(strOriginal)+1;
  131. // allocate that much memory
  132. char *strCopy = (char *)AllocMemory(slSize);
  133. // copy it there
  134. memcpy(strCopy, strOriginal, slSize);
  135. // result is the pointer to the copied string
  136. return strCopy;
  137. }
  138. // return position where we encounter zero byte or iBytes
  139. INDEX FindZero( UBYTE *pubMemory, INDEX iBytes)
  140. {
  141. for( INDEX i=0; i<iBytes; i++) if( pubMemory[i]==0) return i;
  142. return iBytes;
  143. }