summaryrefslogtreecommitdiff
path: root/xcache
diff options
context:
space:
mode:
authorXuefer <xuefer@gmail.com>2013-11-12 09:46:04 +0000
committerXuefer <xuefer@gmail.com>2013-11-12 09:46:04 +0000
commitd19d64546d0d0fb4ab67ef8e4a8c8de744ee35af (patch)
treea6cae53ed4f7dc379e01490df89342e120bab3e7 /xcache
parent548985cdb75cca0937d9cd5a97330fcdb006351e (diff)
downloadxcache-d19d64546d0d0fb4ab67ef8e4a8c8de744ee35af.tar.gz
xcache-d19d64546d0d0fb4ab67ef8e4a8c8de744ee35af.zip
clean up shm handlers
git-svn-id: svn://svn.lighttpd.net/xcache/trunk@1468 c26eb9a1-5813-0410-bd6c-c2e55f420ca7
Diffstat (limited to 'xcache')
-rw-r--r--xcache/xc_malloc.c84
-rw-r--r--xcache/xc_shm.c52
-rw-r--r--xcache/xc_shm.h54
-rw-r--r--xcache/xc_shm_mmap.c49
4 files changed, 116 insertions, 123 deletions
diff --git a/xcache/xc_malloc.c b/xcache/xc_malloc.c
index 4ac19b6..6638ff6 100644
--- a/xcache/xc_malloc.c
+++ b/xcache/xc_malloc.c
@@ -1,5 +1,7 @@
-#define XC_SHM_IMPL _xc_malloc_shm_t
+typedef struct xc_malloc_shm_t xc_shm_t;
+#define XC_SHM_T_DEFINED
#define _xc_allocator_t _xc_allocator_malloc_t
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -18,11 +20,11 @@ struct _xc_allocator_malloc_t {
xc_memsize_t avail; /* total free */
};
-/* {{{ _xc_malloc_shm_t */
-struct _xc_malloc_shm_t {
- xc_shm_handlers_t *handlers;
- xc_shmsize_t size;
- xc_shmsize_t memoffset;
+/* {{{ xc_malloc_shm_t */
+struct xc_malloc_shm_t {
+ xc_shm_vtable_t *vtable;
+ xc_shmsize_t size;
+ xc_shmsize_t memoffset;
#ifndef TEST
HashTable blocks;
#endif
@@ -138,25 +140,45 @@ static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */
}
/* }}} */
-static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
+static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
{
- return 0;
+#ifndef TEST
+ zend_hash_destroy(&shm->blocks);
+#endif
+ free(shm);
+ return;
+}
+/* }}} */
+static XC_SHM_INIT(xc_malloc_init) /* {{{ */
+{
+ xc_shm_t *shm;
+ CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
+ shm->size = size;
+
+#ifndef TEST
+ zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
+#endif
+ return shm;
+err:
+ return NULL;
}
/* }}} */
+
static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
{
#ifndef TEST
HashPosition pos;
size_t *psize;
char **ptr;
+ HashTable *blocks = &((xc_shm_t *) shm)->blocks;
- zend_hash_internal_pointer_reset_ex(&shm->blocks, &pos);
- while (zend_hash_get_current_data_ex(&shm->blocks, (void **) &psize, &pos) == SUCCESS) {
- zend_hash_get_current_key_ex(&shm->blocks, (void *) &ptr, NULL, NULL, 0, &pos);
+ zend_hash_internal_pointer_reset_ex(blocks, &pos);
+ while (zend_hash_get_current_data_ex(blocks, (void **) &psize, &pos) == SUCCESS) {
+ zend_hash_get_current_key_ex(blocks, (void *) &ptr, NULL, NULL, 0, &pos);
if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
return 1;
}
- zend_hash_move_forward_ex(&shm->blocks, &pos);
+ zend_hash_move_forward_ex(blocks, &pos);
}
#endif
@@ -168,40 +190,6 @@ static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
return 0;
}
/* }}} */
-static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
-{
- return p;
-}
-/* }}} */
-static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
-{
- return p;
-}
-/* }}} */
-
-static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
-{
-#ifndef TEST
- zend_hash_destroy(&shm->blocks);
-#endif
- free(shm);
- return;
-}
-/* }}} */
-static XC_SHM_INIT(xc_malloc_init) /* {{{ */
-{
- xc_shm_t *shm;
- CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
- shm->size = size;
-
-#ifndef TEST
- zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
-#endif
- return shm;
-err:
- return NULL;
-}
-/* }}} */
static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
{
@@ -227,7 +215,7 @@ static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
static xc_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc);
#ifndef TEST
-static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
+static xc_shm_vtable_t xc_shm_malloc_vtable = XC_SHM_VTABLE(malloc);
#endif
void xc_allocator_malloc_register() /* {{{ */
{
@@ -242,7 +230,7 @@ void xc_allocator_malloc_register() /* {{{ */
#ifndef TEST
void xc_shm_malloc_register() /* {{{ */
{
- if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
+ if (xc_shm_scheme_register("malloc", &xc_shm_malloc_vtable) == 0) {
zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
}
}
diff --git a/xcache/xc_shm.c b/xcache/xc_shm.c
index 190e80e..2bd05ff 100644
--- a/xcache/xc_shm.c
+++ b/xcache/xc_shm.c
@@ -14,8 +14,8 @@
#include <string.h>
struct _xc_shm_scheme_t {
- const char *name;
- const xc_shm_handlers_t *handlers;
+ const char *name;
+ const xc_shm_vtable_t *vtable;
};
static xc_shm_scheme_t xc_shm_schemes[10];
@@ -35,25 +35,25 @@ void xc_shm_init_modules() /* {{{ */
xc_shm_mmap_register();
}
/* }}} */
-int xc_shm_scheme_register(const char *name, const xc_shm_handlers_t *handlers) /* {{{ */
+int xc_shm_scheme_register(const char *name, const xc_shm_vtable_t *vtable) /* {{{ */
{
int i;
for (i = 0; i < 10; i ++) {
if (!xc_shm_schemes[i].name) {
xc_shm_schemes[i].name = name;
- xc_shm_schemes[i].handlers = handlers;
+ xc_shm_schemes[i].vtable = vtable;
return 1;
}
}
return 0;
}
/* }}} */
-const xc_shm_handlers_t *xc_shm_scheme_find(const char *name) /* {{{ */
+const xc_shm_vtable_t *xc_shm_scheme_find(const char *name) /* {{{ */
{
int i;
for (i = 0; i < 10 && xc_shm_schemes[i].name; i ++) {
if (strcmp(xc_shm_schemes[i].name, name) == 0) {
- return xc_shm_schemes[i].handlers;
+ return xc_shm_schemes[i].vtable;
}
}
return NULL;
@@ -78,12 +78,12 @@ const char *xc_shm_scheme_name(xc_shm_scheme_t *scheme) /* {{{ */
/* }}} */
xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2) /* {{{ */
{
- const xc_shm_handlers_t *handlers = xc_shm_scheme_find(type);
+ const xc_shm_vtable_t *vtable = xc_shm_scheme_find(type);
- if (handlers) {
- xc_shm_t *shm = handlers->init(size, readonly_protection, arg1, arg2);
+ if (vtable) {
+ xc_shm_t *shm = vtable->init(size, readonly_protection, arg1, arg2);
if (shm) {
- shm->handlers = handlers;
+ shm->vtable = vtable;
}
return shm;
}
@@ -93,6 +93,36 @@ xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protecti
/* }}} */
void xc_shm_destroy(xc_shm_t *shm) /* {{{ */
{
- shm->handlers->destroy(shm);
+ shm->vtable->destroy(shm);
+}
+/* }}} */
+
+int xc_shm_can_readonly(const xc_shm_t *shm) /* {{{ */
+{
+ return ((xc_shm_base_t *)shm)->readonlydiff != 0;
+}
+/* }}} */
+void *xc_shm_to_readwrite(const xc_shm_t *shm_, void *p) /* {{{ */
+{
+ const xc_shm_base_t *shm = (const xc_shm_base_t *) shm_;
+
+ if (shm->readonlydiff) {
+ assert(shm->vtable->is_readonly(shm_, p));
+ p = ((char *) p) - shm->readonlydiff;
+ }
+ assert(shm->vtable->is_readwrite(shm_, p));
+ return p;
+}
+/* }}} */
+void *xc_shm_to_readonly(const xc_shm_t *shm_, void *p) /* {{{ */
+{
+ const xc_shm_base_t *shm = (const xc_shm_base_t *) shm_;
+
+ assert(shm->vtable->is_readwrite(shm_, p));
+ if (shm->readonlydiff) {
+ p = ((char *) p) + shm->readonlydiff;
+ assert(shm->vtable->is_readonly(shm_, p));
+ }
+ return p;
}
/* }}} */
diff --git a/xcache/xc_shm.h b/xcache/xc_shm.h
index 07cc59f..f253669 100644
--- a/xcache/xc_shm.h
+++ b/xcache/xc_shm.h
@@ -2,25 +2,26 @@
#define XC_SHM_H
#include <stdlib.h>
+#include <stddef.h>
+#include <assert.h>
-typedef struct _xc_shm_handlers_t xc_shm_handlers_t;
-
-#ifndef XC_SHM_IMPL
-struct _xc_shm_t {
- const xc_shm_handlers_t *handlers;
-};
-#define XC_SHM_IMPL _xc_shm_t
+#ifndef XC_SHM_T_DEFINED
+typedef struct _xc_shm_base_t xc_shm_t;
+#define XC_SHM_T_DEFINED
#endif
-typedef struct XC_SHM_IMPL xc_shm_t;
+typedef struct _xc_shm_vtable_t xc_shm_vtable_t;
+
+typedef struct _xc_shm_base_t {
+ const xc_shm_vtable_t *vtable;
+ ptrdiff_t readonlydiff;
+} xc_shm_base_t;
+
typedef size_t xc_shmsize_t;
/* shm */
-#define XC_SHM_CAN_READONLY(func) int func(xc_shm_t *shm)
-#define XC_SHM_IS_READWRITE(func) int func(xc_shm_t *shm, const void *p)
-#define XC_SHM_IS_READONLY(func) int func(xc_shm_t *shm, const void *p)
-#define XC_SHM_TO_READWRITE(func) void *func(xc_shm_t *shm, void *p)
-#define XC_SHM_TO_READONLY(func) void *func(xc_shm_t *shm, void *p)
+#define XC_SHM_IS_READWRITE(func) int func(const xc_shm_t *shm, const void *p)
+#define XC_SHM_IS_READONLY(func) int func(const xc_shm_t *shm, const void *p)
#define XC_SHM_INIT(func) xc_shm_t *func(xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2)
#define XC_SHM_DESTROY(func) void func(xc_shm_t *shm)
@@ -28,28 +29,22 @@ typedef size_t xc_shmsize_t;
#define XC_SHM_MEMINIT(func) void *func(xc_shm_t *shm, xc_shmsize_t size)
#define XC_SHM_MEMDESTROY(func) void func(void *mem)
-#define XC_SHM_HANDLERS(name) { \
- xc_##name##_can_readonly \
+#define XC_SHM_VTABLE(name) { \
+ xc_##name##_init \
+ , xc_##name##_destroy \
, xc_##name##_is_readwrite \
, xc_##name##_is_readonly \
- , xc_##name##_to_readwrite \
- , xc_##name##_to_readonly \
\
- , xc_##name##_init \
- , xc_##name##_destroy \
\
, xc_##name##_meminit \
, xc_##name##_memdestroy \
}
-struct _xc_shm_handlers_t {
- XC_SHM_CAN_READONLY((*can_readonly));
- XC_SHM_IS_READWRITE((*is_readwrite));
- XC_SHM_IS_READONLY((*is_readonly));
- XC_SHM_TO_READWRITE((*to_readwrite));
- XC_SHM_TO_READONLY((*to_readonly));
+struct _xc_shm_vtable_t {
XC_SHM_INIT((*init));
XC_SHM_DESTROY((*destroy));
+ XC_SHM_IS_READWRITE((*is_readwrite));
+ XC_SHM_IS_READONLY((*is_readonly));
XC_SHM_MEMINIT((*meminit));
XC_SHM_MEMDESTROY((*memdestroy));
@@ -58,12 +53,17 @@ struct _xc_shm_handlers_t {
typedef struct _xc_shm_scheme_t xc_shm_scheme_t;
void xc_shm_init_modules();
-int xc_shm_scheme_register(const char *name, const xc_shm_handlers_t *handlers);
-const xc_shm_handlers_t *xc_shm_scheme_find(const char *name);
+int xc_shm_scheme_register(const char *name, const xc_shm_vtable_t *vtable);
+const xc_shm_vtable_t *xc_shm_scheme_find(const char *name);
xc_shm_scheme_t *xc_shm_scheme_first();
xc_shm_scheme_t *xc_shm_scheme_next(xc_shm_scheme_t *scheme);
const char *xc_shm_scheme_name(xc_shm_scheme_t *scheme);
xc_shm_t *xc_shm_init(const char *type, xc_shmsize_t size, int readonly_protection, const void *arg1, const void *arg2);
void xc_shm_destroy(xc_shm_t *shm);
+
+int xc_shm_can_readonly(const xc_shm_t *shm);
+void *xc_shm_to_readwrite(const xc_shm_t *shm_, void *p);
+void *xc_shm_to_readonly(const xc_shm_t *shm_, void *p);
+
#endif
diff --git a/xcache/xc_shm_mmap.c b/xcache/xc_shm_mmap.c
index 799791a..b80d723 100644
--- a/xcache/xc_shm_mmap.c
+++ b/xcache/xc_shm_mmap.c
@@ -29,7 +29,9 @@
#endif
#include "php.h"
-#define XC_SHM_IMPL _xc_mmap_shm_t
+
+typedef struct xc_mmap_shm_t xc_shm_t;
+#define XC_SHM_T_DEFINED
#include "xc_shm.h"
#include "xc_utils.h"
@@ -38,12 +40,10 @@
#endif
/* {{{ xc_shm_t */
-struct _xc_mmap_shm_t {
- xc_shm_handlers_t *handlers;
- zend_bool disabled;
+struct xc_mmap_shm_t {
+ xc_shm_base_t base;
void *ptr;
void *ptr_ro;
- long diff;
xc_shmsize_t size;
xc_shmsize_t memoffset;
char *name;
@@ -60,11 +60,6 @@ struct _xc_mmap_shm_t {
#define PTR_ADD(ptr, v) (((char *) (ptr)) + (v))
#define PTR_SUB(ptr, v) (((char *) (ptr)) - (v))
-static XC_SHM_CAN_READONLY(xc_mmap_can_readonly) /* {{{ */
-{
- return shm->ptr_ro != NULL;
-}
-/* }}} */
static XC_SHM_IS_READWRITE(xc_mmap_is_readwrite) /* {{{ */
{
return p >= shm->ptr && (char *)p < (char *)shm->ptr + shm->size;
@@ -72,27 +67,7 @@ static XC_SHM_IS_READWRITE(xc_mmap_is_readwrite) /* {{{ */
/* }}} */
static XC_SHM_IS_READONLY(xc_mmap_is_readonly) /* {{{ */
{
- return xc_mmap_can_readonly(shm) && p >= shm->ptr_ro && (char *)p < (char *)shm->ptr_ro + shm->size;
-}
-/* }}} */
-static XC_SHM_TO_READWRITE(xc_mmap_to_readwrite) /* {{{ */
-{
- if (shm->diff) {
- assert(xc_mmap_is_readonly(shm, p));
- p = PTR_SUB(p, shm->diff);
- }
- assert(xc_mmap_is_readwrite(shm, p));
- return p;
-}
-/* }}} */
-static XC_SHM_TO_READONLY(xc_mmap_to_readonly) /* {{{ */
-{
- assert(xc_mmap_is_readwrite(shm, p));
- if (shm->diff) {
- p = PTR_ADD(p, shm->diff);
- assert(xc_mmap_is_readonly(shm, p));
- }
- return p;
+ return xc_shm_can_readonly(shm) && p >= shm->ptr_ro && (char *)p < (char *)shm->ptr_ro + shm->size;
}
/* }}} */
@@ -132,7 +107,7 @@ static XC_SHM_DESTROY(xc_mmap_destroy) /* {{{ */
}
/*
shm->size = NULL;
- shm->diff = 0;
+ shm->base.readonlydiff = 0;
*/
free(shm);
@@ -242,9 +217,9 @@ static XC_SHM_INIT(xc_mmap_init) /* {{{ */
} while (0);
if (ro_ok) {
- shm->diff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr);
+ shm->base.readonlydiff = PTR_SUB(shm->ptr_ro, (char *) shm->ptr);
/* no overlap */
- assert((xc_shmsize_t) abs(shm->diff) >= size);
+ assert((xc_shmsize_t) abs(shm->base.readonlydiff) >= size);
}
else {
if (shm->ptr_ro) {
@@ -256,7 +231,7 @@ static XC_SHM_INIT(xc_mmap_init) /* {{{ */
}
#endif
shm->ptr_ro = NULL;
- shm->diff = 0;
+ shm->base.readonlydiff = 0;
}
}
@@ -308,10 +283,10 @@ static XC_SHM_MEMDESTROY(xc_mmap_memdestroy) /* {{{ */
}
/* }}} */
-static xc_shm_handlers_t xc_shm_mmap_handlers = XC_SHM_HANDLERS(mmap);
+static xc_shm_vtable_t xc_shm_mmap_vtable = XC_SHM_VTABLE(mmap);
void xc_shm_mmap_register() /* {{{ */
{
- if (xc_shm_scheme_register("mmap", &xc_shm_mmap_handlers) == 0) {
+ if (xc_shm_scheme_register("mmap", &xc_shm_mmap_vtable) == 0) {
zend_error(E_ERROR, "XCache: failed to register mmap shm_scheme");
}
return;