File: xc_malloc.c

package info (click to toggle)
xcache 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,724 kB
  • sloc: ansic: 8,175; php: 4,557; awk: 285; sh: 135; makefile: 75
file content (250 lines) | stat: -rwxr-xr-x 5,467 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#define XC_SHM_IMPL _xc_malloc_shm_t
#define XC_MEM_IMPL _xc_malloc_mem_t
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "xc_shm.h"
#include "php.h"
#include "align.h"
#include "utils.h"

struct _xc_malloc_mem_t {
	const xc_mem_handlers_t *handlers;
	xc_shm_t                *shm;
	xc_memsize_t size;
	xc_memsize_t avail;       /* total free */
};

/* {{{ _xc_malloc_shm_t */
struct _xc_malloc_shm_t {
	xc_shm_handlers_t *handlers;
	xc_shmsize_t       size;
	xc_shmsize_t       memoffset;
#ifdef HAVE_XCACHE_TEST
	HashTable blocks;
#endif
};
/* }}} */

#define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0)

static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */
{
#ifdef HAVE_XCACHE_TEST
	if (p) {
		zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL);
	}
#endif
	return p;
}
/* }}} */
static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */
{
#ifdef HAVE_XCACHE_TEST
	zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p));
#endif
}
/* }}} */

static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */
{
	return xc_add_to_blocks(mem, malloc(size), size);
}
/* }}} */
static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */
{
	xc_del_from_blocks(mem, (void *) p);
	free((void *) p);
	return 0;
}
/* }}} */
static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */
{
	return xc_add_to_blocks(mem, calloc(memb, size), size);
}
/* }}} */
static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */
{
	return xc_add_to_blocks(mem, realloc((void *) p, size), size);
}
/* }}} */
static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */
{
	char *p = xc_add_to_blocks(mem, malloc(len), len);
	if (!p) {
		return NULL;
	}
	return memcpy(p, str, len);
}
/* }}} */
static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */
{
	return xc_malloc_strndup(mem, str, strlen(str) + 1);
}
/* }}} */

static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */
{
	return mem->avail;
}
/* }}} */
static XC_MEM_SIZE(xc_malloc_size) /* {{{ */
{
	return mem->size;
}
/* }}} */

static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */
{
	return (void *) -1;
}
/* }}} */
XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */
{
	return NULL;
}
/* }}} */
XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */
{
	return 0;
}
/* }}} */
XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */
{
	return 0;
}
/* }}} */

static XC_MEM_INIT(xc_mem_malloc_init) /* {{{ */
{
#define MINSIZE (ALIGN(sizeof(xc_mem_t)))
	/* requires at least the header and 1 tail block */
	if (size < MINSIZE) {
		fprintf(stderr, "xc_mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);
		return NULL;
	}
	mem->shm = shm;
	mem->size = size;
	mem->avail = size - MINSIZE;
#undef MINSIZE

	return mem;
}
/* }}} */
static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */
{
}
/* }}} */

static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */
{
	return 0;
}
/* }}} */
static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */
{
#ifdef HAVE_XCACHE_TEST
	HashPosition pos;
	size_t *psize;
	char **ptr;

	zend_hash_internal_pointer_reset_ex(&shm->blocks, &pos);
	while (zend_hash_get_current_data_ex(&shm->blocks, (void **) &psize, &pos) == SUCCESS) {
		zend_hash_get_current_key_ex(&shm->blocks, (void *) &ptr, NULL, NULL, 0, &pos);
		if ((char *) p >= *ptr && (char *) p < *ptr + *psize) {
			return 1;
		}
		zend_hash_move_forward_ex(&shm->blocks, &pos);
	}
#endif

	return 0;
}
/* }}} */
static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */
{
	return 0;
}
/* }}} */
static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */
{
	return p;
}
/* }}} */
static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */
{
	return p;
}
/* }}} */

static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */
{
#ifdef HAVE_XCACHE_TEST
	zend_hash_destroy(&shm->blocks);
#endif
	free(shm);
	return;
}
/* }}} */
static XC_SHM_INIT(xc_malloc_init) /* {{{ */
{
	xc_shm_t *shm;
	CHECK(shm = calloc(1, sizeof(xc_shm_t)), "shm OOM");
	shm->size = size;

#ifdef HAVE_XCACHE_TEST
	zend_hash_init(&shm->blocks, 64, NULL, NULL, 1);
#endif
	return shm;
err:
	return NULL;
}
/* }}} */

static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */
{
	xc_mem_t *mem;
	if (shm->memoffset + size > shm->size) {
		zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__);
		return NULL;
	}
	shm->memoffset += size;
	CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM");
	mem->handlers = shm->handlers->memhandlers;
	mem->handlers->init(shm, mem, size);
	return mem;
err:
	return NULL;
}
/* }}} */
static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */
{
	mem->handlers->destroy(mem);
	free(mem);
}
/* }}} */

#define xc_malloc_destroy xc_mem_malloc_destroy
#define xc_malloc_init xc_mem_malloc_init
static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc);
#undef xc_malloc_init
#undef xc_malloc_destroy
static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc);
void xc_shm_malloc_register() /* {{{ */
{
	if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {
		zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");
	}

	CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");
	if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) {
		zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme");
	}
err:
	return;
}
/* }}} */