source: trunk/xc_malloc.c @ 386

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

fix is_readwrite of malloc scheme

  • Property svn:eol-style set to native
File size: 5.2 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    HashPosition pos;
147    size_t *psize;
148    char **ptr;
149
150    zend_hash_internal_pointer_reset_ex(&shm->blocks, &pos);
151    while (zend_hash_get_current_data_ex(&shm->blocks, (void **) &psize, &pos) == SUCCESS) {
152        zend_hash_get_current_key_ex(&shm->blocks, (void *) &ptr, NULL, NULL, 0, &pos);
153        if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
154            return 1;
155        }
156        zend_hash_move_forward_ex(&shm->blocks, &pos);
157    }
158
159    return 0;
160}
161/* }}} */
162static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
163{
164    return 0;
165}
166/* }}} */
167static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
168{
169    return p;
170}
171/* }}} */
172static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
173{
174    return p;
175}
176/* }}} */
177
178static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
179{
180    free(shm);
181#ifdef HAVE_XCACHE_TEST
182    zend_hash_destroy(&shm->blocks);
183#endif
184    return;
185}
186/* }}} */
187static XC_SHM_INIT(xc_malloc_init) /* {{{ */
188{
189    xc_shm_t *shm;
190    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
191    shm->size = size;
192
193#ifdef HAVE_XCACHE_TEST
194    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
195#endif
196    return shm;
197err:
198    return NULL;
199}
200/* }}} */
201
202static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
203{
204    xc_mem_t *mem;
205    if (shm->memoffset + size > shm->size) {
206        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
207        return NULL;
208    }
209    shm->memoffset += size;
210    CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
211    mem->handlers = shm->handlers->memhandlers;
212    mem->handlers->init(shm, mem, size);
213    return mem;
214err:
215    return NULL;
216}
217/* }}} */
218static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
219{
220    mem->handlers->destroy(mem);
221    free(mem);
222}
223/* }}} */
224
225#define xc_malloc_destroy xc_mem_malloc_destroy
226#define xc_malloc_init xc_mem_malloc_init
227static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
228#undef xc_malloc_init
229#undef xc_malloc_destroy
230static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
231void xc_shm_malloc_register() /* {{{ */
232{
233    if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
234        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
235    }
236
237    CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
238    if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
239        zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
240    }
241err:
242    return;
243}
244/* }}} */
Note: See TracBrowser for help on using the repository browser.