298 lines
6.4 KiB
C
298 lines
6.4 KiB
C
/*-
|
|
* Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
|
|
* 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.
|
|
*
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
#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)
|
|
|
|
#include "compiler.h"
|
|
#include "log.h"
|
|
int StrLen(const char *);
|
|
|
|
#if defined(__x86_64__) || defined(__aarch64__) || \
|
|
defined(_M_AMD64) || defined(_M_ARM64) || \
|
|
defined(__powerpc64__)
|
|
#define P64BIT
|
|
#endif
|
|
|
|
#ifdef DEBUG_BUILD
|
|
#define MEM_TRACE 1
|
|
#endif
|
|
|
|
/**
|
|
* @brief Block of allocated memory.
|
|
*/
|
|
struct MemoryBlock
|
|
{
|
|
struct MemoryBlock *next;
|
|
size_t size;
|
|
void *address;
|
|
};
|
|
|
|
/**
|
|
* @brief List of allocated memory. Uses the LIFO principle.
|
|
*/
|
|
struct MemoryList
|
|
{
|
|
struct MemoryBlock *first;
|
|
size_t peak;
|
|
size_t size;
|
|
long count;
|
|
};
|
|
|
|
/**
|
|
* @brief Global list of allocated memory.
|
|
*/
|
|
static struct MemoryList *list = NULL;
|
|
|
|
void AllocationError(char *, size_t);
|
|
void DeAllocationError(char *, void *);
|
|
|
|
/**
|
|
* @brief Allocate memory and add it to the global memory list.
|
|
*/
|
|
void *AllocMemSafe(size_t size)
|
|
{
|
|
struct MemoryBlock *newblock;
|
|
size_t allocsize;
|
|
|
|
Forbid();
|
|
if (list == NULL)
|
|
{
|
|
list = (struct MemoryList *)ALLOC_MEM(sizeof(struct MemoryList));
|
|
if (!list)
|
|
{
|
|
AllocationError("list", sizeof(struct MemoryList));
|
|
return 0;
|
|
}
|
|
|
|
list->first = NULL;
|
|
list->peak = 0;
|
|
list->size = 0;
|
|
list->count = 0;
|
|
}
|
|
|
|
#ifdef P64BIT
|
|
// Align to bytes of 8
|
|
allocsize = (size + 7) & ~0x07;
|
|
#else
|
|
// Align to bytes of 4
|
|
allocsize = (size + 3) & ~0x03;
|
|
#endif
|
|
|
|
newblock = (struct MemoryBlock *)ALLOC_MEM(sizeof(struct MemoryBlock));
|
|
if (!newblock)
|
|
{
|
|
AllocationError("block", sizeof(struct MemoryBlock));
|
|
return 0;
|
|
}
|
|
|
|
newblock->address = (struct MemoryBlock *)ALLOC_MEM(allocsize);
|
|
if (!newblock->address)
|
|
{
|
|
FREE_MEM(newblock);
|
|
AllocationError("memory", allocsize);
|
|
return 0;
|
|
}
|
|
|
|
newblock->size = allocsize;
|
|
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
|
|
Printf("Allocated memory block off %ld bytes at 0x%lx\n",
|
|
newblock->size, newblock->address);
|
|
#endif
|
|
|
|
// Memory allocated
|
|
return newblock->address;
|
|
}
|
|
|
|
void RemoveMemSafe(void *block, bool deallocate)
|
|
{
|
|
struct MemoryBlock *current, *previous;
|
|
|
|
Forbid();
|
|
if (list == NULL || block == NULL)
|
|
{
|
|
DeAllocationError("list", 0);
|
|
return;
|
|
}
|
|
|
|
if (block == NULL)
|
|
{
|
|
DeAllocationError("memory", 0);
|
|
return;
|
|
}
|
|
|
|
previous = NULL;
|
|
current = list->first;
|
|
while (current != NULL && current->address != block)
|
|
{
|
|
previous = current;
|
|
current = current->next;
|
|
}
|
|
|
|
if (current == NULL)
|
|
{
|
|
DeAllocationError("address not found", block);
|
|
return;
|
|
}
|
|
|
|
if (previous == NULL)
|
|
{
|
|
list->first = current->next;
|
|
}
|
|
else
|
|
{
|
|
previous->next = current->next;
|
|
}
|
|
|
|
list->size -= current->size;
|
|
list->count--;
|
|
|
|
if (deallocate)
|
|
{
|
|
FREE_MEM(current->address);
|
|
}
|
|
|
|
current->address = NULL;
|
|
current->next = NULL;
|
|
current->size = 0;
|
|
FREE_MEM(current);
|
|
Permit();
|
|
}
|
|
|
|
/**
|
|
* @brief Deallocate memory from the global memory list.
|
|
*/
|
|
void FreeMemSafe(void *block)
|
|
{
|
|
RemoveMemSafe(block, true);
|
|
}
|
|
|
|
void *MemDupSafe(const void *s1, size_t len)
|
|
{
|
|
char *dup;
|
|
dup = AllocMemSafe(len);
|
|
CopyMem((void *)s1, dup, len);
|
|
return dup;
|
|
}
|
|
|
|
char *StrDupSafe(const char *s1)
|
|
{
|
|
char *s2;
|
|
size_t len = s1 != NULL ? StrLen(s1) : 1;
|
|
s2 = AllocMemSafe(++len);
|
|
|
|
if (s2 == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
CopyMem((void *)s1, s2, --len);
|
|
return s2;
|
|
}
|
|
|
|
/**
|
|
* @brief Deallocate all memory in the global memory list.
|
|
*/
|
|
void FreeAllSafe(void)
|
|
{
|
|
struct MemoryBlock *current, *next;
|
|
|
|
Forbid();
|
|
if (list == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
current = list->first;
|
|
while (current != NULL)
|
|
{
|
|
LogTrace("Released forgotten memory block on %ld bytes at 0x%lx",
|
|
(long)current->size,
|
|
(unsigned long)current->address);
|
|
next = current->next;
|
|
FREE_MEM(current->address);
|
|
FREE_MEM(current);
|
|
current = next;
|
|
}
|
|
|
|
FREE_MEM(list);
|
|
list = NULL;
|
|
Permit();
|
|
}
|
|
|
|
/**
|
|
* @brief Get memory usage in the global memory list.
|
|
*/
|
|
void MemUsage(long *blocks, long *size, long *peak)
|
|
{
|
|
if (blocks != NULL)
|
|
{
|
|
*blocks = list->count;
|
|
}
|
|
|
|
if (size != NULL)
|
|
{
|
|
*size = (long)list->size;
|
|
}
|
|
|
|
if (peak != NULL)
|
|
{
|
|
*peak = (long)list->peak;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Log a memory allocation error.
|
|
*/
|
|
void AllocationError(char *descr, size_t size)
|
|
{
|
|
LogTrace("Memory allocation error (%s) with size (%ld)", descr, (long)size);
|
|
}
|
|
|
|
/**
|
|
* @brief Log a memory deallocation error.
|
|
*/
|
|
void DeAllocationError(char *descr, void *p)
|
|
{
|
|
LogTrace("Memory deallocation error (%s) at 0x%lx", descr, p);
|
|
}
|