bootstrap
[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 STDCALL 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,
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 }