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 if (KeGetCurrentIrql() != PASSIVE_LEVEL
)
176 KeBugCheckEx(BUGCODE_ID_DRIVER
,
177 (ULONG_PTR
)MiniportAdapterHandle
,
183 *VirtualAddress
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
->DmaOperations
->AllocateCommonBuffer(
184 Adapter
->NdisMiniportBlock
.SystemAdapterObject
, Length
, PhysicalAddress
, Cached
);
190 NdisMFreeSharedMemoryPassive(
193 * FUNCTION: Free a common buffer
195 * Context: Pointer to a miniport shared memory context
197 * - Called by NdisMFreeSharedMemory to do the actual work
200 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
201 PRKEVENT Event
= Memory
->Event
;
203 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
205 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
207 Memory
->AdapterObject
->DmaOperations
->FreeCommonBuffer(
208 Memory
->AdapterObject
, Memory
->Length
, Memory
->PhysicalAddress
,
209 Memory
->VirtualAddress
, Memory
->Cached
);
224 NdisMFreeSharedMemory(
225 IN NDIS_HANDLE MiniportAdapterHandle
,
228 IN PVOID VirtualAddress
,
229 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
231 * FUNCTION: Free a shared memory block
233 * MiniportAdapterHandle: Handle passed into MiniportInitialize
234 * Length: Number of bytes in the block to free
235 * Cached: Whether or not the memory was cached
236 * VirtualAddress: Address to free
237 * PhysicalAddress: corresponding physical addres
239 * - This function can be called at dispatch_level or passive_level.
240 * Therefore we have to do this in a worker thread.
244 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
245 PMINIPORT_SHARED_MEMORY Memory
;
248 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
250 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
252 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
253 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
257 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources\n"));
261 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
263 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
264 Memory
->Length
= Length
;
265 Memory
->PhysicalAddress
= PhysicalAddress
;
266 Memory
->VirtualAddress
= VirtualAddress
;
267 Memory
->Cached
= Cached
;
268 Memory
->Adapter
= &Adapter
->NdisMiniportBlock
;
269 Memory
->Event
= &Event
;
271 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMFreeSharedMemoryPassive
, Memory
);
272 ZwClose(ThreadHandle
);
274 KeWaitForSingleObject(&Event
,
283 NdisMAllocateSharedMemoryPassive(
286 * FUNCTION: Allocate a common buffer
288 * Context: Pointer to a miniport shared memory context
290 * - Called by NdisMAllocateSharedMemoryAsync to do the actual work
293 PMINIPORT_SHARED_MEMORY Memory
= (PMINIPORT_SHARED_MEMORY
)Context
;
295 NDIS_DbgPrint(MAX_TRACE
, ("Called.\n"));
297 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL
);
299 Memory
->VirtualAddress
= Memory
->AdapterObject
->DmaOperations
->AllocateCommonBuffer(
300 Memory
->AdapterObject
, Memory
->Length
, &Memory
->PhysicalAddress
, Memory
->Cached
);
302 if (Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler
)
303 Memory
->Adapter
->DriverHandle
->MiniportCharacteristics
.AllocateCompleteHandler(
304 Memory
->Adapter
->MiniportAdapterContext
, Memory
->VirtualAddress
,
305 &Memory
->PhysicalAddress
, Memory
->Length
, Memory
->Context
);
316 NdisMAllocateSharedMemoryAsync(
317 IN NDIS_HANDLE MiniportAdapterHandle
,
323 PLOGICAL_ADAPTER Adapter
= (PLOGICAL_ADAPTER
)MiniportAdapterHandle
;
324 PMINIPORT_SHARED_MEMORY Memory
;
326 NDIS_DbgPrint(MAX_TRACE
,("Called.\n"));
328 ASSERT(KeGetCurrentIrql() <= DISPATCH_LEVEL
);
330 /* Must be NonpagedPool because by definition we're at DISPATCH_LEVEL */
331 Memory
= ExAllocatePool(NonPagedPool
, sizeof(MINIPORT_SHARED_MEMORY
));
335 NDIS_DbgPrint(MIN_TRACE
, ("Insufficient resources\n"));
336 return NDIS_STATUS_FAILURE
;
339 Memory
->AdapterObject
= Adapter
->NdisMiniportBlock
.SystemAdapterObject
;
340 Memory
->Length
= Length
;
341 Memory
->Cached
= Cached
;
342 Memory
->Adapter
= &Adapter
->NdisMiniportBlock
;
343 Memory
->Context
= Context
;
345 PsCreateSystemThread(&ThreadHandle
, THREAD_ALL_ACCESS
, 0, 0, 0, NdisMAllocateSharedMemoryPassive
, Memory
);
346 ZwClose(ThreadHandle
);
348 return NDIS_STATUS_PENDING
;
356 NdisAllocateSharedMemory(
357 IN NDIS_HANDLE NdisAdapterHandle
,
360 OUT PVOID
*VirtualAddress
,
361 OUT PNDIS_PHYSICAL_ADDRESS PhysicalAddress
)
363 NdisMAllocateSharedMemory(NdisAdapterHandle
,
376 NdisFreeSharedMemory(
377 IN NDIS_HANDLE NdisAdapterHandle
,
380 IN PVOID VirtualAddress
,
381 IN NDIS_PHYSICAL_ADDRESS PhysicalAddress
)
389 NdisMFreeSharedMemory(NdisAdapterHandle
,