Changeset 1135 for trunk/xcache/xc_malloc.c
- Timestamp:
- 2012-08-09T11:04:02+02:00 (10 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
xcache/xc_malloc.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
trunk/xcache/xc_malloc.c
r1129 r1135 1 1 #define XC_SHM_IMPL _xc_malloc_shm_t 2 #define XC_MEM_IMPL _xc_malloc_mem_t2 #define _xc_allocator_t _xc_allocator_malloc_t 3 3 #include <stdlib.h> 4 4 #include <stdio.h> … … 9 9 #endif 10 10 #include "xc_shm.h" 11 #include "php.h" 12 #include "xc_utils.h" 11 #include "xc_allocator.h" 12 #ifndef TEST 13 #include "xcache.h" 14 #endif 13 15 #include "util/xc_align.h" 14 16 15 struct _xc_ malloc_mem_t {16 const xc_ mem_handlers_t *handlers;17 xc_shm_t *shm;17 struct _xc_allocator_malloc_t { 18 const xc_allocator_vtable_t *vtable; 19 xc_shm_t *shm; 18 20 xc_memsize_t size; 19 21 xc_memsize_t avail; /* total free */ … … 25 27 xc_shmsize_t size; 26 28 xc_shmsize_t memoffset; 29 #ifndef TEST 27 30 HashTable blocks; 31 #endif 28 32 }; 29 33 /* }}} */ 30 34 31 #define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) 32 33 static void *xc_add_to_blocks(xc_mem_t *mem, void *p, size_t size) /* {{{ */ 35 #ifndef TEST 36 # define CHECK(x, e) do { if ((x) == NULL) { zend_error(E_ERROR, "XCache: " e); goto err; } } while (0) 37 #else 38 # define CHECK(x, e) do { if ((x) == NULL) { fprintf(stderr, "%s\n", "XCache: " e); goto err; } } while (0) 39 #endif 40 41 static void *xc_add_to_blocks(xc_allocator_t *allocator, void *p, size_t size) /* {{{ */ 34 42 { 35 43 if (p) { 36 zend_hash_add(&mem->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL); 44 #ifdef TEST 45 allocator->avail -= size; 46 #else 47 zend_hash_add(&allocator->shm->blocks, (void *) &p, sizeof(p), (void *) &size, sizeof(size), NULL); 48 #endif 37 49 } 38 50 return p; 39 51 } 40 52 /* }}} */ 41 static void xc_del_from_blocks(xc_mem_t *mem, void *p) /* {{{ */ 42 { 43 zend_hash_del(&mem->shm->blocks, (void *) &p, sizeof(p)); 44 } 45 /* }}} */ 46 47 static XC_MEM_MALLOC(xc_malloc_malloc) /* {{{ */ 48 { 49 return xc_add_to_blocks(mem, malloc(size), size); 50 } 51 /* }}} */ 52 static XC_MEM_FREE(xc_malloc_free) /* {{{ return block size freed */ 53 { 54 xc_del_from_blocks(mem, (void *) p); 53 static void xc_del_from_blocks(xc_allocator_t *allocator, void *p) /* {{{ */ 54 { 55 #ifdef TEST 56 /* TODO: allocator->avail += size; */ 57 #else 58 zend_hash_del(&allocator->shm->blocks, (void *) &p, sizeof(p)); 59 #endif 60 } 61 /* }}} */ 62 63 static XC_ALLOCATOR_MALLOC(xc_allocator_malloc_malloc) /* {{{ */ 64 { 65 return xc_add_to_blocks(allocator, malloc(size), size); 66 } 67 /* }}} */ 68 static XC_ALLOCATOR_FREE(xc_allocator_malloc_free) /* {{{ return block size freed */ 69 { 70 xc_del_from_blocks(allocator, (void *) p); 55 71 free((void *) p); 56 72 return 0; 57 73 } 58 74 /* }}} */ 59 static XC_MEM_CALLOC(xc_malloc_calloc) /* {{{ */ 60 { 61 return xc_add_to_blocks(mem, calloc(memb, size), size); 62 } 63 /* }}} */ 64 static XC_MEM_REALLOC(xc_malloc_realloc) /* {{{ */ 65 { 66 return xc_add_to_blocks(mem, realloc((void *) p, size), size); 67 } 68 /* }}} */ 69 static XC_MEM_STRNDUP(xc_malloc_strndup) /* {{{ */ 70 { 71 char *p = xc_add_to_blocks(mem, malloc(len), len); 72 if (!p) { 73 return NULL; 74 } 75 return memcpy(p, str, len); 76 } 77 /* }}} */ 78 static XC_MEM_STRDUP(xc_malloc_strdup) /* {{{ */ 79 { 80 return xc_malloc_strndup(mem, str, strlen(str) + 1); 81 } 82 /* }}} */ 83 84 static XC_MEM_AVAIL(xc_malloc_avail) /* {{{ */ 85 { 86 return mem->avail; 87 } 88 /* }}} */ 89 static XC_MEM_SIZE(xc_malloc_size) /* {{{ */ 90 { 91 return mem->size; 92 } 93 /* }}} */ 94 95 static XC_MEM_FREEBLOCK_FIRST(xc_malloc_freeblock_first) /* {{{ */ 75 static XC_ALLOCATOR_CALLOC(xc_allocator_malloc_calloc) /* {{{ */ 76 { 77 return xc_add_to_blocks(allocator, calloc(memb, size), size); 78 } 79 /* }}} */ 80 static XC_ALLOCATOR_REALLOC(xc_allocator_malloc_realloc) /* {{{ */ 81 { 82 return xc_add_to_blocks(allocator, realloc((void *) p, size), size); 83 } 84 /* }}} */ 85 86 static XC_ALLOCATOR_AVAIL(xc_allocator_malloc_avail) /* {{{ */ 87 { 88 return allocator->avail; 89 } 90 /* }}} */ 91 static XC_ALLOCATOR_SIZE(xc_allocator_malloc_size) /* {{{ */ 92 { 93 return allocator->size; 94 } 95 /* }}} */ 96 97 static XC_ALLOCATOR_FREEBLOCK_FIRST(xc_allocator_malloc_freeblock_first) /* {{{ */ 96 98 { 97 99 return (void *) -1; 98 100 } 99 101 /* }}} */ 100 XC_MEM_FREEBLOCK_NEXT(xc_malloc_freeblock_next) /* {{{ */102 static XC_ALLOCATOR_FREEBLOCK_NEXT(xc_allocator_malloc_freeblock_next) /* {{{ */ 101 103 { 102 104 return NULL; 103 105 } 104 106 /* }}} */ 105 XC_MEM_BLOCK_SIZE(xc_malloc_block_size) /* {{{ */106 { 107 return 0; 108 } 109 /* }}} */ 110 XC_MEM_BLOCK_OFFSET(xc_malloc_block_offset) /* {{{ */111 { 112 return 0; 113 } 114 /* }}} */ 115 116 static XC_ MEM_INIT(xc_mem_malloc_init) /* {{{ */117 { 118 #define MINSIZE (ALIGN(sizeof(xc_ mem_t)))107 static XC_ALLOCATOR_BLOCK_SIZE(xc_allocator_malloc_block_size) /* {{{ */ 108 { 109 return 0; 110 } 111 /* }}} */ 112 static XC_ALLOCATOR_BLOCK_OFFSET(xc_allocator_malloc_block_offset) /* {{{ */ 113 { 114 return 0; 115 } 116 /* }}} */ 117 118 static XC_ALLOCATOR_INIT(xc_allocator_malloc_init) /* {{{ */ 119 { 120 #define MINSIZE (ALIGN(sizeof(xc_allocator_t))) 119 121 /* requires at least the header and 1 tail block */ 120 122 if (size < MINSIZE) { 121 fprintf(stderr, "xc_ mem_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE);123 fprintf(stderr, "xc_allocator_malloc_init requires %lu bytes at least\n", (unsigned long) MINSIZE); 122 124 return NULL; 123 125 } 124 mem->shm = shm;125 mem->size = size;126 mem->avail = size - MINSIZE;126 allocator->shm = shm; 127 allocator->size = size; 128 allocator->avail = size - MINSIZE; 127 129 #undef MINSIZE 128 130 129 return mem;130 } 131 /* }}} */ 132 static XC_ MEM_DESTROY(xc_mem_malloc_destroy) /* {{{ */131 return allocator; 132 } 133 /* }}} */ 134 static XC_ALLOCATOR_DESTROY(xc_allocator_malloc_destroy) /* {{{ */ 133 135 { 134 136 } … … 142 144 static XC_SHM_IS_READWRITE(xc_malloc_is_readwrite) /* {{{ */ 143 145 { 146 #ifndef TEST 144 147 HashPosition pos; 145 148 size_t *psize; … … 154 157 zend_hash_move_forward_ex(&shm->blocks, &pos); 155 158 } 159 #endif 156 160 157 161 return 0; … … 176 180 static XC_SHM_DESTROY(xc_malloc_destroy) /* {{{ */ 177 181 { 182 #ifndef TEST 178 183 zend_hash_destroy(&shm->blocks); 184 #endif 179 185 free(shm); 180 186 return; … … 187 193 shm->size = size; 188 194 195 #ifndef TEST 189 196 zend_hash_init(&shm->blocks, 64, NULL, NULL, 1); 197 #endif 190 198 return shm; 191 199 err: … … 196 204 static XC_SHM_MEMINIT(xc_malloc_meminit) /* {{{ */ 197 205 { 198 xc_mem_t*mem;206 void *mem; 199 207 if (shm->memoffset + size > shm->size) { 208 #ifndef TEST 200 209 zend_error(E_ERROR, "XCache: internal error at %s#%d", __FILE__, __LINE__); 210 #endif 201 211 return NULL; 202 212 } 203 213 shm->memoffset += size; 204 CHECK(mem = calloc(1, sizeof(xc_mem_t)), "mem OOM"); 205 mem->handlers = shm->handlers->memhandlers; 206 mem->handlers->init(shm, mem, size); 214 CHECK(mem = calloc(1, size), "mem OOM"); 207 215 return mem; 208 216 err: … … 212 220 static XC_SHM_MEMDESTROY(xc_malloc_memdestroy) /* {{{ */ 213 221 { 214 mem->handlers->destroy(mem);215 222 free(mem); 216 223 } 217 224 /* }}} */ 218 225 219 #define xc_malloc_destroy xc_mem_malloc_destroy 220 #define xc_malloc_init xc_mem_malloc_init 221 static xc_mem_handlers_t xc_mem_malloc_handlers = XC_MEM_HANDLERS(malloc); 222 #undef xc_malloc_init 223 #undef xc_malloc_destroy 226 static xc_allocator_vtable_t xc_allocator_malloc_vtable = XC_ALLOCATOR_VTABLE(allocator_malloc); 224 227 static xc_shm_handlers_t xc_shm_malloc_handlers = XC_SHM_HANDLERS(malloc); 228 void xc_allocator_malloc_register() /* {{{ */ 229 { 230 if (xc_allocator_register("malloc", &xc_allocator_malloc_vtable) == 0) { 231 #ifndef TEST 232 zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme"); 233 #endif 234 } 235 } 236 /* }}} */ 237 238 #ifndef TEST 225 239 void xc_shm_malloc_register() /* {{{ */ 226 240 { 227 if (xc_mem_scheme_register("malloc", &xc_mem_malloc_handlers) == 0) {228 zend_error(E_ERROR, "XCache: failed to register malloc mem_scheme");229 }230 231 CHECK(xc_shm_malloc_handlers.memhandlers = xc_mem_scheme_find("malloc"), "cannot find malloc handlers");232 241 if (xc_shm_scheme_register("malloc", &xc_shm_malloc_handlers) == 0) { 233 242 zend_error(E_ERROR, "XCache: failed to register malloc shm_scheme"); 234 243 } 235 err: 236 return; 237 } 238 /* }}} */ 244 } 245 /* }}} */ 246 #endif
Note: See TracChangeset
for help on using the changeset viewer.

