source: branches/1.2/xc_malloc.c @ 479

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

scheme=malloc was broken in [465]

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