source: trunk/xc_malloc.c @ 381

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

merged [380] from 1.2: make life easier for debugging

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