Commit 3f28478d323f55014f58815cac1bec104537da78
1 parent
f67ee680
creared some inline functions to access the meminfo structure.
Showing
3 changed files
with
91 additions
and
33 deletions
| ... | ... | @@ -25,8 +25,19 @@ |
| 25 | 25 | |
| 26 | 26 | #define TR_MEM_FREE(seg) (TR_free((void **)&(seg))) |
| 27 | 27 | |
| 28 | +#include <stdlib.h> // for NULL definition | |
| 28 | 29 | #include <sys/types.h> |
| 29 | 30 | |
| 31 | +struct memSegment | |
| 32 | +{ | |
| 33 | + size_t ref_count; | |
| 34 | + size_t size; | |
| 35 | + int idx; | |
| 36 | + void * ptr; | |
| 37 | + | |
| 38 | + struct memSegment * next; | |
| 39 | +}; | |
| 40 | + | |
| 30 | 41 | /** |
| 31 | 42 | * I found this at stanford.edu: |
| 32 | 43 | * https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup |
| ... | ... | @@ -36,10 +47,9 @@ |
| 36 | 47 | * because on a 64bit system size_t is also 64bit and thus it is possible |
| 37 | 48 | * to allocate that much amount of memory theoretically. |
| 38 | 49 | */ |
| 39 | -static | |
| 40 | 50 | inline |
| 41 | 51 | int |
| 42 | -bitwidth(size_t value) | |
| 52 | +TR_bitwidth(size_t value) | |
| 43 | 53 | { |
| 44 | 54 | static const char LogTable256[256] = { |
| 45 | 55 | -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, |
| ... | ... | @@ -52,7 +62,7 @@ bitwidth(size_t value) |
| 52 | 62 | int r; // r will be lg(v) |
| 53 | 63 | register size_t t1, t2, t3; // temporaries |
| 54 | 64 | |
| 55 | - if ((t3 = value >> 32)) { | |
| 65 | + if (sizeof(value) == 8 && (t3 = value >> 32)) { | |
| 56 | 66 | if ((t2 = t3 >> 16)) { |
| 57 | 67 | r = (t1 = t2 >> 8) ? 56 + LogTable256[t1] : 48 + LogTable256[t2]; |
| 58 | 68 | } else { |
| ... | ... | @@ -69,11 +79,50 @@ bitwidth(size_t value) |
| 69 | 79 | return r; |
| 70 | 80 | } |
| 71 | 81 | |
| 82 | +inline | |
| 83 | +struct memSegment * | |
| 84 | +_getMemInfo(void * mem) | |
| 85 | +{ | |
| 86 | + if (NULL == mem) { | |
| 87 | + return 0; | |
| 88 | + } | |
| 89 | + | |
| 90 | + return (struct memSegment *)(mem - sizeof(struct memSegment)); | |
| 91 | +} | |
| 92 | + | |
| 93 | +inline | |
| 94 | +size_t | |
| 95 | +TR_getSize(void * mem) | |
| 96 | +{ | |
| 97 | + struct memSegment * segment = | |
| 98 | + (struct memSegment *)(mem - sizeof(struct memSegment)); | |
| 99 | + | |
| 100 | + return segment ? segment->size : 0; | |
| 101 | +} | |
| 102 | + | |
| 103 | +inline | |
| 104 | +size_t | |
| 105 | +TR_getUsableSize(void * mem) | |
| 106 | +{ | |
| 107 | + size_t size = TR_getSize(mem); | |
| 108 | + | |
| 109 | + return size ? size - sizeof(struct memSegment) : 0; | |
| 110 | +} | |
| 111 | + | |
| 112 | +inline | |
| 113 | +int | |
| 114 | +TR_getIdx(void * mem) | |
| 115 | +{ | |
| 116 | + struct memSegment * segment = | |
| 117 | + (struct memSegment *)(mem - sizeof(struct memSegment)); | |
| 118 | + | |
| 119 | + return segment ? segment->idx : -1; | |
| 120 | +} | |
| 121 | + | |
| 72 | 122 | void * TR_malloc(size_t); |
| 73 | 123 | void * TR_calloc(size_t, size_t); |
| 74 | 124 | void * TR_reference(void *); |
| 75 | 125 | void TR_free(void **); |
| 76 | -size_t TR_getSize(void *); | |
| 77 | 126 | void TR_cleanup(); |
| 78 | 127 | |
| 79 | 128 | char * TR_strdup(const char *); | ... | ... |
| ... | ... | @@ -48,7 +48,6 @@ |
| 48 | 48 | |
| 49 | 49 | #include <stdio.h> |
| 50 | 50 | |
| 51 | -#include <pthread.h> | |
| 52 | 51 | #include <stdlib.h> |
| 53 | 52 | #include <string.h> |
| 54 | 53 | #include <search.h> |
| ... | ... | @@ -57,16 +56,24 @@ |
| 57 | 56 | |
| 58 | 57 | #include "tr/memory.h" |
| 59 | 58 | |
| 59 | +#ifdef _WIN32 | |
| 60 | +#define _SC_PAGESIZE 2048L | |
| 60 | 61 | |
| 61 | -struct memSegment | |
| 62 | +long | |
| 63 | +sysconf(int name) | |
| 62 | 64 | { |
| 63 | - size_t ref_count; | |
| 64 | - size_t size; | |
| 65 | - int idx; | |
| 66 | - void * ptr; | |
| 65 | + switch (name) { | |
| 66 | + case _SC_PAGESIZE: return 2048L; | |
| 67 | + } | |
| 67 | 68 | |
| 68 | - struct memSegment * next; | |
| 69 | -}; | |
| 69 | + return -1; | |
| 70 | +} | |
| 71 | +#endif | |
| 72 | + | |
| 73 | +extern inline int TR_bitwidth(size_t); | |
| 74 | +extern inline size_t TR_getSize(void *); | |
| 75 | +extern inline size_t TR_getUsableSize(void *); | |
| 76 | +extern inline int TR_getIdx(void *); | |
| 70 | 77 | |
| 71 | 78 | static |
| 72 | 79 | struct memSegment * |
| ... | ... | @@ -116,7 +123,6 @@ deleteElement(struct memSegment ** stack) |
| 116 | 123 | #define TR_MAX_MEM_IDX 1024 |
| 117 | 124 | |
| 118 | 125 | struct memSegment * segments[TR_MAX_MEM_IDX] = {}; |
| 119 | -pthread_mutex_t TR_memop_lock = PTHREAD_MUTEX_INITIALIZER; | |
| 120 | 126 | |
| 121 | 127 | static |
| 122 | 128 | inline |
| ... | ... | @@ -176,19 +182,20 @@ TR_malloc(size_t size) |
| 176 | 182 | static int psize_width = 0; |
| 177 | 183 | int idx; |
| 178 | 184 | |
| 179 | - if (psize_width == 0) psize_width = bitwidth(psize); | |
| 185 | + psize_width = psize_width ? psize_width : TR_bitwidth(psize); | |
| 180 | 186 | |
| 181 | 187 | size += sizeof(struct memSegment); |
| 182 | 188 | |
| 183 | 189 | #define MIN_BITS 8 |
| 184 | 190 | |
| 185 | 191 | if (size >= psize) { |
| 192 | + // get a multiple of pagesize | |
| 186 | 193 | idx = size / psize; |
| 187 | 194 | |
| 188 | 195 | if (0 != (size % psize)) { |
| 189 | 196 | // size if not a multiple of pagesize so bring it to one. |
| 190 | - size = (idx + 1) * psize; | |
| 191 | 197 | idx++; |
| 198 | + size = idx * psize; | |
| 192 | 199 | } |
| 193 | 200 | |
| 194 | 201 | idx += psize_width - MIN_BITS; |
| ... | ... | @@ -199,7 +206,7 @@ TR_malloc(size_t size) |
| 199 | 206 | } else { |
| 200 | 207 | size_t mask; |
| 201 | 208 | |
| 202 | - idx = bitwidth(size); | |
| 209 | + idx = TR_bitwidth(size); | |
| 203 | 210 | mask = (1 << (idx + 1)) - 1; |
| 204 | 211 | idx -= (MIN_BITS - 1); |
| 205 | 212 | |
| ... | ... | @@ -214,9 +221,7 @@ TR_malloc(size_t size) |
| 214 | 221 | |
| 215 | 222 | #ifdef MEM_OPT |
| 216 | 223 | if (idx < TR_MAX_MEM_IDX) { |
| 217 | - pthread_mutex_lock(&TR_memop_lock); | |
| 218 | 224 | seg = deleteElement(&(segments[idx])); |
| 219 | - pthread_mutex_unlock(&TR_memop_lock); | |
| 220 | 225 | } else |
| 221 | 226 | #endif |
| 222 | 227 | { |
| ... | ... | @@ -260,9 +265,7 @@ TR_free(void ** mem) |
| 260 | 265 | } else { |
| 261 | 266 | #ifdef MEM_OPT |
| 262 | 267 | if (-1 != seg->idx) { |
| 263 | - pthread_mutex_lock(&TR_memop_lock); | |
| 264 | 268 | insertElement(&(segments[seg->idx]), seg); |
| 265 | - pthread_mutex_unlock(&TR_memop_lock); | |
| 266 | 269 | } else |
| 267 | 270 | #endif |
| 268 | 271 | { |
| ... | ... | @@ -274,19 +277,6 @@ TR_free(void ** mem) |
| 274 | 277 | } |
| 275 | 278 | } |
| 276 | 279 | |
| 277 | -size_t | |
| 278 | -TR_getSize(void * mem) | |
| 279 | -{ | |
| 280 | - struct memSegment * segment; | |
| 281 | - | |
| 282 | - if (NULL == mem) { | |
| 283 | - return 0; | |
| 284 | - } | |
| 285 | - | |
| 286 | - segment = (struct memSegment *)(mem - sizeof(struct memSegment)); | |
| 287 | - return segment->size; | |
| 288 | -} | |
| 289 | - | |
| 290 | 280 | void |
| 291 | 281 | TR_cleanup() |
| 292 | 282 | { | ... | ... |
tests/foo.c
0 → 100644
| 1 | +#include <stdio.h> | |
| 2 | +#include <stdlib.h> | |
| 3 | + | |
| 4 | +#include "trbase.h" | |
| 5 | + | |
| 6 | +int | |
| 7 | +main (int argc, char * argv[]) | |
| 8 | +{ | |
| 9 | + void * foo = TR_malloc(strtol(argv[1], NULL, 10)); | |
| 10 | + | |
| 11 | + printf("idx: %d / size: %zd / usable_size: %zd\n", | |
| 12 | + TR_getIdx(foo), | |
| 13 | + TR_getSize(foo), | |
| 14 | + TR_getUsableSize(foo)); | |
| 15 | + | |
| 16 | + return 0; | |
| 17 | +} | |
| 18 | + | |
| 19 | +// vim: set ts=4 sw=4: | ... | ... |
Please
register
or
login
to post a comment