ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

misc.c
Go to the documentation of this file.
00001 /*
00002  * COPYRIGHT:   See COPYING in the top level directory
00003  * PROJECT:     ReactOS NDIS User I/O driver
00004  * FILE:        misc.c
00005  * PURPOSE:     Helper functions
00006  * PROGRAMMERS: Cameron Gutman (cameron.gutman@reactos.org)
00007  */
00008 
00009 #include "ndisuio.h"
00010 
00011 #define NDEBUG
00012 #include <debug.h>
00013 
00014 NDIS_STATUS
00015 AllocateAndChainBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
00016                        PNDIS_PACKET Packet,
00017                        PVOID Buffer,
00018                        ULONG BufferSize,
00019                        BOOLEAN Front)
00020 {
00021     NDIS_STATUS Status;
00022     PNDIS_BUFFER NdisBuffer;
00023 
00024     /* Allocate the NDIS buffer mapping the pool */
00025     NdisAllocateBuffer(&Status,
00026                        &NdisBuffer,
00027                        AdapterContext->BufferPoolHandle,
00028                        Buffer,
00029                        BufferSize);
00030     if (Status != NDIS_STATUS_SUCCESS)
00031     {
00032         DPRINT1("No free buffer descriptors\n");
00033         return Status;
00034     }
00035 
00036     if (Front)
00037     {
00038         /* Chain the buffer to front */
00039         NdisChainBufferAtFront(Packet, NdisBuffer);
00040     }
00041     else
00042     {
00043         /* Chain the buffer to back */
00044         NdisChainBufferAtBack(Packet, NdisBuffer);
00045     }
00046 
00047     /* Return success */
00048     return NDIS_STATUS_SUCCESS;
00049 }
00050 
00051 PNDIS_PACKET
00052 CreatePacketFromPoolBuffer(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
00053                            PVOID Buffer,
00054                            ULONG BufferSize)
00055 {
00056     PNDIS_PACKET Packet;
00057     NDIS_STATUS Status;
00058 
00059     /* Allocate a packet descriptor */
00060     NdisAllocatePacket(&Status,
00061                        &Packet,
00062                        AdapterContext->PacketPoolHandle);
00063     if (Status != NDIS_STATUS_SUCCESS)
00064     {
00065         DPRINT1("No free packet descriptors\n");
00066         return NULL;
00067     }
00068 
00069     /* Use the helper to chain the buffer */
00070     Status = AllocateAndChainBuffer(AdapterContext, Packet,
00071                                     Buffer, BufferSize, TRUE);
00072     if (Status != NDIS_STATUS_SUCCESS)
00073     {
00074         NdisFreePacket(Packet);
00075         return NULL;
00076     }
00077 
00078     /* Return the packet */
00079     return Packet;
00080 }
00081 
00082 VOID
00083 CleanupAndFreePacket(PNDIS_PACKET Packet, BOOLEAN FreePool)
00084 {
00085     PNDIS_BUFFER Buffer;
00086     PVOID Data;
00087     ULONG Length;
00088 
00089     /* Free each buffer and its backing pool memory */
00090     while (TRUE)
00091     {
00092         /* Unchain each buffer */
00093         NdisUnchainBufferAtFront(Packet, &Buffer);
00094         if (!Buffer)
00095             break;
00096         
00097         /* Get the backing memory */
00098         NdisQueryBuffer(Buffer, &Data, &Length);
00099         
00100         /* Free the buffer */
00101         NdisFreeBuffer(Buffer);
00102 
00103         if (FreePool)
00104         {
00105             /* Free the backing memory */
00106             ExFreePool(Data);
00107         }
00108     }
00109     
00110     /* Free the packet descriptor */
00111     NdisFreePacket(Packet);
00112 }
00113 
00114 PNDISUIO_ADAPTER_CONTEXT
00115 FindAdapterContextByName(PNDIS_STRING DeviceName)
00116 {
00117     KIRQL OldIrql;
00118     PLIST_ENTRY CurrentEntry;
00119     PNDISUIO_ADAPTER_CONTEXT AdapterContext;
00120 
00121     KeAcquireSpinLock(&GlobalAdapterListLock, &OldIrql);
00122     CurrentEntry = GlobalAdapterList.Flink;
00123     while (CurrentEntry != &GlobalAdapterList)
00124     {
00125         AdapterContext = CONTAINING_RECORD(CurrentEntry, NDISUIO_ADAPTER_CONTEXT, ListEntry);
00126         
00127         /* Check if the device name matches */
00128         if (RtlEqualUnicodeString(&AdapterContext->DeviceName, DeviceName, TRUE))
00129         {
00130             KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
00131             return AdapterContext;
00132         }
00133         
00134         CurrentEntry = CurrentEntry->Flink;
00135     }
00136     KeReleaseSpinLock(&GlobalAdapterListLock, OldIrql);
00137     
00138     return NULL;
00139 }
00140 
00141 VOID
00142 ReferenceAdapterContext(PNDISUIO_ADAPTER_CONTEXT AdapterContext)
00143 {
00144     /* Increment the open count */
00145     AdapterContext->OpenCount++;
00146 }
00147 
00148 VOID
00149 DereferenceAdapterContextWithOpenEntry(PNDISUIO_ADAPTER_CONTEXT AdapterContext,
00150                                        PNDISUIO_OPEN_ENTRY OpenEntry)
00151 {
00152     KIRQL OldIrql;
00153 
00154     /* Lock the adapter context */
00155     KeAcquireSpinLock(&AdapterContext->Spinlock, &OldIrql);
00156     
00157     /* Decrement the open count */
00158     AdapterContext->OpenCount--;
00159 
00160     /* Cleanup the open entry if we were given one */
00161     if (OpenEntry != NULL)
00162     {
00163         /* Remove the open entry */
00164         RemoveEntryList(&OpenEntry->ListEntry);
00165 
00166         /* Invalidate the FO */
00167         OpenEntry->FileObject->FsContext = NULL;
00168         OpenEntry->FileObject->FsContext2 = NULL;
00169 
00170         /* Free the open entry */
00171         ExFreePool(OpenEntry);
00172     }
00173 
00174     /* Release the adapter context lock */
00175     KeReleaseSpinLock(&AdapterContext->Spinlock, OldIrql);
00176 }

Generated on Thu May 24 2012 04:16:50 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.