2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS library
5 * PURPOSE: Memory management routines
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Vizzini (vizzini@plasmic.com)
9 * CSH 01/08-2000 Created
10 * 15 Aug 2003 Vizzini - DMA support
11 * 3 Oct 2003 Vizzini - formatting and minor bugfixing
22 NdisAllocateMemoryWithTag(
23 OUT PVOID
*VirtualAddress
,
27 * FUNCTION: Allocates a block of memory, with a 32-bit tag
29 * VirtualAddress = a pointer to the returned memory block
30 * Length = the number of requested bytes
31 * Tag = 32-bit pool tag
33 * NDIS_STATUS_SUCCESS on success
34 * NDIS_STATUS_FAILURE on failure
39 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
41 Block
= ExAllocatePoolWithTag(NonPagedPool
, Length
, Tag
);
42 *VirtualAddress
= Block
;
45 NDIS_DbgPrint(MIN_TRACE
, ("Failed to allocate memory (%lx)\n", Length
));
46 return NDIS_STATUS_FAILURE
;
49 return NDIS_STATUS_SUCCESS
;
60 OUT PVOID
*VirtualAddress
,
63 IN NDIS_PHYSICAL_ADDRESS HighestAcceptableAddress
)
65 * FUNCTION: Allocates a block of memory
67 * VirtualAddress = Address of buffer to place virtual
68 * address of the allocated memory
69 * Length = Size of the memory block to allocate
70 * MemoryFlags = Flags to specify special restrictions
71 * HighestAcceptableAddress = Specifies -1
73 * NDIS_STATUS_SUCCESS on success
74 * NDIS_STATUS_FAILURE on failure
77 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
79 if (MemoryFlags
& NDIS_MEMORY_CONTIGUOUS
)
81 /* Allocate contiguous memory (possibly noncached) */
82 *VirtualAddress
= MmAllocateContiguousMemorySpecifyCache(Length
,
83 RtlConvertUlongToLargeInteger(0),
84 HighestAcceptableAddress
,
85 RtlConvertUlongToLargeInteger(0),
86 (MemoryFlags
& NDIS_MEMORY_NONCACHED
) ? MmNonCached
: MmCached
);
88 else if (MemoryFlags
& NDIS_MEMORY_NONCACHED
)
90 /* Allocate noncached noncontiguous memory */
91 *VirtualAddress
= MmAllocateNonCachedMemory(Length
);
95 /* Allocate plain nonpaged memory */
96 *VirtualAddress
= ExAllocatePool(NonPagedPool
, Length
);
99 if (!*VirtualAddress
) {
100 NDIS_DbgPrint(MIN_TRACE
, ("Allocation failed (%lx, %lx)\n", MemoryFlags
, Length
));
101 return NDIS_STATUS_FAILURE
;
104 return NDIS_STATUS_SUCCESS
;
114 IN PVOID VirtualAddress
,
118 * FUNCTION: Frees a memory block allocated with NdisAllocateMemory
120 * VirtualAddress = Pointer to the base virtual address of the allocated memory
121 * Length = Size of the allocated memory block as passed to NdisAllocateMemory
122 * MemoryFlags = Memory flags passed to NdisAllocateMemory
125 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
127 if (MemoryFlags
& NDIS_MEMORY_CONTIGUOUS
)
129 /* Free contiguous memory (possibly noncached) */
130 MmFreeContiguousMemorySpecifyCache(VirtualAddress
,
132 (MemoryFlags
& NDIS_MEMORY_NONCACHED
) ? MmNonCached
: MmCached
);
134 else if (MemoryFlags
& NDIS_MEMORY_NONCACHED
)
136 /* Free noncached noncontiguous memory */
137 MmFreeNonCachedMemory(VirtualAddress
, Length
);
141 /* Free nonpaged pool */
142 ExFreePool(VirtualAddress
);
152 NdisMAllocateSharedMemory(
153 IN NDIS_HANDLE MiniportAdapterHandle
,
156 OUT PVOID
*VirtualAddress
,
157 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
159 * FUNCTION: Allocate a common buffer for DMA
161 * MiniportAdapterHandle: Handle passed into MiniportInitialize
162 * Length: Number of bytes to allocate
163 * Cached: Whether or not the memory can be cached
164 * VirtualAddress: Pointer to memory is returned here
165 * PhysicalAddress: Physical address corresponding to virtual address
167 * - Cached is ignored; we always allocate non-cached
170 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
172 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
174 *VirtualAddress
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
->DmaOperations
->AllocateCommonBuffer(
175 Adapter
->NdisMiniportBlock
.SystemAdapterObject
, Length
, PhysicalAddress
, Cached
);
181 NdisMFreeSharedMemoryPassive(
184 * FUNCTION: Free a common buffer
186 * Context: Pointer to a miniport shared memory context
188 * - Called by NdisMFreeSharedMemory to do the actual work
191 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
192 PRKEVENT Event
= Memory
->Event
;
194 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
196 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
198 Memory
->AdapterObject
->DmaOperations
->FreeCommonBuffer(
199 Memory
->AdapterObject
, Memory
->Length
, Memory
->PhysicalAddress
,
200 Memory
->VirtualAddress
, Memory
->Cached
);
215 NdisMFreeSharedMemory(
216 IN NDIS_HANDLE MiniportAdapterHandle
,
219 IN PVOID VirtualAddress
,
220 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
222 * FUNCTION: Free a shared memory block
224 * MiniportAdapterHandle: Handle passed into MiniportInitialize
225 * Length: Number of bytes in the block to free
226 * Cached: Whether or not the memory was cached
227 * VirtualAddress: Address to free
228 * PhysicalAddress: corresponding physical addres
230 * - This function can be called at dispatch_level or passive_level.
231 * Therefore we have to do this in a worker thread.
235 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
236 PMINIPORT_SHARED_MEMORY Memory
;
239 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
241 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
243 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
244 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
248 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources\n"));
252 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
254 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
255 Memory
->Length
= Length
;
256 Memory
->PhysicalAddress
= PhysicalAddress
;
257 Memory
->VirtualAddress
= VirtualAddress
;
258 Memory
->Cached
= Cached
;
259 Memory
->Adapter
= &Adapter
->NdisMiniportBlock
;
260 Memory
->Event
= &Event
;
262 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMFreeSharedMemoryPassive
, Memory
);
263 ZwClose(ThreadHandle
);
265 KeWaitForSingleObject(&Event
,
274 NdisMAllocateSharedMemoryPassive(
277 * FUNCTION: Allocate a common buffer
279 * Context: Pointer to a miniport shared memory context
281 * - Called by NdisMAllocateSharedMemoryAsync to do the actual work
284 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
286 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
288 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
290 Memory
->VirtualAddress
= Memory
->AdapterObject
->DmaOperations
->AllocateCommonBuffer(
291 Memory
->AdapterObject
, Memory
->Length
, &Memory
->PhysicalAddress
, Memory
->Cached
);
293 if (Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler
)
294 Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler(
295 Memory
->Adapter
->MiniportAdapterContext
, Memory
->VirtualAddress
,
296 &Memory
->PhysicalAddress
, Memory
->Length
, Memory
->Context
);
307 NdisMAllocateSharedMemoryAsync(
308 IN NDIS_HANDLE MiniportAdapterHandle
,
314 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
315 PMINIPORT_SHARED_MEMORY Memory
;
317 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
319 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
321 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
322 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
326 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources\n"));
327 return NDIS_STATUS_FAILURE
;
330 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
331 Memory
->Length
= Length
;
332 Memory
->Cached
= Cached
;
333 Memory
->Adapter
= &Adapter
->NdisMiniportBlock
;
334 Memory
->Context
= Context
;
336 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMAllocateSharedMemoryPassive
, Memory
);
337 ZwClose(ThreadHandle
);
339 return NDIS_STATUS_PENDING
;
347 NdisAllocateSharedMemory(
348 IN NDIS_HANDLE NdisAdapterHandle
,
351 OUT PVOID
*VirtualAddress
,
352 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
354 NdisMAllocateSharedMemory(NdisAdapterHandle
,
367 NdisFreeSharedMemory(
368 IN NDIS_HANDLE NdisAdapterHandle
,
371 IN PVOID VirtualAddress
,
372 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
380 NdisMFreeSharedMemory(NdisAdapterHandle
,