OSDN Git Service

io/socket/socket.hpp こっちも直さなあかんのか…
[roast/roast.git] / roast / source / roast_shared_memory__win32.c
1 /*      Roast+ License
2
3 //      ### C compilable ###
4 */
5
6 #include "roast_common.h"
7 #include "roast_memory.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <memory.h>
12 #include <windows.h>
13
14 __ROAST_EXTERN_C_START
15
16
17 /*\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡*/
18
19 /*\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡\81 \81¡*/
20
21
22 /*      Open/Create Shared Memory.      */
23 roast_shmem_info* roast_open_shmem( roast_shmem_info *k )
24 {
25         DWORD dwProtectMode = 0;
26         DWORD dwFileMapMode = 0;
27
28         /*      roast_shmem_info Initialized Check.     */
29         if ( k->_zero != 0 ){
30                 k->error_code = ROAST_SHMEM_INFO_NEED_ZEROCLEAR;
31                 return NULL;
32         }
33
34         /*      Error Code set to Unknown Error.        */
35         k->error_code = ROAST_SHMEM_UNKNOWN_ERROR;
36
37         /*      AccessMode      */
38         if ( k->access_mode == ROAST_SHMEM_ACCESS_RO ){
39                 dwProtectMode = PAGE_READONLY;
40                 dwFileMapMode = FILE_MAP_READ;
41         }
42         else if ( k->access_mode == ROAST_SHMEM_ACCESS_RW ){
43                 dwProtectMode = PAGE_READWRITE;
44                 dwFileMapMode = FILE_MAP_WRITE;
45         }
46
47         /*--------------------------------------------*/
48
49         /*      CreateFileMapping()     */
50         if ( k->handle == NULL )
51         {
52                 k->handle =
53                         CreateFileMapping(INVALID_HANDLE_VALUE, NULL, dwProtectMode, 0, k->size, k->str_key);
54
55                 if ( k->handle == NULL ){
56                         k->error_code = ROAST_SHMEM_CREATE_FAILED;
57                         return NULL;
58                 }
59
60                 /*      This name(Key) was already used.        */
61                 if ( k->handle == (void*)ERROR_ALREADY_EXISTS )
62                 {
63                         k->error_code = ROAST_SHMEM_NAME_ALREADY_USED;
64                         return NULL;
65
66                         /*      ERROR_ALREADY_EXISTS\82Í\81A\83t\83@\83C\83\8b\83}\83b\83s\83\93\83O\88È\8aO\82Å\82»\82Ì\96¼\91O\82ª\8eg\82í\82ê\82Ä\82é\82Æ\82«\81A\82¾\82Á\82Ä\82³\81B
67                         k->handle = OpenFileMapping(dwFileMapMode, FALSE, k->str_key);
68
69                         if ( k->handle == NULL ){
70                                 k->error_code = ROAST_SHMEM_OPEN_FAILED;
71                                 return NULL;
72                         }
73                         */
74                 }
75         }
76
77         /*      MapViewOfFile() */
78         if ( k->buf == NULL )
79         {
80                 /*k->buf = MapViewOfFile(k->handle, dwFileMapMode, 0, 0, k->size);*/
81                 k->buf = MapViewOfFile(k->handle, dwFileMapMode, 0, 0, 0);
82
83                 if ( k->buf == NULL ){
84                         k->error_code = ROAST_SHMEM_MAP_FAILED;
85                         return NULL;
86                 }
87         }
88
89         k->error_code = ROAST_SHMEM_NO_ERROR;
90         return k;
91 }
92
93 roast_shmem_info* roast_create_shmem( roast_shmem_info *k )
94 {
95         return roast_open_shmem(k);
96 }
97
98
99 /*=================================================*/
100
101
102 /*      Close/Delete Shared Memory.     */
103 roast_shmem_info* roast_close_shmem( roast_shmem_info *k )
104 {
105         if ( k->buf ){
106                 if ( UnmapViewOfFile(k->buf) == FALSE )
107                 {
108                         DWORD dwLastError = GetLastError();
109
110                         if ( dwLastError == ERROR_INVALID_ADDRESS )
111                                 k->error_code = ROAST_SHMEM_UNMAP_FAILED_INVALID_ADDRESS;
112                         else
113                                 k->error_code = ROAST_SHMEM_UNMAP_FAILED;
114                         
115                         return NULL;
116                 }
117                 k->buf = NULL;
118         }
119
120         if ( k->handle ){
121                 if ( CloseHandle(k->handle) == FALSE ){
122                         k->error_code = ROAST_SHMEM_CLOSE_FAILED;
123                         return NULL;
124                 }
125                 k->handle = NULL;
126         }
127
128         return k;
129 }
130 roast_shmem_info* roast_delete_shmem( roast_shmem_info *k )
131 {
132         return roast_close_shmem(k);
133 }
134
135
136
137
138 __ROAST_EXTERN_C_END
139