source: branches/1.2/xc_malloc.c @ 392

Last change on this file since 392 was 392, checked in by moo, 8 years ago

merged 391 from trunk->1.2: fixed #96, wrong format string for sprintf

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