source: trunk/xcache/xc_malloc.c

Last change on this file was 1468, checked in by moo, 10 months ago

clean up shm handlers

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[1468]1typedef struct xc_malloc_shm_t xc_shm_t;
2#define XC_SHM_T_DEFINED
[1135]3#define _xc_allocator_t _xc_allocator_malloc_t
[1468]4
[148]5#include <stdlib.h>
6#include <stdio.h>
7#include <string.h>
[478]8
[148]9#include "xc_shm.h"
[1135]10#include "xc_allocator.h"
11#ifndef TEST
12#include "xcache.h"
13#endif
[982]14#include "util/xc_align.h"
[148]15
[1135]16struct _xc_allocator_malloc_t {
17    const xc_allocator_vtable_t *vtable;
18    xc_shm_t *shm;
[148]19    xc_memsize_t size;
20    xc_memsize_t avail;       /* total free */
21};
22
[1468]23/* {{{ xc_malloc_shm_t */
24struct xc_malloc_shm_t {
25    xc_shm_vtable_t *vtable;
26    xc_shmsize_t     size;
27    xc_shmsize_t     memoffset;
[1135]28#ifndef TEST
[386]29    HashTable blocks;
[1135]30#endif
[386]31};
32/* }}} */
33
[1135]34#ifndef TEST
35#   define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
36#else
37#   define CHECK(x, e) do { if ((x) == NULL) { fprintf(stderr, "%s\n", "XCache: " e); goto err; } } while (0)
38#endif
[148]39
[1135]40static void *xc_add_to_blocks(xc_allocator_t *allocator, void *p, size_t size) /* {{{ */
[386]41{
42    if (p) {
[1135]43        allocator->avail -= size;
[1392]44#ifndef TEST
[1135]45        zend_hash_add(&allocator->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
46#endif
[386]47    }
48    return p;
49}
50/* }}} */
[1135]51static void xc_del_from_blocks(xc_allocator_t *allocator, void *p) /* {{{ */
[386]52{
[1135]53#ifdef TEST
54    /* TODO: allocator->avail += size; */
55#else
56    zend_hash_del(&allocator->shm->blocks, (void *) &p, sizeof(p));
57#endif
[386]58}
59/* }}} */
60
[1135]61static XC_ALLOCATOR_MALLOC(xc_allocator_malloc_malloc) /* {{{ */
[148]62{
[1392]63    if (allocator->avail < size) {
64        return NULL;
65    }
[1135]66    return xc_add_to_blocks(allocator, malloc(size), size);
[148]67}
68/* }}} */
[1135]69static XC_ALLOCATOR_FREE(xc_allocator_malloc_free) /* {{{ return block size freed */
[148]70{
[1135]71    xc_del_from_blocks(allocator, (void *) p);
[148]72    free((void *) p);
73    return 0;
74}
75/* }}} */
[1135]76static XC_ALLOCATOR_CALLOC(xc_allocator_malloc_calloc) /* {{{ */
[148]77{
[1392]78    if (allocator->avail < size) {
79        return NULL;
80    }
[1135]81    return xc_add_to_blocks(allocator, calloc(memb, size), size);
[148]82}
83/* }}} */
[1135]84static XC_ALLOCATOR_REALLOC(xc_allocator_malloc_realloc) /* {{{ */
[148]85{
[1135]86    return xc_add_to_blocks(allocator, realloc((void *) p, size), size);
[148]87}
88/* }}} */
89
[1135]90static XC_ALLOCATOR_AVAIL(xc_allocator_malloc_avail) /* {{{ */
[148]91{
[1135]92    return allocator->avail;
[148]93}
94/* }}} */
[1135]95static XC_ALLOCATOR_SIZE(xc_allocator_malloc_size) /* {{{ */
[148]96{
[1135]97    return allocator->size;
[148]98}
99/* }}} */
100
[1135]101static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_malloc_freeblock_first) /* {{{ */
[148]102{
103    return (void *) -1;
104}
105/* }}} */
[1135]106static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_malloc_freeblock_next) /* {{{ */
[148]107{
108    return NULL;
109}
110/* }}} */
[1135]111static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_malloc_block_size) /* {{{ */
[148]112{
113    return 0;
114}
115/* }}} */
[1135]116static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_malloc_block_offset) /* {{{ */
[148]117{
118    return 0;
119}
120/* }}} */
121
[1135]122static XC_ALLOCATOR_INIT(xc_allocator_malloc_init) /* {{{ */
[148]123{
[1135]124#define MINSIZE (ALIGN(sizeof(xc_allocator_t)))
[148]125    /* requires at least the header and 1 tail block */
126    if (size < MINSIZE) {
[1135]127        fprintf(stderr, "xc_allocator_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
[148]128        return NULL;
129    }
[1135]130    allocator->shm = shm;
131    allocator->size = size;
132    allocator->avail = size - MINSIZE;
[148]133#undef MINSIZE
134
[1135]135    return allocator;
[148]136}
137/* }}} */
[1135]138static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */
[148]139{
140}
141/* }}} */
142
[1468]143static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
[148]144{
[1468]145#ifndef TEST
146    zend_hash_destroy(&shm->blocks);
147#endif
148    free(shm);
149    return;
[148]150}
151/* }}} */
[1468]152static XC_SHM_INIT(xc_malloc_init) /* {{{ */
153{
154    xc_shm_t *shm;
155    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
156    shm->size = size;
157
158#ifndef TEST
159    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
160#endif
161    return shm;
162err:
163    return NULL;
164}
165/* }}} */
166
[148]167static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
168{
[1135]169#ifndef TEST
[386]170    HashPosition pos;
171    size_t *psize;
172    char **ptr;
[1468]173    HashTable *blocks = &((xc_shm_t *) shm)->blocks;
[386]174
[1468]175    zend_hash_internal_pointer_reset_ex(blocks, &pos);
176    while (zend_hash_get_current_data_ex(blocks, (void **) &psize, &pos) == SUCCESS) {
177        zend_hash_get_current_key_ex(blocks, (void *) &ptr, NULL, NULL, 0, &pos);
[386]178        if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
179            return 1;
180        }
[1468]181        zend_hash_move_forward_ex(blocks, &pos);
[386]182    }
[1135]183#endif
[386]184
[148]185    return 0;
186}
187/* }}} */
188static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
189{
190    return 0;
191}
192/* }}} */
193
194static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
195{
[1135]196    void *mem;
[148]197    if (shm->memoffset + size > shm->size) {
[1135]198#ifndef TEST
[148]199        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
[1135]200#endif
[148]201        return NULL;
202    }
203    shm->memoffset += size;
[1135]204    CHECK(mem = calloc(1, size), "mem OOM");
[148]205    return mem;
206err:
207    return NULL;
208}
209/* }}} */
210static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
211{
212    free(mem);
213}
214/* }}} */
215
[1135]216static xc_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc);
[1391]217#ifndef TEST
[1468]218static xc_shm_vtable_t xc_shm_malloc_vtable = XC_SHM_VTABLE(malloc);
[1391]219#endif
[1135]220void xc_allocator_malloc_register() /* {{{ */
[148]221{
[1135]222    if (xc_allocator_register("malloc", &xc_allocator_malloc_vtable) == 0) {
223#ifndef TEST
[148]224        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
[1135]225#endif
[148]226    }
[1135]227}
228/* }}} */
[148]229
[1135]230#ifndef TEST
231void xc_shm_malloc_register() /* {{{ */
232{
[1468]233    if (xc_shm_scheme_register("malloc", &xc_shm_malloc_vtable) == 0) {
[148]234        zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
235    }
236}
237/* }}} */
[1135]238#endif
Note: See TracBrowser for help on using the repository browser.