Encapsulated Wine internal data into an ifdef __WINE__.
[wine.git] / server / mapping.c
blob66fefd250c5e7974639004baacf05612fa3425ef
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include "config.h"
13 #include "winerror.h"
14 #include "winnt.h"
15 #include "winbase.h"
17 #include "handle.h"
18 #include "thread.h"
19 #include "request.h"
21 struct mapping
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 */
30 static void mapping_dump( struct object *obj, int verbose );
31 static void mapping_destroy( struct object *obj );
33 static const struct object_ops mapping_ops =
35 sizeof(struct mapping), /* size */
36 mapping_dump, /* dump */
37 no_add_queue, /* add_queue */
38 NULL, /* remove_queue */
39 NULL, /* signaled */
40 NULL, /* satisfied */
41 NULL, /* get_poll_events */
42 NULL, /* poll_event */
43 no_read_fd, /* get_read_fd */
44 no_write_fd, /* get_write_fd */
45 no_flush, /* flush */
46 no_get_file_info, /* get_file_info */
47 mapping_destroy /* destroy */
50 #ifdef __i386__
52 /* These are always the same on an i386, and it will be faster this way */
53 # define page_mask 0xfff
54 # define page_shift 12
55 # define init_page_size() /* nothing */
57 #else /* __i386__ */
59 static int page_shift, page_mask;
61 static void init_page_size(void)
63 int page_size;
64 # ifdef HAVE_GETPAGESIZE
65 page_size = getpagesize();
66 # else
67 # ifdef __svr4__
68 page_size = sysconf(_SC_PAGESIZE);
69 # else
70 # error Cannot get the page size on this platform
71 # endif
72 # endif
73 page_mask = page_size - 1;
74 /* Make sure we have a power of 2 */
75 assert( !(page_size & page_mask) );
76 page_shift = 0;
77 while ((1 << page_shift) != page_size) page_shift++;
79 #endif /* __i386__ */
81 #define ROUND_ADDR(addr) \
82 ((int)(addr) & ~page_mask)
84 #define ROUND_SIZE(addr,size) \
85 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
88 static struct object *create_mapping( int size_high, int size_low, int protect,
89 int handle, const WCHAR *name, size_t len )
91 struct mapping *mapping;
92 int access = 0;
94 if (!page_mask) init_page_size();
96 if (!(mapping = create_named_object( &mapping_ops, name, len )))
97 return NULL;
98 if (get_error() == ERROR_ALREADY_EXISTS)
99 return &mapping->obj; /* Nothing else to do */
101 if (protect & VPROT_READ) access |= GENERIC_READ;
102 if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
104 if (handle != -1)
106 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
107 if (!size_high && !size_low)
109 struct get_file_info_request req;
110 struct object *obj = (struct object *)mapping->file;
111 obj->ops->get_file_info( obj, &req );
112 size_high = req.size_high;
113 size_low = ROUND_SIZE( 0, req.size_low );
115 else if (!grow_file( mapping->file, size_high, size_low )) goto error;
117 else /* Anonymous mapping (no associated file) */
119 if (!size_high && !size_low)
121 set_error( ERROR_INVALID_PARAMETER );
122 mapping->file = NULL;
123 goto error;
125 if (!(mapping->file = create_temp_file( access ))) goto error;
126 if (!grow_file( mapping->file, size_high, size_low )) goto error;
128 mapping->size_high = size_high;
129 mapping->size_low = ROUND_SIZE( 0, size_low );
130 mapping->protect = protect;
131 return &mapping->obj;
133 error:
134 release_object( mapping );
135 return NULL;
138 static void mapping_dump( struct object *obj, int verbose )
140 struct mapping *mapping = (struct mapping *)obj;
141 assert( obj->ops == &mapping_ops );
142 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p ",
143 mapping->size_high, mapping->size_low, mapping->protect, mapping->file );
144 dump_object_name( &mapping->obj );
145 fputc( '\n', stderr );
148 static void mapping_destroy( struct object *obj )
150 struct mapping *mapping = (struct mapping *)obj;
151 assert( obj->ops == &mapping_ops );
152 if (mapping->file) release_object( mapping->file );
155 int get_page_size(void)
157 if (!page_mask) init_page_size();
158 return page_mask + 1;
161 /* create a file mapping */
162 DECL_HANDLER(create_mapping)
164 size_t len = get_req_strlenW( req->name );
165 struct object *obj;
167 req->handle = -1;
168 if ((obj = create_mapping( req->size_high, req->size_low,
169 req->protect, req->file_handle, req->name, len )))
171 int access = FILE_MAP_ALL_ACCESS;
172 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
173 req->handle = alloc_handle( current->process, obj, access, req->inherit );
174 release_object( obj );
178 /* open a handle to a mapping */
179 DECL_HANDLER(open_mapping)
181 size_t len = get_req_strlenW( req->name );
182 req->handle = open_object( req->name, len, &mapping_ops, req->access, req->inherit );
185 /* get a mapping information */
186 DECL_HANDLER(get_mapping_info)
188 struct mapping *mapping;
190 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
191 0, &mapping_ops )))
193 req->size_high = mapping->size_high;
194 req->size_low = mapping->size_low;
195 req->protect = mapping->protect;
196 if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
197 release_object( mapping );