Fixed GetRandomRgn.
[wine/dcerpc.git] / server / mapping.c
blobdc0690c68da086e57cb162f85b580e14e20f5966
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),
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
48 #ifdef __i386__
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 */
55 #else /* __i386__ */
57 static int page_shift, page_mask;
59 static void init_page_size(void)
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++;
77 #endif /* __i386__ */
79 #define ROUND_ADDR(addr) \
80 ((int)(addr) & ~page_mask)
82 #define ROUND_SIZE(addr,size) \
83 (((int)(size) + ((int)(addr) & page_mask) + page_mask) & ~page_mask)
86 static struct object *create_mapping( int size_high, int size_low, int protect,
87 int handle, const char *name, size_t len )
89 struct mapping *mapping;
90 int access = 0;
92 if (!page_mask) init_page_size();
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 */
99 if (protect & VPROT_READ) access |= GENERIC_READ;
100 if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
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_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 );
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 = ROUND_SIZE( 0, size_low );
128 mapping->protect = protect;
129 return &mapping->obj;
131 error:
132 release_object( mapping );
133 return NULL;
136 static void mapping_dump( struct object *obj, int verbose )
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 ) );
145 static void mapping_destroy( struct object *obj )
147 struct mapping *mapping = (struct mapping *)obj;
148 assert( obj->ops == &mapping_ops );
149 if (mapping->file) release_object( mapping->file );
152 /* create a file mapping */
153 DECL_HANDLER(create_mapping)
155 size_t len = get_req_strlen( req->name );
156 struct object *obj;
158 req->handle = -1;
159 if ((obj = create_mapping( req->size_high, req->size_low,
160 req->protect, req->file_handle, req->name, len )))
162 int access = FILE_MAP_ALL_ACCESS;
163 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
164 req->handle = alloc_handle( current->process, obj, access, req->inherit );
165 release_object( obj );
169 /* open a handle to a mapping */
170 DECL_HANDLER(open_mapping)
172 size_t len = get_req_strlen( req->name );
173 req->handle = open_object( req->name, len, &mapping_ops, req->access, req->inherit );
176 /* get a mapping information */
177 DECL_HANDLER(get_mapping_info)
179 struct mapping *mapping;
181 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
182 0, &mapping_ops )))
184 req->size_high = mapping->size_high;
185 req->size_low = mapping->size_low;
186 req->protect = mapping->protect;
187 if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
188 release_object( mapping );