Leaked source code of windows server 2003
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.

908 lines
27 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. llcmem.h
  5. Abstract:
  6. Contains type and structure definitions and routine prototypes and macros
  7. for llcmem.c. To aid in tracking memory resources, DLC/LLC now delineates
  8. the following memory categories:
  9. Memory
  10. - arbitrary sized blocks allocated out of non-paged pool using
  11. ExAllocatePool(NonPagedPool, ...)
  12. ZeroMemory
  13. - arbitrary sized blocks allocated out of non-paged pool using
  14. ExAllocatePool(NonPagedPool, ...) and initialized to zeroes
  15. Pool
  16. - small sets of (relatively) small packets are allocated in one
  17. block from Memory or ZeroMemory as a Pool and then subdivided
  18. into packets
  19. Object
  20. - structures which may be packets allocated from Pool which have
  21. a known size and initialization values. Pseudo-category mainly
  22. for debugging purposes
  23. Author:
  24. Richard L Firth (rfirth) 10-Mar-1993
  25. Environment:
  26. kernel mode only.
  27. Revision History:
  28. 09-Mar-1993 RFirth
  29. Created
  30. --*/
  31. #ifndef _LLCMEM_H_
  32. #define _LLCMEM_H_
  33. #define DLC_POOL_TAG ' CLD'
  34. //
  35. // the following types and defines are for the debug version of the driver, but
  36. // need to be defined for the non-debug version too (not used, just defined)
  37. //
  38. //
  39. // In the DEBUG version of DLC, we treat various chunks of memory as 'objects'.
  40. // This serves the following purposes:
  41. //
  42. // 1. We use a signature DWORD, so that when looking at some DLC structure in
  43. // the debugger, we can quickly check if what we are looking at is what we
  44. // think it is. E.g., if you spot a block of memory with a "BIND" signature
  45. // where an "ADAPTER" signature should be, then there is a good chance a
  46. // list or pointer has gotten messed up. The idea is to try and reduce
  47. // the amount of time it can take to guess what you're looking at
  48. //
  49. // 2. We use consistency checks: If a routine is handed a pointer to a structure
  50. // which is supposed to be a FILE_CONTEXT structure, we can check the
  51. // signature and quickly determine if something has gone wrong (like the
  52. // structure has already been freed and the signature contains 0xDAADF00D
  53. //
  54. // 3. We maintain size, head and tail signature information to determine
  55. // whether we have overwritten any part of an object. This is part of the
  56. // consistency check
  57. //
  58. // The object definitions should occur in one place only, but DLC is such a mess
  59. // that it would be a non-trivial amount of work to clean everything up. Do it
  60. // if there's time... (he said, knowing full well there's never any 'time')
  61. //
  62. typedef enum {
  63. DlcDriverObject = 0xCC002001, // start off with a relatively unique id
  64. FileContextObject, // 0xCC002002
  65. AdapterContextObject, // 0xCC002003
  66. BindingContextObject, // 0xCC002004
  67. DlcSapObject, // 0xCC002005
  68. DlcGroupSapObject, // 0xCC002006
  69. DlcLinkObject, // 0xCC002007
  70. DlcDixObject, // 0xCC002008
  71. LlcDataLinkObject, // 0xCC002009
  72. LLcDirectObject, // 0xCC00200A
  73. LlcSapObject, // 0xCC00200B
  74. LlcGroupSapObject, // 0xCC00200C
  75. DlcBufferPoolObject, // 0xCC00200D
  76. DlcLinkPoolObject, // 0xCC00200E
  77. DlcPacketPoolObject, // 0xCC00200F
  78. LlcLinkPoolObject, // 0xCC002010
  79. LlcPacketPoolObject // 0xCC002011
  80. } DLC_OBJECT_TYPE;
  81. typedef struct {
  82. ULONG Signature; // human-sensible signature when DB'd
  83. DLC_OBJECT_TYPE Type; // object identifier
  84. ULONG Size; // size of this object/structure in bytes
  85. ULONG Extra; // additional size over basic object size
  86. } OBJECT_ID, *POBJECT_ID;
  87. #define SIGNATURE_FILE 0x454C4946 // "FILE"
  88. #define SIGNATURE_ADAPTER 0x50414441 // "ADAP"
  89. #define SIGNATURE_BINDING 0x444E4942 // "BIND"
  90. #define SIGNATURE_DLC_SAP 0x44504153 // "SAPD"
  91. #define SIGNATURE_DLC_LINK 0x4B4E494C // "LINK"
  92. #define SIGNATURE_DIX 0x44584944 // "DIXD"
  93. #define SIGNATURE_LLC_LINK 0x41544144 // "DATA"
  94. #define SIGNATURE_LLC_SAP 0x4C504153 // "SAPL"
  95. #define ZAP_DEALLOC_VALUE 0x5A // "Z"
  96. #define ZAP_EX_FREE_VALUE 0x58 // "X"
  97. //
  98. // we try to keep track of memory allocations by subdividing them into driver
  99. // and handle categories. The first charges memory allocated to the driver -
  100. // e.g. a file context 'object'. Once we have open file handles, then allocations
  101. // are charged to them
  102. //
  103. typedef enum {
  104. ChargeToDriver,
  105. ChargeToHandle
  106. } MEMORY_CHARGE;
  107. //
  108. // MEMORY_USAGE - collection of variables used for charging memory. Accessed
  109. // within spinlock
  110. //
  111. typedef struct _MEMORY_USAGE {
  112. struct _MEMORY_USAGE* List; // pointer to next MEMORY_USAGE structure
  113. KSPIN_LOCK SpinLock; // stop alloc & free clashing?
  114. PVOID Owner; // pointer to owning structure/object
  115. DLC_OBJECT_TYPE OwnerObjectId; // identifies who owns this charge
  116. ULONG OwnerInstance; // instance of type of owner
  117. ULONG NonPagedPoolAllocated; // actual amount of non-paged pool charged
  118. ULONG AllocateCount; // number of calls to allocate non-paged pool
  119. ULONG FreeCount; // number of calls to free non-paged pool
  120. LIST_ENTRY PrivateList; // list of allocated blocks owned by this usage
  121. ULONG Unused[2]; // pad to 16-byte boundary
  122. } MEMORY_USAGE, *PMEMORY_USAGE;
  123. //
  124. // PACKET_POOL - this structure describes a packet pool. A packet pool is a
  125. // collection of same-sized packets. The pool starts off with an initial number
  126. // of packets on the FreeList. As packets are allocated, they are put on the
  127. // BusyList and the reverse happens when the packets are deallocated. If there
  128. // are no packets on the FreeList when an allocation call is made, more memory
  129. // is allocated
  130. //
  131. typedef struct {
  132. SINGLE_LIST_ENTRY FreeList; // list of available packets
  133. SINGLE_LIST_ENTRY BusyList; // list of in-use packets
  134. KSPIN_LOCK PoolLock; // stops simultaneous accesses breaking list(s)
  135. ULONG PacketSize; // size of individual packets
  136. //
  137. // the following 2 fields are here because DLC is a piece of garbage. It
  138. // keeps hold of allocated packets even after the pool as been deleted.
  139. // This leads to pool corruption. So if we determine packets are still
  140. // allocated when the pool is deleted, we remove the pool from whatever
  141. // 'object' it is currently stuck to, lamprey-like, and add it to the
  142. // ZombieList. When we next deallocate packets from this pool (assuming that
  143. // DLC at least bothers to do this), we check the zombie state. If ImAZombie
  144. // is TRUE (actually its true to say for the whole DLC device driver) AND
  145. // we are deallocating the last packet in the pool then we really delete
  146. // the pool
  147. //
  148. // SINGLE_LIST_ENTRY UndeadList;
  149. // BOOLEAN ImAZombie;
  150. #if DBG
  151. //
  152. // keep some metrics in the debug version to let us know if the pool is
  153. // growing
  154. //
  155. ULONG Signature; // 0x4C4F4F50 "POOL"
  156. ULONG Viable; // !0 if this pool is valid
  157. ULONG OriginalPacketCount; // number of packets requested
  158. ULONG CurrentPacketCount; // total number in pool
  159. ULONG Allocations; // number of calls to allocate from this pool
  160. ULONG Frees; // number of calls to free to pool
  161. ULONG NoneFreeCount; // number of times allocate call made when no packets free
  162. ULONG MaxInUse; // maximum number allocated at any one time
  163. ULONG ClashCount; // number of simultaneous accesses to pool
  164. ULONG Flags; // type of pool etc.
  165. ULONG ObjectSignature; // signature for checking contents if object pool
  166. PMEMORY_USAGE pMemoryUsage; // pointer to memory equivalent of Discover card
  167. MEMORY_USAGE MemoryUsage; // pool's memory usage charge
  168. ULONG FreeCount; // number of entries on FreeList
  169. ULONG BusyCount; // number of entries on BusyList
  170. ULONG Pad1;
  171. ULONG Pad2;
  172. #endif
  173. } PACKET_POOL, *PPACKET_POOL;
  174. //
  175. // PACKET_POOL defines and flags
  176. //
  177. #define PACKET_POOL_SIGNATURE 0x4C4F4F50 // "POOL"
  178. #define POOL_FLAGS_IN_USE 0x00000001
  179. #define POOL_FLAGS_OBJECT 0x00000002
  180. //
  181. // OBJECT_POOL - synonym for PACKET_POOL. Used in debug version (named 'objects'
  182. // in debug version have an object signature as an aide a debugoire and as
  183. // consistency check)
  184. //
  185. #define OBJECT_POOL PACKET_POOL
  186. #define POBJECT_POOL PPACKET_POOL
  187. //
  188. // PACKET_HEAD - each packet which exists in a PACKET_POOL has this header -
  189. // it links the packet onto the Free or Busy lists and the Flags word contains
  190. // the state of the packet
  191. //
  192. typedef struct {
  193. SINGLE_LIST_ENTRY List; // standard single-linked list
  194. ULONG Flags;
  195. #if DBG
  196. ULONG Signature; // 0x44414548 "HEAD"
  197. PVOID pPacketPool; // owning pool
  198. PVOID CallersAddress_A; // caller - allocation
  199. PVOID CallersCaller_A;
  200. PVOID CallersAddress_D; // caller - deallocation
  201. PVOID CallersCaller_D;
  202. #endif
  203. } PACKET_HEAD, *PPACKET_HEAD;
  204. //
  205. // PACKET_HEAD defines and flags
  206. //
  207. #define PACKET_HEAD_SIGNATURE 0x44414548 // "HEAD"
  208. #define PACKET_FLAGS_BUSY 0x00000001 // packet should be on BusyList
  209. #define PACKET_FLAGS_POST_ALLOC 0x00000002 // this packet was allocated because
  210. // the pool was full
  211. #define PACKET_FLAGS_FREE 0x00000080 // packet should be on FreeList
  212. //
  213. // OBJECT_HEAD - synonym for PACKET_HEAD. Used in debug version (named 'objects'
  214. // in debug version have an object signature as an aide a debugoire and as
  215. // consistency check)
  216. //
  217. #define OBJECT_HEAD PACKET_HEAD
  218. #define POBJECT_HEAD PPACKET_HEAD
  219. #if DBG
  220. //
  221. // anything we allocate from non-paged pool gets the following header pre-pended
  222. // to it
  223. //
  224. typedef struct {
  225. ULONG Size; // inclusive size of allocated block (inc head+tail)
  226. ULONG OriginalSize; // requested size
  227. ULONG Flags; // IN_USE flag
  228. ULONG Signature; // for checking validity of header
  229. LIST_ENTRY GlobalList; // all blocks allocated on one list
  230. LIST_ENTRY PrivateList; // blocks owned by MemoryUsage
  231. PVOID Stack[4]; // stack of return addresses
  232. } PRIVATE_NON_PAGED_POOL_HEAD, *PPRIVATE_NON_PAGED_POOL_HEAD;
  233. #define MEM_FLAGS_IN_USE 0x00000001
  234. #define SIGNATURE1 0x41434C44 // "DLCA" when viewed via db/dc
  235. #define SIGNATURE2 0x434F4C4C // "LLOC" " " " "
  236. //
  237. // anything we allocate from non-paged pool has the following tail appended to it
  238. //
  239. typedef struct {
  240. ULONG Size; // inclusive size; must be same as in header
  241. ULONG Signature; // for checking validity of tail
  242. ULONG Pattern1;
  243. ULONG Pattern2;
  244. } PRIVATE_NON_PAGED_POOL_TAIL, *PPRIVATE_NON_PAGED_POOL_TAIL;
  245. #define PATTERN1 0x55AA6699
  246. #define PATTERN2 0x11EECC33
  247. //
  248. // standard object identifier. Expands to nothing on free build
  249. //
  250. #define DBG_OBJECT_ID OBJECT_ID ObjectId
  251. //
  252. // globally accessible memory
  253. //
  254. extern MEMORY_USAGE DriverMemoryUsage;
  255. extern MEMORY_USAGE DriverStringUsage;
  256. //
  257. // debug prototypes
  258. //
  259. VOID
  260. InitializeMemoryPackage(
  261. VOID
  262. );
  263. PSINGLE_LIST_ENTRY
  264. PullEntryList(
  265. IN PSINGLE_LIST_ENTRY List,
  266. IN PSINGLE_LIST_ENTRY Element
  267. );
  268. VOID
  269. LinkMemoryUsage(
  270. IN PMEMORY_USAGE pMemoryUsage
  271. );
  272. VOID
  273. UnlinkMemoryUsage(
  274. IN PMEMORY_USAGE pMemoryUsage
  275. );
  276. //
  277. // the following 2 functions expand to be ExAllocatePoolWithTag(NonPagedPool, ...)
  278. // and ExFreePool(...) resp. in the retail/Free version of the driver
  279. //
  280. PVOID
  281. AllocateMemory(
  282. IN PMEMORY_USAGE pMemoryUsage,
  283. IN ULONG Size
  284. );
  285. PVOID
  286. AllocateZeroMemory(
  287. IN PMEMORY_USAGE pMemoryUsage,
  288. IN ULONG Size
  289. );
  290. VOID
  291. DeallocateMemory(
  292. IN PMEMORY_USAGE pMemoryUsage,
  293. IN PVOID Pointer
  294. );
  295. PPACKET_POOL
  296. CreatePacketPool(
  297. IN PMEMORY_USAGE pMemoryUsage,
  298. IN PVOID pOwner,
  299. IN DLC_OBJECT_TYPE ObjectType,
  300. IN ULONG PacketSize,
  301. IN ULONG NumberOfPackets
  302. );
  303. VOID
  304. DeletePacketPool(
  305. IN PMEMORY_USAGE pMemoryUsage,
  306. IN PPACKET_POOL* pPacketPool
  307. );
  308. PVOID
  309. AllocateObject(
  310. IN PMEMORY_USAGE pMemoryUsage,
  311. IN DLC_OBJECT_TYPE ObjectType,
  312. IN ULONG ObjectSize
  313. );
  314. VOID
  315. FreeObject(
  316. IN PMEMORY_USAGE pMemoryUsage,
  317. IN PVOID pObject,
  318. IN DLC_OBJECT_TYPE ObjectType
  319. );
  320. VOID
  321. ValidateObject(
  322. IN POBJECT_ID pObject,
  323. IN DLC_OBJECT_TYPE ObjectType
  324. );
  325. POBJECT_POOL
  326. CreateObjectPool(
  327. IN PMEMORY_USAGE pMemoryUsage,
  328. IN DLC_OBJECT_TYPE ObjectType,
  329. IN ULONG ObjectSize,
  330. IN ULONG NumberOfObjects
  331. );
  332. VOID
  333. DeleteObjectPool(
  334. IN PMEMORY_USAGE pMemoryUsage,
  335. IN DLC_OBJECT_TYPE ObjectType,
  336. IN POBJECT_POOL pObjectPool
  337. );
  338. POBJECT_HEAD
  339. AllocatePoolObject(
  340. IN POBJECT_POOL pObjectPool
  341. );
  342. VOID
  343. DeallocatePoolObject(
  344. IN POBJECT_POOL pObjectPool,
  345. IN POBJECT_HEAD pObjectHead
  346. );
  347. VOID
  348. CheckMemoryReturned(
  349. IN PMEMORY_USAGE pMemoryUsage
  350. );
  351. VOID
  352. CheckDriverMemoryUsage(
  353. IN BOOLEAN Break
  354. );
  355. //
  356. // CHECK_DRIVER_MEMORY_USAGE - if (b) breaks into debugger if there is still
  357. // memory allocated to driver
  358. //
  359. #define CHECK_DRIVER_MEMORY_USAGE(b) \
  360. CheckDriverMemoryUsage(b)
  361. //
  362. // CHECK_MEMORY_RETURNED_DRIVER - checks if all charged memory allocation has been
  363. // refunded to the driver
  364. //
  365. #define CHECK_MEMORY_RETURNED_DRIVER() \
  366. CheckMemoryReturned(&DriverMemoryUsage)
  367. //
  368. // CHECK_MEMORY_RETURNED_FILE - checks if all charged memory allocation has been
  369. // refunded to the FILE_CONTEXT
  370. //
  371. #define CHECK_MEMORY_RETURNED_FILE() \
  372. CheckMemoryReturned(&pFileContext->MemoryUsage)
  373. //
  374. // CHECK_MEMORY_RETURNED_ADAPTER - checks if all charged memory allocation has been
  375. // refunded to the ADAPTER_CONTEXT
  376. //
  377. #define CHECK_MEMORY_RETURNED_ADAPTER() \
  378. CheckMemoryReturned(&pAdapterContext->MemoryUsage)
  379. //
  380. // CHECK_STRING_RETURNED_DRIVER - checks if all charged string allocation has been
  381. // refunded to the driver
  382. //
  383. #define CHECK_STRING_RETURNED_DRIVER() \
  384. CheckMemoryReturned(&DriverStringUsage)
  385. //
  386. // CHECK_STRING_RETURNED_ADAPTER - checks if all charged string allocation has been
  387. // refunded to the ADAPTER_CONTEXT
  388. //
  389. #define CHECK_STRING_RETURNED_ADAPTER() \
  390. CheckMemoryReturned(&pAdapterContext->StringUsage)
  391. //
  392. // memory allocators which charge memory usage to the driver
  393. //
  394. //
  395. // ALLOCATE_MEMORY_DRIVER - allocates (n) bytes of memory and charges it to the
  396. // driver
  397. //
  398. #define ALLOCATE_MEMORY_DRIVER(n) \
  399. AllocateMemory(&DriverMemoryUsage, (ULONG)(n))
  400. //
  401. // ALLOCATE_ZEROMEMORY_DRIVER - allocates (n) bytes of ZeroMemory and charges
  402. // it to the driver
  403. //
  404. #define ALLOCATE_ZEROMEMORY_DRIVER(n) \
  405. AllocateZeroMemory(&DriverMemoryUsage, (ULONG)(n))
  406. //
  407. // FREE_MEMORY_DRIVER - deallocates memory and refunds it to the driver
  408. //
  409. #define FREE_MEMORY_DRIVER(p) \
  410. DeallocateMemory(&DriverMemoryUsage, (PVOID)(p))
  411. //
  412. // ALLOCATE_STRING_DRIVER - allocate memory for string usage. Charge to
  413. // DriverStringUsage
  414. //
  415. #define ALLOCATE_STRING_DRIVER(n) \
  416. AllocateZeroMemory(&DriverStringUsage, (ULONG)(n))
  417. //
  418. // FREE_STRING_DRIVER - deallocates memory and refunds it to driver string usage
  419. //
  420. #define FREE_STRING_DRIVER(p) \
  421. DeallocateMemory(&DriverStringUsage, (PVOID)(p))
  422. //
  423. // CREATE_PACKET_POOL_DRIVER - calls CreatePacketPool and charges the pool
  424. // structure to the driver
  425. //
  426. #if !defined(NO_POOLS)
  427. #define CREATE_PACKET_POOL_DRIVER(t, s, n) \
  428. CreatePacketPool(&DriverMemoryUsage,\
  429. NULL,\
  430. (t),\
  431. (ULONG)(s),\
  432. (ULONG)(n))
  433. //
  434. // DELETE_PACKET_POOL_DRIVER - calls DeletePacketPool and refunds the pool
  435. // structure to the driver
  436. //
  437. #define DELETE_PACKET_POOL_DRIVER(p) \
  438. DeletePacketPool(&DriverMemoryUsage, (PPACKET_POOL*)(p))
  439. #endif // NO_POOLS
  440. //
  441. // memory allocators which charge memory usage to an ADAPTER_CONTEXT
  442. //
  443. //
  444. // ALLOCATE_MEMORY_ADAPTER - allocates (n) bytes of memory and charges it to the
  445. // ADAPTER_CONTEXT
  446. //
  447. #define ALLOCATE_MEMORY_ADAPTER(n) \
  448. AllocateMemory(&pAdapterContext->MemoryUsage, (ULONG)(n))
  449. //
  450. // ALLOCATE_ZEROMEMORY_ADAPTER - allocates (n) bytes of ZeroMemory and charges
  451. // it to the ADAPTER_CONTEXT
  452. //
  453. #define ALLOCATE_ZEROMEMORY_ADAPTER(n) \
  454. AllocateZeroMemory(&pAdapterContext->MemoryUsage, (ULONG)(n))
  455. //
  456. // FREE_MEMORY_ADAPTER - deallocates memory and refunds it to the ADAPTER_CONTEXT
  457. //
  458. #define FREE_MEMORY_ADAPTER(p) \
  459. DeallocateMemory(&pAdapterContext->MemoryUsage, (PVOID)(p))
  460. //
  461. // ALLOCATE_STRING_ADAPTER - allocate memory for string usage. Charge to
  462. // pAdapterContext StringUsage
  463. //
  464. #define ALLOCATE_STRING_ADAPTER(n) \
  465. AllocateZeroMemory(&pAdapterContext->StringUsage, (ULONG)(n))
  466. //
  467. // CREATE_PACKET_POOL_ADAPTER - calls CreatePacketPool and charges the pool
  468. // structure to the adapter structure
  469. //
  470. #if !defined(NO_POOLS)
  471. #define CREATE_PACKET_POOL_ADAPTER(t, s, n) \
  472. CreatePacketPool(&pAdapterContext->MemoryUsage,\
  473. (PVOID)pAdapterContext,\
  474. (t),\
  475. (ULONG)(s),\
  476. (ULONG)(n))
  477. //
  478. // DELETE_PACKET_POOL_ADAPTER - calls DeletePacketPool and refunds the pool
  479. // structure to the adapter structure
  480. //
  481. #define DELETE_PACKET_POOL_ADAPTER(p) \
  482. DeletePacketPool(&pAdapterContext->MemoryUsage, (PPACKET_POOL*)(p))
  483. #endif // NO_POOLS
  484. //
  485. // memory allocators which charge memory usage to a FILE_CONTEXT
  486. //
  487. //
  488. // ALLOCATE_MEMORY_FILE - allocates (n) bytes of memory and charges it to the file
  489. // handle
  490. //
  491. #define ALLOCATE_MEMORY_FILE(n) \
  492. AllocateMemory(&pFileContext->MemoryUsage, (ULONG)(n))
  493. //
  494. // ALLOCATE_ZEROMEMORY_FILE - allocates (n) bytes ZeroMemory and charges it to the
  495. // file handle
  496. //
  497. #define ALLOCATE_ZEROMEMORY_FILE(n) \
  498. AllocateZeroMemory(&pFileContext->MemoryUsage, (ULONG)(n))
  499. //
  500. // FREE_MEMORY_FILE - deallocates memory and refunds it to the file handle
  501. //
  502. #define FREE_MEMORY_FILE(p) \
  503. DeallocateMemory(&pFileContext->MemoryUsage, (PVOID)(p))
  504. //
  505. // CREATE_PACKET_POOL_FILE - calls CreatePacketPool and charges the pool structure
  506. // to the file handle
  507. //
  508. #if !defined(NO_POOLS)
  509. #define CREATE_PACKET_POOL_FILE(t, s, n) \
  510. CreatePacketPool(&pFileContext->MemoryUsage,\
  511. (PVOID)pFileContext,\
  512. (t),\
  513. (ULONG)(s),\
  514. (ULONG)(n))
  515. //
  516. // DELETE_PACKET_POOL_FILE - calls DeletePacketPool and refunds the pool structure
  517. // to the file handle
  518. //
  519. #define DELETE_PACKET_POOL_FILE(p) \
  520. DeletePacketPool(&pFileContext->MemoryUsage, (PPACKET_POOL*)(p))
  521. #endif // NO_POOLS
  522. //
  523. // VALIDATE_OBJECT - check that an 'object' is really what it supposed to be.
  524. // Rudimentary check based on object signature and object type fields
  525. //
  526. #define VALIDATE_OBJECT(p, t) ValidateObject(p, t)
  527. #define LINK_MEMORY_USAGE(p) LinkMemoryUsage(&(p)->MemoryUsage)
  528. #define UNLINK_MEMORY_USAGE(p) UnlinkMemoryUsage(&(p)->MemoryUsage)
  529. #define UNLINK_STRING_USAGE(p) UnlinkMemoryUsage(&(p)->StringUsage)
  530. #else // !DBG
  531. //
  532. // DBG_OBJECT_ID in retail version structures is non-existent
  533. //
  534. #define DBG_OBJECT_ID
  535. //
  536. // the non-zero-initialized memory allocator is just a call to ExAllocatePoolWithTag
  537. //
  538. #define AllocateMemory(n) ExAllocatePoolWithTag(NonPagedPool, (n), DLC_POOL_TAG)
  539. //
  540. // AllocateZeroMemory doesn't count memory usage in non-debug version
  541. //
  542. PVOID
  543. AllocateZeroMemory(
  544. IN ULONG Size
  545. );
  546. //
  547. // the memory deallocator is just a call to ExFreePool
  548. //
  549. #define DeallocateMemory(p) ExFreePool(p)
  550. //
  551. // CreatePacketPool doesn't count memory usage in non-debug version
  552. //
  553. PPACKET_POOL
  554. CreatePacketPool(
  555. IN ULONG PacketSize,
  556. IN ULONG NumberOfPackets
  557. );
  558. VOID
  559. DeletePacketPool(
  560. IN PPACKET_POOL* pPacketPool
  561. );
  562. //
  563. // solitary objects in debug version are non-paged pool in retail version
  564. //
  565. #define AllocateObject(n) AllocateZeroMemory(n)
  566. #define DeallocateObject(p) DeallocateMemory(p)
  567. //
  568. // pooled objects in debug version are pooled packets in retail version
  569. //
  570. #define CreateObjectPool(o, s, n) CreatePacketPool(s, n)
  571. #define DeleteObjectPool(p) DeletePacketPool(p)
  572. #define AllocatePoolObject(p) AllocatePacket(p)
  573. #define DeallocatePoolObject(p, h) DeallocatePacket(p)
  574. //
  575. // non-debug build no-op macros
  576. //
  577. #define CHECK_MEMORY_RETURNED_DRIVER()
  578. #define CHECK_MEMORY_RETURNED_FILE()
  579. #define CHECK_MEMORY_RETURNED_ADAPTER()
  580. #define CHECK_STRING_RETURNED_DRIVER()
  581. #define CHECK_STRING_RETURNED_ADAPTER()
  582. #define CHECK_DRIVER_MEMORY_USAGE(b)
  583. //
  584. // non-memory-charging versions of allocation/free macros
  585. //
  586. #define ALLOCATE_MEMORY_DRIVER(n) AllocateMemory((ULONG)(n))
  587. #define ALLOCATE_ZEROMEMORY_DRIVER(n) AllocateZeroMemory((ULONG)(n))
  588. #define FREE_MEMORY_DRIVER(p) DeallocateMemory((PVOID)(p))
  589. #define ALLOCATE_STRING_DRIVER(n) AllocateZeroMemory((ULONG)(n))
  590. #define FREE_STRING_DRIVER(p) DeallocateMemory((PVOID)(p))
  591. #if !defined(NO_POOLS)
  592. #define CREATE_PACKET_POOL_DRIVER(t, s, n) CreatePacketPool((ULONG)(s), (ULONG)(n))
  593. #define DELETE_PACKET_POOL_DRIVER(p) DeletePacketPool((PPACKET_POOL*)(p))
  594. #endif // NO_POOLS
  595. #define ALLOCATE_MEMORY_ADAPTER(n) AllocateMemory((ULONG)(n))
  596. #define ALLOCATE_ZEROMEMORY_ADAPTER(n) AllocateZeroMemory((ULONG)(n))
  597. #define FREE_MEMORY_ADAPTER(p) DeallocateMemory((PVOID)(p))
  598. #define ALLOCATE_STRING_ADAPTER(n) AllocateZeroMemory((ULONG)(n))
  599. #if !defined(NO_POOLS)
  600. #define CREATE_PACKET_POOL_ADAPTER(t, s, n) CreatePacketPool((s), (n))
  601. #define DELETE_PACKET_POOL_ADAPTER(p) DeletePacketPool((PPACKET_POOL*)(p))
  602. #endif // NO_POOLS
  603. #define ALLOCATE_MEMORY_FILE(n) AllocateMemory((ULONG)(n))
  604. #define ALLOCATE_ZEROMEMORY_FILE(n) AllocateZeroMemory((ULONG)(n))
  605. #define FREE_MEMORY_FILE(p) DeallocateMemory((PVOID)(p))
  606. #if !defined(NO_POOLS)
  607. #define CREATE_PACKET_POOL_FILE(t, s, n) CreatePacketPool((ULONG)(s), (ULONG)(n))
  608. #define DELETE_PACKET_POOL_FILE(p) DeletePacketPool((PPACKET_POOL*)(p))
  609. #endif // NO_POOLS
  610. #define VALIDATE_OBJECT(p, t)
  611. #define LINK_MEMORY_USAGE(p)
  612. #define UNLINK_MEMORY_USAGE(p)
  613. #define UNLINK_STRING_USAGE(p)
  614. #endif // DBG
  615. //
  616. // Prototypes for memory allocators and pool and object functions
  617. //
  618. PVOID
  619. AllocatePacket(
  620. IN PPACKET_POOL pPacketPool
  621. );
  622. VOID
  623. DeallocatePacket(
  624. IN PPACKET_POOL pPacketPool,
  625. IN PVOID pPacket
  626. );
  627. #if defined(NO_POOLS)
  628. #define CREATE_PACKET_POOL_DRIVER(t, s, n) (PVOID)0x12345678
  629. #define CREATE_PACKET_POOL_ADAPTER(t, s, n) (PVOID)0x12345679
  630. #define CREATE_PACKET_POOL_FILE(t, s, n) (PVOID)0x1234567A
  631. #define DELETE_PACKET_POOL_DRIVER(p) *p = NULL
  632. #define DELETE_PACKET_POOL_ADAPTER(p) *p = NULL
  633. #define DELETE_PACKET_POOL_FILE(p) *p = NULL
  634. #if defined(BUF_USES_POOL)
  635. #if DBG
  636. #define CREATE_BUFFER_POOL_FILE(t, s, n) \
  637. CreatePacketPool(&pFileContext->MemoryUsage,\
  638. (PVOID)pFileContext,\
  639. (t),\
  640. (ULONG)(s),\
  641. (ULONG)(n))
  642. #define DELETE_BUFFER_POOL_FILE(p) \
  643. DeletePacketPool(&pFileContext->MemoryUsage, (PPACKET_POOL*)(p))
  644. #define ALLOCATE_PACKET_DLC_BUF(p) AllocatePacket(p)
  645. #define DEALLOCATE_PACKET_DLC_BUF(pool, p) DeallocatePacket(pool, p)
  646. #else // !DBG
  647. #define CREATE_BUFFER_POOL_FILE(t, s, n) CreatePacketPool((ULONG)(s), (ULONG)(n))
  648. #define DELETE_BUFFER_POOL_FILE(p) DeletePacketPool((PPACKET_POOL*)(p))
  649. #define ALLOCATE_PACKET_DLC_BUF(p) ALLOCATE_ZEROMEMORY_FILE(sizeof(DLC_BUFFER_HEADER))
  650. #define DEALLOCATE_PACKET_DLC_BUF(pool, p) FREE_MEMORY_FILE(p)
  651. #endif // DBG
  652. #else // !BUF_USES_POOL
  653. #define CREATE_BUFFER_POOL_FILE(t, s, n) (PVOID)0x1234567B
  654. #define DELETE_BUFFER_POOL_FILE(p) *p = NULL
  655. #define ALLOCATE_PACKET_DLC_BUF(p) AllocateZeroMemory(sizeof(DLC_BUFFER_HEADER))
  656. #define DEALLOCATE_PACKET_DLC_BUF(pool, p) DeallocateMemory(p)
  657. #endif // BUF_USES_POOL
  658. #if DBG
  659. #define ALLOCATE_PACKET_DLC_PKT(p) ALLOCATE_ZEROMEMORY_FILE(sizeof(DLC_PACKET))
  660. #define ALLOCATE_PACKET_DLC_OBJ(p) ALLOCATE_ZEROMEMORY_FILE(sizeof(DLC_OBJECT))
  661. #define ALLOCATE_PACKET_LLC_PKT(p) ALLOCATE_ZEROMEMORY_ADAPTER(sizeof(UNITED_PACKETS))
  662. #define ALLOCATE_PACKET_LLC_LNK(p) ALLOCATE_ZEROMEMORY_ADAPTER(sizeof(DATA_LINK) + 32)
  663. #define DEALLOCATE_PACKET_DLC_PKT(pool, p) FREE_MEMORY_FILE(p)
  664. #define DEALLOCATE_PACKET_DLC_OBJ(pool, p) FREE_MEMORY_FILE(p)
  665. #define DEALLOCATE_PACKET_LLC_PKT(pool, p) FREE_MEMORY_ADAPTER(p)
  666. #define DEALLOCATE_PACKET_LLC_LNK(pool, p) FREE_MEMORY_ADAPTER(p)
  667. #else // !DBG
  668. #define CREATE_BUFFER_POOL_FILE(t, s, n) CREATE_PACKET_POOL_FILE(t, s, n)
  669. #define DELETE_BUFFER_POOL_FILE(p) DELETE_PACKET_POOL_FILE(p)
  670. #define ALLOCATE_PACKET_DLC_BUF(p) AllocateZeroMemory(sizeof(DLC_BUFFER_HEADER))
  671. #define ALLOCATE_PACKET_DLC_PKT(p) AllocateZeroMemory(sizeof(DLC_PACKET))
  672. #define ALLOCATE_PACKET_DLC_OBJ(p) AllocateZeroMemory(sizeof(DLC_OBJECT))
  673. #define ALLOCATE_PACKET_LLC_PKT(p) AllocateZeroMemory(sizeof(UNITED_PACKETS))
  674. #define ALLOCATE_PACKET_LLC_LNK(p) AllocateZeroMemory(sizeof(DATA_LINK) + 32)
  675. #define DEALLOCATE_PACKET_DLC_BUF(pool, p) DeallocateMemory(p)
  676. #define DEALLOCATE_PACKET_DLC_PKT(pool, p) DeallocateMemory(p)
  677. #define DEALLOCATE_PACKET_DLC_OBJ(pool, p) DeallocateMemory(p)
  678. #define DEALLOCATE_PACKET_LLC_PKT(pool, p) DeallocateMemory(p)
  679. #define DEALLOCATE_PACKET_LLC_LNK(pool, p) DeallocateMemory(p)
  680. #endif // DBG
  681. #else // !NO_POOLS
  682. #define CREATE_BUFFER_POOL_FILE(t, s, n) CREATE_PACKET_POOL_FILE(t, s, n)
  683. #define DELETE_BUFFER_POOL_FILE(p) DELETE_PACKET_POOL_FILE(p)
  684. #define ALLOCATE_PACKET_DLC_BUF(p) AllocatePacket(p)
  685. #define ALLOCATE_PACKET_DLC_PKT(p) AllocatePacket(p)
  686. #define ALLOCATE_PACKET_DLC_OBJ(p) AllocatePacket(p)
  687. #define ALLOCATE_PACKET_LLC_PKT(p) AllocatePacket(p)
  688. #define ALLOCATE_PACKET_LLC_LNK(p) AllocatePacket(p)
  689. #define DEALLOCATE_PACKET_DLC_BUF(pool, p) DeallocatePacket(pool, p)
  690. #define DEALLOCATE_PACKET_DLC_PKT(pool, p) DeallocatePacket(pool, p)
  691. #define DEALLOCATE_PACKET_DLC_OBJ(pool, p) DeallocatePacket(pool, p)
  692. #define DEALLOCATE_PACKET_LLC_PKT(pool, p) DeallocatePacket(pool, p)
  693. #define DEALLOCATE_PACKET_LLC_LNK(pool, p) DeallocatePacket(pool, p)
  694. #endif // NO_POOLS
  695. #endif // _LLCMEM_H_