2006-05-30 02:05:58 +00:00
|
|
|
#ifdef TEST
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
2008-03-08 03:55:59 +00:00
|
|
|
# define XCACHE_DEBUG
|
2006-05-30 02:05:58 +00:00
|
|
|
#else
|
2006-05-09 10:58:38 +00:00
|
|
|
#include <php.h>
|
2006-05-30 02:05:58 +00:00
|
|
|
#endif
|
|
|
|
|
2008-03-08 03:55:59 +00:00
|
|
|
#ifdef XCACHE_DEBUG
|
|
|
|
# define ALLOC_DEBUG_BLOCK_CHECK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-05-30 02:05:58 +00:00
|
|
|
#include <assert.h>
|
2006-05-09 10:58:38 +00:00
|
|
|
#include <stdlib.h>
|
2006-05-30 02:05:58 +00:00
|
|
|
#include <string.h>
|
2007-05-10 01:26:12 +00:00
|
|
|
#define XC_MEMBLOCK_IMPL _xc_mem_block_t
|
|
|
|
#define XC_MEM_IMPL _xc_mem_mem_t
|
2006-09-09 00:56:44 +00:00
|
|
|
#include "xc_shm.h"
|
2006-05-09 10:58:38 +00:00
|
|
|
#include "align.h"
|
2006-12-08 16:59:00 +00:00
|
|
|
#include "utils.h"
|
2006-05-30 02:05:58 +00:00
|
|
|
|
|
|
|
#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))
|
2006-05-09 10:58:38 +00:00
|
|
|
|
2006-12-07 14:13:38 +00:00
|
|
|
/* {{{ mem */
|
2007-05-10 01:26:12 +00:00
|
|
|
struct _xc_mem_block_t {
|
2006-05-30 02:05:58 +00:00
|
|
|
#ifdef ALLOC_DEBUG_BLOCK_CHECK
|
2006-05-09 10:58:38 +00:00
|
|
|
unsigned int magic;
|
|
|
|
#endif
|
|
|
|
xc_memsize_t size; /* reserved even after alloc */
|
|
|
|
xc_block_t *next; /* not used after alloc */
|
|
|
|
};
|
|
|
|
|
2007-05-10 01:26:12 +00:00
|
|
|
struct _xc_mem_mem_t {
|
2006-09-09 00:56:44 +00:00
|
|
|
const xc_mem_handlers_t *handlers;
|
|
|
|
xc_shm_t *shm;
|
2006-05-09 10:58:38 +00:00
|
|
|
xc_memsize_t size;
|
|
|
|
xc_memsize_t avail; /* total free */
|
|
|
|
xc_block_t headblock[1]; /* just as a pointer to first block*/
|
|
|
|
};
|
|
|
|
|
2006-05-30 02:05:58 +00:00
|
|
|
#ifndef XtOffsetOf
|
|
|
|
# include <linux/stddef.h>
|
|
|
|
# define XtOffsetOf(s_type, field) offsetof(s_type, field)
|
2006-05-09 10:58:38 +00:00
|
|
|
#endif
|
2006-05-30 02:05:58 +00:00
|
|
|
|
2006-05-09 10:58:38 +00:00
|
|
|
#define SizeOf(type, field) sizeof( ((type *) 0)->field )
|
2006-05-30 02:05:58 +00:00
|
|
|
#define BLOCK_HEADER_SIZE() (ALIGN( XtOffsetOf(xc_block_t, size) + SizeOf(xc_block_t, size) ))
|
2006-05-09 10:58:38 +00:00
|
|
|
|
|
|
|
#define BLOCK_MAGIC ((unsigned int) 0x87655678)
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
static inline void xc_block_setup(xc_block_t *b, xc_memsize_t size, xc_block_t *next) /* {{{ */
|
|
|
|
{
|
2006-05-30 02:05:58 +00:00
|
|
|
#ifdef ALLOC_DEBUG_BLOCK_CHECK
|
2006-05-09 10:58:38 +00:00
|
|
|
b->magic = BLOCK_MAGIC;
|
|
|
|
#endif
|
|
|
|
b->size = size;
|
|
|
|
b->next = next;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-05-30 02:05:58 +00:00
|
|
|
#ifdef ALLOC_DEBUG_BLOCK_CHECK
|
2006-05-09 10:58:38 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_MALLOC(xc_mem_malloc) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE("avail: %d (%dKB). Allocate size: %d realsize: %d (%dKB)"
|
2006-05-30 02:05:58 +00:00
|
|
|
, mem->avail, mem->avail / 1024
|
|
|
|
, size
|
|
|
|
, realsize, realsize / 1024
|
|
|
|
);
|
2006-05-09 10:58:38 +00:00
|
|
|
do {
|
|
|
|
p = NULL;
|
|
|
|
if (mem->avail < realsize) {
|
2006-12-08 16:59:00 +00:00
|
|
|
TRACE("%s", " oom");
|
2006-05-09 10:58:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
b = NULL;
|
|
|
|
minsize = INT_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) {
|
2006-12-08 16:59:00 +00:00
|
|
|
TRACE("%s", " no fit chunk");
|
2006-05-09 10:58:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = b;
|
|
|
|
|
|
|
|
cur = prev->next;
|
|
|
|
p = PADD(cur, BLOCK_HEADER_SIZE());
|
|
|
|
|
|
|
|
/* update the block header */
|
|
|
|
mem->avail -= realsize;
|
|
|
|
|
2006-05-30 02:05:58 +00:00
|
|
|
/* perfect fit, just unlink */
|
2006-05-09 10:58:38 +00:00
|
|
|
if (cur->size == realsize) {
|
|
|
|
prev->next = cur->next;
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE(" perfect fit. Got: %p", p);
|
2006-05-09 10:58:38 +00:00
|
|
|
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
|
|
|
|
* `--^
|
|
|
|
*/
|
|
|
|
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE(" -> avail: %d (%dKB). new next: %p offset: %d %dKB. Got: %p"
|
2006-05-30 02:05:58 +00:00
|
|
|
, mem->avail, mem->avail / 1024
|
2006-05-09 10:58:38 +00:00
|
|
|
, newb
|
2006-05-30 02:05:58 +00:00
|
|
|
, PSUB(newb, mem), PSUB(newb, mem) / 1024
|
|
|
|
, p
|
2006-05-09 10:58:38 +00:00
|
|
|
);
|
|
|
|
prev->next = newb;
|
|
|
|
/* prev|cur|newb|next
|
|
|
|
* `-----^
|
|
|
|
*/
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_FREE(xc_mem_free) /* {{{ return block size freed */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
xc_block_t *cur, *b;
|
|
|
|
int size;
|
|
|
|
|
2006-05-30 02:05:58 +00:00
|
|
|
cur = (xc_block_t *) (CHAR_PTR(p) - BLOCK_HEADER_SIZE());
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE("freeing: %p, size=%d", p, cur->size);
|
2006-05-09 10:58:38 +00:00
|
|
|
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;
|
|
|
|
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE(" avail %d (%dKB)", mem->avail, mem->avail / 1024);
|
2006-05-09 10:58:38 +00:00
|
|
|
mem->avail += size;
|
|
|
|
|
|
|
|
/* combine prev|cur */
|
|
|
|
if (PADD(b, b->size) == (char *)cur) {
|
|
|
|
b->size += cur->size;
|
|
|
|
b->next = cur->next;
|
|
|
|
cur = b;
|
2006-12-08 16:59:00 +00:00
|
|
|
TRACE("%s", " combine prev");
|
2006-05-09 10:58:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* combine cur|next */
|
|
|
|
b = cur->next;
|
|
|
|
if (PADD(cur, cur->size) == (char *)b) {
|
|
|
|
cur->size += b->size;
|
|
|
|
cur->next = b->next;
|
2006-12-08 16:59:00 +00:00
|
|
|
TRACE("%s", " combine next");
|
2006-05-09 10:58:38 +00:00
|
|
|
}
|
2006-12-08 16:11:19 +00:00
|
|
|
TRACE(" -> avail %d (%dKB)", mem->avail, mem->avail / 1024);
|
2006-05-09 10:58:38 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_CALLOC(xc_mem_calloc) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
xc_memsize_t realsize = memb * size;
|
|
|
|
void *p = xc_mem_malloc(mem, realsize);
|
|
|
|
|
2006-08-29 11:11:42 +00:00
|
|
|
if (p) {
|
|
|
|
memset(p, 0, realsize);
|
|
|
|
}
|
2006-05-09 10:58:38 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_REALLOC(xc_mem_realloc) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
void *newp = xc_mem_malloc(mem, size);
|
2006-09-10 00:30:09 +00:00
|
|
|
if (p && newp) {
|
2006-08-29 11:11:42 +00:00
|
|
|
memcpy(newp, p, size);
|
|
|
|
xc_mem_free(mem, p);
|
|
|
|
}
|
2006-05-09 10:58:38 +00:00
|
|
|
return newp;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_STRNDUP(xc_mem_strndup) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
void *p = xc_mem_malloc(mem, len + 1);
|
2006-08-29 11:11:42 +00:00
|
|
|
if (p) {
|
|
|
|
memcpy(p, str, len + 1);
|
|
|
|
}
|
2006-05-09 10:58:38 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_STRDUP(xc_mem_strdup) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return xc_mem_strndup(mem, str, strlen(str));
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_AVAIL(xc_mem_avail) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return mem->avail;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_SIZE(xc_mem_size) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return mem->size;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_FREEBLOCK_FIRST(xc_mem_freeblock_first) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return mem->headblock->next;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
XC_MEM_FREEBLOCK_NEXT(xc_mem_freeblock_next) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return block->next;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
XC_MEM_BLOCK_SIZE(xc_mem_block_size) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return block->size;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
XC_MEM_BLOCK_OFFSET(xc_mem_block_offset) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
return ((char *) block) - ((char *) mem);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_INIT(xc_mem_init) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
2006-05-30 02:05:58 +00:00
|
|
|
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) {
|
2007-05-23 01:00:26 +00:00
|
|
|
fprintf(stderr, "xc_mem_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
|
2006-05-30 02:05:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-09-09 00:56:44 +00:00
|
|
|
mem->shm = shm;
|
2006-05-09 10:58:38 +00:00
|
|
|
mem->size = size;
|
2006-05-30 02:05:58 +00:00
|
|
|
mem->avail = size - MINSIZE;
|
2006-05-09 10:58:38 +00:00
|
|
|
|
2006-05-30 02:05:58 +00:00
|
|
|
/* pointer to first block, right after ALIGNed header */
|
2006-05-09 10:58:38 +00:00
|
|
|
b = mem->headblock;
|
2006-05-30 02:05:58 +00:00
|
|
|
xc_block_setup(b, 0, (xc_block_t *) PADD(mem, ALIGN(sizeof(xc_mem_t))));
|
2006-05-09 10:58:38 +00:00
|
|
|
|
|
|
|
/* first block*/
|
|
|
|
b = b->next;
|
|
|
|
xc_block_setup(b, mem->avail, 0);
|
2006-05-30 02:05:58 +00:00
|
|
|
#undef MINSIZE
|
2006-05-09 10:58:38 +00:00
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-09-09 00:56:44 +00:00
|
|
|
static XC_MEM_DESTROY(xc_mem_destroy) /* {{{ */
|
2006-05-09 10:58:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
/* }}} */
|
2006-05-30 02:05:58 +00:00
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
/* {{{ */
|
|
|
|
#undef CHECK
|
|
|
|
#define CHECK(a, msg) do { if ((a) == NULL) { puts(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 = 100;
|
|
|
|
#endif
|
|
|
|
CHECK(memory = malloc(size), "OOM");
|
|
|
|
CHECK(ptrs = malloc(size * sizeof(void*)), "OOM");
|
|
|
|
CHECK(mem = xc_mem_init(memory, 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 --;
|
|
|
|
}
|
|
|
|
|
2006-05-31 01:21:41 +00:00
|
|
|
free(ptrs);
|
2006-05-30 02:05:58 +00:00
|
|
|
free(memory);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
#endif
|
2006-09-09 00:56:44 +00:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|