source: trunk/xc_malloc.c @ 148

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

reconstruct shm/allocator

  • Property svn:eol-style set to native
File size: 4.1 KB
RevLine 
[148]1#define XC_SHM_IMPL
2#define XC_MEM_IMPL
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_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    xc_block_t *b;
90
91#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
92    /* requires at least the header and 1 tail block */
93    if (size < MINSIZE) {
94        fprintf(stderr, "xc_mem_malloc_init requires %d bytes at least\n", MINSIZE);
95        return NULL;
96    }
97    mem->shm = shm;
98    mem->size = size;
99    mem->avail = size - MINSIZE;
100#undef MINSIZE
101
102    return mem;
103}
104/* }}} */
105static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
106{
107}
108/* }}} */
109
110// {{{ xc_shm_t
111struct _xc_shm_t {
112    xc_shm_handlers_t *handlers;
113    xc_shmsize_t       size;
114    xc_shmsize_t       memoffset;
115};
116
117#undef NDEBUG
118#ifdef ALLOC_DEBUG
119#   define inline
120#else
121#   define NDEBUG
122#endif
123#include <assert.h>
124/* }}} */
125
126static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
127{
128    return 0;
129}
130/* }}} */
131static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
132{
133    return 0;
134}
135/* }}} */
136static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
137{
138    return 0;
139}
140/* }}} */
141static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
142{
143    return p;
144}
145/* }}} */
146static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
147{
148    return p;
149}
150/* }}} */
151
152static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
153{
154    free(shm);
155    return;
156}
157/* }}} */
158static XC_SHM_INIT(xc_malloc_init) /* {{{ */
159{
160    xc_shm_t *shm;
161    CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
162    shm->size = size;
163
164    return shm;
165err:
166    return NULL;
167}
168/* }}} */
169
170static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
171{
172    xc_mem_t *mem;
173    if (shm->memoffset + size > shm->size) {
174        zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
175        return NULL;
176    }
177    shm->memoffset += size;
178    CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
179    mem->handlers = shm->handlers->memhandlers;
180    mem->handlers->init(shm, mem, size);
181    return mem;
182err:
183    return NULL;
184}
185/* }}} */
186static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
187{
188    mem->handlers->destroy(mem);
189    free(mem);
190}
191/* }}} */
192
193#define xc_malloc_destroy xc_mem_malloc_destroy
194#define xc_malloc_init xc_mem_malloc_init
195static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
196#undef xc_malloc_init
197#undef xc_malloc_destroy
198static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
199void xc_shm_malloc_register() /* {{{ */
200{
201    if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
202        zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
203    }
204
205    CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
206    if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
207        zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
208    }
209err:
210    return;
211}
212/* }}} */
Note: See TracBrowser for help on using the repository browser.