source: trunk/xcache/xc_malloc.c @ 1135

Last change on this file since 1135 was 1135, checked in by moo, 2 years ago

refactor: s/mem/allocator/

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