mirror of
https://frontier.innolan.net/rainlance/amiga-ntimed.git
synced 2025-11-21 13:36:38 +00:00
Added Amiga OS memory management
This commit is contained in:
201
mem.c
Normal file
201
mem.c
Normal file
@ -0,0 +1,201 @@
|
||||
/*
|
||||
* Copyright (c) 2015 Carsten Larsen
|
||||
* 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 <clib/exec_protos.h>
|
||||
|
||||
#include "ntimed.h"
|
||||
#include "mem.h"
|
||||
|
||||
struct MemoryBlock
|
||||
{
|
||||
struct MemoryBlock *next;
|
||||
size_t size;
|
||||
void *address;
|
||||
};
|
||||
|
||||
struct MemoryList
|
||||
{
|
||||
struct MemoryBlock *first;
|
||||
struct MemoryBlock *last;
|
||||
long size;
|
||||
long count;
|
||||
};
|
||||
|
||||
struct MemoryList *list = NULL;
|
||||
|
||||
void allocerror(char*, size_t);
|
||||
void deallocerror(char*, void*);
|
||||
|
||||
void* allocmem(size_t size)
|
||||
{
|
||||
struct MemoryBlock *newblock;
|
||||
size_t allocsize;
|
||||
|
||||
if (list == NULL) {
|
||||
list = (struct MemoryList*)AllocVec(sizeof(struct MemoryList), MEMF_ANY | MEMF_CLEAR);
|
||||
if (!list) {
|
||||
allocerror("list", sizeof(struct MemoryList));
|
||||
return 0;
|
||||
}
|
||||
|
||||
list->first = NULL;
|
||||
list->last = NULL;
|
||||
list->size = 0;
|
||||
list->count = 0;
|
||||
}
|
||||
|
||||
// Align to bytes of 4
|
||||
allocsize = (size + 3) & ~0x03;
|
||||
|
||||
newblock = (struct MemoryBlock*)AllocVec(sizeof(struct MemoryBlock), MEMF_ANY | MEMF_CLEAR);
|
||||
if (!newblock) {
|
||||
allocerror("block", sizeof(struct MemoryBlock));
|
||||
return 0;
|
||||
}
|
||||
|
||||
newblock->address = (struct MemoryBlock*)AllocVec(allocsize, MEMF_ANY | MEMF_CLEAR);
|
||||
if (!newblock->address) {
|
||||
FreeVec(newblock);
|
||||
allocerror("memory", allocsize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
newblock->size = allocsize;
|
||||
newblock->next = NULL;
|
||||
|
||||
if(list->first == NULL) {
|
||||
list->first = newblock;
|
||||
list->last = newblock;
|
||||
} else {
|
||||
list->last->next = newblock;
|
||||
list->last = newblock;
|
||||
}
|
||||
|
||||
list->size += allocsize;
|
||||
list->count++;
|
||||
|
||||
// DEBUG code
|
||||
// Debug(NULL, "Mememory allocated at address (%x)\n", newblock->address);
|
||||
|
||||
return newblock->address;
|
||||
}
|
||||
|
||||
void freemem(void* block)
|
||||
{
|
||||
struct MemoryBlock *current, *last;
|
||||
|
||||
if (list == NULL || block == NULL) {
|
||||
deallocerror("list", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (block == NULL) {
|
||||
deallocerror("memory", 0);
|
||||
return;
|
||||
}
|
||||
|
||||
last = NULL;
|
||||
current = list->first;
|
||||
while (current != NULL && current->address != block) {
|
||||
last = current;
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
if (current == NULL) {
|
||||
deallocerror("address not found", block);
|
||||
return;
|
||||
}
|
||||
|
||||
list->size -= current->size;
|
||||
list->count--;
|
||||
|
||||
if (list->first == current) {
|
||||
list->first = NULL;
|
||||
list->last = NULL;
|
||||
} else if (list->last == current) {
|
||||
last->next = current->next;
|
||||
list->last = last;
|
||||
} else {
|
||||
last->next = current->next;
|
||||
}
|
||||
|
||||
FreeVec(current->address);
|
||||
FreeVec(current);
|
||||
|
||||
// DEBUG code
|
||||
// Debug(NULL, "Mememory deallocated at address (%x)\n", block);
|
||||
}
|
||||
|
||||
void freeall()
|
||||
{
|
||||
struct MemoryBlock *current, *next;
|
||||
|
||||
if (list == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
current = list->first;
|
||||
while (current != NULL) {
|
||||
next = current->next;
|
||||
FreeVec(current->address);
|
||||
FreeVec(current);
|
||||
current = next;
|
||||
}
|
||||
|
||||
FreeVec(list);
|
||||
list = NULL;
|
||||
}
|
||||
|
||||
void allocerror(char *descr, size_t size)
|
||||
{
|
||||
Debug(NULL, "Mememory allocation error (%s) with size (%d)\n", descr, size);
|
||||
}
|
||||
|
||||
void deallocerror(char *descr, void *p)
|
||||
{
|
||||
Debug(NULL, "Mememory deallocation error (%s) address (%x)\n", descr, p);
|
||||
}
|
||||
|
||||
#ifdef __c_plus
|
||||
inline void* operator new (size_t size) {
|
||||
return allocmem(size);
|
||||
}
|
||||
|
||||
inline void* operator new[] (size_t size) {
|
||||
return allocmem(size);
|
||||
}
|
||||
|
||||
inline void operator delete (void* ptr) {
|
||||
freemem(ptr);
|
||||
}
|
||||
|
||||
inline void operator delete[] (void* ptr) {
|
||||
freemem(ptr);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user