2022-10-13 13:09:26 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Jordan Bancino <@jordan:bancino.net>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation files
|
|
|
|
* (the "Software"), to deal in the Software without restriction,
|
|
|
|
* including without limitation the rights to use, copy, modify, merge,
|
|
|
|
* publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
* and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
2022-10-13 01:29:05 +00:00
|
|
|
#include <Memory.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
struct MemoryInfo
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
size_t size;
|
|
|
|
const char *file;
|
|
|
|
int line;
|
2022-10-31 12:08:32 +00:00
|
|
|
void *pointer;
|
2022-10-13 13:09:26 +00:00
|
|
|
|
|
|
|
MemoryInfo *next;
|
|
|
|
MemoryInfo *prev;
|
2022-10-13 01:29:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static MemoryInfo *lastAllocation = NULL;
|
2022-10-13 13:09:26 +00:00
|
|
|
static void (*hook) (MemoryAction, MemoryInfo *, void *) = NULL;
|
2022-10-13 12:50:27 +00:00
|
|
|
static void *hookArgs = NULL;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
|
|
|
void *
|
2022-10-15 13:34:47 +00:00
|
|
|
MemoryAllocate(size_t size, const char *file, int line)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-31 12:08:32 +00:00
|
|
|
void *p;
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
p = malloc(size);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
a = malloc(sizeof(MemoryInfo));
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!a)
|
|
|
|
{
|
2022-10-31 12:08:32 +00:00
|
|
|
free(p);
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
a->size = size;
|
2022-10-13 13:09:26 +00:00
|
|
|
a->file = file;
|
|
|
|
a->line = line;
|
2022-10-31 12:08:32 +00:00
|
|
|
a->pointer = p;
|
2022-10-13 13:09:26 +00:00
|
|
|
a->next = NULL;
|
|
|
|
a->prev = lastAllocation;
|
|
|
|
|
|
|
|
if (lastAllocation)
|
|
|
|
{
|
|
|
|
lastAllocation->next = a;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastAllocation = a;
|
|
|
|
|
|
|
|
if (hook)
|
|
|
|
{
|
|
|
|
hook(MEMORY_ALLOCATE, a, hookArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-31 12:08:32 +00:00
|
|
|
return p;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
MemoryReallocate(void *p, size_t size)
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
|
|
|
void *new = NULL;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-28 18:07:44 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
a = lastAllocation;
|
|
|
|
while (a)
|
2022-10-28 18:07:44 +00:00
|
|
|
{
|
2022-10-31 12:08:32 +00:00
|
|
|
if (a->pointer == p)
|
|
|
|
{
|
|
|
|
new = realloc(p, size);
|
|
|
|
if (new)
|
|
|
|
{
|
|
|
|
a->pointer = new;
|
|
|
|
a->size = size;
|
|
|
|
|
|
|
|
if (hook)
|
|
|
|
{
|
|
|
|
hook(MEMORY_REALLOCATE, a, hookArgs);
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 18:07:44 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-10-28 18:07:44 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
a = a->prev;
|
2022-10-13 13:09:26 +00:00
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
return new;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MemoryFree(void *p)
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
a = lastAllocation;
|
2022-10-13 13:09:26 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
while (a)
|
2022-10-28 18:07:44 +00:00
|
|
|
{
|
2022-10-31 12:08:32 +00:00
|
|
|
if (a->pointer == p)
|
|
|
|
{
|
|
|
|
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);
|
2022-10-28 18:07:44 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-10-28 18:07:44 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
a = a->prev;
|
2022-10-13 13:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MemoryAllocated(void)
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
|
|
|
size_t total = 0;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = lastAllocation;
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
total += a->size;
|
|
|
|
a = a->prev;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
return total;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MemoryFreeAll(void)
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = lastAllocation;
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
MemoryInfo *prev = a->prev;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
free(a->pointer);
|
2022-10-13 13:09:26 +00:00
|
|
|
free(a);
|
2022-10-31 12:08:32 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = prev;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
lastAllocation = NULL;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MemoryInfo *
|
|
|
|
MemoryInfoGet(void *p)
|
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = lastAllocation;
|
|
|
|
while (a)
|
|
|
|
{
|
2022-10-31 12:08:32 +00:00
|
|
|
if (a->pointer == p)
|
2022-10-13 13:09:26 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = a->prev;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
return a;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetSize(MemoryInfo * a)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
return a->size;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetFile(MemoryInfo * a)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
return a->file;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetLine(MemoryInfo * a)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
return a->line;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfoGetPointer(MemoryInfo * a)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-31 12:08:32 +00:00
|
|
|
return a->pointer;
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryIterate(void (*iterFunc) (MemoryInfo *, void *), void *args)
|
2022-10-13 01:29:05 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryInfo *a;
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
a = lastAllocation;
|
|
|
|
while (a)
|
|
|
|
{
|
|
|
|
iterFunc(a, args);
|
|
|
|
a = a->prev;
|
|
|
|
}
|
2022-10-13 01:29:05 +00:00
|
|
|
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 01:29:05 +00:00
|
|
|
}
|
2022-10-13 12:50:27 +00:00
|
|
|
|
|
|
|
void
|
2022-10-13 13:09:26 +00:00
|
|
|
MemoryHook(void (*memHook) (MemoryAction, MemoryInfo *, void *), void *args)
|
2022-10-13 12:50:27 +00:00
|
|
|
{
|
2022-10-13 13:09:26 +00:00
|
|
|
pthread_mutex_lock(&lock);
|
|
|
|
hook = memHook;
|
|
|
|
hookArgs = args;
|
|
|
|
pthread_mutex_unlock(&lock);
|
2022-10-13 12:50:27 +00:00
|
|
|
}
|