| 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 | #include "utils.h" |
|---|
| 10 | |
|---|
| 11 | struct _xc_mem_t { |
|---|
| 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 | |
|---|
| 20 | static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */ |
|---|
| 21 | { |
|---|
| 22 | return malloc(size); |
|---|
| 23 | } |
|---|
| 24 | /* }}} */ |
|---|
| 25 | static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */ |
|---|
| 26 | { |
|---|
| 27 | free((void *) p); |
|---|
| 28 | return 0; |
|---|
| 29 | } |
|---|
| 30 | /* }}} */ |
|---|
| 31 | static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */ |
|---|
| 32 | { |
|---|
| 33 | return calloc(memb, size); |
|---|
| 34 | } |
|---|
| 35 | /* }}} */ |
|---|
| 36 | static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */ |
|---|
| 37 | { |
|---|
| 38 | return realloc((void *) p, size); |
|---|
| 39 | } |
|---|
| 40 | /* }}} */ |
|---|
| 41 | static 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 | /* }}} */ |
|---|
| 50 | static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */ |
|---|
| 51 | { |
|---|
| 52 | return xc_malloc_strndup(mem, str, strlen(str) + 1); |
|---|
| 53 | } |
|---|
| 54 | /* }}} */ |
|---|
| 55 | |
|---|
| 56 | static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */ |
|---|
| 57 | { |
|---|
| 58 | return mem->avail; |
|---|
| 59 | } |
|---|
| 60 | /* }}} */ |
|---|
| 61 | static XC_MEM_SIZE(xc_malloc_size) /* {{{ */ |
|---|
| 62 | { |
|---|
| 63 | return mem->size; |
|---|
| 64 | } |
|---|
| 65 | /* }}} */ |
|---|
| 66 | |
|---|
| 67 | static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */ |
|---|
| 68 | { |
|---|
| 69 | return (void *) -1; |
|---|
| 70 | } |
|---|
| 71 | /* }}} */ |
|---|
| 72 | XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */ |
|---|
| 73 | { |
|---|
| 74 | return NULL; |
|---|
| 75 | } |
|---|
| 76 | /* }}} */ |
|---|
| 77 | XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */ |
|---|
| 78 | { |
|---|
| 79 | return 0; |
|---|
| 80 | } |
|---|
| 81 | /* }}} */ |
|---|
| 82 | XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */ |
|---|
| 83 | { |
|---|
| 84 | return 0; |
|---|
| 85 | } |
|---|
| 86 | /* }}} */ |
|---|
| 87 | |
|---|
| 88 | static 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 | /* }}} */ |
|---|
| 104 | static XC_MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */ |
|---|
| 105 | { |
|---|
| 106 | } |
|---|
| 107 | /* }}} */ |
|---|
| 108 | |
|---|
| 109 | /* {{{ xc_shm_t */ |
|---|
| 110 | struct _xc_shm_t { |
|---|
| 111 | xc_shm_handlers_t *handlers; |
|---|
| 112 | xc_shmsize_t size; |
|---|
| 113 | xc_shmsize_t memoffset; |
|---|
| 114 | }; |
|---|
| 115 | /* }}} */ |
|---|
| 116 | |
|---|
| 117 | static XC_SHM_CAN_READONLY(xc_malloc_can_readonly) /* {{{ */ |
|---|
| 118 | { |
|---|
| 119 | return 0; |
|---|
| 120 | } |
|---|
| 121 | /* }}} */ |
|---|
| 122 | static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */ |
|---|
| 123 | { |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | /* }}} */ |
|---|
| 127 | static XC_SHM_IS_READONLY(xc_malloc_is_readonly) /* {{{ */ |
|---|
| 128 | { |
|---|
| 129 | return 0; |
|---|
| 130 | } |
|---|
| 131 | /* }}} */ |
|---|
| 132 | static XC_SHM_TO_READWRITE(xc_malloc_to_readwrite) /* {{{ */ |
|---|
| 133 | { |
|---|
| 134 | return p; |
|---|
| 135 | } |
|---|
| 136 | /* }}} */ |
|---|
| 137 | static XC_SHM_TO_READONLY(xc_malloc_to_readonly) /* {{{ */ |
|---|
| 138 | { |
|---|
| 139 | return p; |
|---|
| 140 | } |
|---|
| 141 | /* }}} */ |
|---|
| 142 | |
|---|
| 143 | static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */ |
|---|
| 144 | { |
|---|
| 145 | free(shm); |
|---|
| 146 | return; |
|---|
| 147 | } |
|---|
| 148 | /* }}} */ |
|---|
| 149 | static 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; |
|---|
| 156 | err: |
|---|
| 157 | return NULL; |
|---|
| 158 | } |
|---|
| 159 | /* }}} */ |
|---|
| 160 | |
|---|
| 161 | static 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; |
|---|
| 173 | err: |
|---|
| 174 | return NULL; |
|---|
| 175 | } |
|---|
| 176 | /* }}} */ |
|---|
| 177 | static 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 |
|---|
| 186 | static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc); |
|---|
| 187 | #undef xc_malloc_init |
|---|
| 188 | #undef xc_malloc_destroy |
|---|
| 189 | static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc); |
|---|
| 190 | void 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 | } |
|---|
| 200 | err: |
|---|
| 201 | return; |
|---|
| 202 | } |
|---|
| 203 | /* }}} */ |
|---|