source: trunk/xcache/xc_malloc.c @ 1391

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

fix xtest

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