Mercurial > hg > nginx
annotate src/os/win32/ngx_shmem.c @ 2903:beb0707ed8ab
allow shared memory segments more than 4G
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sat, 30 May 2009 17:06:38 +0000 |
parents | d52cf82d0d77 |
children | 474a9daf8c80 |
rev | line source |
---|---|
605 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 | |
10 | |
11 ngx_int_t | |
12 ngx_shm_alloc(ngx_shm_t *shm) | |
13 { | |
2903
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
14 u_char *name; |
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
15 uint64_t size; |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
16 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
17 name = ngx_alloc(shm->name.len + 2 + sizeof(NGX_INT32_LEN), shm->log); |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
18 if (name == NULL) { |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
19 return NGX_ERROR; |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
20 } |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
21 |
2737
d52cf82d0d77
style fix: remove trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
2720
diff
changeset
|
22 ngx_sprintf(name, "%V_%s%Z", &shm->name, ngx_unique); |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
23 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
24 ngx_set_errno(0); |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
25 |
2903
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
26 size = shm->size; |
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
27 |
605 | 28 shm->handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, |
2903
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
29 (u_long) (size >> 32), |
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
30 (u_long) (size & 0xffffffff), |
beb0707ed8ab
allow shared memory segments more than 4G
Igor Sysoev <igor@sysoev.ru>
parents:
2737
diff
changeset
|
31 (char *) name); |
605 | 32 |
33 if (shm->handle == NULL) { | |
34 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
2716
d5896f6608e8
move zone name from ngx_shm_zone_t to ngx_shm_t to use Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
605
diff
changeset
|
35 "CreateFileMapping(%uz, %s) failed", |
d5896f6608e8
move zone name from ngx_shm_zone_t to ngx_shm_t to use Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
605
diff
changeset
|
36 shm->size, shm->name.data); |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
37 goto failed; |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
38 } |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
39 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
40 if (ngx_errno == ERROR_ALREADY_EXISTS) { |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
41 shm->exists = 1; |
605 | 42 } |
43 | |
44 shm->addr = MapViewOfFile(shm->handle, FILE_MAP_WRITE, 0, 0, 0); | |
45 | |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
46 if (shm->addr != NULL) { |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
47 return NGX_OK; |
605 | 48 } |
49 | |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
50 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
51 "MapViewOfFile(%uz) failed", shm->size); |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
52 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
53 if (CloseHandle(shm->handle) == 0) { |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
54 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
55 "CloseHandle() failed"); |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
56 } |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
57 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
58 failed: |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
59 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
60 ngx_free(name); |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
61 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
62 return NGX_ERROR; |
605 | 63 } |
64 | |
65 | |
66 void | |
67 ngx_shm_free(ngx_shm_t *shm) | |
68 { | |
69 if (UnmapViewOfFile(shm->addr) == 0) { | |
70 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
71 "UnmapViewOfFile(%p) failed", shm->addr); | |
72 } | |
73 | |
74 if (CloseHandle(shm->handle) == 0) { | |
75 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
76 "CloseHandle() failed"); | |
77 } | |
78 } |