2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
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
=
36 NULL
, /* should never get called */
37 NULL
, /* should never get called */
38 NULL
, /* should never get called */
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 */
55 static int page_shift
, page_mask
;
57 static void init_page_size(void)
60 # ifdef HAVE_GETPAGESIZE
61 page_size
= getpagesize();
64 page_size
= sysconf(_SC_PAGESIZE
);
66 # error Cannot get the page size on this platform
69 page_mask
= page_size
- 1;
70 /* Make sure we have a power of 2 */
71 assert( !(page_size
& page_mask
) );
73 while ((1 << page_shift
) != page_size
) page_shift
++;
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
;
90 if (!page_mask
) init_page_size();
92 if (!(mapping
= (struct mapping
*)create_named_object( name
, &mapping_ops
,
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
);
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
;
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
;
132 release_object( mapping
);
136 static int get_mapping_info( int handle
, struct get_mapping_info_reply
*reply
)
138 struct mapping
*mapping
;
141 if (!(mapping
= (struct mapping
*)get_handle_obj( current
->process
, handle
,
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
);
149 release_object( mapping
);
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
);
170 /* create a file mapping */
171 DECL_HANDLER(create_mapping
)
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
) );