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 return NDIS_STATUS_FAILURE
;
47 return NDIS_STATUS_SUCCESS
;
58 OUT PVOID
*VirtualAddress
,
61 IN NDIS_PHYSICAL_ADDRESS HighestAcceptableAddress
)
63 * FUNCTION: Allocates a block of memory
65 * VirtualAddress = Address of buffer to place virtual
66 * address of the allocated memory
67 * Length = Size of the memory block to allocate
68 * MemoryFlags = Flags to specify special restrictions
69 * HighestAcceptableAddress = Specifies -1
71 * NDIS_STATUS_SUCCESS on success
72 * NDIS_STATUS_FAILURE on failure
75 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
77 if (MemoryFlags
& NDIS_MEMORY_NONCACHED
)
79 *VirtualAddress
= MmAllocateNonCachedMemory(Length
);
81 return NDIS_STATUS_FAILURE
;
83 return NDIS_STATUS_SUCCESS
;
86 if (MemoryFlags
& NDIS_MEMORY_CONTIGUOUS
)
88 *VirtualAddress
= MmAllocateContiguousMemory(Length
, HighestAcceptableAddress
);
90 return NDIS_STATUS_FAILURE
;
92 return NDIS_STATUS_SUCCESS
;
95 /* Plain nonpaged memory */
96 *VirtualAddress
= ExAllocatePool(NonPagedPool
, Length
);
98 return NDIS_STATUS_FAILURE
;
100 return NDIS_STATUS_SUCCESS
;
110 IN PVOID VirtualAddress
,
114 * FUNCTION: Frees a memory block allocated with NdisAllocateMemory
116 * VirtualAddress = Pointer to the base virtual address of the allocated memory
117 * Length = Size of the allocated memory block as passed to NdisAllocateMemory
118 * MemoryFlags = Memory flags passed to NdisAllocateMemory
121 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
123 if (MemoryFlags
& NDIS_MEMORY_NONCACHED
)
125 MmFreeNonCachedMemory(VirtualAddress
, Length
);
129 if (MemoryFlags
& NDIS_MEMORY_CONTIGUOUS
)
131 MmFreeContiguousMemory(VirtualAddress
);
135 ExFreePool(VirtualAddress
);
144 NdisMAllocateSharedMemory(
145 IN NDIS_HANDLE MiniportAdapterHandle
,
148 OUT PVOID
*VirtualAddress
,
149 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
151 * FUNCTION: Allocate a common buffer for DMA
153 * MiniportAdapterHandle: Handle passed into MiniportInitialize
154 * Length: Number of bytes to allocate
155 * Cached: Whether or not the memory can be cached
156 * VirtualAddress: Pointer to memory is returned here
157 * PhysicalAddress: Physical address corresponding to virtual address
159 * - Cached is ignored; we always allocate non-cached
162 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
164 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
166 *VirtualAddress
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
->DmaOperations
->AllocateCommonBuffer(
167 Adapter
->NdisMiniportBlock
.SystemAdapterObject
, Length
, PhysicalAddress
, Cached
);
173 NdisMFreeSharedMemoryPassive(
176 * FUNCTION: Free a common buffer
178 * Context: Pointer to a miniport shared memory context
180 * - Called by NdisMFreeSharedMemory to do the actual work
183 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
185 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
187 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
189 Memory
->AdapterObject
->DmaOperations
->FreeCommonBuffer(
190 Memory
->AdapterObject
, Memory
->Length
, Memory
->PhysicalAddress
,
191 Memory
->VirtualAddress
, Memory
->Cached
);
202 NdisMFreeSharedMemory(
203 IN NDIS_HANDLE MiniportAdapterHandle
,
206 IN PVOID VirtualAddress
,
207 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
209 * FUNCTION: Free a shared memory block
211 * MiniportAdapterHandle: Handle passed into MiniportInitialize
212 * Length: Number of bytes in the block to free
213 * Cached: Whether or not the memory was cached
214 * VirtualAddress: Address to free
215 * PhysicalAddress: corresponding physical addres
217 * - This function can be called at dispatch_level or passive_level.
218 * Therefore we have to do this in a worker thread.
222 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
223 PMINIPORT_SHARED_MEMORY Memory
;
225 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
227 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
229 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
230 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
234 NDIS_DbgPrint(MID_TRACE
, ("Insufficient resources\n"));
238 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
239 Memory
->Length
= Length
;
240 Memory
->PhysicalAddress
= PhysicalAddress
;
241 Memory
->VirtualAddress
= VirtualAddress
;
242 Memory
->Cached
= Cached
;
244 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMFreeSharedMemoryPassive
, Memory
);
245 ZwClose(ThreadHandle
);
250 NdisMAllocateSharedMemoryPassive(
253 * FUNCTION: Allocate a common buffer
255 * Context: Pointer to a miniport shared memory context
257 * - Called by NdisMAllocateSharedMemoryAsync to do the actual work
260 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
262 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
264 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
266 Memory
->VirtualAddress
= Memory
->AdapterObject
->DmaOperations
->AllocateCommonBuffer(
267 Memory
->AdapterObject
, Memory
->Length
, &Memory
->PhysicalAddress
, Memory
->Cached
);
269 if (Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler
)
270 Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler(
271 Memory
->Adapter
, Memory
->VirtualAddress
, &Memory
->PhysicalAddress
, Memory
->Length
, Memory
->Context
);
282 NdisMAllocateSharedMemoryAsync(
283 IN NDIS_HANDLE MiniportAdapterHandle
,
289 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
290 PMINIPORT_SHARED_MEMORY Memory
;
292 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
294 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
296 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
297 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
301 NDIS_DbgPrint(MID_TRACE
, ("Insufficient resources\n"));
302 return NDIS_STATUS_FAILURE
;
305 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
306 Memory
->Length
= Length
;
307 Memory
->Cached
= Cached
;
308 Memory
->Adapter
= &Adapter
->NdisMiniportBlock
;
309 Memory
->Context
= Context
;
311 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMAllocateSharedMemoryPassive
, Memory
);
312 ZwClose(ThreadHandle
);
314 return NDIS_STATUS_PENDING
;
322 NdisAllocateSharedMemory(
323 IN NDIS_HANDLE NdisAdapterHandle
,
326 OUT PVOID
*VirtualAddress
,
327 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
329 NdisMAllocateSharedMemory(NdisAdapterHandle
,
342 NdisFreeSharedMemory(
343 IN NDIS_HANDLE NdisAdapterHandle
,
346 IN PVOID VirtualAddress
,
347 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
355 NdisMFreeSharedMemory(NdisAdapterHandle
,