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