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.

321 lines
9.5 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: implements the basic structures for superscopes
  5. // ThreadSafe: no
  6. // Locks: none
  7. // Please read stdinfo.txt for programming style.
  8. //================================================================================
  9. #include <mm.h>
  10. #include <array.h>
  11. #include <opt.h>
  12. #include <optl.h>
  13. #include <optclass.h>
  14. //BeginExport(typedef)
  15. typedef struct _M_RESERVATION {
  16. LPVOID SubnetPtr;
  17. DWORD Address;
  18. DWORD Flags;
  19. DWORD nBytes;
  20. LPBYTE ClientUID;
  21. M_OPTCLASS Options;
  22. } M_RESERVATION , *PM_RESERVATION , *LPM_RESERVATION ;
  23. //EndExport(typedef)
  24. //BeginExport(typedef)
  25. typedef ARRAY M_RESERVATIONS;
  26. typedef PARRAY PM_RESERVATIONS;
  27. typedef LPARRAY LPM_RESERVATIONS;
  28. //EndExport(typedef)
  29. //BeginExport(inline)
  30. DWORD _inline
  31. MemReserve1Init(
  32. OUT PM_RESERVATION *Reservation,
  33. IN DWORD Address,
  34. IN DWORD Flags,
  35. IN LPBYTE ClientUID,
  36. IN DWORD nBytesClientUID
  37. ) {
  38. PM_RESERVATION Res1;
  39. DWORD Size;
  40. DWORD Error;
  41. AssertRet(Reservation && ClientUID && nBytesClientUID, ERROR_INVALID_PARAMETER);
  42. Require(Address);
  43. *Reservation = NULL;
  44. Size = ROUND_UP_COUNT(sizeof(M_RESERVATION ), ALIGN_WORST);
  45. Size += nBytesClientUID;
  46. Res1 = MemAlloc(Size);
  47. if( NULL == Res1 ) return ERROR_NOT_ENOUGH_MEMORY;
  48. Res1->SubnetPtr = NULL;
  49. Res1->Address = Address;
  50. Res1->Flags = Flags;
  51. Res1->nBytes = nBytesClientUID;
  52. Res1->ClientUID = Size - nBytesClientUID + (LPBYTE)Res1;
  53. memcpy(Res1->ClientUID, ClientUID, nBytesClientUID);
  54. Error = MemOptClassInit(&(Res1->Options));
  55. Require(ERROR_SUCCESS == Error);
  56. *Reservation = Res1;
  57. return ERROR_SUCCESS;
  58. }
  59. //EndExport(inline)
  60. //BeginExport(inline)
  61. DWORD _inline
  62. MemReserve1Cleanup(
  63. IN PM_RESERVATION Reservation
  64. ) {
  65. DWORD Error;
  66. AssertRet(Reservation, ERROR_INVALID_PARAMETER);
  67. Error = MemOptClassCleanup(&(Reservation->Options));
  68. Require(ERROR_SUCCESS == Error);
  69. MemFree(Reservation);
  70. return ERROR_SUCCESS;
  71. }
  72. //EndExport(inline)
  73. //BeginExport(inline)
  74. DWORD _inline
  75. MemReserveInit(
  76. IN OUT PM_RESERVATIONS Reservation
  77. ) {
  78. return MemArrayInit(Reservation);
  79. }
  80. //EndExport(inline)
  81. //BeginExport(inline)
  82. DWORD _inline
  83. MemReserveCleanup(
  84. IN PM_RESERVATIONS Reservation
  85. ) {
  86. return MemArrayCleanup(Reservation);
  87. }
  88. //EndExport(inline)
  89. //BeginExport(function)
  90. DWORD
  91. MemReserveAdd( // new client, should not exist before
  92. IN OUT PM_RESERVATIONS Reservation,
  93. IN DWORD Address,
  94. IN DWORD Flags,
  95. IN LPBYTE ClientUID,
  96. IN DWORD ClientUIDSize
  97. ) //EndExport(function)
  98. {
  99. DWORD Error;
  100. DWORD LocalError;
  101. PM_RESERVATION Res1;
  102. ARRAY_LOCATION Loc;
  103. AssertRet(Reservation && Address && ClientUID && ClientUIDSize, ERROR_INVALID_PARAMETER);
  104. Error = MemArrayInitLoc(Reservation, &Loc);
  105. while( ERROR_FILE_NOT_FOUND != Error ) { // check to see if this Address already exists
  106. Require(ERROR_SUCCESS == Error );
  107. Error = MemArrayGetElement(Reservation, &Loc, &Res1);
  108. Require(ERROR_SUCCESS == Error && Res1);
  109. if( Address == Res1->Address ) return ERROR_OBJECT_ALREADY_EXISTS;
  110. if( ClientUIDSize == Res1->nBytes && 0 == memcmp(ClientUID, Res1->ClientUID, Res1->nBytes) )
  111. return ERROR_OBJECT_ALREADY_EXISTS;
  112. Error = MemArrayNextLoc(Reservation, &Loc);
  113. }
  114. Error = MemReserve1Init(
  115. &Res1,
  116. Address,
  117. Flags,
  118. ClientUID,
  119. ClientUIDSize
  120. );
  121. if( ERROR_SUCCESS != Error ) return Error;
  122. Error = MemArrayAddElement(Reservation, Res1);
  123. if( ERROR_SUCCESS == Error ) return ERROR_SUCCESS;
  124. LocalError = MemReserve1Cleanup(Res1);
  125. Require(ERROR_SUCCESS == LocalError);
  126. return Error;
  127. }
  128. //BeginExport(function)
  129. DWORD
  130. MemReserveReplace( // old client, should exist before
  131. IN OUT PM_RESERVATIONS Reservation,
  132. IN DWORD Address,
  133. IN DWORD Flags,
  134. IN LPBYTE ClientUID,
  135. IN DWORD ClientUIDSize
  136. ) //EndExport(function)
  137. {
  138. DWORD Error;
  139. DWORD LocalError;
  140. PM_RESERVATION Res1, Res_Deleted;
  141. ARRAY_LOCATION Loc;
  142. AssertRet(Reservation && Address && ClientUID && ClientUIDSize, ERROR_INVALID_PARAMETER);
  143. Error = MemArrayInitLoc(Reservation, &Loc);
  144. while( ERROR_FILE_NOT_FOUND != Error ) { // check to see if this Address already exists
  145. Require(ERROR_SUCCESS == Error );
  146. Error = MemArrayGetElement(Reservation, &Loc, &Res1);
  147. Require(ERROR_SUCCESS == Error && Res1);
  148. if( Address == Res1->Address ) {
  149. Error = MemArrayDelElement(Reservation, &Loc, (LPVOID *)&Res_Deleted);
  150. Require(ERROR_SUCCESS == Error && Res_Deleted);
  151. break;
  152. }
  153. Error = MemArrayNextLoc(Reservation, &Loc);
  154. }
  155. if( ERROR_SUCCESS != Error ) return Error;
  156. Error = MemReserve1Init(
  157. &Res1,
  158. Address,
  159. Flags,
  160. ClientUID,
  161. ClientUIDSize
  162. );
  163. if( ERROR_SUCCESS != Error ) {
  164. LocalError = MemArrayAddElement(Reservation, Res_Deleted);
  165. Require(ERROR_SUCCESS == LocalError); // just deleted this guy -- should not have trouble adding back
  166. return Error;
  167. }
  168. Res1->Options = Res_Deleted->Options;
  169. Res1->SubnetPtr = Res_Deleted->SubnetPtr;
  170. MemFree(Res_Deleted);
  171. Error = MemArrayAddElement(Reservation, Res1);
  172. if( ERROR_SUCCESS == Error ) return ERROR_SUCCESS;
  173. LocalError = MemReserve1Cleanup(Res1);
  174. Require(ERROR_SUCCESS == LocalError);
  175. return Error;
  176. }
  177. //BeginExport(function)
  178. DWORD
  179. MemReserveDel(
  180. IN OUT PM_RESERVATIONS Reservation,
  181. IN DWORD Address
  182. ) //EndExport(function)
  183. {
  184. DWORD Error;
  185. PM_RESERVATION Res1;
  186. ARRAY_LOCATION Loc;
  187. AssertRet(Reservation && Address, ERROR_INVALID_PARAMETER);
  188. Error = MemArrayInitLoc(Reservation, &Loc);
  189. while( ERROR_FILE_NOT_FOUND != Error ) { // check to see if this Address already exists
  190. Require(ERROR_SUCCESS == Error );
  191. Error = MemArrayGetElement(Reservation, &Loc, &Res1);
  192. Require(ERROR_SUCCESS == Error && Res1);
  193. if( Address == Res1->Address ) {
  194. Error = MemArrayDelElement(Reservation, &Loc, (LPVOID *)&Res1);
  195. Require(ERROR_SUCCESS == Error && Res1);
  196. Error = MemReserve1Cleanup(Res1);
  197. Require(ERROR_SUCCESS == Error);
  198. return Error;
  199. }
  200. Error = MemArrayNextLoc(Reservation, &Loc);
  201. }
  202. return ERROR_FILE_NOT_FOUND;
  203. }
  204. //BeginExport(function)
  205. DWORD
  206. MemReserveFindByClientUID(
  207. IN PM_RESERVATIONS Reservation,
  208. IN LPBYTE ClientUID,
  209. IN DWORD ClientUIDSize,
  210. OUT PM_RESERVATION *Res
  211. ) //EndExport(function)
  212. {
  213. DWORD Error;
  214. PM_RESERVATION Res1;
  215. ARRAY_LOCATION Loc;
  216. AssertRet(Reservation && Res && ClientUID && ClientUIDSize, ERROR_INVALID_PARAMETER);
  217. *Res = NULL;
  218. Error = MemArrayInitLoc(Reservation, &Loc);
  219. while( ERROR_FILE_NOT_FOUND != Error ) { // check to see if this Address already exists
  220. Require(ERROR_SUCCESS == Error );
  221. Error = MemArrayGetElement(Reservation, &Loc, &Res1);
  222. Require(ERROR_SUCCESS == Error && Res1);
  223. if( ClientUIDSize == Res1->nBytes && 0 == memcmp(ClientUID, Res1->ClientUID, ClientUIDSize)) {
  224. *Res = Res1;
  225. return ERROR_SUCCESS;
  226. }
  227. Error = MemArrayNextLoc(Reservation, &Loc);
  228. }
  229. return ERROR_FILE_NOT_FOUND;
  230. }
  231. //BeginExport(function)
  232. DWORD
  233. MemReserveFindByAddress(
  234. IN PM_RESERVATIONS Reservation,
  235. IN DWORD Address,
  236. OUT PM_RESERVATION *Res
  237. ) //EndExport(function)
  238. {
  239. DWORD Error;
  240. PM_RESERVATION Res1;
  241. ARRAY_LOCATION Loc;
  242. AssertRet(Reservation && Address, ERROR_INVALID_PARAMETER);
  243. *Res = 0;
  244. Error = MemArrayInitLoc(Reservation, &Loc);
  245. while( ERROR_FILE_NOT_FOUND != Error ) { // check to see if this Address already exists
  246. Require(ERROR_SUCCESS == Error );
  247. Error = MemArrayGetElement(Reservation, &Loc, &Res1);
  248. Require(ERROR_SUCCESS == Error && Res1);
  249. if( Address == Res1->Address ) {
  250. *Res = Res1;
  251. return ERROR_SUCCESS;
  252. }
  253. Error = MemArrayNextLoc(Reservation, &Loc);
  254. }
  255. return ERROR_FILE_NOT_FOUND;
  256. }
  257. //================================================================================
  258. // end of file
  259. //================================================================================