source: trunk/xcache/xc_malloc.c @ 1129

Last change on this file since 1129 was 1129, checked in by moo, 21 months ago

refactor: code clean up

  • Property svn:eol-style set to native
File size: 5.2 KB
RevLine 
[381]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>
[478]6
7#ifdef HAVE_CONFIG_H
8#include <config.h>
9#endif
[148]10#include "xc_shm.h"
11#include "php.h"
[982]12#include "xc_utils.h"
13#include "util/xc_align.h"
[148]14
[381]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
[386]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    HashTable blocks;
28};
29/* }}} */
30
[148]31#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)
32
[386]33static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
34{
35    if (p) {
36        zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
37    }
38    return p;
39}
40/* }}} */
41static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
42{
43    zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
44}
45/* }}} */
46
[148]47static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
48{
[386]49    return xc_add_to_blocks(mem, malloc(size), size);
[148]50}
51/* }}} */
52static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
53{
[386]54    xc_del_from_blocks(mem, (void *) p);
[148]55    free((void *) p);
56    return 0;
57}
58/* }}} */
59static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
60{
[386]61    return xc_add_to_blocks(mem, calloc(memb, size), size);
[148]62}
63/* }}} */
64static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
65{
[386]66    return xc_add_to_blocks(mem, realloc((void *) p, size), size);
[148]67}
68/* }}} */
69static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
70{
[386]71    char *p = xc_add_to_blocks(mem, malloc(len), len);
[148]72    if (!p) {
73        return NULL;
74    }
75    return memcpy(p, str, len);
76}
77/* }}} */
78static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
79{
80    return xc_malloc_strndup(mem, str, strlen(str) + 1);
81}
82/* }}} */
83
84static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
85{
86    return mem->avail;
87}
88/* }}} */
89static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
90{
91    return mem->size;
92}
93/* }}} */
94
95static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
96{
97    return (void *) -1;
98}
99/* }}} */
100XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
101{
102    return NULL;
103}
104/* }}} */
105XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
106{
107    return 0;
108}
109/* }}} */
110XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
111{
112    return 0;
113}
114/* }}} */
115
116static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
117{
118#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
119    /* requires at least the header and 1 tail block */
120    if (size < MINSIZE) {
[391]121        fprintf(stderr, "xc_mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
[148]122        return NULL;
123    }
124    mem->shm = shm;
125    mem->size = size;
126    mem->avail = size - MINSIZE;
127#undef MINSIZE
128
129    return mem;
130}
131/* }}} */
132static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
133{
134}
135/* }}} */
136
137static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
138{
139    return 0;
140}
141/* }}} */
142static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
143{
[386]144    HashPosition pos;
145    size_t *psize;
146    char **ptr;
147
148    zend_hash_internal_pointer_reset_ex(&shm->blocks, &pos);
149    while (zend_hash_get_current_data_ex(&shm->blocks, (void **) &psize, &pos) == SUCCESS) {
150        zend_hash_get_current_key_ex(&shm->blocks, (void *) &ptr, NULL, NULL, 0, &pos);
151        if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
152            return 1;
153        }
154        zend_hash_move_forward_ex(&shm->blocks, &pos);
155    }
156
[148]157    return 0;
158}
159/* }}} */
160static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
161{
162    return 0;
163}
164/* }}} */
165static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
166{
167    return p;
168}
169/* }}} */
170static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
171{
172    return p;
173}
174/* }}} */
175
176static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
177{
[386]178    zend_hash_destroy(&shm->blocks);
[387]179    free(shm);
[148]180    return;
181}
182/* }}} */
183static XC_SHM_INIT(xc_malloc_init) /* {{{ */
184{
185    xc_shm_t *shm;
186    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
187    shm->size = size;
188
[386]189    zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
[148]190    return shm;
191err:
192    return NULL;
193}
194/* }}} */
195
196static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
197{
198    xc_mem_t *mem;
199    if (shm->memoffset + size > shm->size) {
200        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
201        return NULL;
202    }
203    shm->memoffset += size;
204    CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
205    mem->handlers = shm->handlers->memhandlers;
206    mem->handlers->init(shm, mem, size);
207    return mem;
208err:
209    return NULL;
210}
211/* }}} */
212static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
213{
214    mem->handlers->destroy(mem);
215    free(mem);
216}
217/* }}} */
218
219#define xc_malloc_destroy xc_mem_malloc_destroy
220#define xc_malloc_init xc_mem_malloc_init
221static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
222#undef xc_malloc_init
223#undef xc_malloc_destroy
224static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
225void xc_shm_malloc_register() /* {{{ */
226{
227    if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
228        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
229    }
230
231    CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
232    if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
233        zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
234    }
235err:
236    return;
237}
238/* }}} */
Note: See TracBrowser for help on using the repository browser.