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