ReactOS  0.4.14-dev-317-g96040ec
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 
17  PVOID Buffer,
19  BOOLEAN Front)
20 {
22  PNDIS_BUFFER NdisBuffer;
23 
24  /* Allocate the NDIS buffer mapping the pool */
26  &NdisBuffer,
27  AdapterContext->BufferPoolHandle,
28  Buffer,
29  BufferSize);
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 */
48  return NDIS_STATUS_SUCCESS;
49 }
50 
53  PVOID Buffer,
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 
82 VOID
84 {
86  PVOID Data;
87  ULONG Length;
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 */
106  ExFreePool(Data);
107  }
108  }
109 
110  /* Free the packet descriptor */
112 }
113 
116 {
117  KIRQL OldIrql;
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 
141 VOID
143 {
144  /* Increment the open count */
145  AdapterContext->OpenCount++;
146 }
147 
148 VOID
150  PNDISUIO_OPEN_ENTRY OpenEntry)
151 {
152  KIRQL OldIrql;
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 }
VOID DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PNDISUIO_OPEN_ENTRY OpenEntry)
Definition: misc.c:149
VOID EXPORT NdisAllocatePacket(OUT PNDIS_STATUS Status, OUT PNDIS_PACKET *Packet, IN NDIS_HANDLE PoolHandle)
Definition: buffer.c:392
#define TRUE
Definition: types.h:120
PNDISUIO_ADAPTER_CONTEXT FindAdapterContextByName(PNDIS_STRING DeviceName)
Definition: misc.c:115
NDIS_HANDLE BufferPoolHandle
Definition: ndisuio.h:34
_In_ NDIS_HANDLE _In_ PNDIS_PACKET Packet
Definition: ndis.h:1548
MDL * PNDIS_BUFFER
Definition: ndis.h:343
KSPIN_LOCK Spinlock
Definition: ndisuio.h:50
VOID CleanupAndFreePacket(PNDIS_PACKET Packet, BOOLEAN FreePool)
Definition: misc.c:83
VOID EXPORT NdisFreeBuffer(IN PNDIS_BUFFER Buffer)
Definition: buffer.c:809
WCHAR DeviceName[]
Definition: adapter.cpp:21
#define NdisChainBufferAtFront(Packet, Buffer)
Definition: ndis.h:3106
#define FreePool(P)
Definition: mntmgr.h:154
int NDIS_STATUS
Definition: ntddndis.h:471
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
VOID ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
Definition: misc.c:142
UCHAR KIRQL
Definition: env_spec_w32.h:591
NDIS_STATUS AllocateAndChainBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PNDIS_PACKET Packet, PVOID Buffer, ULONG BufferSize, BOOLEAN Front)
Definition: misc.c:15
unsigned char BOOLEAN
Definition: ndisuio.h:53
smooth NULL
Definition: ftsmooth.c:416
_In_ LPGUID _In_ PVOID Data
Definition: classpnp.h:778
NDIS_HANDLE PacketPoolHandle
Definition: ndisuio.h:33
Definition: bufpool.h:45
KSPIN_LOCK GlobalAdapterListLock
Definition: main.c:16
VOID EXPORT NdisAllocateBuffer(OUT PNDIS_STATUS Status, OUT PNDIS_BUFFER *Buffer, IN NDIS_HANDLE PoolHandle, IN PVOID VirtualAddress, IN UINT Length)
Definition: buffer.c:334
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
VOID EXPORT NdisFreePacket(IN PNDIS_PACKET Packet)
Definition: buffer.c:826
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
#define BufferSize
Definition: classpnp.h:419
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
PFILE_OBJECT FileObject
Definition: ndisuio.h:56
UNICODE_STRING DeviceName
Definition: ndisuio.h:44
PNDIS_PACKET CreatePacketFromPoolBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext, PVOID Buffer, ULONG BufferSize)
Definition: misc.c:52
#define NDIS_STATUS_SUCCESS
Definition: ndis.h:346
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
LIST_ENTRY ListEntry
Definition: ndisuio.h:62
LIST_ENTRY GlobalAdapterList
Definition: main.c:17
VOID EXPORT NdisUnchainBufferAtFront(IN OUT PNDIS_PACKET Packet, OUT PNDIS_BUFFER *Buffer)
Definition: buffer.c:1067
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
#define DPRINT1
Definition: precomp.h:8
VOID EXPORT NdisQueryBuffer(IN PNDIS_BUFFER Buffer, OUT PVOID *VirtualAddress OPTIONAL, OUT PUINT Length)
Definition: buffer.c:953
unsigned int ULONG
Definition: retypes.h:1
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
NTSYSAPI BOOLEAN NTAPI RtlEqualUnicodeString(PUNICODE_STRING String1, PUNICODE_STRING String2, BOOLEAN CaseInSensitive)
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define NdisChainBufferAtBack(Packet, Buffer)
Definition: ndis.h:3128