1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "apr_general.h"
18
#include "apr_errno.h"
19
#include "apr_file_io.h"
21
#include "apr_arch_file_io.h"
24
typedef struct memblock_t {
38
static apr_status_t shm_cleanup(void* shm)
40
apr_status_t rv = APR_SUCCESS;
43
if (UnmapViewOfFile(m->memblk)) {
44
rv = apr_get_os_error();
46
if (CloseHandle(m->hMap)) {
47
return (rv != APR_SUCCESS) ? rv : apr_get_os_error();
49
/* ### Do we want to make a point of unlinking m->file here?
50
* Need to add the fname to the apr_shm_t, in that case.
55
APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
60
static apr_size_t memblock = 0;
67
DWORD err, sizelo, sizehi;
69
reqsize += sizeof(memblock_t);
75
memblock = si.dwAllocationGranularity;
78
/* Compute the granualar multiple of the pagesize */
79
size = memblock * (1 + (reqsize - 1) / memblock);
82
sizehi = (DWORD)(size >> 32);
88
/* Do Anonymous, which must be passed as a duplicated handle */
90
hFile = INVALID_HANDLE_VALUE;
95
/* Do file backed, which is not an inherited handle
96
* While we could open APR_EXCL, it doesn't seem that Unix
97
* ever did. Ignore that error here, but fail later when
98
* we discover we aren't the creator of the file map object.
100
rv = apr_file_open(&f, file,
101
APR_READ | APR_WRITE | APR_BINARY | APR_CREATE,
102
APR_UREAD | APR_UWRITE, pool);
103
if ((rv != APR_SUCCESS)
104
|| ((rv = apr_os_file_get(&hFile, f)) != APR_SUCCESS)) {
107
rv = apr_file_trunc(f, size);
109
/* res_name_from_filename turns file into a pseudo-name
110
* without slashes or backslashes, and prepends the \global
111
* prefix on Win2K and later
113
mapkey = res_name_from_filename(file, 1, pool);
116
#if APR_HAS_UNICODE_FS
119
hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE,
120
sizehi, sizelo, mapkey);
126
hMap = CreateFileMappingA(hFile, NULL, PAGE_READWRITE,
127
sizehi, sizelo, mapkey);
130
err = apr_get_os_error();
136
if (hMap && err == ERROR_ALREADY_EXISTS) {
144
base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE,
148
return apr_get_os_error();
151
*m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
157
(*m)->usrmem = (char*)base + sizeof(memblock_t);
158
(*m)->length = reqsize - sizeof(memblock_t);;
160
(*m)->memblk->length = (*m)->length;
161
(*m)->memblk->size = (*m)->size;
163
apr_pool_cleanup_register((*m)->pool, *m,
164
shm_cleanup, apr_pool_cleanup_null);
168
APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m)
170
apr_status_t rv = shm_cleanup(m);
171
apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
175
APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
181
APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
193
/* res_name_from_filename turns file into a pseudo-name
194
* without slashes or backslashes, and prepends the \global
195
* prefix on Win2K and later
197
mapkey = res_name_from_filename(file, 1, pool);
200
#if APR_HAS_UNICODE_FS
204
hMap = OpenFileMappingW(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
206
/* The WCE 3.0 lacks OpenFileMapping. So we emulate one with
207
* opening the existing shmem and reading its size from the header
209
hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
210
PAGE_READWRITE, 0, sizeof(apr_shm_t), mapkey);
217
hMap = OpenFileMappingA(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mapkey);
222
return apr_get_os_error();
225
base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
228
return apr_get_os_error();
231
*m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
234
/* Real (*m)->mem->size could be recovered with VirtualQuery */
235
(*m)->size = (*m)->memblk->size;
237
/* Reopen with real size */
238
UnmapViewOfFile(base);
241
hMap = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
242
PAGE_READWRITE, 0, (*m)->size, mapkey);
244
return apr_get_os_error();
246
base = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
249
return apr_get_os_error();
253
(*m)->length = (*m)->memblk->length;
254
(*m)->usrmem = (char*)base + sizeof(memblock_t);
255
apr_pool_cleanup_register((*m)->pool, *m,
256
shm_cleanup, apr_pool_cleanup_null);
260
APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m)
262
apr_status_t rv = shm_cleanup(m);
263
apr_pool_cleanup_kill(m->pool, m, shm_cleanup);
267
APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m)
272
APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m)
277
APR_POOL_IMPLEMENT_ACCESSOR(shm)
279
APR_DECLARE(apr_status_t) apr_os_shm_get(apr_os_shm_t *osshm,
286
APR_DECLARE(apr_status_t) apr_os_shm_put(apr_shm_t **m,
291
base = MapViewOfFile(*osshm, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
293
return apr_get_os_error();
296
*m = (apr_shm_t *) apr_palloc(pool, sizeof(apr_shm_t));
300
(*m)->usrmem = (char*)base + sizeof(memblock_t);
301
/* Real (*m)->mem->size could be recovered with VirtualQuery */
302
(*m)->size = (*m)->memblk->size;
303
(*m)->length = (*m)->memblk->length;
305
apr_pool_cleanup_register((*m)->pool, *m,
306
shm_cleanup, apr_pool_cleanup_null);