Added get_page_size function.
[wine] / server / mapping.c
1 /*
2  * Server-side file mapping management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "config.h"
13 #include "winerror.h"
14 #include "winnt.h"
15 #include "winbase.h"
16
17 #include "handle.h"
18 #include "thread.h"
19 #include "request.h"
20
21 struct mapping
22 {
23     struct object  obj;             /* object header */
24     int            size_high;       /* mapping size */
25     int            size_low;        /* mapping size */
26     int            protect;         /* protection flags */
27     struct file   *file;            /* file mapped */
28 };
29
30 static void mapping_dump( struct object *obj, int verbose );
31 static void mapping_destroy( struct object *obj );
32
33 static const struct object_ops mapping_ops =
34 {
35     sizeof(struct mapping),
36     mapping_dump,
37     no_add_queue,
38     NULL,  /* should never get called */
39     NULL,  /* should never get called */
40     NULL,  /* should never get called */
41     no_read_fd,
42     no_write_fd,
43     no_flush,
44     no_get_file_info,
45     mapping_destroy
46 };
47
48 #ifdef __i386__
49
50 /* These are always the same on an i386, and it will be faster this way */
51 # define page_mask  0xfff
52 # define page_shift 12
53 # define init_page_size() /* nothing */
54
55 #else  /* __i386__ */
56
57 static int page_shift, page_mask;
58
59 static void init_page_size(void)
60 {
61     int page_size;
62 # ifdef HAVE_GETPAGESIZE
63     page_size = getpagesize();
64 # else
65 #  ifdef __svr4__
66     page_size = sysconf(_SC_PAGESIZE);
67 #  else
68 #   error Cannot get the page size on this platform
69 #  endif
70 # endif
71     page_mask = page_size - 1;
72     /* Make sure we have a power of 2 */
73     assert( !(page_size & page_mask) );
74     page_shift = 0;
75     while ((1 << page_shift) != page_size) page_shift++;
76 }
77 #endif  /* __i386__ */
78
79 #define ROUND_ADDR(addr) \
80    ((int)(addr) & ~page_mask)
81
82 #define ROUND_SIZE(addr,size) \
83    (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
84
85
86 static struct object *create_mapping( int size_high, int size_low, int protect,
87                                       int handle, const char *name, size_t len )
88 {
89     struct mapping *mapping;
90     int access = 0;
91
92     if (!page_mask) init_page_size();
93
94     if (!(mapping = create_named_object( &mapping_ops, name, len )))
95         return NULL;
96     if (get_error() == ERROR_ALREADY_EXISTS)
97         return &mapping->obj;  /* Nothing else to do */
98
99     if (protect & VPROT_READ) access |= GENERIC_READ;
100     if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
101
102     if (handle != -1)
103     {
104         if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
105         if (!size_high && !size_low)
106         {
107             struct get_file_info_request req;
108             struct object *obj = (struct object *)mapping->file;
109             obj->ops->get_file_info( obj, &req );
110             size_high = req.size_high;
111             size_low  = ROUND_SIZE( 0, req.size_low );
112         }
113         else if (!grow_file( mapping->file, size_high, size_low )) goto error;
114     }
115     else  /* Anonymous mapping (no associated file) */
116     {
117         if (!size_high && !size_low)
118         {
119             set_error( ERROR_INVALID_PARAMETER );
120             mapping->file = NULL;
121             goto error;
122         }
123         if (!(mapping->file = create_temp_file( access ))) goto error;
124         if (!grow_file( mapping->file, size_high, size_low )) goto error;
125     }
126     mapping->size_high = size_high;
127     mapping->size_low  = ROUND_SIZE( 0, size_low );
128     mapping->protect   = protect;
129     return &mapping->obj;
130
131  error:
132     release_object( mapping );
133     return NULL;
134 }
135
136 static void mapping_dump( struct object *obj, int verbose )
137 {
138     struct mapping *mapping = (struct mapping *)obj;
139     assert( obj->ops == &mapping_ops );
140     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p name='%s'\n",
141              mapping->size_high, mapping->size_low, mapping->protect,
142              mapping->file, get_object_name( &mapping->obj ) );
143 }
144
145 static void mapping_destroy( struct object *obj )
146 {
147     struct mapping *mapping = (struct mapping *)obj;
148     assert( obj->ops == &mapping_ops );
149     if (mapping->file) release_object( mapping->file );
150 }
151
152 int get_page_size(void)
153 {
154     if (!page_mask) init_page_size();
155     return page_mask + 1;
156 }
157
158 /* create a file mapping */
159 DECL_HANDLER(create_mapping)
160 {
161     size_t len = get_req_strlen( req->name );
162     struct object *obj;
163
164     req->handle = -1;
165     if ((obj = create_mapping( req->size_high, req->size_low,
166                                req->protect, req->file_handle, req->name, len )))
167     {
168         int access = FILE_MAP_ALL_ACCESS;
169         if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
170         req->handle = alloc_handle( current->process, obj, access, req->inherit );
171         release_object( obj );
172     }
173 }
174
175 /* open a handle to a mapping */
176 DECL_HANDLER(open_mapping)
177 {
178     size_t len = get_req_strlen( req->name );
179     req->handle = open_object( req->name, len, &mapping_ops, req->access, req->inherit );
180 }
181
182 /* get a mapping information */
183 DECL_HANDLER(get_mapping_info)
184 {
185     struct mapping *mapping;
186
187     if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
188                                                      0, &mapping_ops )))
189     {
190         req->size_high = mapping->size_high;
191         req->size_low  = mapping->size_low;
192         req->protect   = mapping->protect;
193         if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
194         release_object( mapping );
195     }
196 }
197