2002-10-01 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / ntoskrnl / io / mdl.c
1 /* $Id: mdl.c,v 1.11 2002/10/01 19:27:21 chorns Exp $
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 VOID
58 STDCALL
59 IoBuildPartialMdl(PMDL SourceMdl,
60 PMDL TargetMdl,
61 PVOID VirtualAddress,
62 ULONG Length)
63 {
64 PULONG TargetPages = (PULONG)(TargetMdl + 1);
65 PULONG SourcePages = (PULONG)(SourceMdl + 1);
66 ULONG Va;
67 ULONG Delta = (PAGE_ROUND_DOWN(VirtualAddress) - (ULONG)SourceMdl->StartVa)/
68 PAGE_SIZE;
69
70 for (Va = 0; Va < (PAGE_ROUND_UP(Length)/PAGE_SIZE); Va++)
71 {
72 TargetPages[Va] = SourcePages[Va+Delta];
73 }
74 }
75
76 VOID STDCALL
77 IoFreeMdl(PMDL Mdl)
78 {
79 MmUnmapLockedPages(MmGetSystemAddressForMdl(Mdl), Mdl);
80 MmUnlockPages(Mdl);
81 ExFreePool(Mdl);
82 }
83
84
85 /* EOF */