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 char *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 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
);
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
,
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
);