source: branches/1.3/xc_malloc.c @ 602

Last change on this file since 602 was 602, checked in by moo, 5 years ago

merged r363,r366,r376,r381,r382,r383,r384,r385,r386,r387 from trunk

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