Added TEB in init_thread request.
[wine/multimedia.git] / server / mapping.c
blob06091189a082d0a82d6862ab867c8c5044f4829b
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"
20 struct mapping
22 struct object obj; /* object header */
23 int size_high; /* mapping size */
24 int size_low; /* mapping size */
25 int protect; /* protection flags */
26 struct file *file; /* file mapped */
29 static void mapping_dump( struct object *obj, int verbose );
30 static void mapping_destroy( struct object *obj );
32 static const struct object_ops mapping_ops =
34 mapping_dump,
35 no_add_queue,
36 NULL, /* should never get called */
37 NULL, /* should never get called */
38 NULL, /* should never get called */
39 no_read_fd,
40 no_write_fd,
41 no_flush,
42 no_get_file_info,
43 mapping_destroy
46 #ifdef __i386__
48 /* These are always the same on an i386, and it will be faster this way */
49 # define page_mask 0xfff
50 # define page_shift 12
51 # define init_page_size() /* nothing */
53 #else /* __i386__ */
55 static int page_shift, page_mask;
57 static void init_page_size(void)
59 int page_size;
60 # ifdef HAVE_GETPAGESIZE
61 page_size = getpagesize();
62 # else
63 # ifdef __svr4__
64 page_size = sysconf(_SC_PAGESIZE);
65 # else
66 # error Cannot get the page size on this platform
67 # endif
68 # endif
69 page_mask = page_size - 1;
70 /* Make sure we have a power of 2 */
71 assert( !(page_size & page_mask) );
72 page_shift = 0;
73 while ((1 << page_shift) != page_size) page_shift++;
75 #endif /* __i386__ */
77 #define ROUND_ADDR(addr) \
78 ((int)(addr) & ~page_mask)
80 #define ROUND_SIZE(addr,size) \
81 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
84 static struct object *create_mapping( int size_high, int size_low, int protect,
85 int handle, const char *name )
87 struct mapping *mapping;
88 int access = 0;
90 if (!page_mask) init_page_size();
92 if (!(mapping = (struct mapping *)create_named_object( name, &mapping_ops,
93 sizeof(*mapping) )))
94 return NULL;
95 if (GET_ERROR() == ERROR_ALREADY_EXISTS)
96 return &mapping->obj; /* Nothing else to do */
98 if (protect & VPROT_READ) access |= GENERIC_READ;
99 if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
101 size_low = ROUND_SIZE( 0, size_low );
102 if (handle != -1)
104 if (!(mapping->file = get_file_obj( current->process, handle, access ))) goto error;
105 if (!size_high && !size_low)
107 struct get_file_info_reply reply;
108 struct object *obj = (struct object *)mapping->file;
109 obj->ops->get_file_info( obj, &reply );
110 size_high = reply.size_high;
111 size_low = ROUND_SIZE( 0, reply.size_low );
113 else if (!grow_file( mapping->file, size_high, size_low )) goto error;
115 else /* Anonymous mapping (no associated file) */
117 if (!size_high && !size_low)
119 SET_ERROR( ERROR_INVALID_PARAMETER );
120 mapping->file = NULL;
121 goto error;
123 if (!(mapping->file = create_temp_file( access ))) goto error;
124 if (!grow_file( mapping->file, size_high, size_low )) goto error;
126 mapping->size_high = size_high;
127 mapping->size_low = size_low;
128 mapping->protect = protect;
129 return &mapping->obj;
131 error:
132 release_object( mapping );
133 return NULL;
136 static int get_mapping_info( int handle, struct get_mapping_info_reply *reply )
138 struct mapping *mapping;
139 int fd;
141 if (!(mapping = (struct mapping *)get_handle_obj( current->process, handle,
142 0, &mapping_ops )))
143 return -1;
144 reply->size_high = mapping->size_high;
145 reply->size_low = mapping->size_low;
146 reply->protect = mapping->protect;
147 if (mapping->file) fd = file_get_mmap_fd( mapping->file );
148 else fd = -1;
149 release_object( mapping );
150 return fd;
153 static void mapping_dump( struct object *obj, int verbose )
155 struct mapping *mapping = (struct mapping *)obj;
156 assert( obj->ops == &mapping_ops );
157 fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p name='%s'\n",
158 mapping->size_high, mapping->size_low, mapping->protect,
159 mapping->file, get_object_name( &mapping->obj ) );
162 static void mapping_destroy( struct object *obj )
164 struct mapping *mapping = (struct mapping *)obj;
165 assert( obj->ops == &mapping_ops );
166 if (mapping->file) release_object( mapping->file );
167 free( mapping );
170 /* create a file mapping */
171 DECL_HANDLER(create_mapping)
173 struct object *obj;
174 struct create_mapping_reply reply = { -1 };
175 char *name = (char *)data;
176 if (!len) name = NULL;
177 else CHECK_STRING( "create_mapping", name, len );
179 if ((obj = create_mapping( req->size_high, req->size_low,
180 req->protect, req->handle, name )))
182 int access = FILE_MAP_ALL_ACCESS;
183 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
184 reply.handle = alloc_handle( current->process, obj, access, req->inherit );
185 release_object( obj );
187 send_reply( current, -1, 1, &reply, sizeof(reply) );
190 /* open a handle to a mapping */
191 DECL_HANDLER(open_mapping)
193 struct open_mapping_reply reply;
194 char *name = (char *)data;
195 if (!len) name = NULL;
196 else CHECK_STRING( "open_mapping", name, len );
198 reply.handle = open_object( name, &mapping_ops, req->access, req->inherit );
199 send_reply( current, -1, 1, &reply, sizeof(reply) );
202 /* get a mapping information */
203 DECL_HANDLER(get_mapping_info)
205 struct get_mapping_info_reply reply;
206 int map_fd = get_mapping_info( req->handle, &reply );
207 send_reply( current, map_fd, 1, &reply, sizeof(reply) );