Skip to content

Commit 4e41836

Browse files
madmajestronikic
authored andcommitted
Cleanup mmap and shm implementation
Better separation of the sma layer and the mmap / shm implementation.
1 parent 7f84f2b commit 4e41836

File tree

5 files changed

+31
-49
lines changed

5 files changed

+31
-49
lines changed

apc_mmap.c

+14-19
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,29 @@
5151
# define MAP_ANON MAP_ANONYMOUS
5252
#endif
5353

54-
apc_segment_t apc_mmap(char *file_mask, size_t size)
54+
void *apc_mmap(char *file_mask, size_t size)
5555
{
56-
apc_segment_t segment;
57-
56+
void *shmaddr;
5857
int fd = -1;
5958
int flags = MAP_SHARED | MAP_NOSYNC;
6059

6160
/* If no filename was provided, do an anonymous mmap */
62-
if(!file_mask || (file_mask && !strlen(file_mask))) {
61+
if (!file_mask || (file_mask && !strlen(file_mask))) {
6362
#if !defined(MAP_ANON)
6463
zend_error_noreturn(E_CORE_ERROR, "Anonymous mmap does not appear to be available on this system (MAP_ANON/MAP_ANONYMOUS). Please see the apc.mmap_file_mask INI option.");
6564
#else
6665
fd = -1;
6766
flags = MAP_SHARED | MAP_ANON;
6867
#endif
69-
} else if(!strcmp(file_mask,"/dev/zero")) {
68+
} else if (!strcmp(file_mask,"/dev/zero")) {
7069
fd = open("/dev/zero", O_RDWR, S_IRUSR | S_IWUSR);
71-
if(fd == -1) {
70+
if (fd == -1) {
7271
zend_error_noreturn(E_CORE_ERROR, "apc_mmap: open on /dev/zero failed");
7372
}
7473
} else {
75-
/*
76-
* Otherwise we do a normal filesystem mmap
77-
*/
74+
/* Otherwise we do a normal filesystem mmap */
7875
fd = mkstemp(file_mask);
79-
if(fd == -1) {
76+
if (fd == -1) {
8077
zend_error_noreturn(E_CORE_ERROR, "apc_mmap: mkstemp on %s failed", file_mask);
8178
}
8279
if (ftruncate(fd, size) < 0) {
@@ -87,27 +84,25 @@ apc_segment_t apc_mmap(char *file_mask, size_t size)
8784
unlink(file_mask);
8885
}
8986

90-
segment.shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
91-
segment.size = size;
87+
shmaddr = (void *)mmap(NULL, size, PROT_READ | PROT_WRITE, flags, fd, 0);
9288

93-
if ((long)segment.shmaddr == -1) {
89+
if ((long)shmaddr == -1) {
9490
zend_error_noreturn(E_CORE_ERROR, "apc_mmap: Failed to mmap %zu bytes. Is your apc.shm_size too large?", size);
9591
}
9692

9793
#ifdef MADV_HUGEPAGE
98-
/* enable transparent huge pages to reduce TLB misses (Linux
99-
only) */
100-
madvise(segment.shmaddr, size, MADV_HUGEPAGE);
94+
/* enable transparent huge pages to reduce TLB misses (Linux only) */
95+
madvise(shmaddr, size, MADV_HUGEPAGE);
10196
#endif
10297

10398
if (fd != -1) close(fd);
10499

105-
return segment;
100+
return shmaddr;
106101
}
107102

108-
void apc_unmap(apc_segment_t *segment)
103+
void apc_unmap(void *shmaddr, size_t size)
109104
{
110-
if (munmap(segment->shmaddr, segment->size) < 0) {
105+
if (munmap(shmaddr, size) < 0) {
111106
apc_warning("apc_unmap: munmap failed");
112107
}
113108
}

apc_mmap.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
#include <limits.h>
3232

3333
#include "apc.h"
34-
#include "apc_sma.h"
3534

3635
/* Wrapper functions for shared memory mapped files */
3736

3837
#ifdef APC_MMAP
39-
apc_segment_t apc_mmap(char *file_mask, size_t size);
40-
void apc_unmap(apc_segment_t* segment);
38+
void *apc_mmap(char *file_mask, size_t size);
39+
void apc_unmap(void *shmaddr, size_t size);
4140
#endif
4241

4342
#endif

apc_shm.c

+9-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# define SHM_A 0222 /* write permission */
4646
#endif
4747

48-
int apc_shm_create(int proj, size_t size)
48+
static int apc_shm_create(size_t size)
4949
{
5050
int shmid; /* shared memory id */
5151
int oflag; /* permissions on shm */
@@ -59,33 +59,32 @@ int apc_shm_create(int proj, size_t size)
5959
return shmid;
6060
}
6161

62-
void apc_shm_destroy(int shmid)
62+
static void apc_shm_destroy(int shmid)
6363
{
6464
/* we expect this call to fail often, so we do not check */
6565
shmctl(shmid, IPC_RMID, 0);
6666
}
6767

68-
apc_segment_t apc_shm_attach(int shmid, size_t size)
68+
void *apc_shm_attach(size_t size)
6969
{
70-
apc_segment_t segment; /* shm segment */
70+
void *shmaddr;
71+
int shmid = apc_shm_create(size);
7172

72-
if ((zend_long)(segment.shmaddr = shmat(shmid, 0, 0)) == -1) {
73+
if ((zend_long)(shmaddr = shmat(shmid, 0, 0)) == -1) {
7374
zend_error_noreturn(E_CORE_ERROR, "apc_shm_attach: shmat failed:");
7475
}
7576

76-
segment.size = size;
77-
7877
/*
7978
* We set the shmid for removal immediately after attaching to it. The
8079
* segment won't disappear until all processes have detached from it.
8180
*/
8281
apc_shm_destroy(shmid);
83-
return segment;
82+
return shmaddr;
8483
}
8584

86-
void apc_shm_detach(apc_segment_t* segment)
85+
void apc_shm_detach(void *shmaddr)
8786
{
88-
if (shmdt(segment->shmaddr) < 0) {
87+
if (shmdt(shmaddr) < 0) {
8988
apc_warning("apc_shm_detach: shmdt failed:");
9089
}
9190
}

apc_shm.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@
3333
#include <time.h>
3434
#endif
3535

36-
#include "apc_sma.h"
37-
3836
/* Wrapper functions for unix shared memory */
3937

40-
extern int apc_shm_create(int proj, size_t size);
41-
extern void apc_shm_destroy(int shmid);
42-
extern apc_segment_t apc_shm_attach(int shmid, size_t size);
43-
extern void apc_shm_detach(apc_segment_t* segment);
38+
void *apc_shm_attach(size_t size);
39+
void apc_shm_detach(void *shmaddr);
4440

4541
#endif
4642

apc_sma.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,11 @@ PHP_APCU_API void apc_sma_init(apc_sma_t* sma, void** data, apc_sma_expunge_f ex
325325
void* shmaddr;
326326

327327
#ifdef APC_MMAP
328-
sma->segs[i] = apc_mmap(mask, sma->size);
328+
sma->segs[i].shmaddr = apc_mmap(mask, sma->size);
329329
if(sma->num != 1)
330330
memcpy(&mask[strlen(mask)-6], "XXXXXX", 6);
331331
#else
332-
{
333-
int j = apc_shm_create(i, sma->size);
334-
#ifdef PHP_WIN32
335-
/* TODO remove the line below after 7.1 EOL. */
336-
SetLastError(0);
337-
#endif
338-
sma->segs[i] = apc_shm_attach(j, sma->size);
339-
}
332+
sma->segs[i].shmaddr = apc_shm_attach(sma->size);
340333
#endif
341334

342335
sma->segs[i].size = sma->size;
@@ -389,9 +382,9 @@ PHP_APCU_API void apc_sma_detach(apc_sma_t* sma) {
389382

390383
for (i = 0; i < sma->num; i++) {
391384
#ifdef APC_MMAP
392-
apc_unmap(&sma->segs[i]);
385+
apc_unmap(sma->segs[i].shmaddr, sma->segs[i].size);
393386
#else
394-
apc_shm_detach(&sma->segs[i]);
387+
apc_shm_detach(sma->segs[i].shmaddr);
395388
#endif
396389
}
397390

0 commit comments

Comments
 (0)