source: trunk/xcache/xc_malloc.c @ 1392

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

calc avail for test

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1#define XC_SHM_IMPL _xc_malloc_shm_t
2#define _xc_allocator_t _xc_allocator_malloc_t
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6
7#include "xc_shm.h"
8#include "xc_allocator.h"
9#ifndef TEST
10#include "xcache.h"
11#endif
12#include "util/xc_align.h"
13
14struct _xc_allocator_malloc_t {
15    const xc_allocator_vtable_t *vtable;
16    xc_shm_t *shm;
17    xc_memsize_t size;
18    xc_memsize_t avail;       /* total free */
19};
20
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;
26#ifndef TEST
27    HashTable blocks;
28#endif
29};
30/* }}} */
31
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
37
38static void *xc_add_to_blocks(xc_allocator_t *allocator, void *p, size_t size) /* {{{ */
39{
40    if (p) {
41        allocator->avail -= size;
42#ifndef TEST
43        zend_hash_add(&allocator->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
44#endif
45    }
46    return p;
47}
48/* }}} */
49static void xc_del_from_blocks(xc_allocator_t *allocator, void *p) /* {{{ */
50{
51#ifdef TEST
52    /* TODO: allocator->avail += size; */
53#else
54    zend_hash_del(&allocator->shm->blocks, (void *) &p, sizeof(p));
55#endif
56}
57/* }}} */
58
59static XC_ALLOCATOR_MALLOC(xc_allocator_malloc_malloc) /* {{{ */
60{
61    if (allocator->avail < size) {
62        return NULL;
63    }
64    return xc_add_to_blocks(allocator, malloc(size), size);
65}
66/* }}} */
67static XC_ALLOCATOR_FREE(xc_allocator_malloc_free) /* {{{ return block size freed */
68{
69    xc_del_from_blocks(allocator, (void *) p);
70    free((void *) p);
71    return 0;
72}
73/* }}} */
74static XC_ALLOCATOR_CALLOC(xc_allocator_malloc_calloc) /* {{{ */
75{
76    if (allocator->avail < size) {
77        return NULL;
78    }
79    return xc_add_to_blocks(allocator, calloc(memb, size), size);
80}
81/* }}} */
82static XC_ALLOCATOR_REALLOC(xc_allocator_malloc_realloc) /* {{{ */
83{
84    return xc_add_to_blocks(allocator, realloc((void *) p, size), size);
85}
86/* }}} */
87
88static XC_ALLOCATOR_AVAIL(xc_allocator_malloc_avail) /* {{{ */
89{
90    return allocator->avail;
91}
92/* }}} */
93static XC_ALLOCATOR_SIZE(xc_allocator_malloc_size) /* {{{ */
94{
95    return allocator->size;
96}
97/* }}} */
98
99static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_malloc_freeblock_first) /* {{{ */
100{
101    return (void *) -1;
102}
103/* }}} */
104static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_malloc_freeblock_next) /* {{{ */
105{
106    return NULL;
107}
108/* }}} */
109static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_malloc_block_size) /* {{{ */
110{
111    return 0;
112}
113/* }}} */
114static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_malloc_block_offset) /* {{{ */
115{
116    return 0;
117}
118/* }}} */
119
120static XC_ALLOCATOR_INIT(xc_allocator_malloc_init) /* {{{ */
121{
122#define MINSIZE (ALIGN(sizeof(xc_allocator_t)))
123    /* requires at least the header and 1 tail block */
124    if (size < MINSIZE) {
125        fprintf(stderr, "xc_allocator_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
126        return NULL;
127    }
128    allocator->shm = shm;
129    allocator->size = size;
130    allocator->avail = size - MINSIZE;
131#undef MINSIZE
132
133    return allocator;
134}
135/* }}} */
136static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */
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{
148#ifndef TEST
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    }
161#endif
162
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{
184#ifndef TEST
185    zend_hash_destroy(&shm->blocks);
186#endif
187    free(shm);
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
197#ifndef TEST
198    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
199#endif
200    return shm;
201err:
202    return NULL;
203}
204/* }}} */
205
206static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
207{
208    void *mem;
209    if (shm->memoffset + size > shm->size) {
210#ifndef TEST
211        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
212#endif
213        return NULL;
214    }
215    shm->memoffset += size;
216    CHECK(mem = calloc(1, size), "mem OOM");
217    return mem;
218err:
219    return NULL;
220}
221/* }}} */
222static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
223{
224    free(mem);
225}
226/* }}} */
227
228static xc_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc);
229#ifndef TEST
230static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
231#endif
232void xc_allocator_malloc_register() /* {{{ */
233{
234    if (xc_allocator_register("malloc", &xc_allocator_malloc_vtable) == 0) {
235#ifndef TEST
236        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
237#endif
238    }
239}
240/* }}} */
241
242#ifndef TEST
243void xc_shm_malloc_register() /* {{{ */
244{
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/* }}} */
250#endif
Note: See TracBrowser for help on using the repository browser.