amath/src/clib/mem.c

304 lines
6.9 KiB
C
Raw Normal View History

2017-02-27 22:23:06 +00:00
/*-
2021-01-11 19:37:42 +00:00
* Copyright (c) 2014-2021 Carsten Sonne Larsen <cs@innolan.net>
2015-04-01 12:43:50 +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.
*
2017-02-27 22:31:21 +00:00
* Project homepage:
2017-07-14 09:30:24 +00:00
* https://amath.innolan.net
2021-01-11 19:37:42 +00:00
*
2015-04-01 12:43:50 +00:00
*/
2017-03-11 22:37:45 +00:00
2017-02-27 22:23:06 +00:00
#include "amathc.h"
2015-04-01 12:43:50 +00:00
2017-04-15 18:36:40 +00:00
#if defined(AMIGA)
2015-04-01 12:43:50 +00:00
#include <stddef.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/semaphores.h>
#include <clib/exec_protos.h>
2021-01-30 16:00:55 +00:00
#define ALLOC_LIST(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR)
#define FREE_LIST(x) FreeVec(x)
2021-01-30 16:42:13 +00:00
#define ALLOC_MEM(x, y) AllocPooledZero(x, y)
2021-01-30 16:00:55 +00:00
#define FREE_MEM(x, y, z) FreePooled(x, y, z)
#define Debug(x, y, z)
2015-04-01 12:43:50 +00:00
#else
2017-02-27 22:23:06 +00:00
#include <stdlib.h>
2021-01-30 16:00:55 +00:00
#define ALLOC_LIST(x) calloc(1L, x)
#define FREE_LIST(x) free(x)
#define ALLOC_MEM(x, y) calloc(1L, y)
#define FREE_MEM(x, y, z) free(y)
#define Debug(x, y, z)
2015-04-01 12:43:50 +00:00
#endif
2017-02-04 11:55:27 +00:00
#if defined(__x86_64__) || defined(__aarch64__) || \
2021-01-30 16:00:55 +00:00
defined(_M_AMD64) || defined(_M_ARM64) || \
2017-02-04 11:55:27 +00:00
defined(__powerpc64__)
#define P64BIT
#endif
2017-01-24 19:39:55 +00:00
/**
* @brief Block of allocated memory.
*/
2015-04-01 12:43:50 +00:00
struct MemoryBlock
{
2021-01-30 16:00:55 +00:00
struct MemoryBlock *next;
2015-04-01 12:43:50 +00:00
size_t size;
2021-01-30 16:00:55 +00:00
void *address;
2015-04-01 12:43:50 +00:00
};
2017-01-24 19:39:55 +00:00
/**
* @brief List of allocated memory. Uses the LIFO principle.
*/
2015-04-01 12:43:50 +00:00
struct MemoryList
{
2021-01-30 16:00:55 +00:00
struct MemoryBlock *first;
void *pool;
2017-02-04 11:55:27 +00:00
size_t peak;
size_t size;
2015-04-01 12:43:50 +00:00
long count;
};
2017-01-24 19:39:55 +00:00
/**
* @brief Global list of allocated memory.
*/
2021-01-30 16:00:55 +00:00
struct MemoryList *list = nullptr;
2015-04-01 12:43:50 +00:00
2021-01-30 16:00:55 +00:00
void alloc_error(char *, size_t);
void dealloc_error(char *, void *);
2015-04-01 12:43:50 +00:00
2021-01-30 16:42:13 +00:00
#if defined(AMIGA)
static void MemZero(void *address, ULONG size)
{
char *c = (char *)address;
int n = size;
do
{
*c++ = '\0';
} while (--n);
}
static void *AllocPooledZero(void *pool, size_t size)
{
ULONG memSize = (ULONG)size;
void *memory = AllocPooled(pool, memSize);
if (memory != NULL)
{
MemZero(memory, memSize);
}
return memory;
}
#endif
2017-01-24 19:39:55 +00:00
/**
* @brief Allocate memory and add it to the global memory list.
*/
2021-01-30 16:00:55 +00:00
void *AllocMemSafe(size_t size)
2015-04-01 12:43:50 +00:00
{
2021-01-30 16:00:55 +00:00
struct MemoryBlock *newblock;
2015-04-01 12:43:50 +00:00
size_t allocsize;
2017-03-24 19:09:05 +00:00
if (list == nullptr)
2017-02-27 22:23:06 +00:00
{
2021-01-30 16:00:55 +00:00
list = (struct MemoryList *)ALLOC_LIST(sizeof(struct MemoryList));
2017-02-27 22:23:06 +00:00
if (!list)
{
2015-04-01 12:43:50 +00:00
alloc_error("list", sizeof(struct MemoryList));
return 0;
}
2021-01-30 16:00:55 +00:00
#if defined(AMIGA)
list->pool = CreatePool(MEMF_ANY, 4096, 512);
#endif
2017-03-24 19:09:05 +00:00
list->first = nullptr;
2015-04-01 12:43:50 +00:00
list->peak = 0;
list->size = 0;
list->count = 0;
}
2017-02-04 11:55:27 +00:00
#ifdef P64BIT
2021-01-30 16:42:13 +00:00
// Align to bytes of 8, remove 0 bytes allocations
allocsize = (size + 8) & ~0x07;
2017-01-21 19:51:25 +00:00
#else
2021-01-30 16:42:13 +00:00
// Align to bytes of 4, remove 0 bytes allocations
allocsize = (size + 4) & ~0x03;
2017-01-21 19:51:25 +00:00
#endif
2015-04-01 12:43:50 +00:00
2021-01-30 16:00:55 +00:00
newblock = (struct MemoryBlock *)ALLOC_MEM(list->pool, sizeof(struct MemoryBlock));
if (newblock == NULL)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
alloc_error("block", sizeof(struct MemoryBlock));
return 0;
}
2021-01-30 16:00:55 +00:00
newblock->address = (struct MemoryBlock *)ALLOC_MEM(list->pool, allocsize);
if (newblock->address == NULL)
2017-02-27 22:23:06 +00:00
{
2021-01-30 16:00:55 +00:00
FREE_MEM(list->pool, newblock, sizeof(struct MemoryBlock));
2015-04-01 12:43:50 +00:00
alloc_error("memory", allocsize);
return 0;
}
newblock->size = allocsize;
2017-01-24 19:39:55 +00:00
newblock->next = list->first;
list->first = newblock;
2015-04-01 12:43:50 +00:00
list->size += allocsize;
list->count++;
2017-02-27 22:23:06 +00:00
if (list->size > list->peak)
{
2015-04-01 12:43:50 +00:00
list->peak = list->size;
}
2017-02-27 22:23:06 +00:00
// Memory allocated
2015-04-01 12:43:50 +00:00
return newblock->address;
}
2021-01-30 16:00:55 +00:00
void RemoveMemSafe(void *block, bool deallocate)
2015-04-01 12:43:50 +00:00
{
2017-01-24 19:39:55 +00:00
struct MemoryBlock *current, *previous;
2015-04-01 12:43:50 +00:00
2017-03-24 19:09:05 +00:00
if (list == nullptr || block == nullptr)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
dealloc_error("list", 0);
return;
}
2017-03-24 19:09:05 +00:00
if (block == nullptr)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
dealloc_error("memory", 0);
return;
}
2017-03-24 19:09:05 +00:00
previous = nullptr;
2015-04-01 12:43:50 +00:00
current = list->first;
2017-03-24 19:09:05 +00:00
while (current != nullptr && current->address != block)
2017-02-27 22:23:06 +00:00
{
2017-01-24 19:39:55 +00:00
previous = current;
2015-04-01 12:43:50 +00:00
current = current->next;
}
2017-03-24 19:09:05 +00:00
if (current == nullptr)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
dealloc_error("address not found", block);
return;
}
2017-03-24 19:09:05 +00:00
if (previous == nullptr)
2017-02-27 22:23:06 +00:00
{
2017-01-24 19:39:55 +00:00
list->first = current->next;
2017-02-27 22:23:06 +00:00
}
else
{
2017-01-24 19:39:55 +00:00
previous->next = current->next;
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
list->size -= current->size;
list->count--;
2017-03-24 19:09:05 +00:00
if (deallocate)
{
2021-01-30 16:00:55 +00:00
FREE_MEM(list->pool, current->address, current->size);
2017-03-24 19:09:05 +00:00
}
current->address = nullptr;
current->next = nullptr;
2017-01-24 19:39:55 +00:00
current->size = 0;
2021-01-30 16:00:55 +00:00
FREE_MEM(list->pool, current, sizeof(struct MemoryBlock));
2015-04-01 12:43:50 +00:00
}
2017-03-24 19:09:05 +00:00
/**
* @brief Deallocate memory from the global memory list.
*/
2021-01-30 16:00:55 +00:00
void FreeMemSafe(void *block)
2017-03-24 19:09:05 +00:00
{
RemoveMemSafe(block, true);
}
/**
* @brief Detach an allocated memory from the global memory list.
* @details The memory block is only detached, not deallocated.
*/
2021-01-30 16:00:55 +00:00
void DetachMemSafe(void *block)
2017-03-24 19:09:05 +00:00
{
RemoveMemSafe(block, false);
}
2017-01-24 19:39:55 +00:00
/**
* @brief Deallocate all memory in the global memory list.
*/
2015-04-01 12:43:50 +00:00
void FreeAllSafe()
{
struct MemoryBlock *current, *next;
2017-03-24 19:09:05 +00:00
if (list == nullptr)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
return;
}
current = list->first;
2017-03-24 19:09:05 +00:00
while (current != nullptr)
2017-02-27 22:23:06 +00:00
{
2015-04-01 12:43:50 +00:00
next = current->next;
2021-01-30 16:00:55 +00:00
FREE_MEM(list->pool, current->address, current->size);
FREE_MEM(list->pool, current, sizeof(struct MemoryBlock));
2015-04-01 12:43:50 +00:00
current = next;
}
2021-01-30 16:00:55 +00:00
#if defined(AMIGA)
DeletePool(list->pool);
#endif
FREE_LIST(list);
2017-03-24 19:09:05 +00:00
list = nullptr;
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
/**
* @brief Get memory usage in the global memory list.
*/
2021-01-30 16:00:55 +00:00
void MemUsage(long *blocks, long *size, long *peak)
2015-04-01 12:43:50 +00:00
{
*blocks = list->count;
2017-02-04 13:41:34 +00:00
*size = (long)list->size;
2021-01-30 16:00:55 +00:00
*peak = (long)list->peak;
;
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
/**
2017-02-27 22:23:06 +00:00
* @brief Log a memory allocation error
2017-01-24 19:39:55 +00:00
*/
2021-01-30 16:00:55 +00:00
void alloc_error(char *descr, size_t size)
2015-04-01 12:43:50 +00:00
{
2017-02-27 22:23:06 +00:00
Debug("Memory allocation error (%s) with size (%d)\n", descr, size);
//if (size == 0)
// exit(10);
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
/**
2017-02-27 22:23:06 +00:00
* @brief Log a memory deallocation error
2017-01-24 19:39:55 +00:00
*/
2021-01-30 16:00:55 +00:00
void dealloc_error(char *descr, void *p)
2015-04-01 12:43:50 +00:00
{
2017-02-27 22:23:06 +00:00
Debug("Memory deallocation error (%s) address (%x)\n", descr, p);
2015-04-01 12:43:50 +00:00
}