/nxtvepg-2.8.1/dsdrv/hwmem.c

# · C · 226 lines · 173 code · 26 blank · 27 comment · 35 complexity · 5c732f3455e23627f7f708e8ade5e0a0 MD5 · raw file

  1. /////////////////////////////////////////////////////////////////////////////
  2. // #Id: HardwareMemory.cpp,v 1.8 2002/10/22 16:01:41 adcockj Exp #
  3. /////////////////////////////////////////////////////////////////////////////
  4. // Copyright (c) 2001 John Adcock. All rights reserved.
  5. /////////////////////////////////////////////////////////////////////////////
  6. //
  7. // This file is subject to the terms of the GNU General Public License as
  8. // published by the Free Software Foundation. A copy of this license is
  9. // included with this software distribution in the file COPYING. If you
  10. // do not have a copy, you may obtain a copy by writing to the Free
  11. // Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  12. //
  13. // This software is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details
  17. /////////////////////////////////////////////////////////////////////////////
  18. // nxtvepg $Id: hwmem.c,v 1.4 2003/02/03 20:03:21 tom Exp tom $
  19. /////////////////////////////////////////////////////////////////////////////
  20. #define WIN32_LEAN_AND_MEAN
  21. #include <windows.h>
  22. #include <winsvc.h>
  23. #include <stdlib.h>
  24. #include "dsdrv/dsdrv.h"
  25. #include "dsdrv/hwmem.h"
  26. #include "dsdrv/debuglog.h"
  27. void * HwMem_GetUserPointer( THwMem * pInstance )
  28. {
  29. if (pInstance->pMemStruct != NULL)
  30. {
  31. return pInstance->pMemStruct->dwUser;
  32. }
  33. else
  34. {
  35. return NULL;
  36. }
  37. }
  38. DWORD HwMem_TranslateToPhysical( THwMem * pInstance,
  39. void * pUser, DWORD dwSizeWanted, DWORD* pdwSizeAvailable )
  40. {
  41. if(pInstance->pMemStruct != NULL)
  42. {
  43. TPageStruct* pPages = (TPageStruct*)(pInstance->pMemStruct + 1);
  44. DWORD Offset;
  45. DWORD i;
  46. DWORD sum;
  47. DWORD pRetVal = 0;
  48. Offset = (DWORD)pUser - (DWORD)pInstance->pMemStruct->dwUser;
  49. sum = 0;
  50. i = 0;
  51. while (i < pInstance->pMemStruct->dwPages)
  52. {
  53. if (sum + pPages[i].dwSize > (unsigned)Offset)
  54. {
  55. Offset -= sum;
  56. pRetVal = pPages[i].dwPhysical + Offset;
  57. if ( pdwSizeAvailable != NULL )
  58. {
  59. *pdwSizeAvailable = pPages[i].dwSize - Offset;
  60. }
  61. break;
  62. }
  63. sum += pPages[i].dwSize;
  64. i++;
  65. }
  66. if(pRetVal == 0)
  67. {
  68. sum++;
  69. }
  70. if ( pdwSizeAvailable != NULL )
  71. {
  72. if (*pdwSizeAvailable < dwSizeWanted)
  73. {
  74. sum++;
  75. }
  76. }
  77. return pRetVal;
  78. }
  79. else
  80. {
  81. return 0;
  82. }
  83. }
  84. BOOL HwMem_IsValid( THwMem * pInstance )
  85. {
  86. return (pInstance->pMemStruct != NULL);
  87. }
  88. /** Memory that is allocated in user space and mapped to driver space */
  89. BOOL HwMem_AllocUserMemory( THwMem * pInstance, size_t Bytes )
  90. {
  91. TDSDrvParam paramIn;
  92. DWORD dwReturnedLength;
  93. DWORD status;
  94. DWORD nPages = 0;
  95. DWORD dwOutParamLength;
  96. memset(pInstance, 0, sizeof(*pInstance));
  97. pInstance->AllocatedBlock = malloc(Bytes + 0xFFF);
  98. if(pInstance->AllocatedBlock == NULL)
  99. {
  100. //throw std::runtime_error("Out of memory");
  101. return FALSE;
  102. }
  103. memset(pInstance->AllocatedBlock, 0, Bytes + 0xFFF);
  104. nPages = Bytes / 0xFFF + 1;
  105. dwOutParamLength = sizeof(TMemStruct) + nPages * sizeof(TPageStruct);
  106. pInstance->pMemStruct = (TMemStruct*) malloc(dwOutParamLength);
  107. if(pInstance->pMemStruct == NULL)
  108. {
  109. free(pInstance->AllocatedBlock);
  110. pInstance->AllocatedBlock = NULL;
  111. //throw std::runtime_error("Out of memory");
  112. return FALSE;
  113. }
  114. memset(pInstance->pMemStruct, 0, dwOutParamLength);
  115. paramIn.dwValue = Bytes;
  116. paramIn.dwFlags = 0;
  117. // align memory to page boundary
  118. if(((DWORD)pInstance->AllocatedBlock & 0xFFFFF000) < (DWORD)pInstance->AllocatedBlock)
  119. {
  120. paramIn.dwAddress = (((DWORD)pInstance->AllocatedBlock + 0xFFF) & 0xFFFFF000);
  121. }
  122. else
  123. {
  124. paramIn.dwAddress = (DWORD)pInstance->AllocatedBlock;
  125. }
  126. status = HwDrv_SendCommandEx(IOCTL_DSDRV_ALLOCMEMORY,
  127. &paramIn,
  128. sizeof(paramIn),
  129. pInstance->pMemStruct,
  130. dwOutParamLength,
  131. &dwReturnedLength);
  132. if(status != ERROR_SUCCESS || pInstance->pMemStruct->dwUser == 0)
  133. {
  134. free(pInstance->pMemStruct);
  135. free((void*)pInstance->AllocatedBlock);
  136. pInstance->AllocatedBlock = NULL;
  137. pInstance->pMemStruct = NULL;
  138. //throw std::runtime_error("Memory mapping failed");
  139. return FALSE;
  140. }
  141. return TRUE;
  142. }
  143. void HwMem_FreeUserMemory( THwMem * pInstance )
  144. {
  145. DWORD status = ERROR_SUCCESS;
  146. if(pInstance->pMemStruct != NULL)
  147. {
  148. DWORD dwInParamLength = sizeof(TMemStruct) + pInstance->pMemStruct->dwPages * sizeof(TPageStruct);
  149. status = HwDrv_SendCommand(IOCTL_DSDRV_FREEMEMORY, pInstance->pMemStruct, dwInParamLength);
  150. free(pInstance->pMemStruct);
  151. }
  152. if(pInstance->AllocatedBlock != NULL)
  153. {
  154. free((void*)pInstance->AllocatedBlock);
  155. }
  156. }
  157. /** Memory that is contiguous in both driver and user space */
  158. BOOL HwMem_AllocContigMemory( THwMem * pInstance, size_t Bytes )
  159. {
  160. TDSDrvParam paramIn;
  161. DWORD dwReturnedLength;
  162. DWORD status;
  163. DWORD dwOutParamLength = sizeof(TMemStruct) + sizeof(TPageStruct);
  164. pInstance->pMemStruct = (TMemStruct*) malloc(dwOutParamLength);
  165. if(pInstance->pMemStruct == NULL)
  166. {
  167. //throw std::runtime_error("Out of memory");
  168. return FALSE;
  169. }
  170. paramIn.dwValue = Bytes;
  171. paramIn.dwFlags = ALLOC_MEMORY_CONTIG;
  172. paramIn.dwAddress = 0;
  173. status = HwDrv_SendCommandEx(IOCTL_DSDRV_ALLOCMEMORY,
  174. &paramIn,
  175. sizeof(paramIn),
  176. pInstance->pMemStruct,
  177. dwOutParamLength,
  178. &dwReturnedLength);
  179. if(status != ERROR_SUCCESS || pInstance->pMemStruct->dwUser == 0)
  180. {
  181. free(pInstance->pMemStruct);
  182. //throw std::runtime_error("Memory mapping failed");
  183. return FALSE;
  184. }
  185. return TRUE;
  186. }
  187. void HwMem_FreeContigMemory( THwMem * pInstance )
  188. {
  189. DWORD Status = ERROR_SUCCESS;
  190. if(pInstance->pMemStruct != NULL)
  191. {
  192. DWORD dwInParamLength = sizeof(TMemStruct) + sizeof(TPageStruct);
  193. Status = HwDrv_SendCommand(
  194. IOCTL_DSDRV_FREEMEMORY,
  195. pInstance->pMemStruct,
  196. dwInParamLength
  197. );
  198. free(pInstance->pMemStruct);
  199. }
  200. }