source: trunk/xcache/xc_malloc.c @ 1392

Last change on this file since 1392 was 1392, checked in by moo, 11 months ago

calc avail for test

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