Back to index

php5  5.3.10
fpm_shm.c
Go to the documentation of this file.
00001 
00002        /* $Id: fpm_shm.c,v 1.3 2008/05/24 17:38:47 anight Exp $ */
00003        /* (c) 2007,2008 Andrei Nigmatulin, Jerome Loyet */
00004 
00005 #include <sys/mman.h>
00006 #include <errno.h>
00007 #include <string.h>
00008 
00009 #include "fpm_shm.h"
00010 #include "zlog.h"
00011 
00012 
00013 /* MAP_ANON is deprecated, but not in macosx */
00014 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
00015 #define MAP_ANONYMOUS MAP_ANON
00016 #endif
00017 
00018 static size_t fpm_shm_size = 0;
00019 
00020 void *fpm_shm_alloc(size_t size) /* {{{ */
00021 {
00022        void *mem;
00023 
00024        mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
00025 
00026 #ifdef MAP_FAILED
00027        if (mem == MAP_FAILED) {
00028               zlog(ZLOG_SYSERROR, "unable to allocate %zu bytes in shared memory: %s", size, strerror(errno));
00029               return NULL;
00030        }
00031 #endif
00032 
00033        if (!mem) {
00034               zlog(ZLOG_SYSERROR, "unable to allocate %zu bytes in shared memory", size);
00035               return NULL;
00036        }
00037 
00038        memset(mem, size, 0);
00039        fpm_shm_size += size;
00040        return mem;
00041 }
00042 /* }}} */
00043 
00044 int fpm_shm_free(void *mem, size_t size) /* {{{ */
00045 {
00046        if (!mem) {
00047               zlog(ZLOG_ERROR, "mem is NULL");
00048               return 0;
00049        }
00050 
00051        if (munmap(mem, size) == -1) {
00052               zlog(ZLOG_SYSERROR, "Unable to free shm");
00053               return 0;
00054        }
00055 
00056        if (fpm_shm_size - size > 0) {
00057               fpm_shm_size -= size;
00058        } else {
00059               fpm_shm_size = 0;
00060        }
00061 
00062        return 1;
00063 }
00064 /* }}} */
00065 
00066 size_t fpm_shm_get_size_allocated() /* {{{*/
00067 {
00068        return fpm_shm_size;
00069 }
00070 /* }}} */