source: trunk/xc_malloc.c @ 390

Last change on this file since 390 was 390, checked in by moo, 7 years ago

fix build error without --enable-xcache-test

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1#define XC_SHM_IMPL _xc_malloc_shm_t
2#define XC_MEM_IMPL _xc_malloc_mem_t
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include "xc_shm.h"
7#include "php.h"
8#include "align.h"
9#include "utils.h"
10
11struct _xc_malloc_mem_t {
12    const xc_mem_handlers_t *handlers;
13    xc_shm_t                *shm;
14    xc_memsize_t size;
15    xc_memsize_t avail;       /* total free */
16};
17
18/* {{{ _xc_malloc_shm_t */
19struct _xc_malloc_shm_t {
20    xc_shm_handlers_t *handlers;
21    xc_shmsize_t       size;
22    xc_shmsize_t       memoffset;
23#ifdef HAVE_XCACHE_TEST
24    HashTable blocks;
25#endif
26};
27/* }}} */
28
29#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
30
31static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
32{
33#ifdef HAVE_XCACHE_TEST
34    if (p) {
35        zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
36    }
37#endif
38    return p;
39}
40/* }}} */
41static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
42{
43#ifdef HAVE_XCACHE_TEST
44    zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
45#endif
46}
47/* }}} */
48
49static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
50{
51    return xc_add_to_blocks(mem, malloc(size), size);
52}
53/* }}} */
54static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
55{
56    xc_del_from_blocks(mem, (void *) p);
57    free((void *) p);
58    return 0;
59}
60/* }}} */
61static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
62{
63    return xc_add_to_blocks(mem, calloc(memb, size), size);
64}
65/* }}} */
66static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
67{
68    return xc_add_to_blocks(mem, realloc((void *) p, size), size);
69}
70/* }}} */
71static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
72{
73    char *p = xc_add_to_blocks(mem, malloc(len), len);
74    if (!p) {
75        return NULL;
76    }
77    return memcpy(p, str, len);
78}
79/* }}} */
80static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
81{
82    return xc_malloc_strndup(mem, str, strlen(str) + 1);
83}
84/* }}} */
85
86static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
87{
88    return mem->avail;
89}
90/* }}} */
91static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
92{
93    return mem->size;
94}
95/* }}} */
96
97static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
98{
99    return (void *) -1;
100}
101/* }}} */
102XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
103{
104    return NULL;
105}
106/* }}} */
107XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
108{
109    return 0;
110}
111/* }}} */
112XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
113{
114    return 0;
115}
116/* }}} */
117
118static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
119{
120#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
121    /* requires at least the header and 1 tail block */
122    if (size < MINSIZE) {
123        fprintf(stderr, "xc_mem_malloc_init requires %d bytes at least\n", MINSIZE);
124        return NULL;
125    }
126    mem->shm = shm;
127    mem->size = size;
128    mem->avail = size - MINSIZE;
129#undef MINSIZE
130
131    return mem;
132}
133/* }}} */
134static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
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{
146#ifdef HAVE_XCACHE_TEST
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    }
159#endif
160
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{
182#ifdef HAVE_XCACHE_TEST
183    zend_hash_destroy(&shm->blocks);
184#endif
185    free(shm);
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
195#ifdef HAVE_XCACHE_TEST
196    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
197#endif
198    return shm;
199err:
200    return NULL;
201}
202/* }}} */
203
204static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
205{
206    xc_mem_t *mem;
207    if (shm->memoffset + size > shm->size) {
208        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
209        return NULL;
210    }
211    shm->memoffset += size;
212    CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
213    mem->handlers = shm->handlers->memhandlers;
214    mem->handlers->init(shm, mem, size);
215    return mem;
216err:
217    return NULL;
218}
219/* }}} */
220static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
221{
222    mem->handlers->destroy(mem);
223    free(mem);
224}
225/* }}} */
226
227#define xc_malloc_destroy xc_mem_malloc_destroy
228#define xc_malloc_init xc_mem_malloc_init
229static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
230#undef xc_malloc_init
231#undef xc_malloc_destroy
232static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
233void xc_shm_malloc_register() /* {{{ */
234{
235    if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
236        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
237    }
238
239    CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
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    }
243err:
244    return;
245}
246/* }}} */
Note: See TracBrowser for help on using the repository browser.