(FEA) Featured for Location_Literalisation, Status_Literalisation etc.

This commit is contained in:
William
2024-06-06 02:22:54 +08:00
parent df073877cc
commit 54042cf2cf
28 changed files with 744 additions and 358 deletions

View File

@@ -4,15 +4,14 @@
# include <Compound/common.h>
# include <Compound/status.h>
/*
Higher the priority it is, the less time for lifespan it has.
*/
/* Higher the priority it is, the less lifespan it has. */
typedef struct {
void *addr;
size_t length;
size_t size;
int priority; // Negative for manual; Higher than 0 it is,
// higher the priority is.
bool alive;
} Memory; // 20 Bytes
typedef struct {
@@ -27,27 +26,27 @@ typedef struct {
void (*deallocator)(void *addr);
} MemoryPoolManager;
Status MemMan_Memory_Allocate(Memory *inst, size_t length);
Status MemMan_Memory_Reallocate(Memory *inst, size_t length);
void MemMan_Memory_Release(Memory *inst);
Status MemMan_Memory_Prioritise(Memory *inst);
Status MemMan_Memory_Deprioritise(Memory *inst);
bool MemMan_Memory_Equals(Memory *inst, Memory *other);
Status Memory_Create(Memory *inst, size_t size);
Status Memory_Delete(Memory *inst);
Status Memory_Allocate(Memory *inst);
Status Memory_Reallocate(Memory *inst, size_t size);
Status Memory_Release(Memory *inst);
bool Memory_Equals(Memory *inst, Memory *other);
Status MemMan_MemoryPool_Create(MemoryPool *inst, size_t volume);
Status MemMan_MemoryPool_Constr(MemoryPool *inst, size_t volume, int priority);
Status MemMan_MemoryPool_AllocateAt(MemoryPool *inst, int idx, size_t sz);
Status MemMan_MemoryPool_ReallocateAt(MemoryPool *inst, int idx, size_t sz);
Status MemMan_MemoryPool_ReleaseAt(MemoryPool *inst, int idx);
void MemMan_MemoryPool_Delete(MemoryPool *inst);
Status MemoryPool_Create(MemoryPool *inst, size_t volume);
Status MemoryPool_Constr(MemoryPool *inst, size_t volume, int priority);
Status MemoryPool_AllocateAt(MemoryPool *inst, int idx, size_t sz);
Status MemoryPool_ReallocateAt(MemoryPool *inst, int idx, size_t sz);
Status MemoryPool_ReleaseAt(MemoryPool *inst, int idx);
void MemoryPool_Delete(MemoryPool *inst);
Status MemMan_MemoryPoolManager_Create(MemoryPoolManager *inst,
Status MemoryPoolManager_Create(MemoryPoolManager *inst,
MemoryPool **members);
Status MemMan_MemoryPoolManager_Constr(MemoryPoolManager *inst,
Status MemoryPoolManager_Constr(MemoryPoolManager *inst,
MemoryPool **members,
void *(*allocator)(size_t sz),
void (*deallocator)(void *addr));
Status MemMan_MemoryPoolManager_Merge(MemoryPool *pool1, MemoryPool *pool2);
Status MemMan_MemoryPoolManager_Divide(MemoryPool *src, int off, int len);
Status MemoryPoolManager_Merge(MemoryPool *pool1, MemoryPool *pool2);
Status MemoryPoolManager_Divide(MemoryPool *src, int off, int len);
#endif /* COMPOUND_MEMMAN_H */

View File

@@ -1,35 +1,75 @@
#include <Compound/common.h>
#include <Compound/memman.h>
/*
enum {
MEMMAN_RELEASE_LEVEL_INSTANTAL = 0,
MEMMAN_RELEASE_LEVEL_STACK = 1,
MEMMAN_RELEASE_LEVEL_HEAP = 2
};
typedef struct {
void *addr;
int release_level;
} Memory;
typedef struct {
Memory *members;
int release_level;
} MemoryPool;
typedef struct {
MemoryPool *members;
void *(*allocator)(size_t sz);
void (*delocator)(void *addr);
} MemoryPoolManager;
*/
int memman_memorypoolmanager_create(MemoryPoolManager *inst,
MemoryPool **membersptr)
Status Memory_Create(Memory *inst, size_t size)
{
fails(inst, COMMON_ERROR_INVALID_ARGUMENT);
fails(membersptr, COMMON_ERROR_INVALID_ARGUMENT);
fails(inst, UnavailableInstance);
*inst = (Memory) {
.addr = NULL,
.size = size,
.priority = 0,
.alive = false
};
return NormalStatus;
}
Status Memory_Allocate(Memory *inst)
{
fails(inst, UnavailableInstance);
state(inst->alive, InstanceStillAlive);
/* When failed on allocating. */
state(!(inst->addr = malloc(inst->size)), InsufficientMemory);
inst->alive = true;
return NormalStatus;
}
Status Memory_Reallocate(Memory *inst, size_t size)
{
fails(inst, UnavailableBuffer);
state(!inst->alive, InstanceNotAlive);
/* When failed on reallocating. */
state(!(inst->addr = realloc(inst->addr, size)),
error(InsufficientMemory, "Unsuccessful reallocation was received."))
return NormalStatus;
}
Status Memory_Release(Memory *inst)
{
fails(inst, UnavailableInstance);
state(!inst->alive, error(InstanceNotAlive, "Cannot release a non-alive "
"instance."));
free(inst->addr);
inst->alive = false;
return NormalStatus;
}
Status Memory_Delete(Memory *inst)
{
fails(inst, UnavailableInstance);
state(inst->alive, error(InstanceStillAlive, "Cannot deinitialise a instance "
"still alive."));
inst->addr = NULL;
inst->priority = 0;
inst->size = 0;
inst = NULL;
return NormalStatus;
}
bool Memory_Equals(Memory *inst, Memory *other)
{
state(!inst || !other, false);
return (inst->addr == other->addr
&& inst->size == other->size
&& inst->priority == other->priority
&& inst->alive == other->alive);
}