2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
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),
38 NULL, /* should never get called */
39 NULL, /* should never get called */
40 NULL, /* should never get called */
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 */
57 static int page_shift, page_mask;
59 static void init_page_size(void)
62 # ifdef HAVE_GETPAGESIZE
63 page_size = getpagesize();
66 page_size = sysconf(_SC_PAGESIZE);
68 # error Cannot get the page size on this platform
71 page_mask = page_size - 1;
72 /* Make sure we have a power of 2 */
73 assert( !(page_size & page_mask) );
75 while ((1 << page_shift) != page_size) page_shift++;
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 WCHAR *name, size_t len )
89 struct mapping *mapping;
92 if (!page_mask) init_page_size();
94 if (!(mapping = create_named_object( &mapping_ops, name, len )))
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;
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;
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;
132 release_object( mapping );
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 ",
141 mapping->size_high, mapping->size_low, mapping->protect, mapping->file );
142 dump_object_name( &mapping->obj );
143 fputc( '\n', stderr );
146 static void mapping_destroy( struct object *obj )
148 struct mapping *mapping = (struct mapping *)obj;
149 assert( obj->ops == &mapping_ops );
150 if (mapping->file) release_object( mapping->file );
153 int get_page_size(void)
155 if (!page_mask) init_page_size();
156 return page_mask + 1;
159 /* create a file mapping */
160 DECL_HANDLER(create_mapping)
162 size_t len = get_req_strlenW( req->name );
166 if ((obj = create_mapping( req->size_high, req->size_low,
167 req->protect, req->file_handle, req->name, len )))
169 int access = FILE_MAP_ALL_ACCESS;
170 if (!(req->protect & VPROT_WRITE)) access &= ~FILE_MAP_WRITE;
171 req->handle = alloc_handle( current->process, obj, access, req->inherit );
172 release_object( obj );
176 /* open a handle to a mapping */
177 DECL_HANDLER(open_mapping)
179 size_t len = get_req_strlenW( req->name );
180 req->handle = open_object( req->name, len, &mapping_ops, req->access, req->inherit );
183 /* get a mapping information */
184 DECL_HANDLER(get_mapping_info)
186 struct mapping *mapping;
188 if ((mapping = (struct mapping *)get_handle_obj( current->process, req->handle,
191 req->size_high = mapping->size_high;
192 req->size_low = mapping->size_low;
193 req->protect = mapping->protect;
194 if (mapping->file) set_reply_fd( current, file_get_mmap_fd( mapping->file ) );
195 release_object( mapping );