Fixed message typo.
[captive.git] / src / libcaptive / mm / section.c
1 /* $Id$
2  * reactos memory areas emulation of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captive/mm.h" /* self, for captive_flProtect_to_mmap_prot() */
23 #include "reactos/internal/mm.h"        /* self */
24 #include "reactos/ntos/types.h" /* for PVOID etc. */
25 #include <glib/gmessages.h>
26 #include <sys/mman.h>   /* for PROT_NONE etc. */
27
28
29 /**
30  * MmAllocateSection:
31  * @Length: Length (in bytes) of the area to allocate. Does not have to be page
32  * aligned. Value 0 is forbidden.
33  *
34  * Allocates @Length area with %PAGE_READWRITE flags.
35  *
36  * Returns: Allocated address space if the allocation was successful.
37  */
38 PVOID MmAllocateSection(IN ULONG Length)
39 {
40 int mmap_prot=captive_flProtect_to_mmap_prot(PAGE_READWRITE);
41 PVOID r;
42
43         g_return_val_if_fail(Length>0,NULL);
44
45         Length=(Length|(PAGE_SIZE-1))+1;        /* round up to PAGE_SIZE */
46
47         r=mmap(
48                         NULL,   /* start */
49                         Length, /* length */
50                         mmap_prot,      /* prot */
51                         MAP_PRIVATE|MAP_ANONYMOUS,      /* flags */
52                         -1,     /* fd; ignored due to MAP_ANONYMOUS */
53                         0);     /* offset; ignored due to MAP_ANONYMOUS */
54         g_return_val_if_fail(r!=NULL,NULL);
55
56         captive_mmap_map_new(r,Length,mmap_prot);
57
58         /* assumed r!=NULL */
59         return r;
60 }
61
62
63 /**
64  * MmFlushImageSection:
65  * @SectionObjectPointer: Section to flush description.
66  * %NULL value is forbidden.
67  * @FlushType: %MmFlushForDelete or %MmFlushForWrite.
68  *
69  * Function flushes any cached #ImageSectionObject.
70  * libcaptive does not cache any images and therefore this function is a NOP there.
71  *
72  * Returns: %TRUE if the operation was successful.
73  */
74 BOOLEAN MmFlushImageSection(IN PSECTION_OBJECT_POINTERS SectionObjectPointer,IN MMFLUSH_TYPE FlushType)
75 {
76         g_return_val_if_fail(SectionObjectPointer!=NULL,FALSE);
77         g_return_val_if_fail(FlushType==MmFlushForDelete || FlushType==MmFlushForWrite,FALSE);
78
79         g_return_val_if_fail(SectionObjectPointer->ImageSectionObject==NULL,FALSE);     /* NOT IMPLEMENTED YET */
80
81   return TRUE;  /* No section mapped or it was successfuly flushed. */
82 }
83
84
85 BOOLEAN MmCanFileBeTruncated(IN PSECTION_OBJECT_POINTERS SectionObjectPointer,IN PLARGE_INTEGER NewFileSize)
86 {
87         g_return_val_if_fail(SectionObjectPointer!=NULL,FALSE); /* means do not allow truncation */
88         /* 'NewFileSize' may by NULL
89          * Passed as NULL by ntfs.sys of NT-5.1sp1; FIXME: What does it mean?
90          */
91
92         g_assert(!NewFileSize || NewFileSize->QuadPart>=0);
93
94         return TRUE;    /* allowed */
95 }