Changeset 148 in svn for trunk/mem.c


Ignore:
Timestamp:
2006-09-09T02:56:44+02:00 (9 years ago)
Author:
Xuefer
Message:

reconstruct shm/allocator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mem.c

    r112 r148  
    99#include <stdlib.h>
    1010#include <string.h>
    11 #include "mem.h"
     11#define XC_SHM_IMPL
     12#define XC_MEM_IMPL
     13#include "xc_shm.h"
    1214#include "align.h"
    1315
     
    3739
    3840struct _xc_mem_t {
     41    const xc_mem_handlers_t *handlers;
     42    xc_shm_t                *shm;
    3943    xc_memsize_t size;
    4044    xc_memsize_t avail;       /* total free */
     
    7579
    7680
    77 void *xc_mem_malloc(xc_mem_t *mem, xc_memsize_t size) /* {{{ */
     81static XC_MEM_MALLOC(xc_mem_malloc) /* {{{ */
    7882{
    7983    xc_block_t *prev, *cur;
     
    183187}
    184188/* }}} */
    185 int xc_mem_free(xc_mem_t *mem, const void *p) /* {{{ return block size freed */
     189static XC_MEM_FREE(xc_mem_free) /* {{{ return block size freed */
    186190{
    187191    xc_block_t *cur, *b;
     
    237241}
    238242/* }}} */
    239 void *xc_mem_calloc(xc_mem_t *mem, xc_memsize_t memb, xc_memsize_t size) /* {{{ */
     243static XC_MEM_CALLOC(xc_mem_calloc) /* {{{ */
    240244{
    241245    xc_memsize_t realsize = memb * size;
     
    248252}
    249253/* }}} */
    250 void *xc_mem_realloc(xc_mem_t *mem, const void *p, xc_memsize_t size) /* {{{ */
     254static XC_MEM_REALLOC(xc_mem_realloc) /* {{{ */
    251255{
    252256    void *newp = xc_mem_malloc(mem, size);
     
    258262}
    259263/* }}} */
    260 char *xc_mem_strndup(xc_mem_t *mem, const char *str, xc_memsize_t len) /* {{{ */
     264static XC_MEM_STRNDUP(xc_mem_strndup) /* {{{ */
    261265{
    262266    void *p = xc_mem_malloc(mem, len + 1);
     
    267271}
    268272/* }}} */
    269 char *xc_mem_strdup(xc_mem_t *mem, const char *str) /* {{{ */
     273static XC_MEM_STRDUP(xc_mem_strdup) /* {{{ */
    270274{
    271275    return xc_mem_strndup(mem, str, strlen(str));
     
    273277/* }}} */
    274278
    275 xc_memsize_t xc_mem_avail(xc_mem_t *mem) /* {{{ */
     279static XC_MEM_AVAIL(xc_mem_avail) /* {{{ */
    276280{
    277281    return mem->avail;
    278282}
    279283/* }}} */
    280 xc_memsize_t xc_mem_size(xc_mem_t *mem) /* {{{ */
     284static XC_MEM_SIZE(xc_mem_size) /* {{{ */
    281285{
    282286    return mem->size;
     
    284288/* }}} */
    285289
    286 const xc_block_t *xc_mem_freeblock_first(xc_mem_t *mem) /* {{{ */
     290static XC_MEM_FREEBLOCK_FIRST(xc_mem_freeblock_first) /* {{{ */
    287291{
    288292    return mem->headblock->next;
    289293}
    290294/* }}} */
    291 const xc_block_t *xc_mem_freeblock_next(const xc_block_t *block) /* {{{ */
     295XC_MEM_FREEBLOCK_NEXT(xc_mem_freeblock_next) /* {{{ */
    292296{
    293297    return block->next;
    294298}
    295299/* }}} */
    296 xc_memsize_t xc_mem_block_size(const xc_block_t *block) /* {{{ */
     300XC_MEM_BLOCK_SIZE(xc_mem_block_size) /* {{{ */
    297301{
    298302    return block->size;
    299303}
    300304/* }}} */
    301 xc_memsize_t xc_mem_block_offset(const xc_mem_t *mem, const xc_block_t *block) /* {{{ */
     305XC_MEM_BLOCK_OFFSET(xc_mem_block_offset) /* {{{ */
    302306{
    303307    return ((char *) block) - ((char *) mem);
     
    305309/* }}} */
    306310
    307 xc_mem_t *xc_mem_init(void *ptr, xc_memsize_t size) /* {{{ */
    308 {
    309     xc_mem_t   *mem;
     311static XC_MEM_INIT(xc_mem_init) /* {{{ */
     312{
    310313    xc_block_t *b;
    311 
    312314#define MINSIZE (ALIGN(sizeof(xc_mem_t)) + sizeof(xc_block_t))
    313315    /* requires at least the header and 1 tail block */
     
    316318        return NULL;
    317319    }
    318     mem = (xc_mem_t *) ptr;
     320    mem->shm = shm;
    319321    mem->size = size;
    320322    mem->avail = size - MINSIZE;
     
    332334}
    333335/* }}} */
    334 void xc_mem_destroy(xc_mem_t *mem) /* {{{ */
     336static XC_MEM_DESTROY(xc_mem_destroy) /* {{{ */
    335337{
    336338}
     
    381383/* }}} */
    382384#endif
     385
     386typedef struct {
     387    const char              *name;
     388    const xc_mem_handlers_t *handlers;
     389} xc_mem_scheme_t;
     390static xc_mem_scheme_t xc_mem_schemes[10];
     391
     392int xc_mem_scheme_register(const char *name, const xc_mem_handlers_t *handlers) /* {{{ */
     393{
     394    int i;
     395    for (i = 0; i < 10; i ++) {
     396        if (!xc_mem_schemes[i].name) {
     397            xc_mem_schemes[i].name = name;
     398            xc_mem_schemes[i].handlers = handlers;
     399            return 1;
     400        }
     401    }
     402    return 0;
     403}
     404/* }}} */
     405const xc_mem_handlers_t *xc_mem_scheme_find(const char *name) /* {{{ */
     406{
     407    int i;
     408    for (i = 0; i < 10 && xc_mem_schemes[i].name; i ++) {
     409        if (strcmp(xc_mem_schemes[i].name, name) == 0) {
     410            return xc_mem_schemes[i].handlers;
     411        }
     412    }
     413    return NULL;
     414}
     415/* }}} */
     416
     417static xc_mem_handlers_t xc_mem_mem_handlers = XC_MEM_HANDLERS(mem);
     418void xc_shm_mem_init() /* {{{ */
     419{
     420    memset(xc_mem_schemes, 0, sizeof(xc_mem_schemes));
     421
     422    if (xc_mem_scheme_register("mem", &xc_mem_mem_handlers) == 0) {
     423        zend_error(E_ERROR, "XCache: failed to register mem mem_scheme");
     424    }
     425}
     426/* }}} */
Note: See TracChangeset for help on using the changeset viewer.