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