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.

193 lines
4.0 KiB

  1. /* ---File: memutil.c -----------------------------------------------------
  2. *
  3. * Description:
  4. * Contains Control Panel memory allocation routines.
  5. *
  6. * This document contains confidential/proprietary information.
  7. * Copyright (c) 1990-1992 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. ReallocStr () -
  20. Local Functions:
  21. */
  22. //==========================================================================
  23. // Include files
  24. //==========================================================================
  25. // C Runtime
  26. #include <string.h>
  27. #include <memory.h>
  28. // Application specific
  29. #include "ups.h"
  30. LPVOID AllocMem (DWORD cb)
  31. /*++
  32. Routine Description:
  33. This function will allocate local memory. It will possibly allocate extra
  34. memory and fill this with debugging information for the debugging version.
  35. Arguments:
  36. cb - The amount of memory to allocate
  37. Return Value:
  38. NON-NULL - A pointer to the allocated memory
  39. FALSE/NULL - The operation failed. Extended error status is available
  40. using GetLastError.
  41. --*/
  42. {
  43. LPDWORD pMem;
  44. DWORD cbNew;
  45. LPVOID pRet = NULL;
  46. cbNew = cb+2*sizeof(DWORD);
  47. if (cbNew & 3)
  48. cbNew += sizeof(DWORD) - (cbNew & 3);
  49. // pMem = (LPDWORD)HeapAlloc (hHeap, 0, cbNew);
  50. pMem = (LPDWORD)LocalAlloc (LMEM_FIXED, cbNew);
  51. if (pMem)
  52. {
  53. memset (pMem, 0, cbNew); // This might go later if done in NT
  54. *pMem = cb;
  55. *(LPDWORD)((LPTSTR)pMem+cbNew-sizeof(DWORD)) = 0xdeadbeef;
  56. pRet = (LPVOID)(pMem+1);
  57. }
  58. return pRet;
  59. }
  60. BOOL FreeMem (LPVOID pMem, DWORD cb)
  61. {
  62. DWORD cbNew;
  63. LPDWORD pNewMem;
  64. if (!pMem)
  65. return TRUE;
  66. pNewMem = pMem;
  67. pNewMem--;
  68. cbNew = cb+2*sizeof(DWORD);
  69. if (cbNew & 3)
  70. cbNew += sizeof(DWORD) - (cbNew & 3);
  71. #ifdef DEBU
  72. if ((*pNewMem != cb) ||
  73. (*(LPDWORD)((LPTSTR)pNewMem + cbNew - sizeof(DWORD)) != 0xdeadbeef))
  74. {
  75. OutputDebugStringA("Corrupt Memory in Control Panel : %0lx\n");
  76. DbgBreakPoint();
  77. }
  78. #endif
  79. return (((HLOCAL) pNewMem == LocalFree ((LPVOID)pNewMem)));
  80. }
  81. LPTSTR AllocStr (LPTSTR lpStr)
  82. /*++
  83. Routine Description:
  84. This function will allocate enough local memory to store the specified
  85. string, and copy that string to the allocated memory
  86. Arguments:
  87. lpStr - Pointer to the string that needs to be allocated and stored
  88. Return Value:
  89. NON-NULL - A pointer to the allocated memory containing the string
  90. FALSE/NULL - The operation failed. Extended error status is available
  91. using GetLastError.
  92. --*/
  93. {
  94. LPTSTR lpMem;
  95. if (!lpStr)
  96. return 0;
  97. if (lpMem = AllocMem (strlen (lpStr) + sizeof(TCHAR)))
  98. strcpy (lpMem, lpStr);
  99. return lpMem;
  100. }
  101. BOOL FreeStr (LPTSTR lpStr)
  102. {
  103. return lpStr ? FreeMem (lpStr, strlen (lpStr) + sizeof(TCHAR)) : FALSE;
  104. }
  105. BOOL ReallocStr (LPTSTR *plpStr, LPTSTR lpStr)
  106. {
  107. FreeStr (*plpStr);
  108. *plpStr = AllocStr (lpStr);
  109. return TRUE;
  110. }
  111. int MyMessageBox (HWND hWnd, DWORD wText, DWORD wCaption, DWORD wType, ...)
  112. {
  113. char szText[256+PATHMAX], szCaption[256];
  114. int ival;
  115. va_list parg;
  116. va_start (parg, wType);
  117. if (wText == LSFAIL)
  118. goto NoMem;
  119. if (!LoadString(hModule, wText, szCaption, sizeof (szCaption)))
  120. goto NoMem;
  121. wvsprintf(szText, szCaption, parg);
  122. if (!LoadString(hModule, wCaption, szCaption, sizeof (szCaption)))
  123. goto NoMem;
  124. if ((ival = MessageBox(hWnd, szText, szCaption, wType)) == 0)
  125. goto NoMem;
  126. return(ival);
  127. NoMem:
  128. va_end (parg);
  129. ErrLoadString(hWnd);
  130. return 0;
  131. }
  132. void ErrLoadString (HWND hParent)
  133. {
  134. MessageBox (hParent, szErrLS, szCtlPanel, MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
  135. }
  136. void ErrMemDlg (HWND hParent)
  137. {
  138. MessageBox (hParent, szErrMem, szCtlPanel, MB_OK | MB_ICONHAND | MB_SYSTEMMODAL);
  139. }