amath/lib/clib/mem.c

230 lines
5.4 KiB
C
Raw Normal View History

2015-04-01 12:43:50 +00:00
/*
2017-01-11 18:27:01 +00:00
* Copyright (c) 2015-2017 Carsten Sonne Larsen <cs@innolan.dk>
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.
*
*/
#include "mem.h"
#include "clib.h"
#ifdef AMIGA
#include <stddef.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/semaphores.h>
#include <clib/exec_protos.h>
#define ALLOC_MEM(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR)
#define FREE_MEM(x) FreeVec(x)
2017-01-24 19:39:55 +00:00
#define Debug(x,y,z)
2015-04-01 12:43:50 +00:00
#else
2017-02-13 20:02:55 +00:00
#include <stdlib.h>
#define ALLOC_MEM(x) calloc(1L,x)
#define FREE_MEM(x) free(x)
#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__) || \
defined(_M_AMD64) || defined(_M_ARM64) || \
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
{
struct MemoryBlock *next;
size_t size;
void *address;
};
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
{
struct MemoryBlock *first;
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.
*/
2015-04-01 12:43:50 +00:00
struct MemoryList *list = NULL;
void alloc_error(char*, size_t);
void dealloc_error(char*, void*);
2017-01-24 19:39:55 +00:00
/**
* @brief Allocate memory and add it to the global memory list.
*/
2015-04-01 12:43:50 +00:00
void* AllocMemSafe(size_t size)
{
struct MemoryBlock *newblock;
size_t allocsize;
if (list == NULL) {
list = (struct MemoryList*)ALLOC_MEM(sizeof(struct MemoryList));
if (!list) {
alloc_error("list", sizeof(struct MemoryList));
return 0;
}
list->first = NULL;
list->peak = 0;
list->size = 0;
list->count = 0;
}
2017-02-04 11:55:27 +00:00
#ifdef P64BIT
2017-01-21 19:51:25 +00:00
// Align to bytes of 8
allocsize = (size + 7) & ~0x07;
#else
2015-04-01 12:43:50 +00:00
// Align to bytes of 4
allocsize = (size + 3) & ~0x03;
2017-01-21 19:51:25 +00:00
#endif
2015-04-01 12:43:50 +00:00
newblock = (struct MemoryBlock*)ALLOC_MEM(sizeof(struct MemoryBlock));
if (!newblock) {
alloc_error("block", sizeof(struct MemoryBlock));
return 0;
}
newblock->address = (struct MemoryBlock*)ALLOC_MEM(allocsize);
if (!newblock->address) {
FREE_MEM(newblock);
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++;
if (list->size > list->peak) {
list->peak = list->size;
}
2017-02-13 20:02:55 +00:00
// Memory allocated
2015-04-01 12:43:50 +00:00
return newblock->address;
}
2017-01-24 19:39:55 +00:00
/**
* @brief Deallocate memory from the global memory list.
*/
2015-04-01 12:43:50 +00:00
void FreeMemSafe(void* block)
{
2017-01-24 19:39:55 +00:00
struct MemoryBlock *current, *previous;
2015-04-01 12:43:50 +00:00
if (list == NULL || block == NULL) {
dealloc_error("list", 0);
return;
}
if (block == NULL) {
dealloc_error("memory", 0);
return;
}
2017-01-24 19:39:55 +00:00
previous = NULL;
2015-04-01 12:43:50 +00:00
current = list->first;
while (current != NULL && current->address != block) {
2017-01-24 19:39:55 +00:00
previous = current;
2015-04-01 12:43:50 +00:00
current = current->next;
}
if (current == NULL) {
dealloc_error("address not found", block);
return;
}
2017-01-24 19:39:55 +00:00
if (previous == NULL) {
list->first = current->next;
2015-04-01 12:43:50 +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--;
2015-04-01 12:43:50 +00:00
FREE_MEM(current->address);
2017-01-24 19:39:55 +00:00
current->address = NULL;
current->next = NULL;
current->size = 0;
2015-04-01 12:43:50 +00:00
FREE_MEM(current);
}
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;
if (list == NULL) {
return;
}
current = list->first;
while (current != NULL) {
next = current->next;
FREE_MEM(current->address);
FREE_MEM(current);
current = next;
}
FREE_MEM(list);
list = NULL;
}
2017-01-24 19:39:55 +00:00
/**
* @brief Get memory usage in the global memory list.
*/
2015-04-01 12:43:50 +00:00
void MemUsage(long *blocks, long *size, long *peak)
{
*blocks = list->count;
2017-02-04 13:41:34 +00:00
*size = (long)list->size;
*peak = (long)list->peak;;
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
/**
2017-02-13 20:02:55 +00:00
* @brief Log a memory allocation error
2017-01-24 19:39:55 +00:00
*/
2015-04-01 12:43:50 +00:00
void alloc_error(char *descr, size_t size)
{
2017-02-13 20:02:55 +00:00
Debug("Memory allocation error (%s) with size (%d)\n", descr, size);
2015-04-01 12:43:50 +00:00
}
2017-01-24 19:39:55 +00:00
/**
2017-02-13 20:02:55 +00:00
* @brief Log a memory deallocation error
2017-01-24 19:39:55 +00:00
*/
2015-04-01 12:43:50 +00:00
void dealloc_error(char *descr, void *p)
{
2017-02-13 20:02:55 +00:00
Debug("Memory deallocation error (%s) address (%x)\n", descr, p);
2015-04-01 12:43:50 +00:00
}