+IoFreeMdl()
[reactos.git] / ntoskrnl / io / mdl.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            ntoskrnl/io/mdl.c
6  * PURPOSE:         Io manager mdl functions
7  * PROGRAMMER:      David Welch (welch@mcmail.com)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/pool.h>
16
17 #include <internal/debug.h>
18
19 /* GLOBALS *******************************************************************/
20
21 #define TAG_MDL    TAG('M', 'D', 'L', ' ')
22
23 /* FUNCTIONS *****************************************************************/
24
25 PMDL
26 STDCALL
27 IoAllocateMdl(PVOID VirtualAddress,
28                    ULONG Length,
29                    BOOLEAN SecondaryBuffer,
30                    BOOLEAN ChargeQuota,
31                    PIRP Irp)
32 {
33    PMDL Mdl;
34    
35    if (ChargeQuota)
36      {
37 //      Mdl = ExAllocatePoolWithQuota(NonPagedPool,
38 //                                    MmSizeOfMdl(VirtualAddress,Length));
39         Mdl = ExAllocatePoolWithTag(NonPagedPool,
40                                     MmSizeOfMdl(VirtualAddress,Length),
41                                     TAG_MDL);
42      }
43    else
44      {
45         Mdl = ExAllocatePoolWithTag(NonPagedPool,
46                                     MmSizeOfMdl(VirtualAddress,Length),
47                                     TAG_MDL);
48      }
49    MmInitializeMdl(Mdl,VirtualAddress,Length);
50    if (Irp!=NULL && !SecondaryBuffer)
51      {
52         Irp->MdlAddress = Mdl;
53      }
54    return(Mdl);
55 }
56
57 #ifndef LIBCAPTIVE
58
59 VOID
60 STDCALL
61 IoBuildPartialMdl(PMDL SourceMdl,
62                        PMDL TargetMdl,
63                        PVOID VirtualAddress,
64                        ULONG Length)
65 {
66    PULONG TargetPages = (PULONG)(TargetMdl + 1);
67    PULONG SourcePages = (PULONG)(SourceMdl + 1);
68    ULONG Va;
69    ULONG Delta = (PAGE_ROUND_DOWN(VirtualAddress) - (ULONG)SourceMdl->StartVa)/
70                  PAGE_SIZE;
71
72    for (Va = 0; Va < (PAGE_ROUND_UP(Length)/PAGE_SIZE); Va++)
73      {
74         TargetPages[Va] = SourcePages[Va+Delta];
75      }
76 }
77
78 #endif /* LIBCAPTIVE */
79
80 VOID STDCALL
81 IoFreeMdl(PMDL Mdl)
82 {   
83    MmUnmapLockedPages(MmGetSystemAddressForMdl(Mdl), Mdl);
84    MmUnlockPages(Mdl);
85    ExFreePool(Mdl);
86 }
87
88 /* EOF */