Browse Source

refactor: s/mem/allocator/

git-svn-id: svn://svn.lighttpd.net/xcache/trunk@1135 c26eb9a1-5813-0410-bd6c-c2e55f420ca7
3.0
Xuefer 9 years ago
parent
commit
1883e8ded9
  1. 35
      Makefile.frag.deps
  2. 3
      config.m4
  3. 3
      config.w32
  4. 16
      devel/test.mak
  5. 70
      mod_cacher/xc_cacher.c
  6. 18
      processor/head.m4
  7. 112
      xcache/xc_allocator.c
  8. 73
      xcache/xc_allocator.h
  9. 136
      xcache/xc_malloc.c
  10. 415
      xcache/xc_mem.c
  11. 74
      xcache/xc_mem.h
  12. 4
      xcache/xc_shm.c
  13. 13
      xcache/xc_shm.h
  14. 8
      xcache/xc_shm_mmap.c

35
Makefile.frag.deps

@ -1,24 +1,25 @@
$(XCACHE_INCLUDES_I) $(builddir)/includes.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h
$(XCACHE_INCLUDES_I) $(builddir)/includes.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h
$(builddir)/mod_assembler/xc_assembler.lo:
$(builddir)/mod_cacher/xc_cacher.lo: $(srcdir)/mod_cacher/xc_cacher.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(XCACHE_PROC_H) $(srcdir)/xcache.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/util/xc_trace.h $(srcdir)/util/xc_vector.h $(srcdir)/util/xc_align.h
$(builddir)/mod_coverager/xc_coverager.lo: $(srcdir)/mod_coverager/xc_coverager.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/mod_cacher/xc_cacher.lo: $(srcdir)/mod_cacher/xc_cacher.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(XCACHE_PROC_H) $(srcdir)/xcache.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/util/xc_trace.h $(srcdir)/util/xc_vector.h $(srcdir)/util/xc_align.h
$(builddir)/mod_coverager/xc_coverager.lo: $(srcdir)/mod_coverager/xc_coverager.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/mod_decoder/xc_decoder.lo:
$(builddir)/mod_disassembler/xc_disassembler.lo: $(srcdir)/mod_disassembler/xc_disassembler.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/xcache/xc_compatibility.h $(XCACHE_PROC_H) $(srcdir)/xcache.h $(srcdir)/mod_cacher/xc_cache.h
$(builddir)/mod_disassembler/xc_disassembler.lo: $(srcdir)/mod_disassembler/xc_disassembler.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/xcache/xc_compatibility.h $(XCACHE_PROC_H) $(srcdir)/xcache.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_shm.h
$(builddir)/mod_encoder/xc_encoder.lo:
$(builddir)/mod_optimizer/xc_optimizer.lo: $(srcdir)/mod_optimizer/xc_optimizer.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/mod_optimizer/xc_optimizer.lo: $(srcdir)/mod_optimizer/xc_optimizer.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/util/xc_stack.lo: $(srcdir)/util/xc_trace.h $(srcdir)/util/xc_stack.h
$(builddir)/util/xc_trace.lo: $(srcdir)/util/xc_trace.h
$(builddir)/xcache.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_foreachcoresig.h
$(builddir)/xcache.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_ini.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_foreachcoresig.h
$(builddir)/xcache/xc_allocator_bestfit.lo: $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_shm.h $(srcdir)/util/xc_align.h $(srcdir)/util/xc_trace.h
$(builddir)/xcache/xc_allocator.lo: $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_shm.h
$(builddir)/xcache/xc_compatibility.lo: $(srcdir)/xcache/xc_compatibility.h
$(builddir)/xcache/xc_const_string.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_const_string_opcodes_php5.4.h
$(builddir)/xcache/xc_extension.lo: $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h
$(builddir)/xcache/xc_const_string.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_const_string_opcodes_php5.4.h
$(builddir)/xcache/xc_extension.lo: $(srcdir)/xcache/xc_extension.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_trace.h
$(builddir)/xcache/xc_ini.lo: $(srcdir)/xcache/xc_ini.h
$(builddir)/xcache/xc_lock.lo: $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h
$(builddir)/xcache/xc_malloc.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_align.h
$(builddir)/xcache/xc_mem.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/util/xc_align.h $(srcdir)/util/xc_trace.h
$(builddir)/xcache/xc_opcode_spec.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_opcode_spec_def.h
$(builddir)/xcache/xc_processor.lo: $(XCACHE_PROC_C) $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_align.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(XCACHE_PROC_H) $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/xcache/xc_sandbox.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/xcache/xc_shm.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h
$(builddir)/xcache/xc_shm_mmap.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h
$(builddir)/xcache/xc_utils.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_mem.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/util/xc_trace.h
$(builddir)/xcache/xc_lock.lo: $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h
$(builddir)/xcache/xc_malloc.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_align.h
$(builddir)/xcache/xc_opcode_spec.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_opcode_spec_def.h
$(builddir)/xcache/xc_processor.lo: $(XCACHE_PROC_C) $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/mod_cacher/xc_cache.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_allocator.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_const_string.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/util/xc_align.h $(XCACHE_PROC_H) $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/xcache/xc_sandbox.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_sandbox.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h
$(builddir)/xcache/xc_shm.lo: $(srcdir)/xcache/xc_shm.h
$(builddir)/xcache/xc_shm_mmap.lo: $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_compatibility.h
$(builddir)/xcache/xc_utils.lo: $(srcdir)/xcache.h $(srcdir)/xcache/xc_shm.h $(srcdir)/xcache/xc_lock.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache_globals.h $(srcdir)/util/xc_stack.h $(srcdir)/xcache/xc_utils.h $(srcdir)/xcache/xc_compatibility.h $(srcdir)/xcache/xc_opcode_spec.h $(srcdir)/util/xc_trace.h

3
config.m4

@ -31,12 +31,13 @@ xc_trace.c \
xcache_sources="$xcache_sources util/$i"
done
for i in \
xc_allocator.c \
xc_allocator_bestfit.c \
xc_compatibility.c \
xc_const_string.c \
xc_extension.c \
xc_ini.c \
xc_lock.c \
xc_mem.c \
xc_opcode_spec.c \
xc_processor.c \
xc_sandbox.c \

3
config.w32

@ -18,12 +18,13 @@ xc_trace.c \
", "xcache");
ADD_SOURCES(configure_module_dirname + "/xcache", " \
xc_allocator.c \
xc_allocator_bestfit.c \
xc_compatibility.c \
xc_const_string.c \
xc_extension.c \
xc_ini.c \
xc_lock.c \
xc_mem.c \
xc_opcode_spec.c \
xc_processor.c \
xc_sandbox.c \

16
devel/test.mak

@ -1,18 +1,18 @@
#! /usr/bin/make -f
EXES=mem_test
OBJS=mem.o
EXES=allocator_test
OBJS=allocator.o
CC=gcc
CFLAGS=-g -O0 -I. -D TEST -Wall
CFLAGS=-g -O0 -I. -D TEST -D HAVE_XCACHE_TEST -Wall
TEST=valgrind
all: mem
all: allocator
mem_test: xcache/xc_mem.c xcache/xc_mem.h util/xc_trace.c util/xc_trace.h
$(CC) $(CFLAGS) -o mem_test xcache/xc_mem.c util/xc_trace.c
allocator_test: xcache/xc_allocator.h xcache/xc_allocator.c xcache/xc_malloc.c xcache/xc_allocator_bestfit.c util/xc_trace.c util/xc_trace.h
$(CC) $(CFLAGS) -o allocator_test xcache/xc_allocator.c xcache/xc_malloc.c xcache/xc_allocator_bestfit.c util/xc_trace.c
mem: mem_test
$(TEST) ./mem_test
allocator: allocator_test
$(TEST) ./allocator_test
clean:
rm -f $(OBJS) $(EXES)

70
mod_cacher/xc_cacher.c

@ -99,7 +99,7 @@ typedef struct { /* {{{ xc_cache_t: only cache info, not in shm */
struct _xc_lock_t *lck;
struct _xc_shm_t *shm; /* which shm contains us */
struct _xc_mem_t *mem; /* which mem contains us */
xc_allocator_t *allocator;
xc_hash_t *hentry; /* hash settings to entry */
xc_hash_t *hphp; /* hash settings to php */
@ -124,6 +124,9 @@ enum { xc_deletes_gc_interval = 120 };
static zend_ulong xc_php_gc_interval = 0;
static zend_ulong xc_var_gc_interval = 0;
static char *xc_php_allocator = NULL;
static char *xc_var_allocator = NULL;
/* total size */
static zend_ulong xc_php_size = 0;
static zend_ulong xc_var_size = 0;
@ -162,7 +165,7 @@ static xc_entry_data_php_t *xc_php_store_unlocked(xc_cache_t *cache, xc_entry_da
php->hits = 0;
php->refcount = 0;
stored_php = xc_processor_store_xc_entry_data_php_t(cache->shm, cache->mem, php TSRMLS_CC);
stored_php = xc_processor_store_xc_entry_data_php_t(cache->shm, cache->allocator, php TSRMLS_CC);
if (stored_php) {
xc_php_add_unlocked(cache->cached, stored_php);
return stored_php;
@ -187,7 +190,7 @@ static xc_entry_data_php_t *xc_php_find_unlocked(xc_cached_t *cached, xc_entry_d
/* }}} */
static void xc_php_free_unlocked(xc_cache_t *cache, xc_entry_data_php_t *php) /* {{{ */
{
cache->mem->handlers->free(cache->mem, (xc_entry_data_php_t *)php);
cache->allocator->vtable->free(cache->allocator, (xc_entry_data_php_t *)php);
}
/* }}} */
static void xc_php_addref_unlocked(xc_entry_data_php_t *php) /* {{{ */
@ -309,8 +312,8 @@ static xc_entry_t *xc_entry_store_unlocked(xc_entry_type_t type, xc_cache_t *cac
entry->ctime = XG(request_time);
entry->atime = XG(request_time);
stored_entry = type == XC_TYPE_PHP
? (xc_entry_t *) xc_processor_store_xc_entry_php_t(cache->shm, cache->mem, (xc_entry_php_t *) entry TSRMLS_CC)
: (xc_entry_t *) xc_processor_store_xc_entry_var_t(cache->shm, cache->mem, (xc_entry_var_t *) entry TSRMLS_CC);
? (xc_entry_t *) xc_processor_store_xc_entry_php_t(cache->shm, cache->allocator, (xc_entry_php_t *) entry TSRMLS_CC)
: (xc_entry_t *) xc_processor_store_xc_entry_var_t(cache->shm, cache->allocator, (xc_entry_var_t *) entry TSRMLS_CC);
if (stored_entry) {
xc_entry_add_unlocked(cache->cached, entryslotid, stored_entry);
++cache->cached->updates;
@ -337,7 +340,7 @@ static void xc_entry_free_real_unlocked(xc_entry_type_t type, xc_cache_t *cache,
if (type == XC_TYPE_PHP) {
xc_php_release_unlocked(cache, ((xc_entry_php_t *) entry)->php);
}
cache->mem->handlers->free(cache->mem, (xc_entry_t *)entry);
cache->allocator->vtable->free(cache->allocator, (xc_entry_t *)entry);
}
/* }}} */
static void xc_entry_free_unlocked(xc_entry_type_t type, xc_cache_t *cache, xc_entry_t *entry TSRMLS_DC) /* {{{ */
@ -603,12 +606,12 @@ static void xc_fillinfo_unlocked(int cachetype, xc_cache_t *cache, zval *return_
{
zval *blocks, *hits;
size_t i;
const xc_block_t *b;
const xc_allocator_block_t *b;
#ifndef NDEBUG
xc_memsize_t avail = 0;
#endif
const xc_mem_t *mem = cache->mem;
const xc_mem_handlers_t *handlers = mem->handlers;
const xc_allocator_t *allocator = cache->allocator;
const xc_allocator_vtable_t *vtable = allocator->vtable;
zend_ulong interval;
const xc_cached_t *cached = cache->cached;
@ -656,26 +659,26 @@ static void xc_fillinfo_unlocked(int cachetype, xc_cache_t *cache, zval *return_
MAKE_STD_ZVAL(blocks);
array_init(blocks);
add_assoc_long_ex(return_value, ZEND_STRS("size"), handlers->size(mem));
add_assoc_long_ex(return_value, ZEND_STRS("avail"), handlers->avail(mem));
add_assoc_long_ex(return_value, ZEND_STRS("size"), vtable->size(allocator));
add_assoc_long_ex(return_value, ZEND_STRS("avail"), vtable->avail(allocator));
add_assoc_bool_ex(return_value, ZEND_STRS("can_readonly"), xc_readonly_protection);
for (b = handlers->freeblock_first(mem); b; b = handlers->freeblock_next(b)) {
for (b = vtable->freeblock_first(allocator); b; b = vtable->freeblock_next(b)) {
zval *bi;
MAKE_STD_ZVAL(bi);
array_init(bi);
add_assoc_long_ex(bi, ZEND_STRS("size"), handlers->block_size(b));
add_assoc_long_ex(bi, ZEND_STRS("offset"), handlers->block_offset(mem, b));
add_assoc_long_ex(bi, ZEND_STRS("size"), vtable->block_size(b));
add_assoc_long_ex(bi, ZEND_STRS("offset"), vtable->block_offset(allocator, b));
add_next_index_zval(blocks, bi);
#ifndef NDEBUG
avail += handlers->block_size(b);
avail += vtable->block_size(b);
#endif
}
add_assoc_zval_ex(return_value, ZEND_STRS("free_blocks"), blocks);
#ifndef NDEBUG
assert(avail == handlers->avail(mem));
assert(avail == vtable->avail(allocator));
#endif
}
/* }}} */
@ -2310,17 +2313,17 @@ static xc_shm_t *xc_cache_destroy(xc_cache_t *caches, xc_hash_t *hcache) /* {{{
}
/* do NOT touch cached data */
shm = cache->shm;
cache->shm->handlers->memdestroy(cache->mem);
cache->shm->handlers->memdestroy(cache->allocator);
}
}
free(caches);
return shm;
}
/* }}} */
static xc_cache_t *xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *hentry, xc_hash_t *hphp, xc_shmsize_t shmsize) /* {{{ */
static xc_cache_t *xc_cache_init(xc_shm_t *shm, const char *allocator_name, xc_hash_t *hcache, xc_hash_t *hentry, xc_hash_t *hphp, xc_shmsize_t shmsize) /* {{{ */
{
xc_cache_t *caches = NULL;
xc_mem_t *mem;
xc_allocator_t *allocator;
time_t now = time(NULL);
size_t i;
xc_memsize_t memsize;
@ -2342,11 +2345,16 @@ static xc_cache_t *xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *he
for (i = 0; i < hcache->size; i ++) {
xc_cache_t *cache = &caches[i];
CHECK(mem = shm->handlers->meminit(shm, memsize), "Failed init memory allocator");
CHECK(cache->cached = mem->handlers->calloc(mem, 1, sizeof(xc_cached_t)), "cache OOM");
CHECK(cache->cached->entries = mem->handlers->calloc(mem, hentry->size, sizeof(xc_entry_t*)), "entries OOM");
CHECK(allocator = shm->handlers->meminit(shm, memsize), "Failed init shm");
if (!(allocator->vtable = xc_allocator_find(allocator_name))) {
zend_error(E_ERROR, "Allocator %s not found", allocator_name);
goto err;
}
CHECK(allocator->vtable->init(shm, allocator, memsize), "Failed init allocator");
CHECK(cache->cached = allocator->vtable->calloc(allocator, 1, sizeof(xc_cached_t)), "cache OOM");
CHECK(cache->cached->entries = allocator->vtable->calloc(allocator, hentry->size, sizeof(xc_entry_t*)), "entries OOM");
if (hphp) {
CHECK(cache->cached->phps = mem->handlers->calloc(mem, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM");
CHECK(cache->cached->phps = allocator->vtable->calloc(allocator, hphp->size, sizeof(xc_entry_data_php_t*)), "phps OOM");
}
CHECK(cache->lck = xc_lock_init(NULL), "can't create lock");
@ -2354,7 +2362,7 @@ static xc_cache_t *xc_cache_init(xc_shm_t *shm, xc_hash_t *hcache, xc_hash_t *he
cache->hentry = hentry;
cache->hphp = hphp;
cache->shm = shm;
cache->mem = mem;
cache->allocator = allocator;
cache->cacheid = i;
cache->cached->last_gc_deletes = now;
cache->cached->last_gc_expires = now;
@ -2412,11 +2420,11 @@ static int xc_init() /* {{{ */
}
if (xc_php_size) {
CHECK(xc_php_caches = xc_cache_init(shm, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache");
CHECK(xc_php_caches = xc_cache_init(shm, xc_php_allocator, &xc_php_hcache, &xc_php_hentry, &xc_php_hentry, xc_php_size), "failed init opcode cache");
}
if (xc_var_size) {
CHECK(xc_var_caches = xc_cache_init(shm, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache");
CHECK(xc_var_caches = xc_cache_init(shm, xc_var_allocator, &xc_var_hcache, &xc_var_hentry, NULL, xc_var_size), "failed init variable cache");
}
}
return SUCCESS;
@ -3183,6 +3191,7 @@ PHP_INI_BEGIN()
PHP_INI_ENTRY1 ("xcache.size", "0", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL)
PHP_INI_ENTRY1 ("xcache.count", "1", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL)
PHP_INI_ENTRY1 ("xcache.slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL)
PHP_INI_ENTRY1 ("xcache.allocator", "bestfit", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_php_allocator)
PHP_INI_ENTRY1 ("xcache.ttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_ttl)
PHP_INI_ENTRY1 ("xcache.gc_interval", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_php_gc_interval)
STD_PHP_INI_BOOLEAN("xcache.cacher", "1", PHP_INI_ALL, OnUpdateBool, cacher, zend_xcache_globals, xcache_globals)
@ -3193,6 +3202,7 @@ PHP_INI_BEGIN()
PHP_INI_ENTRY1 ("xcache.var_slots", "8K", PHP_INI_SYSTEM, xcache_OnUpdateDummy, NULL)
PHP_INI_ENTRY1 ("xcache.var_maxttl", "0", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_maxttl)
PHP_INI_ENTRY1 ("xcache.var_gc_interval", "120", PHP_INI_SYSTEM, xcache_OnUpdateULong, &xc_var_gc_interval)
PHP_INI_ENTRY1 ("xcache.var_allocator", "bestfit", PHP_INI_SYSTEM, xcache_OnUpdateString, &xc_var_allocator)
STD_PHP_INI_ENTRY ("xcache.var_ttl", "0", PHP_INI_ALL, OnUpdateLong, var_ttl, zend_xcache_globals, xcache_globals)
PHP_INI_END()
/* }}} */
@ -3360,6 +3370,14 @@ static PHP_MSHUTDOWN_FUNCTION(xcache_cacher) /* {{{ */
pefree(xc_shm_scheme, 1);
xc_shm_scheme = NULL;
}
if (xc_php_allocator) {
pefree(xc_php_allocator, 1);
xc_php_allocator = NULL;
}
if (xc_var_allocator) {
pefree(xc_var_allocator, 1);
xc_var_allocator = NULL;
}
return SUCCESS;
}

18
processor/head.m4

@ -13,9 +13,13 @@ divert(0)
#include "xcache.h"
/* export: #include "mod_cacher/xc_cache.h" :export */
#include "mod_cacher/xc_cache.h"
#include "util/xc_align.h"
/* export: #include "xcache/xc_shm.h" :export */
#include "xcache/xc_shm.h"
/* export: #include "xcache/xc_allocator.h" :export */
#include "xcache/xc_allocator.h"
#include "xcache/xc_const_string.h"
#include "xcache/xc_utils.h"
#include "util/xc_align.h"
#include "xc_processor.h"
#include "xcache_globals.h"
@ -94,7 +98,7 @@ struct _xc_processor_t {
const xc_entry_data_php_t *php_src;
const xc_entry_data_php_t *php_dst;
xc_shm_t *shm;
xc_mem_t *mem;
xc_allocator_t *allocator;
const zend_class_entry *cache_ce;
zend_uint cache_class_index;
@ -394,15 +398,15 @@ static int xc_check_names(const char *file, int line, const char *functionName,
/* }}} */
dnl ================ export API
define(`DEFINE_STORE_API', `
/* export: $1 *xc_processor_store_$1(xc_shm_t *shm, xc_mem_t *mem, $1 *src TSRMLS_DC); :export {{{ */
$1 *xc_processor_store_$1(xc_shm_t *shm, xc_mem_t *mem, $1 *src TSRMLS_DC) {
/* export: $1 *xc_processor_store_$1(xc_shm_t *shm, xc_allocator_t *allocator, $1 *src TSRMLS_DC); :export {{{ */
$1 *xc_processor_store_$1(xc_shm_t *shm, xc_allocator_t *allocator, $1 *src TSRMLS_DC) {
$1 *dst;
xc_processor_t processor;
memset(&processor, 0, sizeof(processor));
processor.reference = 1;
processor.shm = shm;
processor.mem = mem;
processor.allocator = allocator;
IFAUTOCHECK(`xc_stack_init(&processor.allocsizes);')
@ -439,8 +443,8 @@ $1 *xc_processor_store_$1(xc_shm_t *shm, xc_mem_t *mem, $1 *src TSRMLS_DC) {
zend_hash_init(&processor.zvalptrs, 0, NULL, NULL, 0);
}
/* mem :) */
processor.p = (char *) processor.mem->handlers->malloc(processor.mem, processor.size);
/* allocator :) */
processor.p = (char *) processor.allocator->vtable->malloc(processor.allocator, processor.size);
if (processor.p == NULL) {
dst = NULL;
goto err_alloc;

112
xcache/xc_allocator.c

@ -0,0 +1,112 @@
#include "xc_allocator.h"
#include <string.h>
#include <stdio.h>
typedef struct {
const char *name;
const xc_allocator_vtable_t *allocator_vtable;
} xc_allocator_info_t;
static xc_allocator_info_t xc_allocator_infos[10];
int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable) /* {{{ */
{
size_t i;
for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]); i ++) {
if (!xc_allocator_infos[i].name) {
xc_allocator_infos[i].name = name;
xc_allocator_infos[i].allocator_vtable = allocator_vtable;
return 1;
}
}
return 0;
}
/* }}} */
const xc_allocator_vtable_t *xc_allocator_find(const char *name) /* {{{ */
{
size_t i;
for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
if (strcmp(xc_allocator_infos[i].name, name) == 0) {
return xc_allocator_infos[i].allocator_vtable;
}
}
return NULL;
}
/* }}} */
void xc_allocator_init() /* {{{ */
{
extern void xc_allocator_bestfit_register();
#ifdef HAVE_XCACHE_TEST
extern void xc_allocator_malloc_register();
#endif
memset(xc_allocator_infos, 0, sizeof(xc_allocator_infos));
xc_allocator_bestfit_register();
#ifdef HAVE_XCACHE_TEST
xc_allocator_malloc_register();
#endif
}
/* }}} */
#ifdef TEST
/* {{{ testing */
#undef CHECK
#define CHECK(a, msg) do { \
if (!(a)) { \
fprintf(stderr, "%s\n", msg); return -1; \
} \
} while (0)
#include <time.h>
int testAllocator(const xc_allocator_vtable_t *allocator_vtable)
{
int count = 0;
void *p;
xc_allocator_t *allocator;
void *memory;
void **ptrs;
int size, i;
#if 0
fprintf(stderr, "%s", "Input test size: ");
scanf("%d", &size);
#else
size = 1024;
#endif
CHECK(memory = malloc(size), "OOM");
CHECK(ptrs = malloc(size * sizeof(void *)), "OOM");
allocator = (xc_allocator_t *) memory;
allocator->vtable = allocator_vtable;
CHECK(allocator = allocator->vtable->init(NULL, allocator, size), "Failed init memory allocator");
while ((p = allocator->vtable->malloc(allocator, 1))) {
ptrs[count ++] = p;
}
fprintf(stderr, "count=%d, random freeing\n", count);
srandom(time(NULL));
while (count) {
i = (random() % count);
fprintf(stderr, "freeing %d: ", i);
allocator->vtable->free(allocator, ptrs[i]);
ptrs[i] = ptrs[count - 1];
count --;
}
free(ptrs);
free(memory);
return 0;
}
/* }}} */
int main() /* {{{ */
{
int i;
xc_allocator_init();
for (i = 0; i < sizeof(xc_allocator_infos) / sizeof(xc_allocator_infos[0]) && xc_allocator_infos[i].name; i ++) {
fprintf(stderr, "testing %s...\n", xc_allocator_infos[i].name);
testAllocator(xc_allocator_infos[i].allocator_vtable);
}
return 0;
}
/* }}} */
#endif

73
xcache/xc_allocator.h

@ -0,0 +1,73 @@
#ifndef XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D
#define XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D
#if _MSC_VER > 1000
#pragma once
#endif /* _MSC_VER > 1000 */
#include "xc_shm.h"
typedef struct _xc_allocator_vtable_t xc_allocator_vtable_t;
#ifndef _xc_allocator_t
struct _xc_allocator_t {
const xc_allocator_vtable_t *vtable;
xc_shm_t *shm;
};
#endif
typedef struct _xc_allocator_t xc_allocator_t;
typedef struct _xc_allocator_block_t xc_allocator_block_t;
typedef xc_shmsize_t xc_memsize_t;
/* allocator */
#define XC_ALLOCATOR_MALLOC(func) void *func(xc_allocator_t *allocator, xc_memsize_t size)
#define XC_ALLOCATOR_FREE(func) xc_memsize_t func(xc_allocator_t *allocator, const void *p)
#define XC_ALLOCATOR_CALLOC(func) void *func(xc_allocator_t *allocator, xc_memsize_t memb, xc_memsize_t size)
#define XC_ALLOCATOR_REALLOC(func) void *func(xc_allocator_t *allocator, const void *p, xc_memsize_t size)
#define XC_ALLOCATOR_AVAIL(func) xc_memsize_t func(const xc_allocator_t *allocator)
#define XC_ALLOCATOR_SIZE(func) xc_memsize_t func(const xc_allocator_t *allocator)
#define XC_ALLOCATOR_FREEBLOCK_FIRST(func) const xc_allocator_block_t *func(const xc_allocator_t *allocator)
#define XC_ALLOCATOR_FREEBLOCK_NEXT(func) const xc_allocator_block_t *func(const xc_allocator_block_t *block)
#define XC_ALLOCATOR_BLOCK_SIZE(func) xc_memsize_t func(const xc_allocator_block_t *block)
#define XC_ALLOCATOR_BLOCK_OFFSET(func) xc_memsize_t func(const xc_allocator_t *allocator, const xc_allocator_block_t *block)
#define XC_ALLOCATOR_INIT(func) xc_allocator_t *func(xc_shm_t *shm, xc_allocator_t *allocator, xc_memsize_t size)
#define XC_ALLOCATOR_DESTROY(func) void func(xc_allocator_t *allocator)
#define XC_ALLOCATOR_VTABLE(name) { \
xc_##name##_malloc \
, xc_##name##_free \
, xc_##name##_calloc \
, xc_##name##_realloc \
, xc_##name##_avail \
, xc_##name##_size \
, xc_##name##_freeblock_first \
, xc_##name##_freeblock_next \
, xc_##name##_block_size \
, xc_##name##_block_offset \
\
, xc_##name##_init \
, xc_##name##_destroy \
}
struct _xc_allocator_vtable_t {
XC_ALLOCATOR_MALLOC((*malloc));
XC_ALLOCATOR_FREE((*free));
XC_ALLOCATOR_CALLOC((*calloc));
XC_ALLOCATOR_REALLOC((*realloc));
XC_ALLOCATOR_AVAIL((*avail));
XC_ALLOCATOR_SIZE((*size));
XC_ALLOCATOR_FREEBLOCK_FIRST((*freeblock_first));
XC_ALLOCATOR_FREEBLOCK_NEXT((*freeblock_next));
XC_ALLOCATOR_BLOCK_SIZE((*block_size));
XC_ALLOCATOR_BLOCK_OFFSET((*block_offset));
XC_ALLOCATOR_INIT((*init));
XC_ALLOCATOR_DESTROY((*destroy));
};
int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable);
const xc_allocator_vtable_t *xc_allocator_find(const char *name);
#endif /* XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D */

136
xcache/xc_malloc.c

@ -1,5 +1,5 @@
#define XC_SHM_IMPL _xc_malloc_shm_t
#define XC_MEM_IMPL _xc_malloc_mem_t
#define _xc_allocator_t _xc_allocator_malloc_t
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@ -8,13 +8,15 @@
#include <config.h>
#endif
#include "xc_shm.h"
#include "php.h"
#include "xc_utils.h"
#include "xc_allocator.h"
#ifndef TEST
#include "xcache.h"
#endif
#include "util/xc_align.h"
struct _xc_malloc_mem_t {
const xc_mem_handlers_t *handlers;
xc_shm_t *shm;
struct _xc_allocator_malloc_t {
const xc_allocator_vtable_t *vtable;
xc_shm_t *shm;
xc_memsize_t size;
xc_memsize_t avail; /* total free */
};
@ -24,112 +26,112 @@ struct _xc_malloc_shm_t {
xc_shm_handlers_t *handlers;
xc_shmsize_t size;
xc_shmsize_t memoffset;
#ifndef TEST
HashTable blocks;
#endif
};
/* }}} */
#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
#ifndef TEST
# define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
#else
# define CHECK(x, e) do { if ((x) == NULL) { fprintf(stderr, "%s\n", "XCache: " e); goto err; } } while (0)
#endif
static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
static void *xc_add_to_blocks(xc_allocator_t *allocator, void *p, size_t size) /* {{{ */
{
if (p) {
zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
#ifdef TEST
allocator->avail -= size;
#else
zend_hash_add(&allocator->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
#endif
}
return p;
}
/* }}} */
static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
static void xc_del_from_blocks(xc_allocator_t *allocator, void *p) /* {{{ */
{
zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
#ifdef TEST
/* TODO: allocator->avail += size; */
#else
zend_hash_del(&allocator->shm->blocks, (void *) &p, sizeof(p));
#endif
}
/* }}} */
static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
static XC_ALLOCATOR_MALLOC(xc_allocator_malloc_malloc) /* {{{ */
{
return xc_add_to_blocks(mem, malloc(size), size);
return xc_add_to_blocks(allocator, malloc(size), size);
}
/* }}} */
static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
static XC_ALLOCATOR_FREE(xc_allocator_malloc_free) /* {{{ return block size freed */
{
xc_del_from_blocks(mem, (void *) p);
xc_del_from_blocks(allocator, (void *) p);
free((void *) p);
return 0;
}
/* }}} */
static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
{
return xc_add_to_blocks(mem, calloc(memb, size), size);
}
/* }}} */
static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
static XC_ALLOCATOR_CALLOC(xc_allocator_malloc_calloc) /* {{{ */
{
return xc_add_to_blocks(mem, realloc((void *) p, size), size);
}
/* }}} */
static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
{
char *p = xc_add_to_blocks(mem, malloc(len), len);
if (!p) {
return NULL;
}
return memcpy(p, str, len);
return xc_add_to_blocks(allocator, calloc(memb, size), size);
}
/* }}} */
static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
static XC_ALLOCATOR_REALLOC(xc_allocator_malloc_realloc) /* {{{ */
{
return xc_malloc_strndup(mem, str, strlen(str) + 1);
return xc_add_to_blocks(allocator, realloc((void *) p, size), size);
}
/* }}} */
static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
static XC_ALLOCATOR_AVAIL(xc_allocator_malloc_avail) /* {{{ */
{
return mem->avail;
return allocator->avail;
}
/* }}} */
static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
static XC_ALLOCATOR_SIZE(xc_allocator_malloc_size) /* {{{ */
{
return mem->size;
return allocator->size;
}
/* }}} */
static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_malloc_freeblock_first) /* {{{ */
{
return (void *) -1;
}
/* }}} */
XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_malloc_freeblock_next) /* {{{ */
{
return NULL;
}
/* }}} */
XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_malloc_block_size) /* {{{ */
{
return 0;
}
/* }}} */
XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_malloc_block_offset) /* {{{ */
{
return 0;
}
/* }}} */
static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
static XC_ALLOCATOR_INIT(xc_allocator_malloc_init) /* {{{ */
{
#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
#define MINSIZE (ALIGN(sizeof(xc_allocator_t)))
/* requires at least the header and 1 tail block */
if (size < MINSIZE) {
fprintf(stderr, "xc_mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
fprintf(stderr, "xc_allocator_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
return NULL;
}
mem->shm = shm;
mem->size = size;
mem->avail = size - MINSIZE;
allocator->shm = shm;
allocator->size = size;
allocator->avail = size - MINSIZE;
#undef MINSIZE
return mem;
return allocator;
}
/* }}} */
static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */
{
}
/* }}} */
@ -141,6 +143,7 @@ static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
/* }}} */
static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
{
#ifndef TEST
HashPosition pos;
size_t *psize;
char **ptr;
@ -153,6 +156,7 @@ static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
}
zend_hash_move_forward_ex(&shm->blocks, &pos);
}
#endif
return 0;
}
@ -175,7 +179,9 @@ static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
{
#ifndef TEST
zend_hash_destroy(&shm->blocks);
#endif
free(shm);
return;
}
@ -186,7 +192,9 @@ static XC_SHM_INIT(xc_malloc_init) /* {{{ */
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;
@ -195,15 +203,15 @@ err:
static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
{
xc_mem_t *mem;
void *mem;
if (shm->memoffset + size > shm->size) {
#ifndef TEST
zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
#endif
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);
CHECK(mem = calloc(1, size), "mem OOM");
return mem;
err:
return NULL;
@ -211,28 +219,28 @@ err:
/* }}} */
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_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc);
static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
void xc_shm_malloc_register() /* {{{ */
void xc_allocator_malloc_register() /* {{{ */
{
if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
if (xc_allocator_register("malloc", &xc_allocator_malloc_vtable) == 0) {
#ifndef TEST
zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
#endif
}
}
/* }}} */
CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
#ifndef TEST
void xc_shm_malloc_register() /* {{{ */
{
if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
}
err:
return;
}
/* }}} */
#endif

415
xcache/xc_mem.c

@ -1,415 +0,0 @@
#ifdef TEST
# include <limits.h>
# include <stdio.h>
# define XCACHE_DEBUG
typedef int zend_bool;
# define ZEND_ATTRIBUTE_PTR_FORMAT(a, b, c)
# define zend_error(type, error) fprintf(stderr, "%s", error)
#else
# include <php.h>
#endif
#ifdef XCACHE_DEBUG
# define ALLOC_DEBUG_BLOCK_CHECK
#endif
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define XC_MEMBLOCK_IMPL _xc_mem_block_t
#define XC_MEM_IMPL _xc_mem_mem_t
#include "xc_shm.h"
#include "util/xc_align.h"
#include "util/xc_trace.h"
#if 0
#undef ALLOC_DEBUG_BLOCK_CHECK
#endif
#define CHAR_PTR(p) ((char *) (p))
#define PADD(p, a) (CHAR_PTR(p) + a)
#define PSUB(p1, p2) (CHAR_PTR(p1) - CHAR_PTR(p2))
/* {{{ mem */
struct _xc_mem_block_t {
#ifdef ALLOC_DEBUG_BLOCK_CHECK
unsigned int magic;
#endif
xc_memsize_t size; /* reserved even after alloc */
xc_block_t *next; /* not used after alloc */
};
struct _xc_mem_mem_t {
const xc_mem_handlers_t *handlers;
xc_shm_t *shm;
xc_memsize_t size;
xc_memsize_t avail; /* total free */
xc_block_t headblock[1]; /* just as a pointer to first block*/
};
#ifndef XtOffsetOf
# include <linux/stddef.h>
# define XtOffsetOf(s_type, field) offsetof(s_type, field)
#endif
#define SizeOf(type, field) sizeof( ((type *) 0)->field )
#define BLOCK_HEADER_SIZE() (ALIGN( XtOffsetOf(xc_block_t, size) + SizeOf(xc_block_t, size) ))
#define BLOCK_MAGIC ((unsigned int) 0x87655678)
/* }}} */
static inline void xc_block_setup(xc_block_t *b, xc_memsize_t size, xc_block_t *next) /* {{{ */
{
#ifdef ALLOC_DEBUG_BLOCK_CHECK
b->magic = BLOCK_MAGIC;
#endif
b->size = size;
b->next = next;
}
/* }}} */
#ifdef ALLOC_DEBUG_BLOCK_CHECK
static void xc_block_check(xc_block_t *b) /* {{{ */
{
if (b->magic != BLOCK_MAGIC) {
fprintf(stderr, "0x%X != 0x%X magic wrong \n", b->magic, BLOCK_MAGIC);
}
}
/* }}} */
#else
# define xc_block_check(b) do { } while(0)
#endif
static XC_MEM_MALLOC(xc_mem_malloc) /* {{{ */
{
xc_block_t *prev, *cur;
xc_block_t *newb, *b;
xc_memsize_t realsize;
xc_memsize_t minsize;
void *p;
/* [xc_block_t:size|size] */
realsize = BLOCK_HEADER_SIZE() + size;
/* realsize is ALIGNed so next block start at ALIGNed address */
realsize = ALIGN(realsize);
TRACE("avail: %lu (%luKB). Allocate size: %lu realsize: %lu (%luKB)"
, mem->avail, mem->avail / 1024
, size
, realsize, realsize / 1024
);
do {
p = NULL;
if (mem->avail < realsize) {
TRACE("%s", " oom");
break;
}
b = NULL;
minsize = ULONG_MAX;
/* prev|cur */
for (prev = mem->headblock; prev->next; prev = cur) {
/* while (prev->next != 0) { */
cur = prev->next;
xc_block_check(cur);
if (cur->size == realsize) {
/* found a perfect fit, stop searching */
b = prev;
break;
}
/* make sure we can split on the block */
else if (cur->size > (sizeof(xc_block_t) + realsize) &&
cur->size < minsize) {
/* cur is acceptable and memller */
b = prev;
minsize = cur->size;
}
prev = cur;
}
if (b == NULL) {
TRACE("%s", " no fit chunk");
break;
}
prev = b;
cur = prev->next;
p = PADD(cur, BLOCK_HEADER_SIZE());
/* update the block header */
mem->avail -= realsize;
/* perfect fit, just unlink */
if (cur->size == realsize) {
prev->next = cur->next;
TRACE(" perfect fit. Got: %p", p);
break;
}
/* make new free block after alloced space */
/* save, as it might be overwrited by newb (cur->size is ok) */
b = cur->next;
/* prev|cur |next=b */
newb = (xc_block_t *)PADD(cur, realsize);
xc_block_setup(newb, cur->size - realsize, b);
cur->size = realsize;
/* prev|cur|newb|next
* `--^
*/
TRACE(" -> avail: %lu (%luKB). new next: %p offset: %lu %luKB. Got: %p"
, mem->avail, mem->avail / 1024
, newb
, PSUB(newb, mem), PSUB(newb, mem) / 1024
, p
);
prev->next = newb;
/* prev|cur|newb|next
* `-----^
*/
} while (0);
return p;
}
/* }}} */
static XC_MEM_FREE(xc_mem_free) /* {{{ return block size freed */
{
xc_block_t *cur, *b;
int size;
cur = (xc_block_t *) (CHAR_PTR(p) - BLOCK_HEADER_SIZE());
TRACE("freeing: %p, size=%lu", p, cur->size);
xc_block_check(cur);
assert((char*)mem < (char*)cur && (char*)cur < (char*)mem + mem->size);
/* find free block right before the p */
b = mem->headblock;
while (b->next != 0 && b->next < cur) {
b = b->next;
}
/* restore block */
cur->next = b->next;
b->next = cur;
size = cur->size;
TRACE(" avail %lu (%luKB)", mem->avail, mem->avail / 1024);
mem->avail += size;
/* combine prev|cur */
if (PADD(b, b->size) == (char *)cur) {
b->size += cur->size;
b->next = cur->next;
cur = b;
TRACE("%s", " combine prev");
}
/* combine cur|next */
b = cur->next;
if (PADD(cur, cur->size) == (char *)b) {
cur->size += b->size;
cur->next = b->next;
TRACE("%s", " combine next");
}
TRACE(" -> avail %lu (%luKB)", mem->avail, mem->avail / 1024);
return size;
}
/* }}} */
static XC_MEM_CALLOC(xc_mem_calloc) /* {{{ */
{
xc_memsize_t realsize = memb * size;
void *p = xc_mem_malloc(mem, realsize);
if (p) {
memset(p, 0, realsize);
}
return p;
}
/* }}} */
static XC_MEM_REALLOC(xc_mem_realloc) /* {{{ */
{
void *newp = xc_mem_malloc(mem, size);
if (p && newp) {
memcpy(newp, p, size);
xc_mem_free(mem, p);
}
return newp;
}
/* }}} */
static XC_MEM_STRNDUP(xc_mem_strndup) /* {{{ */
{
void *p = xc_mem_malloc(mem, len + 1);
if (p) {
memcpy(p, str, len + 1);
}
return p;
}
/* }}} */
static XC_MEM_STRDUP(xc_mem_strdup) /* {{{ */
{
return xc_mem_strndup(mem, str, strlen(str));
}
/* }}} */
static XC_MEM_AVAIL(xc_mem_avail) /* {{{ */
{
return mem->avail;
}
/* }}} */
static XC_MEM_SIZE(xc_mem_size) /* {{{ */
{
return mem->size;
}
/* }}} */
static XC_MEM_FREEBLOCK_FIRST(xc_mem_freeblock_first) /* {{{ */
{
return mem->headblock->next;
}
/* }}} */
XC_MEM_FREEBLOCK_NEXT(xc_mem_freeblock_next) /* {{{ */
{
return block->next;
}
/* }}} */
XC_MEM_BLOCK_SIZE(xc_mem_block_size) /* {{{ */
{
return block->size;
}
/* }}} */
XC_MEM_BLOCK_OFFSET(xc_mem_block_offset) /* {{{ */
{
return ((char *) block) - ((char *) mem);
}
/* }}} */
static XC_MEM_INIT(xc_mem_init) /* {{{ */
{
xc_block_t *b;
#define MINSIZE (ALIGN(sizeof(xc_mem_t)) + sizeof(xc_block_t))
/* requires at least the header and 1 tail block */
if (size < MINSIZE) {
fprintf(stderr, "xc_mem_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
return NULL;
}
TRACE("size=%lu", size);
mem->shm = shm;
mem->size = size;
mem->avail = size - MINSIZE;
/* pointer to first block, right after ALIGNed header */
b = mem->headblock;
xc_block_setup(b, 0, (xc_block_t *) PADD(mem, ALIGN(sizeof(xc_mem_t))));
/* first block*/
b = b->next;
xc_block_setup(b, mem->avail, 0);
#undef MINSIZE
return mem;
}
/* }}} */
static XC_MEM_DESTROY(xc_mem_destroy) /* {{{ */
{
}
/* }}} */
#ifdef TEST
/* {{{ testing */
#undef CHECK
#define CHECK(a, msg) do { \
if (!(a)) { \
fprintf(stderr, "%s\n", msg); return -1; \
} \
} while (0)
#include <time.h>
int main()
{
int count = 0;
void *p;
void *memory;
xc_mem_t *mem;
void **ptrs;
int size, i;
#if 0
fprintf(stderr, "%s", "Input test size: ");
scanf("%d", &size);
#else
size = 1024;
#endif
CHECK(memory = malloc(size), "OOM");
CHECK(ptrs = malloc(size * sizeof(void *)), "OOM");
mem = (xc_mem_t *) memory;
CHECK(mem = xc_mem_init(NULL, mem, size), "Failed init memory allocator");
while ((p = xc_mem_malloc(mem, 1))) {
ptrs[count ++] = p;
}
fprintf(stderr, "count=%d, random freeing\n", count);
srandom(time(NULL));
while (count) {
i = (random() % count);
fprintf(stderr, "freeing %d: ", i);
xc_mem_free(mem, ptrs[i]);
ptrs[i] = ptrs[count - 1];
count --;
}
free(ptrs);
free(memory);
return 0;
}
/* }}} */
#endif
typedef struct {
const char *name;
const xc_mem_handlers_t *handlers;
} xc_mem_scheme_t;
static xc_mem_scheme_t xc_mem_schemes[10];
int xc_mem_scheme_register(const char *name, const xc_mem_handlers_t *handlers) /* {{{ */
{
int i;
for (i = 0; i < 10; i ++) {
if (!xc_mem_schemes[i].name) {
xc_mem_schemes[i].name = name;
xc_mem_schemes[i].handlers = handlers;
return 1;
}
}
return 0;
}
/* }}} */
const xc_mem_handlers_t *xc_mem_scheme_find(const char *name) /* {{{ */
{
int i;
for (i = 0; i < 10 && xc_mem_schemes[i].name; i ++) {
if (strcmp(xc_mem_schemes[i].name, name) == 0) {
return xc_mem_schemes[i].handlers;
}
}
return NULL;
}
/* }}} */
static xc_mem_handlers_t xc_mem_mem_handlers = XC_MEM_HANDLERS(mem);
void xc_shm_mem_init() /* {{{ */
{
memset(xc_mem_schemes, 0, sizeof(xc_mem_schemes));
if (xc_mem_scheme_register("mem", &xc_mem_mem_handlers) == 0) {
zend_error(E_ERROR, "XCache: failed to register mem mem_scheme");
}
}
/* }}} */

74
xcache/xc_mem.h

@ -1,74 +0,0 @@
#include "xc_shm.h"
typedef struct _xc_mem_handlers_t xc_mem_handlers_t;
#ifndef XC_MEM_IMPL
struct _xc_mem_t {
const xc_mem_handlers_t *handlers;
xc_shm_t *shm;
};
# define XC_MEM_IMPL _xc_mem_t
#endif
#ifndef XC_MEMBLOCK_IMPL
# define XC_MEMBLOCK_IMPL _xc_block_t
#endif
typedef struct XC_MEM_IMPL xc_mem_t;
typedef struct XC_MEMBLOCK_IMPL xc_block_t;
typedef xc_shmsize_t xc_memsize_t;
/* shm::mem */
#define XC_MEM_MALLOC(func) void *func(xc_mem_t *mem, xc_memsize_t size)
#define XC_MEM_FREE(func) xc_memsize_t func(xc_mem_t *mem, const void *p)
#define XC_MEM_CALLOC(func) void *func(xc_mem_t *mem, xc_memsize_t memb, xc_memsize_t size)
#define XC_MEM_REALLOC(func) void *func(xc_mem_t *mem, const void *p, xc_memsize_t size)
#define XC_MEM_STRNDUP(func) char *func(xc_mem_t *mem, const char *str, xc_memsize_t len)
#define XC_MEM_STRDUP(func) char *func(xc_mem_t *mem, const char *str)
#define XC_MEM_AVAIL(func) xc_memsize_t func(const xc_mem_t *mem)
#define XC_MEM_SIZE(func) xc_memsize_t func(const xc_mem_t *mem)
#define XC_MEM_FREEBLOCK_FIRST(func) const xc_block_t *func(const xc_mem_t *mem)
#define XC_MEM_FREEBLOCK_NEXT(func) const xc_block_t *func(const xc_block_t *block)
#define XC_MEM_BLOCK_SIZE(func) xc_memsize_t func(const xc_block_t *block)
#define XC_MEM_BLOCK_OFFSET(func) xc_memsize_t func(const xc_mem_t *mem, const xc_block_t *block)
#define XC_MEM_INIT(func) xc_mem_t *func(xc_shm_t *shm, xc_mem_t *mem, xc_memsize_t size)
#define XC_MEM_DESTROY(func) void func(xc_mem_t *mem)
#define XC_MEM_HANDLERS(name) { \
xc_##name##_malloc \
, xc_##name##_free \
, xc_##name##_calloc \
, xc_##name##_realloc \
, xc_##name##_strndup \
, xc_##name##_strdup \
, xc_##name##_avail \
, xc_##name##_size \
, xc_##name##_freeblock_first \
, xc_##name##_freeblock_next \
, xc_##name##_block_size \
, xc_##name##_block_offset \
\
, xc_##name##_init \
, xc_##name##_destroy \
}
struct _xc_mem_handlers_t {
XC_MEM_MALLOC((*malloc));
XC_MEM_FREE((*free));
XC_MEM_CALLOC((*calloc));
XC_MEM_REALLOC((*realloc));
XC_MEM_STRNDUP((*strndup));
XC_MEM_STRDUP((*strdup));
XC_MEM_AVAIL((*avail));
XC_MEM_SIZE((*size));
XC_MEM_FREEBLOCK_FIRST((*freeblock_first));
XC_MEM_FREEBLOCK_NEXT((*freeblock_next));
XC_MEM_BLOCK_SIZE((*block_size));
XC_MEM_BLOCK_OFFSET((*block_offset));
XC_MEM_INIT((*init));
XC_MEM_DESTROY((*destroy));
};
int xc_mem_scheme_register(const char *name, const xc_mem_handlers_t *handlers);
const xc_mem_handlers_t *xc_mem_scheme_find(const char *name);

4
xcache/xc_shm.c

@ -22,14 +22,14 @@ static xc_shm_scheme_t xc_shm_schemes[10];
void xc_shm_init_modules() /* {{{ */
{
extern void xc_shm_mem_init();
extern void xc_allocator_init();
#ifdef HAVE_XCACHE_TEST
extern void xc_shm_malloc_register();
#endif
extern void xc_shm_mmap_register();
memset(xc_shm_schemes, 0, sizeof(xc_shm_schemes));
xc_shm_mem_init();
xc_allocator_init();
#ifdef HAVE_XCACHE_TEST
xc_shm_malloc_register();
#endif

13
xcache/xc_shm.h

@ -1,5 +1,8 @@
#ifndef XC_SHM_H
#define XC_SHM_H
#include <stdlib.h>
typedef struct _xc_shm_handlers_t xc_shm_handlers_t;
#ifndef XC_SHM_IMPL
@ -12,8 +15,6 @@ struct _xc_shm_t {
typedef struct XC_SHM_IMPL xc_shm_t;
typedef size_t xc_shmsize_t;
#include "xc_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)
@ -24,12 +25,11 @@ typedef size_t xc_shmsize_t;
#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_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) { \
NULL \
, xc_##name##_can_readonly \
xc_##name##_can_readonly \
, xc_##name##_is_readwrite \
, xc_##name##_is_readonly \
, xc_##name##_to_readwrite \
@ -43,7 +43,6 @@ typedef size_t xc_shmsize_t;
}
struct _xc_shm_handlers_t {
const xc_mem_handlers_t *memhandlers;
XC_SHM_CAN_READONLY((*can_readonly));
XC_SHM_IS_READWRITE((*is_readwrite));
XC_SHM_IS_READONLY((*is_readonly));

8
xcache/xc_shm_mmap.c

@ -294,15 +294,13 @@ err:
</