git-svn-id: svn://svn.lighttpd.net/xcache/trunk@148 c26eb9a1-5813-0410-bd6c-c2e55f420ca71.1
parent
a29d074126
commit
42efedb793
@ -1,13 +0,0 @@
|
||||
typedef struct _xc_shm_t xc_shm_t;
|
||||
typedef size_t xc_shmsize_t;
|
||||
|
||||
int xc_shm_can_readonly(xc_shm_t *shm);
|
||||
int xc_shm_is_readwrite(xc_shm_t *shm, const void *p);
|
||||
int xc_shm_is_readonly(xc_shm_t *shm, const void *p);
|
||||
void *xc_shm_to_readwrite(xc_shm_t *shm, void *p);
|
||||
void *xc_shm_to_readonly(xc_shm_t *shm, void *p);
|
||||
|
||||
void *xc_shm_ptr(xc_shm_t *shm);
|
||||
|
||||
xc_shm_t *xc_shm_init(const char *path, xc_shmsize_t size, zend_bool readonly_protection);
|
||||
void xc_shm_destroy(xc_shm_t *shm);
|
@ -0,0 +1,212 @@
|
||||
#define XC_SHM_IMPL
|
||||
#define XC_MEM_IMPL
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "xc_shm.h"
|
||||
#include "php.h"
|
||||
#include "align.h"
|
||||
|
||||
struct _xc_mem_t {
|
||||
const xc_mem_handlers_t *handlers;
|
||||
xc_shm_t *shm;
|
||||
xc_memsize_t size;
|
||||
xc_memsize_t avail; /* total free */
|
||||
};
|
||||
|
||||
#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
|
||||
|
||||
static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
|
||||
{
|
||||
return malloc(size);
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
|
||||
{
|
||||
free((void *) p);
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
|
||||
{
|
||||
return calloc(memb, size);
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
|
||||
{
|
||||
return realloc((void *) p, size);
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
|
||||
{
|
||||
char *p = malloc(len);
|
||||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
return memcpy(p, str, len);
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
|
||||
{
|
||||
return xc_malloc_strndup(mem, str, strlen(str) + 1);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
|
||||
{
|
||||
return mem->avail;
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
|
||||
{
|
||||
return mem->size;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
|
||||
{
|
||||
return (void *) -1;
|
||||
}
|
||||
/* }}} */
|
||||
XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
|
||||
{
|
||||
xc_block_t *b;
|
||||
|
||||
#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
|
||||
/* requires at least the header and 1 tail block */
|
||||
if (size < MINSIZE) {
|
||||
fprintf(stderr, "xc_mem_malloc_init requires %d bytes at least\n", MINSIZE);
|
||||
return NULL;
|
||||
}
|
||||
mem->shm = shm;
|
||||
mem->size = size;
|
||||
mem->avail = size - MINSIZE;
|
||||
#undef MINSIZE
|
||||
|
||||
return mem;
|
||||
}
|
||||
/* }}} */
|
||||
static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
|
||||
{
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
// {{{ xc_shm_t
|
||||
struct _xc_shm_t {
|
||||
xc_shm_handlers_t *handlers;
|
||||
xc_shmsize_t size;
|
||||
xc_shmsize_t memoffset;
|
||||
};
|
||||
|
||||
#undef NDEBUG
|
||||
#ifdef ALLOC_DEBUG
|
||||
# define inline
|
||||
#else
|
||||
# define NDEBUG
|
||||
#endif
|
||||
#include <assert.h>
|
||||
/* }}} */
|
||||
|
||||
static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
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) /* {{{ */
|
||||
{
|
||||
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;
|
||||
|
||||
return shm;
|
||||
err:
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
|
||||
{
|
||||
xc_mem_t *mem;
|
||||
if (shm->memoffset + size > shm->size) {
|
||||
zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
|
||||
return NULL;
|
||||
}
|
||||
shm->memoffset += size;
|
||||
CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
|
||||
mem->handlers = shm->handlers->memhandlers;
|
||||
mem->handlers->init(shm, mem, size);
|
||||
return mem;
|
||||
err:
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
|
||||
{
|
||||
mem->handlers->destroy(mem);
|
||||
free(mem);
|
||||
}
|
||||
/* }}} */
|
||||
|
||||
#define xc_malloc_destroy xc_mem_malloc_destroy
|
||||
#define xc_malloc_init xc_mem_malloc_init
|
||||
static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
|
||||
#undef xc_malloc_init
|
||||
#undef xc_malloc_destroy
|
||||
static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
|
||||
void xc_shm_malloc_register() /* {{{ */
|
||||
{
|
||||
if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
|
||||
zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
|
||||
}
|
||||
|
||||
CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
|
||||
if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
|
||||
zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
|
||||
}
|
||||
err:
|
||||
return;
|
||||
}
|
||||
/* }}} */
|
@ -0,0 +1,76 @@
|
||||
#ifdef TEST
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#else
|
||||
#include <php.h>
|
||||
#endif
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "xc_shm.h"
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const xc_shm_handlers_t *handlers;
|
||||
} xc_shm_scheme_t;
|
||||
static xc_shm_scheme_t xc_shm_schemes[10];
|
||||
|
||||
void xc_shm_init_modules() /* {{{ */
|
||||
{
|
||||
memset(xc_shm_schemes, 0, sizeof(xc_shm_schemes));
|
||||
|
||||
extern void xc_shm_mem_init();
|
||||
xc_shm_mem_init();
|
||||
|
||||
extern void xc_shm_malloc_register();
|
||||
xc_shm_malloc_register();
|
||||
|
||||
extern void xc_shm_mmap_register();
|
||||
xc_shm_mmap_register();
|
||||
}
|
||||
/* }}} */
|
||||
int xc_shm_scheme_register(const char *name, const xc_shm_handlers_t *handlers) /* {{{ */
|
||||
{
|
||||
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;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* }}} */
|
||||
const xc_shm_handlers_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 NULL;
|
||||
}
|
||||
/* }}} */
|
||||
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);
|
||||
|
||||
if (handlers) {
|
||||
xc_shm_t *shm = handlers->init(size, readonly_protection, arg1, arg2);
|
||||
if (shm) {
|
||||
shm->handlers = handlers;
|
||||
}
|
||||
return shm;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
/* }}} */
|
||||
void xc_shm_destroy(xc_shm_t *shm) /* {{{ */
|
||||
{
|
||||
shm->handlers->destroy(shm);
|
||||
}
|
||||
/* }}} */
|
@ -0,0 +1,60 @@
|
||||
typedef struct _xc_shm_t xc_shm_t;
|
||||
typedef size_t xc_shmsize_t;
|
||||
|
||||
#include "mem.h"
|
||||
|
||||
/* 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_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)
|
||||
|
||||
#define XC_SHM_MEMINIT(func) xc_mem_t *func(xc_shm_t *shm, xc_memsize_t size)
|
||||
#define XC_SHM_MEMDESTROY(func) void func(xc_mem_t *mem)
|
||||
|
||||
#define XC_SHM_HANDLERS(name) { \
|
||||
NULL \
|
||||
, xc_##name##_can_readonly \
|
||||
, 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 \
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
const xc_mem_handlers_t *memhandlers;
|
||||
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));
|
||||
XC_SHM_INIT((*init));
|
||||
XC_SHM_DESTROY((*destroy));
|
||||
|
||||
XC_SHM_MEMINIT((*meminit));
|
||||
XC_SHM_MEMDESTROY((*memdestroy));
|
||||
} xc_shm_handlers_t;
|
||||
|
||||
|
||||
#ifndef XC_SHM_IMPL
|
||||
struct _xc_shm_t {
|
||||
const xc_shm_handlers_t *handlers;
|
||||
};
|
||||
#endif
|
||||
|
||||
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);
|
||||
|
||||
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);
|
Loading…
Reference in new issue