|
|
|
@ -25,8 +25,19 @@ |
|
|
|
|
|
|
|
#define TR_MEM_FREE(seg) (TR_free((void **)&(seg))) |
|
|
|
|
|
|
|
#include <stdlib.h> // for NULL definition |
|
|
|
#include <sys/types.h> |
|
|
|
|
|
|
|
struct memSegment |
|
|
|
{ |
|
|
|
size_t ref_count; |
|
|
|
size_t size; |
|
|
|
int idx; |
|
|
|
void * ptr; |
|
|
|
|
|
|
|
struct memSegment * next; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* I found this at stanford.edu: |
|
|
|
* https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup |
|
|
|
@ -36,10 +47,9 @@ |
|
|
|
* because on a 64bit system size_t is also 64bit and thus it is possible |
|
|
|
* to allocate that much amount of memory theoretically. |
|
|
|
*/ |
|
|
|
static |
|
|
|
inline |
|
|
|
int |
|
|
|
bitwidth(size_t value) |
|
|
|
TR_bitwidth(size_t value) |
|
|
|
{ |
|
|
|
static const char LogTable256[256] = { |
|
|
|
-1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, |
|
|
|
@ -52,7 +62,7 @@ bitwidth(size_t value) |
|
|
|
int r; // r will be lg(v) |
|
|
|
register size_t t1, t2, t3; // temporaries |
|
|
|
|
|
|
|
if ((t3 = value >> 32)) { |
|
|
|
if (sizeof(value) == 8 && (t3 = value >> 32)) { |
|
|
|
if ((t2 = t3 >> 16)) { |
|
|
|
r = (t1 = t2 >> 8) ? 56 + LogTable256[t1] : 48 + LogTable256[t2]; |
|
|
|
} else { |
|
|
|
@ -69,11 +79,50 @@ bitwidth(size_t value) |
|
|
|
return r; |
|
|
|
} |
|
|
|
|
|
|
|
inline |
|
|
|
struct memSegment * |
|
|
|
_getMemInfo(void * mem) |
|
|
|
{ |
|
|
|
if (NULL == mem) { |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
return (struct memSegment *)(mem - sizeof(struct memSegment)); |
|
|
|
} |
|
|
|
|
|
|
|
inline |
|
|
|
size_t |
|
|
|
TR_getSize(void * mem) |
|
|
|
{ |
|
|
|
struct memSegment * segment = |
|
|
|
(struct memSegment *)(mem - sizeof(struct memSegment)); |
|
|
|
|
|
|
|
return segment ? segment->size : 0; |
|
|
|
} |
|
|
|
|
|
|
|
inline |
|
|
|
size_t |
|
|
|
TR_getUsableSize(void * mem) |
|
|
|
{ |
|
|
|
size_t size = TR_getSize(mem); |
|
|
|
|
|
|
|
return size ? size - sizeof(struct memSegment) : 0; |
|
|
|
} |
|
|
|
|
|
|
|
inline |
|
|
|
int |
|
|
|
TR_getIdx(void * mem) |
|
|
|
{ |
|
|
|
struct memSegment * segment = |
|
|
|
(struct memSegment *)(mem - sizeof(struct memSegment)); |
|
|
|
|
|
|
|
return segment ? segment->idx : -1; |
|
|
|
} |
|
|
|
|
|
|
|
void * TR_malloc(size_t); |
|
|
|
void * TR_calloc(size_t, size_t); |
|
|
|
void * TR_reference(void *); |
|
|
|
void TR_free(void **); |
|
|
|
size_t TR_getSize(void *); |
|
|
|
void TR_cleanup(); |
|
|
|
|
|
|
|
char * TR_strdup(const char *); |
|
|
|
|