Changeset 1135 in svn for trunk/xcache/xc_malloc.c


Ignore:
Timestamp:
2012-08-09T09:04:02Z (3 years ago)
Author:
Xuefer
Message:

refactor: s/mem/allocator/

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        11acinclude.m4
        22aclocal.m4
         3allocator_test
        34autom4te.cache
        45build
         
        1617conftest.c
        1718.deps
         19devel.fpm
        1820devel.ini
        1921devel.php
        20 devel.fpm
        2122.gdb_history
        2223include
  • trunk/xcache/xc_malloc.c

    r1129 r1135  
    11#define XC_SHM_IMPL _xc_malloc_shm_t
    2 #define XC_MEM_IMPL _xc_malloc_mem_t
     2#define _xc_allocator_t _xc_allocator_malloc_t
    33#include <stdlib.h>
    44#include <stdio.h>
     
    99#endif
    1010#include "xc_shm.h"
    11 #include "php.h"
    12 #include "xc_utils.h"
     11#include "xc_allocator.h"
     12#ifndef TEST
     13#include "xcache.h"
     14#endif
    1315#include "util/xc_align.h"
    1416
    15 struct _xc_malloc_mem_t {
    16     const xc_mem_handlers_t *handlers;
    17     xc_shm_t                *shm;
     17struct _xc_allocator_malloc_t {
     18    const xc_allocator_vtable_t *vtable;
     19    xc_shm_t *shm;
    1820    xc_memsize_t size;
    1921    xc_memsize_t avail;       /* total free */
     
    2527    xc_shmsize_t       size;
    2628    xc_shmsize_t       memoffset;
     29#ifndef TEST
    2730    HashTable blocks;
     31#endif
    2832};
    2933/* }}} */
    3034
    31 #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
    32 
    33 static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
     35#ifndef TEST
     36#   define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
     37#else
     38#   define CHECK(x, e) do { if ((x) == NULL) { fprintf(stderr, "%s\n", "XCache: " e); goto err; } } while (0)
     39#endif
     40
     41static void *xc_add_to_blocks(xc_allocator_t *allocator, void *p, size_t size) /* {{{ */
    3442{
    3543    if (p) {
    36         zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
     44#ifdef TEST
     45        allocator->avail -= size;
     46#else
     47        zend_hash_add(&allocator->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
     48#endif
    3749    }
    3850    return p;
    3951}
    4052/* }}} */
    41 static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
    42 {
    43     zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
    44 }
    45 /* }}} */
    46 
    47 static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
    48 {
    49     return xc_add_to_blocks(mem, malloc(size), size);
    50 }
    51 /* }}} */
    52 static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
    53 {
    54     xc_del_from_blocks(mem, (void *) p);
     53static void xc_del_from_blocks(xc_allocator_t *allocator, void *p) /* {{{ */
     54{
     55#ifdef TEST
     56    /* TODO: allocator->avail += size; */
     57#else
     58    zend_hash_del(&allocator->shm->blocks, (void *) &p, sizeof(p));
     59#endif
     60}
     61/* }}} */
     62
     63static XC_ALLOCATOR_MALLOC(xc_allocator_malloc_malloc) /* {{{ */
     64{
     65    return xc_add_to_blocks(allocator, malloc(size), size);
     66}
     67/* }}} */
     68static XC_ALLOCATOR_FREE(xc_allocator_malloc_free) /* {{{ return block size freed */
     69{
     70    xc_del_from_blocks(allocator, (void *) p);
    5571    free((void *) p);
    5672    return 0;
    5773}
    5874/* }}} */
    59 static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
    60 {
    61     return xc_add_to_blocks(mem, calloc(memb, size), size);
    62 }
    63 /* }}} */
    64 static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
    65 {
    66     return xc_add_to_blocks(mem, realloc((void *) p, size), size);
    67 }
    68 /* }}} */
    69 static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
    70 {
    71     char *p = xc_add_to_blocks(mem, malloc(len), len);
    72     if (!p) {
    73         return NULL;
    74     }
    75     return memcpy(p, str, len);
    76 }
    77 /* }}} */
    78 static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
    79 {
    80     return xc_malloc_strndup(mem, str, strlen(str) + 1);
    81 }
    82 /* }}} */
    83 
    84 static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
    85 {
    86     return mem->avail;
    87 }
    88 /* }}} */
    89 static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
    90 {
    91     return mem->size;
    92 }
    93 /* }}} */
    94 
    95 static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
     75static XC_ALLOCATOR_CALLOC(xc_allocator_malloc_calloc) /* {{{ */
     76{
     77    return xc_add_to_blocks(allocator, calloc(memb, size), size);
     78}
     79/* }}} */
     80static XC_ALLOCATOR_REALLOC(xc_allocator_malloc_realloc) /* {{{ */
     81{
     82    return xc_add_to_blocks(allocator, realloc((void *) p, size), size);
     83}
     84/* }}} */
     85
     86static XC_ALLOCATOR_AVAIL(xc_allocator_malloc_avail) /* {{{ */
     87{
     88    return allocator->avail;
     89}
     90/* }}} */
     91static XC_ALLOCATOR_SIZE(xc_allocator_malloc_size) /* {{{ */
     92{
     93    return allocator->size;
     94}
     95/* }}} */
     96
     97static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_malloc_freeblock_first) /* {{{ */
    9698{
    9799    return (void *) -1;
    98100}
    99101/* }}} */
    100 XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
     102static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_malloc_freeblock_next) /* {{{ */
    101103{
    102104    return NULL;
    103105}
    104106/* }}} */
    105 XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
    106 {
    107     return 0;
    108 }
    109 /* }}} */
    110 XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
    111 {
    112     return 0;
    113 }
    114 /* }}} */
    115 
    116 static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
    117 {
    118 #define MINSIZE (ALIGN(sizeof(xc_mem_t)))
     107static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_malloc_block_size) /* {{{ */
     108{
     109    return 0;
     110}
     111/* }}} */
     112static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_malloc_block_offset) /* {{{ */
     113{
     114    return 0;
     115}
     116/* }}} */
     117
     118static XC_ALLOCATOR_INIT(xc_allocator_malloc_init) /* {{{ */
     119{
     120#define MINSIZE (ALIGN(sizeof(xc_allocator_t)))
    119121    /* requires at least the header and 1 tail block */
    120122    if (size < MINSIZE) {
    121         fprintf(stderr, "xc_mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
     123        fprintf(stderr, "xc_allocator_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
    122124        return NULL;
    123125    }
    124     mem->shm = shm;
    125     mem->size = size;
    126     mem->avail = size - MINSIZE;
     126    allocator->shm = shm;
     127    allocator->size = size;
     128    allocator->avail = size - MINSIZE;
    127129#undef MINSIZE
    128130
    129     return mem;
    130 }
    131 /* }}} */
    132 static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
     131    return allocator;
     132}
     133/* }}} */
     134static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */
    133135{
    134136}
     
    142144static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
    143145{
     146#ifndef TEST
    144147    HashPosition pos;
    145148    size_t *psize;
     
    154157        zend_hash_move_forward_ex(&shm->blocks, &pos);
    155158    }
     159#endif
    156160
    157161    return 0;
     
    176180static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
    177181{
     182#ifndef TEST
    178183    zend_hash_destroy(&shm->blocks);
     184#endif
    179185    free(shm);
    180186    return;
     
    187193    shm->size = size;
    188194
     195#ifndef TEST
    189196    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
     197#endif
    190198    return shm;
    191199err:
     
    196204static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
    197205{
    198     xc_mem_t *mem;
     206    void *mem;
    199207    if (shm->memoffset + size > shm->size) {
     208#ifndef TEST
    200209        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
     210#endif
    201211        return NULL;
    202212    }
    203213    shm->memoffset += size;
    204     CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
    205     mem->handlers = shm->handlers->memhandlers;
    206     mem->handlers->init(shm, mem, size);
     214    CHECK(mem = calloc(1, size), "mem OOM");
    207215    return mem;
    208216err:
     
    212220static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
    213221{
    214     mem->handlers->destroy(mem);
    215222    free(mem);
    216223}
    217224/* }}} */
    218225
    219 #define xc_malloc_destroy xc_mem_malloc_destroy
    220 #define xc_malloc_init xc_mem_malloc_init
    221 static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
    222 #undef xc_malloc_init
    223 #undef xc_malloc_destroy
     226static xc_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc);
    224227static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
     228void xc_allocator_malloc_register() /* {{{ */
     229{
     230    if (xc_allocator_register("malloc", &xc_allocator_malloc_vtable) == 0) {
     231#ifndef TEST
     232        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
     233#endif
     234    }
     235}
     236/* }}} */
     237
     238#ifndef TEST
    225239void xc_shm_malloc_register() /* {{{ */
    226240{
    227     if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
    228         zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
    229     }
    230 
    231     CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
    232241    if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
    233242        zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
    234243    }
    235 err:
    236     return;
    237 }
    238 /* }}} */
     244}
     245/* }}} */
     246#endif
Note: See TracChangeset for help on using the changeset viewer.