Mercurial > hg > nginx
annotate src/os/unix/ngx_shmem.c @ 2483:29494780d978
free addrs only it has been allocated before: non single address
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 29 Jan 2009 14:32:58 +0000 |
parents | 715d24327080 |
children | d620f497c50f |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
358
diff
changeset
|
5 |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
10 |
469 | 11 #if (NGX_HAVE_MAP_ANON) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
605 | 13 ngx_int_t |
14 ngx_shm_alloc(ngx_shm_t *shm) | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
15 { |
639 | 16 shm->addr = (u_char *) mmap(NULL, shm->size, |
17 PROT_READ|PROT_WRITE, | |
18 MAP_ANON|MAP_SHARED, -1, 0); | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
19 |
605 | 20 if (shm->addr == MAP_FAILED) { |
21 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
22 "mmap(MAP_ANON|MAP_SHARED, %uz) failed", shm->size); | |
23 return NGX_ERROR; | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
24 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
25 |
605 | 26 return NGX_OK; |
27 } | |
28 | |
29 | |
30 void | |
31 ngx_shm_free(ngx_shm_t *shm) | |
32 { | |
639 | 33 if (munmap((void *) shm->addr, shm->size) == -1) { |
605 | 34 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
35 "munmap(%p, %uz) failed", shm->addr, shm->size); | |
36 } | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
37 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 |
469 | 39 #elif (NGX_HAVE_MAP_DEVZERO) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
40 |
605 | 41 ngx_int_t |
42 ngx_shm_alloc(ngx_shm_t *shm) | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
43 { |
605 | 44 ngx_fd_t fd; |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
45 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
46 fd = open("/dev/zero", O_RDWR); |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
47 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
48 if (fd == -1) { |
605 | 49 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
467 | 50 "open(\"/dev/zero\") failed"); |
605 | 51 return NGX_ERROR; |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
52 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
53 |
639 | 54 shm->addr = (u_char *) mmap(NULL, shm->size, PROT_READ|PROT_WRITE, |
55 MAP_SHARED, fd, 0); | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
56 |
605 | 57 if (shm->addr == MAP_FAILED) { |
58 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
59 "mmap(/dev/zero, MAP_SHARED, %uz) failed", shm->size); | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
61 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
62 if (close(fd) == -1) { |
605 | 63 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
467 | 64 "close(\"/dev/zero\") failed"); |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
65 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
66 |
605 | 67 return (shm->addr == MAP_FAILED) ? NGX_ERROR : NGX_OK; |
68 } | |
69 | |
70 | |
71 void | |
72 ngx_shm_free(ngx_shm_t *shm) | |
73 { | |
639 | 74 if (munmap((void *) shm->addr, shm->size) == -1) { |
605 | 75 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
76 "munmap(%p, %uz) failed", shm->addr, shm->size); | |
77 } | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
78 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
79 |
469 | 80 #elif (NGX_HAVE_SYSVSHM) |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
81 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
82 #include <sys/ipc.h> |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
83 #include <sys/shm.h> |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
84 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
85 |
605 | 86 ngx_int_t |
87 ngx_shm_alloc(ngx_shm_t *shm) | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
88 { |
605 | 89 int id; |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 |
605 | 91 id = shmget(IPC_PRIVATE, shm->size, (SHM_R|SHM_W|IPC_CREAT)); |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
92 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
93 if (id == -1) { |
605 | 94 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
95 "shmget(%uz) failed", shm->size); | |
96 return NGX_ERROR; | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
97 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
98 |
605 | 99 ngx_log_debug1(NGX_LOG_DEBUG_CORE, shm->log, 0, "shmget id: %d", id); |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
100 |
605 | 101 shm->addr = shmat(id, NULL, 0); |
102 | |
103 if (shm->addr == (void *) -1) { | |
104 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, "shmat() failed"); | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
105 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
106 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
107 if (shmctl(id, IPC_RMID, NULL) == -1) { |
605 | 108 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, |
109 "shmctl(IPC_RMID) failed"); | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
110 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
111 |
605 | 112 return (shm->addr == (void *) -1) ? NGX_ERROR : NGX_OK; |
113 } | |
114 | |
115 | |
116 void | |
117 ngx_shm_free(ngx_shm_t *shm) | |
118 { | |
119 if (shmdt(shm->addr) == -1) { | |
120 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, | |
121 "shmdt(%p) failed", shm->addr); | |
122 } | |
358
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
123 } |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
124 |
0a03c921c81d
nginx-0.0.7-2004-06-17-21:18:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
125 #endif |