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 NdisImmediateReadSharedMemory(
145 IN NDIS_HANDLE WrapperConfigurationContext
,
146 IN ULONG SharedMemoryAddress
,
159 NdisImmediateWriteSharedMemory(
160 IN NDIS_HANDLE WrapperConfigurationContext
,
161 IN ULONG SharedMemoryAddress
,
174 NdisMAllocateSharedMemory(
175 IN NDIS_HANDLE MiniportAdapterHandle
,
178 OUT PVOID
*VirtualAddress
,
179 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
181 * FUNCTION: Allocate a common buffer for DMA
183 * MiniportAdapterHandle: Handle passed into MiniportInitialize
184 * Length: Number of bytes to allocate
185 * Cached: Whether or not the memory can be cached
186 * VirtualAddress: Pointer to memory is returned here
187 * PhysicalAddress: Physical address corresponding to virtual address
189 * - Cached is ignored; we always allocate non-cached
192 PNDIS_MINIPORT_BLOCK Adapter
= (PNDIS_MINIPORT_BLOCK
)MiniportAdapterHandle
;
194 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
196 *VirtualAddress
= Adapter
->SystemAdapterObject
->DmaOperations
->AllocateCommonBuffer(
197 Adapter
->SystemAdapterObject
, Length
, PhysicalAddress
, Cached
);
206 NdisMAllocateSharedMemoryAsync(
207 IN NDIS_HANDLE MiniportAdapterHandle
,
212 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
215 return NDIS_STATUS_FAILURE
;
221 NdisMFreeSharedMemoryPassive(
224 * FUNCTION: Free a common buffer
226 * Context: Pointer to a miniport shared memory context
228 * - Called by NdisMFreeSharedMemory to do the actual work
231 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
233 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
235 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
237 Memory
->AdapterObject
->DmaOperations
->FreeCommonBuffer(
238 Memory
->AdapterObject
, Memory
->Length
, Memory
->PhysicalAddress
,
239 Memory
->VirtualAddress
, Memory
->Cached
);
250 NdisMFreeSharedMemory(
251 IN NDIS_HANDLE MiniportAdapterHandle
,
254 IN PVOID VirtualAddress
,
255 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
257 * FUNCTION: Free a shared memory block
259 * MiniportAdapterHandle: Handle passed into MiniportInitialize
260 * Length: Number of bytes in the block to free
261 * Cached: Whether or not the memory was cached
262 * VirtualAddress: Address to free
263 * PhysicalAddress: corresponding physical addres
265 * - This function can be called at dispatch_level or passive_level.
266 * Therefore we have to do this in a worker thread.
270 PNDIS_MINIPORT_BLOCK Adapter
= (PNDIS_MINIPORT_BLOCK
)MiniportAdapterHandle
;
271 PMINIPORT_SHARED_MEMORY Memory
;
273 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
275 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
277 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
278 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
282 NDIS_DbgPrint(MID_TRACE
, ("Insufficient resources\n"));
286 Memory
->AdapterObject
= Adapter
->SystemAdapterObject
;
287 Memory
->Length
= Length
;
288 Memory
->PhysicalAddress
= PhysicalAddress
;
289 Memory
->VirtualAddress
= VirtualAddress
;
290 Memory
->Cached
= Cached
;
292 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMFreeSharedMemoryPassive
, Memory
);
293 ZwClose(ThreadHandle
);