ReactOS 0.4.16-dev-334-g4d9f67c
misc.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NDIS User I/O driver
4 * FILE: misc.c
5 * PURPOSE: Helper functions
6 * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
7 */
8
9#include "ndisuio.h"
10
11#define NDEBUG
12#include <debug.h>
13
19 BOOLEAN Front)
20{
22 PNDIS_BUFFER NdisBuffer;
23
24 /* Allocate the NDIS buffer mapping the pool */
26 &NdisBuffer,
27 AdapterContext->BufferPoolHandle,
28 Buffer,
31 {
32 DPRINT1("No free buffer descriptors\n");
33 return Status;
34 }
35
36 if (Front)
37 {
38 /* Chain the buffer to front */
39 NdisChainBufferAtFront(Packet, NdisBuffer);
40 }
41 else
42 {
43 /* Chain the buffer to back */
44 NdisChainBufferAtBack(Packet, NdisBuffer);
45 }
46
47 /* Return success */
49}
50
55{
58
59 /* Allocate a packet descriptor */
61 &Packet,
62 AdapterContext->PacketPoolHandle);
64 {
65 DPRINT1("No free packet descriptors\n");
66 return NULL;
67 }
68
69 /* Use the helper to chain the buffer */
70 Status = AllocateAndChainBuffer(AdapterContext, Packet,
73 {
75 return NULL;
76 }
77
78 /* Return the packet */
79 return Packet;
80}
81
82VOID
84{
86 PVOID Data;
88
89 /* Free each buffer and its backing pool memory */
90 while (TRUE)
91 {
92 /* Unchain each buffer */
94 if (!Buffer)
95 break;
96
97 /* Get the backing memory */
99
100 /* Free the buffer */
102
103 if (FreePool)
104 {
105 /* Free the backing memory */
107 }
108 }
109
110 /* Free the packet descriptor */
112}
113
116{
118 PLIST_ENTRY CurrentEntry;
119 PNDISUIO_ADAPTER_CONTEXT AdapterContext;
120
122 CurrentEntry = GlobalAdapterList.Flink;
123 while (CurrentEntry != &GlobalAdapterList)
124 {
125 AdapterContext = CONTAINING_RECORD(CurrentEntry, NDISUIO_ADAPTER_CONTEXT, ListEntry);
126
127 /* Check if the device name matches */
128 if (RtlEqualUnicodeString(&AdapterContext->DeviceName, DeviceName, TRUE))
129 {
131 return AdapterContext;
132 }
133
134 CurrentEntry = CurrentEntry->Flink;
135 }
137
138 return NULL;
139}
140
141VOID
143{
144 /* Increment the open count */
145 AdapterContext->OpenCount++;
146}
147
148VOID
150 PNDISUIO_OPEN_ENTRY OpenEntry)
151{
153
154 /* Lock the adapter context */
155 KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
156
157 /* Decrement the open count */
158 AdapterContext->OpenCount--;
159
160 /* Cleanup the open entry if we were given one */
161 if (OpenEntry != NULL)
162 {
163 /* Remove the open entry */
164 RemoveEntryList(&OpenEntry->ListEntry);
165
166 /* Invalidate the FO */
167 OpenEntry->FileObject->FsContext = NULL;
168 OpenEntry->FileObject->FsContext2 = NULL;
169
170 /* Free the open entry */
171 ExFreePool(OpenEntry);
172 }
173
174 /* Release the adapter context lock */
175 KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
176}
unsigned char BOOLEAN
#define DPRINT1
Definition: precomp.h:8
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
VOID EXPORT NdisAllocateBuffer(OUT PNDIS_STATUS Status, OUT PNDIS_BUFFER *Buffer, IN NDIS_HANDLE PoolHandle, IN PVOID VirtualAddress, IN UINT Length)
Definition: buffer.c:336
VOID EXPORT NdisFreePacket(IN PNDIS_PACKET Packet)
Definition: buffer.c:828
VOID EXPORT NdisAllocatePacket(OUT PNDIS_STATUS Status, OUT PNDIS_PACKET *Packet, IN NDIS_HANDLE PoolHandle)
Definition: buffer.c:394
VOID EXPORT NdisUnchainBufferAtFront(IN OUT PNDIS_PACKET Packet, OUT PNDIS_BUFFER *Buffer)
Definition: buffer.c:1069
LIST_ENTRY GlobalAdapterList
Definition: main.c:17
KSPIN_LOCK GlobalAdapterListLock
Definition: main.c:16
VOID DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PNDISUIO_OPEN_ENTRY OpenEntry)
Definition: misc.c:149
NDIS_STATUS AllocateAndChainBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PNDIS_PACKET Packet, PVOID Buffer, ULONG BufferSize, BOOLEAN Front)
Definition: misc.c:15
PNDISUIO_ADAPTER_CONTEXT FindAdapterContextByName(PNDIS_STRING DeviceName)
Definition: misc.c:115
PNDIS_PACKET CreatePacketFromPoolBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PVOID Buffer, ULONG BufferSize)
Definition: misc.c:52
VOID ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
Definition: misc.c:142
VOID CleanupAndFreePacket(PNDIS_PACKET Packet, BOOLEAN FreePool)
Definition: misc.c:83
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
Status
Definition: gdiplustypes.h:25
#define FreePool(P)
Definition: mntmgr.h:154
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1549
#define NdisChainBufferAtBack(Packet, Buffer)
Definition: ndis.h:3128
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
MDL * PNDIS_BUFFER
Definition: ndis.h:343
#define NdisFreeBuffer
Definition: ndis.h:2895
#define NdisQueryBuffer(_Buffer, _VirtualAddress, _Length)
Definition: ndis.h:3029
#define NdisChainBufferAtFront(Packet, Buffer)
Definition: ndis.h:3106
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
int NDIS_STATUS
Definition: ntddndis.h:475
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
NDIS_HANDLE PacketPoolHandle
Definition: ndisuio.h:33
KSPIN_LOCK Spinlock
Definition: ndisuio.h:50
NDIS_HANDLE BufferPoolHandle
Definition: ndisuio.h:34
UNICODE_STRING DeviceName
Definition: ndisuio.h:44
Definition: ndisuio.h:54
LIST_ENTRY ListEntry
Definition: ndisuio.h:62
PFILE_OBJECT FileObject
Definition: ndisuio.h:56
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3275
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778