Revert revision 1.4: Memory in O(1) time.

This has some bugs in it that I don't have time to work out right
now. Best to revert to a known working version as the project gains
attention until I can address this properly.
This commit is contained in:
Jordan Bancino 2022-10-31 08:08:32 -04:00
parent bcff59bacb
commit b24ab05e8b

View file

@ -32,6 +32,7 @@ struct MemoryInfo
size_t size;
const char *file;
int line;
void *pointer;
MemoryInfo *next;
MemoryInfo *prev;
@ -45,20 +46,30 @@ static void *hookArgs = NULL;
void *
MemoryAllocate(size_t size, const char *file, int line)
{
void *p;
MemoryInfo *a;
pthread_mutex_lock(&lock);
a = malloc(sizeof(MemoryInfo) + size);
if (!a)
p = malloc(size);
if (!p)
{
pthread_mutex_unlock(&lock);
return NULL;
}
a->size = sizeof(MemoryInfo) + size;
a = malloc(sizeof(MemoryInfo));
if (!a)
{
free(p);
pthread_mutex_unlock(&lock);
return NULL;
}
a->size = size;
a->file = file;
a->line = line;
a->pointer = p;
a->next = NULL;
a->prev = lastAllocation;
@ -75,7 +86,7 @@ MemoryAllocate(size_t size, const char *file, int line)
}
pthread_mutex_unlock(&lock);
return MemoryInfoGetPointer(a);
return p;
}
void *
@ -84,55 +95,34 @@ MemoryReallocate(void *p, size_t size)
MemoryInfo *a;
void *new = NULL;
a = MemoryInfoGet(p);
if (!a)
{
if (hook)
{
hook(MEMORY_BAD_POINTER, NULL, hookArgs);
}
pthread_mutex_unlock(&lock);
return NULL;
}
pthread_mutex_lock(&lock);
new = realloc(a, sizeof(MemoryInfo) + size);
if (!new)
a = lastAllocation;
while (a)
{
pthread_mutex_unlock(&lock);
return NULL;
}
a = new;
a->size = sizeof(MemoryInfo) + size;
if (a->prev)
if (a->pointer == p)
{
a->prev->next = a;
}
else
new = realloc(p, size);
if (new)
{
lastAllocation = a;
}
if (a->next)
{
a->next->prev = a;
}
else
{
lastAllocation = a;
}
a->pointer = new;
a->size = size;
if (hook)
{
hook(MEMORY_REALLOCATE, a, hookArgs);
}
}
break;
}
a = a->prev;
}
pthread_mutex_unlock(&lock);
return MemoryInfoGetPointer(a);
return new;
}
void
@ -142,17 +132,12 @@ MemoryFree(void *p)
pthread_mutex_lock(&lock);
a = MemoryInfoGet(p);
if (!a)
{
if (hook)
{
hook(MEMORY_BAD_POINTER, NULL, hookArgs);
}
pthread_mutex_unlock(&lock);
return;
}
a = lastAllocation;
while (a)
{
if (a->pointer == p)
{
if (a->prev)
{
a->prev->next = a->next;
@ -177,6 +162,13 @@ MemoryFree(void *p)
}
free(a);
free(p);
break;
}
a = a->prev;
}
pthread_mutex_unlock(&lock);
}
@ -213,7 +205,9 @@ MemoryFreeAll(void)
{
MemoryInfo *prev = a->prev;
free(a->pointer);
free(a);
a = prev;
}
@ -227,18 +221,12 @@ MemoryInfoGet(void *p)
{
MemoryInfo *a;
if (!p)
{
return NULL;
}
p = &(((MemoryInfo *) p)[-1]);
pthread_mutex_lock(&lock);
a = lastAllocation;
while (a)
{
if (a == p)
if (a->pointer == p)
{
break;
}
@ -290,7 +278,7 @@ MemoryInfoGetPointer(MemoryInfo * a)
return NULL;
}
return &(a[1]);
return a->pointer;
}
void