- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[wine] / server / mapping.c
1 /*
2  * Server-side file mapping management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "winerror.h"
13 #include "winnt.h"
14 #include "server/process.h"
15 #include "server/thread.h"
16
17 struct mapping
18 {
19     struct object  obj;             /* object header */
20     int            size_high;       /* mapping size */
21     int            size_low;        /* mapping size */
22     int            protect;         /* protection flags */
23     struct file   *file;            /* file mapped */
24 };
25
26 static void mapping_dump( struct object *obj, int verbose );
27 static void mapping_destroy( struct object *obj );
28
29 static const struct object_ops mapping_ops =
30 {
31     mapping_dump,
32     no_add_queue,
33     NULL,  /* should never get called */
34     NULL,  /* should never get called */
35     NULL,  /* should never get called */
36     no_read_fd,
37     no_write_fd,
38     no_flush,
39     no_get_file_info,
40     mapping_destroy
41 };
42
43 struct object *create_mapping( int size_high, int size_low, int protect,
44                                int handle, const char *name )
45 {
46     struct mapping *mapping;
47
48     if (!(mapping = (struct mapping *)create_named_object( name, &mapping_ops,
49                                                            sizeof(*mapping) )))
50         return NULL;
51     if (GET_ERROR() == ERROR_ALREADY_EXISTS)
52         return &mapping->obj;  /* Nothing else to do */
53
54     mapping->size_high = size_high;
55     mapping->size_low  = size_low;
56     mapping->protect   = protect;
57     if (handle != -1)
58     {
59         int access = 0;
60         if (protect & VPROT_READ) access |= GENERIC_READ;
61         if (protect & VPROT_WRITE) access |= GENERIC_WRITE;
62         if (!(mapping->file = get_file_obj( current->process, handle, access )))
63         {
64             release_object( mapping );
65             return NULL;
66         }
67     }
68     else mapping->file = NULL;
69     return &mapping->obj;
70 }
71
72 int open_mapping( unsigned int access, int inherit, const char *name )
73 {
74     return open_object( name, &mapping_ops, access, inherit );
75 }
76
77 int get_mapping_info( int handle, struct get_mapping_info_reply *reply )
78 {
79     struct mapping *mapping;
80     int fd;
81
82     if (!(mapping = (struct mapping *)get_handle_obj( current->process, handle,
83                                                       0, &mapping_ops )))
84         return -1;
85     reply->size_high = mapping->size_high;
86     reply->size_low  = mapping->size_low;
87     reply->protect   = mapping->protect;
88     if (mapping->file) fd = file_get_mmap_fd( mapping->file );
89     else fd = -1;
90     release_object( mapping );
91     return fd;
92 }
93
94 static void mapping_dump( struct object *obj, int verbose )
95 {
96     struct mapping *mapping = (struct mapping *)obj;
97     assert( obj->ops == &mapping_ops );
98     fprintf( stderr, "Mapping size=%08x%08x prot=%08x file=%p name='%s'\n",
99              mapping->size_high, mapping->size_low, mapping->protect,
100              mapping->file, get_object_name( &mapping->obj ) );
101 }
102
103 static void mapping_destroy( struct object *obj )
104 {
105     struct mapping *mapping = (struct mapping *)obj;
106     assert( obj->ops == &mapping_ops );
107     if (mapping->file) release_object( mapping->file );
108     free( mapping );
109 }