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