Compare commits

..

No commits in common. "494be7b4dc356f04bdec368f487b5cd641c5f6b3" and "b6b915530cdb74884cd5c42342890e1c6d7865e1" have entirely different histories.

View file

@ -44,20 +44,14 @@
struct MemoryInfo struct MemoryInfo
{ {
uint64_t magic;
size_t size; size_t size;
char file[MEMORY_FILE_SIZE]; char file[MEMORY_FILE_SIZE];
int line; int line;
void *pointer; void *pointer;
MemoryInfo *prev;
MemoryInfo *next;
}; };
#define MEM_BOUND_TYPE uint32_t #define MEM_BOUND_TYPE uint32_t
#define MEM_BOUND 0xDEADBEEF #define MEM_BOUND 0xDEADBEEF
#define MEM_MAGIC 0xDEADBEEFDEADBEEF
#define MEM_BOUND_LOWER(p) *((MEM_BOUND_TYPE *) p) #define MEM_BOUND_LOWER(p) *((MEM_BOUND_TYPE *) p)
#define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((uint8_t *) p) + x)) + 1) #define MEM_BOUND_UPPER(p, x) *(((MEM_BOUND_TYPE *) (((uint8_t *) p) + x)) + 1)
@ -67,10 +61,10 @@ static pthread_mutex_t lock;
static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook; static void (*hook) (MemoryAction, MemoryInfo *, void *) = MemoryDefaultHook;
static void *hookArgs = NULL; static void *hookArgs = NULL;
static MemoryInfo **allocations = NULL;
static size_t allocationsSize = 0;
static size_t allocationsLen = 0; static size_t allocationsLen = 0;
static MemoryInfo *allocationTail = NULL;
int int
MemoryRuntimeInit(void) MemoryRuntimeInit(void)
{ {
@ -99,18 +93,70 @@ MemoryRuntimeDestroy(void)
return pthread_mutex_destroy(&lock) == 0; return pthread_mutex_destroy(&lock) == 0;
} }
static size_t
MemoryHash(void *p)
{
return (((size_t) p) >> 2 * 7) % allocationsSize;
}
static int static int
MemoryInsert(MemoryInfo * a) MemoryInsert(MemoryInfo * a)
{ {
if (allocationTail) size_t hash;
{
allocationTail->next = a;
}
a->next = NULL;
a->prev = allocationTail;
a->magic = MEM_MAGIC;
allocationTail = a; if (!allocations)
{
allocationsSize = MEMORY_TABLE_CHUNK;
allocations = calloc(allocationsSize, sizeof(void *));
if (!allocations)
{
return 0;
}
}
/* If the next insertion would cause the table to be at least 3/4
* full, re-allocate and re-hash. */
if ((allocationsLen + 1) >= ((allocationsSize * 3) >> 2))
{
size_t i;
size_t tmpAllocationsSize = allocationsSize;
MemoryInfo **tmpAllocations;
allocationsSize += MEMORY_TABLE_CHUNK;
tmpAllocations = calloc(allocationsSize, sizeof(void *));
if (!tmpAllocations)
{
return 0;
}
for (i = 0; i < tmpAllocationsSize; i++)
{
if (allocations[i])
{
hash = MemoryHash(allocations[i]->pointer);
while (tmpAllocations[hash])
{
hash = (hash + 1) % allocationsSize;
}
tmpAllocations[hash] = allocations[i];
}
}
free(allocations);
allocations = tmpAllocations;
}
hash = MemoryHash(a->pointer);
while (allocations[hash])
{
hash = (hash + 1) % allocationsSize;
}
allocations[hash] = a;
allocationsLen++; allocationsLen++;
return 1; return 1;
@ -119,24 +165,23 @@ MemoryInsert(MemoryInfo * a)
static void static void
MemoryDelete(MemoryInfo * a) MemoryDelete(MemoryInfo * a)
{ {
MemoryInfo *aPrev = a->prev; size_t hash = MemoryHash(a->pointer);
MemoryInfo *aNext = a->next; size_t count = 0;
if (aPrev) while (count <= allocationsSize)
{ {
aPrev->next = aNext; if (allocations[hash] && allocations[hash] == a)
{
allocations[hash] = NULL;
allocationsLen--;
return;
}
else
{
hash = (hash + 1) % allocationsSize;
count++;
}
} }
if (aNext)
{
aNext->prev = aPrev;
}
if (a == allocationTail)
{
allocationTail = aPrev;
}
a->magic = ~MEM_MAGIC;
} }
static int static int
@ -160,18 +205,24 @@ MemoryAllocate(size_t size, const char *file, int line)
void *p; void *p;
MemoryInfo *a; MemoryInfo *a;
//MemoryIterate(NULL, NULL); MemoryIterate(NULL, NULL);
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
a = malloc(sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size)); a = malloc(sizeof(MemoryInfo));
if (!a) if (!a)
{ {
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }
p = a + 1; p = malloc(MEM_SIZE_ACTUAL(size));
if (!p)
{
free(a);
pthread_mutex_unlock(&lock);
return NULL;
}
memset(p, 0, MEM_SIZE_ACTUAL(size)); memset(p, 0, MEM_SIZE_ACTUAL(size));
MEM_BOUND_LOWER(p) = MEM_BOUND; MEM_BOUND_LOWER(p) = MEM_BOUND;
@ -185,6 +236,7 @@ MemoryAllocate(size_t size, const char *file, int line)
if (!MemoryInsert(a)) if (!MemoryInsert(a))
{ {
free(a); free(a);
free(p);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return NULL; return NULL;
} }
@ -204,7 +256,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
MemoryInfo *a; MemoryInfo *a;
void *new = NULL; void *new = NULL;
//MemoryIterate(NULL, NULL); MemoryIterate(NULL, NULL);
if (!p) if (!p)
{ {
@ -215,17 +267,15 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
if (a) if (a)
{ {
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
MemoryDelete(a); new = realloc(a->pointer, MEM_SIZE_ACTUAL(size));
new = realloc(a, sizeof(MemoryInfo) + MEM_SIZE_ACTUAL(size));
if (new) if (new)
{ {
a = new; MemoryDelete(a);
a->size = MEM_SIZE_ACTUAL(size); a->size = MEM_SIZE_ACTUAL(size);
strncpy(a->file, file, MEMORY_FILE_SIZE - 1); strncpy(a->file, file, MEMORY_FILE_SIZE - 1);
a->line = line; a->line = line;
a->magic = MEM_MAGIC;
a->pointer = a + 1; a->pointer = new;
MemoryInsert(a); MemoryInsert(a);
MEM_BOUND_LOWER(a->pointer) = MEM_BOUND; MEM_BOUND_LOWER(a->pointer) = MEM_BOUND;
@ -253,7 +303,7 @@ MemoryReallocate(void *p, size_t size, const char *file, int line)
} }
} }
return ((MEM_BOUND_TYPE *) a->pointer) + 1; return ((MEM_BOUND_TYPE *) new) + 1;
} }
void void
@ -261,7 +311,7 @@ MemoryFree(void *p, const char *file, int line)
{ {
MemoryInfo *a; MemoryInfo *a;
//MemoryIterate(NULL, NULL); MemoryIterate(NULL, NULL);
if (!p) if (!p)
{ {
@ -279,6 +329,7 @@ MemoryFree(void *p, const char *file, int line)
hook(MEMORY_FREE, a, hookArgs); hook(MEMORY_FREE, a, hookArgs);
} }
MemoryDelete(a); MemoryDelete(a);
free(a->pointer);
free(a); free(a);
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
@ -301,15 +352,17 @@ MemoryFree(void *p, const char *file, int line)
size_t size_t
MemoryAllocated(void) MemoryAllocated(void)
{ {
size_t i;
size_t total = 0; size_t total = 0;
MemoryInfo *cur;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
/* TODO */ for (i = 0; i < allocationsSize; i++)
for (cur = allocationTail; cur; cur = cur->prev)
{ {
total += MemoryInfoGetSize(cur); if (allocations[i])
{
total += MemoryInfoGetSize(allocations[i]);
}
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
@ -320,40 +373,55 @@ MemoryAllocated(void)
void void
MemoryFreeAll(void) MemoryFreeAll(void)
{ {
MemoryInfo *cur; size_t i;
MemoryInfo *prev;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
/* TODO */ for (i = 0; i < allocationsSize; i++)
for (cur = allocationTail; cur; cur = prev)
{ {
prev = cur->prev; if (allocations[i])
free(cur); {
free(allocations[i]->pointer);
free(allocations[i]);
}
} }
free(allocations);
allocations = NULL;
allocationsSize = 0;
allocationsLen = 0; allocationsLen = 0;
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
} }
MemoryInfo * MemoryInfo *
MemoryInfoGet(void *po) MemoryInfoGet(void *p)
{ {
void *p = po; size_t hash, count;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
p = ((MEM_BOUND_TYPE *) po) - 1; p = ((MEM_BOUND_TYPE *) p) - 1;
p = ((MemoryInfo *) p) - 1; hash = MemoryHash(p);
if (((MemoryInfo *)p)->magic != MEM_MAGIC) count = 0;
while (count <= allocationsSize)
{ {
p = NULL; if (!allocations[hash] || allocations[hash]->pointer != p)
{
hash = (hash + 1) % allocationsSize;
count++;
}
else
{
MemoryCheck(allocations[hash]);
pthread_mutex_unlock(&lock);
return allocations[hash];
}
} }
pthread_mutex_unlock(&lock); pthread_mutex_unlock(&lock);
return p; return NULL;
} }
size_t size_t
@ -401,18 +469,21 @@ MemoryInfoGetPointer(MemoryInfo * a)
} }
void void
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args) MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
{ {
MemoryInfo *cur; size_t i;
pthread_mutex_lock(&lock); pthread_mutex_lock(&lock);
for (cur = allocationTail; cur; cur = cur->prev) for (i = 0; i < allocationsSize; i++)
{ {
MemoryCheck(cur); if (allocations[i])
if (iterFunc)
{ {
iterFunc(cur, args); MemoryCheck(allocations[i]);
if (iterFunc)
{
iterFunc(allocations[i], args);
}
} }
} }