2018-11-24 21:39:18 +00:00
|
|
|
/*-
|
2021-01-12 22:02:29 +00:00
|
|
|
* Copyright (c) 2014-2020 Carsten Sonne Larsen <cs@innolan.net>
|
2018-11-24 21:39:18 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2021-01-12 22:00:49 +00:00
|
|
|
*
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2021-01-12 22:02:29 +00:00
|
|
|
#include <stdbool.h>
|
2018-11-24 21:39:18 +00:00
|
|
|
#include <exec/types.h>
|
|
|
|
#include <exec/memory.h>
|
|
|
|
#include <exec/semaphores.h>
|
|
|
|
#include <proto/exec.h>
|
|
|
|
#define ALLOC_MEM(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR)
|
|
|
|
#define FREE_MEM(x) FreeVec(x)
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
#include "mem.h"
|
2018-11-24 21:39:18 +00:00
|
|
|
#include "compiler.h"
|
|
|
|
#include "log.h"
|
2021-01-12 22:02:29 +00:00
|
|
|
|
|
|
|
#include "logmod.h"
|
|
|
|
#define MODULENAME "Memory"
|
|
|
|
#define LogMemTrace(...) LogMemTrace(MODULENAME, __VA_ARGS__)
|
|
|
|
//#define MEM_TRACE 1
|
|
|
|
|
2018-11-24 21:39:18 +00:00
|
|
|
int StrLen(const char *);
|
|
|
|
|
|
|
|
#if defined(__x86_64__) || defined(__aarch64__) || \
|
|
|
|
defined(_M_AMD64) || defined(_M_ARM64) || \
|
|
|
|
defined(__powerpc64__)
|
|
|
|
#define P64BIT
|
|
|
|
#endif
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Block of allocated memory.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
|
|
|
struct MemoryBlock
|
|
|
|
{
|
|
|
|
struct MemoryBlock *next;
|
|
|
|
size_t size;
|
|
|
|
void *address;
|
2021-01-12 22:02:29 +00:00
|
|
|
char *file;
|
|
|
|
char *function;
|
|
|
|
int line;
|
2018-11-24 21:39:18 +00:00
|
|
|
};
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* List of allocated memory. Uses the LIFO principle.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
|
|
|
struct MemoryList
|
|
|
|
{
|
|
|
|
struct MemoryBlock *first;
|
|
|
|
size_t peak;
|
|
|
|
size_t size;
|
|
|
|
long count;
|
|
|
|
};
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Global list of allocated memory.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
2021-01-12 22:02:29 +00:00
|
|
|
volatile static struct MemoryList *list = NULL;
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Get memory usage in the global memory list.
|
|
|
|
*/
|
|
|
|
void MemUsage(long *blocks, long *size, long *peak)
|
|
|
|
{
|
|
|
|
if (blocks != NULL)
|
|
|
|
{
|
|
|
|
*blocks = list->count;
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (size != NULL)
|
|
|
|
{
|
|
|
|
*size = (long)list->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peak != NULL)
|
|
|
|
{
|
|
|
|
*peak = (long)list->peak;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log a memory allocation error.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
2021-01-12 22:02:29 +00:00
|
|
|
static void AllocationError(const char *descr, size_t size, const char *file, const char *function, int line)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
LogDebug("Memory allocation error (%s) from %s, %s, line %ld, %ld bytes",
|
|
|
|
descr, function, file, line, (long)size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Log a memory deallocation error.
|
|
|
|
*/
|
|
|
|
static void DeAllocationError(const char *descr, void *p, const char *file, const char *function, int line)
|
|
|
|
{
|
|
|
|
LogDebug("Memory deallocation error (%s) from %s, %s, line %ld at 0x%lx",
|
|
|
|
descr, function, file, line, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *StrDupRaw(const char *s1)
|
|
|
|
{
|
|
|
|
char *s2;
|
|
|
|
size_t len = s1 != NULL ? StrLen(s1) : 1;
|
|
|
|
s2 = ALLOC_MEM(++len);
|
|
|
|
|
|
|
|
if (s2 == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMem((void *)s1, s2, --len);
|
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MemZero(void *address, size_t size)
|
|
|
|
{
|
|
|
|
if (address == NULL || size == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *c = (char *)address;
|
|
|
|
int n = size;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*c++ = '\0';
|
|
|
|
} while (--n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitMemSafe(void)
|
|
|
|
{
|
|
|
|
volatile struct MemoryList *m = (volatile struct MemoryList *)ALLOC_MEM(sizeof(struct MemoryList));
|
|
|
|
bool newList = false;
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
|
|
|
Forbid();
|
|
|
|
if (list == NULL)
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
list = m;
|
|
|
|
;
|
2018-11-24 21:39:18 +00:00
|
|
|
list->first = NULL;
|
|
|
|
list->peak = 0;
|
|
|
|
list->size = 0;
|
|
|
|
list->count = 0;
|
2021-01-12 22:02:29 +00:00
|
|
|
newList = true;
|
|
|
|
}
|
|
|
|
Permit();
|
|
|
|
|
|
|
|
if (newList && !list)
|
|
|
|
{
|
|
|
|
AllocationError("list", sizeof(struct MemoryList), __FILE__, __FUNCTION__, __LINE__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newList)
|
|
|
|
{
|
|
|
|
FREE_MEM((void *)m);
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
2021-01-12 22:02:29 +00:00
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Allocate memory and add it to the global memory list.
|
|
|
|
*/
|
|
|
|
void *AllocMemTraced(size_t size, const char *file, const char *function, int line, bool trace)
|
|
|
|
{
|
|
|
|
struct MemoryBlock *newblock;
|
|
|
|
#ifdef P64BIT // Align to bytes of 8
|
|
|
|
size_t allocsize = (size + 7) & ~0x07;
|
|
|
|
#else // Align to bytes of 4
|
|
|
|
size_t allocsize = (size + 3) & ~0x03;
|
2018-11-24 21:39:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
newblock = (struct MemoryBlock *)ALLOC_MEM(sizeof(struct MemoryBlock));
|
|
|
|
if (!newblock)
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
AllocationError("block", sizeof(struct MemoryBlock), file, function, line);
|
2018-11-24 21:39:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
newblock->address = (struct MemoryBlock *)ALLOC_MEM(allocsize);
|
|
|
|
if (!newblock->address)
|
|
|
|
{
|
|
|
|
FREE_MEM(newblock);
|
2021-01-12 22:02:29 +00:00
|
|
|
AllocationError("memory", allocsize, file, function, line);
|
2018-11-24 21:39:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
newblock->size = allocsize;
|
2021-01-12 22:02:29 +00:00
|
|
|
newblock->file = StrDupRaw(file);
|
|
|
|
newblock->function = StrDupRaw(function);
|
|
|
|
newblock->line = line;
|
|
|
|
|
|
|
|
Forbid();
|
2018-11-24 21:39:18 +00:00
|
|
|
newblock->next = list->first;
|
|
|
|
list->first = newblock;
|
|
|
|
list->size += allocsize;
|
|
|
|
list->count++;
|
|
|
|
if (list->size > list->peak)
|
|
|
|
{
|
|
|
|
list->peak = list->size;
|
|
|
|
}
|
|
|
|
Permit();
|
|
|
|
|
|
|
|
#ifdef MEM_TRACE
|
2021-01-12 22:02:29 +00:00
|
|
|
if (trace)
|
|
|
|
{
|
|
|
|
LogMemTrace("[Memory] Allocated memory block from %s, %s, line %ld, %ld bytes at 0x%lx",
|
|
|
|
newblock->function, newblock->file, newblock->line,
|
|
|
|
newblock->size, newblock->address);
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Memory allocated
|
|
|
|
return newblock->address;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
static void RemoveMemSafe(void *block, const char *file, const char *function, int line, bool deallocate)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
|
|
|
struct MemoryBlock *current, *previous;
|
2021-01-12 22:02:29 +00:00
|
|
|
bool found = true;
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (list == NULL)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
DeAllocationError("list", 0, file, function, line);
|
2018-11-24 21:39:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block == NULL)
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
DeAllocationError("block", 0, file, function, line);
|
2018-11-24 21:39:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
Forbid();
|
2018-11-24 21:39:18 +00:00
|
|
|
previous = NULL;
|
|
|
|
current = list->first;
|
|
|
|
while (current != NULL && current->address != block)
|
|
|
|
{
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (current != NULL)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
if (previous == NULL)
|
|
|
|
{
|
|
|
|
list->first = current->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
previous->next = current->next;
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
list->size -= current->size;
|
|
|
|
list->count--;
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
found = false;
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
2021-01-12 22:02:29 +00:00
|
|
|
Permit();
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
#ifdef MEM_TRACE
|
|
|
|
LogMemTrace("[Memory] Deallocated memory block from %s, %s, line %ld, %ld bytes at 0x%lx",
|
|
|
|
current->function, current->file, current->line,
|
|
|
|
current->size, current->address);
|
|
|
|
#endif
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (deallocate)
|
|
|
|
{
|
|
|
|
MemZero(current->address, current->size);
|
|
|
|
FREE_MEM(current->address);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current->file != NULL)
|
|
|
|
{
|
|
|
|
MemZero(current->file, StrLen(current->file));
|
|
|
|
FREE_MEM(current->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current->function != NULL)
|
|
|
|
{
|
|
|
|
MemZero(current->function, StrLen(current->function));
|
|
|
|
FREE_MEM(current->function);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemZero(current, sizeof(struct MemoryBlock));
|
|
|
|
FREE_MEM(current);
|
|
|
|
}
|
|
|
|
else
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
DeAllocationError("address not found", block, file, function, line);
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Deallocate memory from the global memory list.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
2021-01-12 22:02:29 +00:00
|
|
|
void FreeMemTraced(void *block, const char *file, const char *function, int line)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
RemoveMemSafe(block, file, function, line, true);
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
void *MemDupTraced(const void *s1, size_t len, const char *file, const char *function, int line, bool trace)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
|
|
|
char *dup;
|
2021-01-12 22:02:29 +00:00
|
|
|
dup = AllocMemTraced(len, file, function, line, trace);
|
2018-11-24 21:39:18 +00:00
|
|
|
CopyMem((void *)s1, dup, len);
|
|
|
|
return dup;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
char *StrDupTraced(const char *s1, const char *file, const char *function, int line, bool trace)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
|
|
|
char *s2;
|
|
|
|
size_t len = s1 != NULL ? StrLen(s1) : 1;
|
2021-01-12 22:02:29 +00:00
|
|
|
s2 = AllocMemTraced(++len, file, function, line, trace);
|
2018-11-24 21:39:18 +00:00
|
|
|
|
|
|
|
if (s2 == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMem((void *)s1, s2, --len);
|
|
|
|
return s2;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
/*
|
|
|
|
* Deallocate all memory in the global memory list.
|
2018-11-24 21:39:18 +00:00
|
|
|
*/
|
|
|
|
void FreeAllSafe(void)
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
volatile struct MemoryList *m;
|
2018-11-24 21:39:18 +00:00
|
|
|
struct MemoryBlock *current, *next;
|
|
|
|
|
|
|
|
Forbid();
|
2021-01-12 22:02:29 +00:00
|
|
|
m = list;
|
|
|
|
list = NULL;
|
|
|
|
Permit();
|
|
|
|
|
|
|
|
if (m == NULL)
|
2018-11-24 21:39:18 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
current = m->first;
|
2018-11-24 21:39:18 +00:00
|
|
|
while (current != NULL)
|
|
|
|
{
|
2021-01-12 22:02:29 +00:00
|
|
|
if (current->file && current->function)
|
|
|
|
{
|
|
|
|
LogDebug("Released forgotten memory block from %s, %s, line %ld, %ld bytes at 0x%lx",
|
|
|
|
current->function, current->file, current->line,
|
|
|
|
(long)current->size, current->address);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogDebug("Released forgotten memory block on %ld bytes at 0x%lx",
|
|
|
|
(long)current->size,
|
|
|
|
current->address);
|
|
|
|
}
|
|
|
|
|
2018-11-24 21:39:18 +00:00
|
|
|
next = current->next;
|
2021-01-12 22:02:29 +00:00
|
|
|
|
|
|
|
MemZero(current->address, current->size);
|
2018-11-24 21:39:18 +00:00
|
|
|
FREE_MEM(current->address);
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (current->file != NULL)
|
|
|
|
{
|
|
|
|
MemZero(current->file, StrLen(current->file));
|
|
|
|
FREE_MEM(current->file);
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
if (current->function != NULL)
|
|
|
|
{
|
|
|
|
MemZero(current->function, StrLen(current->function));
|
|
|
|
FREE_MEM(current->function);
|
|
|
|
}
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
MemZero(current->file, sizeof(struct MemoryBlock));
|
|
|
|
FREE_MEM(current);
|
2018-11-24 21:39:18 +00:00
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
current = next;
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 22:02:29 +00:00
|
|
|
FREE_MEM((void *)m);
|
2018-11-24 21:39:18 +00:00
|
|
|
}
|