debug_internal.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /////////////////////////////////////////////////////////////////////////EA-V1
  19. // $File: //depot/GeneralsMD/Staging/code/Libraries/Source/debug/debug_internal.cpp $
  20. // $Author: mhoffe $
  21. // $Revision: #1 $
  22. // $DateTime: 2003/07/03 11:55:26 $
  23. //
  24. // ©2003 Electronic Arts
  25. //
  26. // Implementation of internal code
  27. //////////////////////////////////////////////////////////////////////////////
  28. #include "_pch.h"
  29. void DebugInternalAssert(const char *file, int line, const char *expr)
  30. {
  31. // dangerous as well but since this function is used in this
  32. // module only we know how long stuff can get
  33. char buf[512];
  34. wsprintf(buf,"File %s, line %i:\n%s",file,line,expr);
  35. MessageBox(NULL,buf,"Internal assert failed",
  36. MB_OK|MB_ICONSTOP|MB_TASKMODAL|MB_SETFOREGROUND);
  37. // stop right now!
  38. TerminateProcess(GetCurrentProcess(),666);
  39. }
  40. void *DebugAllocMemory(unsigned numBytes)
  41. {
  42. HGLOBAL h=GlobalAlloc(GMEM_FIXED,numBytes);
  43. if (!h)
  44. DCRASH_RELEASE("Debug mem alloc failed");
  45. return (void *)h;
  46. }
  47. void *DebugReAllocMemory(void *oldPtr, unsigned newSize)
  48. {
  49. // Windows doesn't like ReAlloc with NULL handle/ptr...
  50. if (!oldPtr)
  51. return newSize?DebugAllocMemory(newSize):0;
  52. // Shrinking to 0 size is basically freeing memory
  53. if (!newSize)
  54. {
  55. GlobalFree((HGLOBAL)oldPtr);
  56. return 0;
  57. }
  58. // now try GlobalReAlloc first
  59. HGLOBAL h=GlobalReAlloc((HGLOBAL)oldPtr,newSize,0);
  60. if (!h)
  61. {
  62. // this failed (Windows doesn't like ReAlloc'ing larger
  63. // fixed memory blocks) - go with Alloc/Free instead
  64. h=GlobalAlloc(GMEM_FIXED,newSize);
  65. if (!h)
  66. DCRASH_RELEASE("Debug mem realloc failed");
  67. unsigned oldSize=GlobalSize((HGLOBAL)oldPtr);
  68. memcpy((void *)h,oldPtr,oldSize<newSize?oldSize:newSize);
  69. GlobalFree((HGLOBAL)oldPtr);
  70. }
  71. return (void *)h;
  72. }
  73. void DebugFreeMemory(void *ptr)
  74. {
  75. if (ptr)
  76. GlobalFree((HGLOBAL)ptr);
  77. }