source: branches/1.3/xc_malloc.c @ 593

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

merged r304,r305,r306,r307 from trunk

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