Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
3.5 KiB

  1. /* ---File: alloc.c -------------------------------------------------------
  2. *
  3. * Description:
  4. * Contains memory allocation routines.
  5. *
  6. * This document contains confidential/proprietary information.
  7. * Copyright (c) 1990-1994 Microsoft Corporation, All Rights Reserved.
  8. *
  9. * Revision History:
  10. *
  11. * ---------------------------------------------------------------------- */
  12. /* Notes -
  13. Global Functions:
  14. AllocMem () -
  15. AllocStr () -
  16. FreeMem () -
  17. FreeStr () -
  18. ReallocMem () -
  19. */
  20. #include <windows.h>
  21. #include "mplayer.h"
  22. #ifdef _DEBUG
  23. LPVOID AllocMem( DWORD cb )
  24. /*++
  25. Routine Description:
  26. This function will allocate local memory. It will possibly allocate extra
  27. memory and fill this with debugging information for the debugging version.
  28. Arguments:
  29. cb - The amount of memory to allocate
  30. Return Value:
  31. NON-NULL - A pointer to the allocated memory
  32. FALSE/NULL - The operation failed. Extended error status is available
  33. using GetLastError.
  34. --*/
  35. {
  36. LPDWORD pMem;
  37. DWORD cbNew;
  38. cbNew = cb + 2 * sizeof( DWORD );
  39. if( cbNew & 3 )
  40. cbNew += sizeof( DWORD ) - ( cbNew & 3 );
  41. pMem = (LPDWORD)LocalAlloc (LPTR | LMEM_ZEROINIT, cbNew );
  42. if( !pMem )
  43. {
  44. DPF0( "LocalAlloc failed.\n" );
  45. return NULL;
  46. }
  47. *pMem=cb;
  48. *(LPDWORD)( (LPBYTE)pMem + cbNew - sizeof( DWORD ) ) = 0xdeadbeef;
  49. DPF4( "Allocated %d bytes @%08x\n", cbNew, pMem );
  50. return (LPVOID)( pMem + 1 );
  51. }
  52. VOID FreeMem( LPVOID pMem, DWORD cb )
  53. {
  54. DWORD cbNew;
  55. LPDWORD pNewMem;
  56. if( !pMem )
  57. return;
  58. pNewMem = pMem;
  59. pNewMem--;
  60. cbNew = *pNewMem + 2 * sizeof( DWORD );
  61. if( cbNew & 3 )
  62. cbNew += sizeof( DWORD ) - ( cbNew & 3 );
  63. /* Check that the size the caller thinks the block is tallies with
  64. * the size we placed before beginning of the block, and that the
  65. * end of the block has our signature:
  66. */
  67. if( ( cb && ( *pNewMem != cb ) ) || /* If cb == 0, don't worry about this check */
  68. ( *(LPDWORD)( (LPBYTE)pNewMem + cbNew - sizeof( DWORD ) ) != 0xdeadbeef ) )
  69. {
  70. DPF0( "Corrupt Memory detected freeing block: %0lx\n", pNewMem );
  71. #ifdef DEBUG
  72. DebugBreak();
  73. #endif
  74. }
  75. memset( pNewMem, 0xFE, cbNew ); // Mark frEEd blocks
  76. DPF4( "Freed %d bytes @%08x\n", cbNew, pNewMem );
  77. LocalFree((HANDLE) pNewMem );
  78. }
  79. LPVOID ReallocMem( LPVOID lpOldMem, DWORD cbOld, DWORD cbNew )
  80. {
  81. LPVOID lpNewMem;
  82. lpNewMem = AllocMem( cbNew );
  83. if( lpOldMem )
  84. {
  85. if( lpNewMem )
  86. {
  87. memcpy( lpNewMem, lpOldMem, min( cbNew, cbOld ) );
  88. }
  89. FreeMem( lpOldMem, cbOld );
  90. }
  91. return lpNewMem;
  92. }
  93. #endif // debug
  94. LPTSTR AllocStr( LPTSTR lpStr )
  95. /*++
  96. Routine Description:
  97. This function will allocate enough local memory to store the specified
  98. string, and copy that string to the allocated memory
  99. Arguments:
  100. lpStr - Pointer to the string that needs to be allocated and stored
  101. Return Value:
  102. NON-NULL - A pointer to the allocated memory containing the string
  103. FALSE/NULL - The operation failed. Extended error status is available
  104. using GetLastError.
  105. --*/
  106. {
  107. LPTSTR lpMem;
  108. if( !lpStr )
  109. return NULL;
  110. lpMem = AllocMem( STRING_BYTE_COUNT( lpStr ) );
  111. if( lpMem )
  112. lstrcpy( lpMem, lpStr );
  113. return lpMem;
  114. }
  115. VOID FreeStr( LPTSTR lpStr )
  116. {
  117. FreeMem( lpStr, STRING_BYTE_COUNT( lpStr ) );
  118. }
  119. VOID ReallocStr( LPTSTR *plpStr, LPTSTR lpStr )
  120. {
  121. FreeStr( *plpStr );
  122. *plpStr = AllocStr( lpStr );
  123. }
  124.