mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
use new _vd element for possible memtype state
This commit is contained in:
parent
92952f9321
commit
78f5a3f818
3 changed files with 33 additions and 36 deletions
|
@ -33,28 +33,23 @@ struct memtype {
|
|||
|
||||
memzone_allocator_t alloc;
|
||||
memzone_deallocator_t free;
|
||||
|
||||
void *_vd; /**<Virtual data for possible state */
|
||||
};
|
||||
|
||||
enum memblock_flags {
|
||||
MEMBLOCK_USED = 1,
|
||||
};
|
||||
|
||||
// Descriptor of a memory block. Associated block always starts at
|
||||
// &m + sizeof(struct memblock).
|
||||
/** Descriptor of a memory block. Associated block always starts at
|
||||
* &m + sizeof(struct memblock). */
|
||||
struct memblock {
|
||||
struct memblock* prev;
|
||||
struct memblock* next;
|
||||
size_t len; // lenght of the block; doesn't include the descriptor itself
|
||||
size_t len; /**<Length of the block; doesn't include the descriptor itself */
|
||||
int flags;
|
||||
};
|
||||
|
||||
struct memtype_managed {
|
||||
struct memtype mt;
|
||||
void *base;
|
||||
size_t len;
|
||||
struct memblock *first;
|
||||
};
|
||||
|
||||
/** @todo Unused for now */
|
||||
struct memzone {
|
||||
struct memtype * const type;
|
||||
|
|
51
lib/memory.c
51
lib/memory.c
|
@ -106,9 +106,9 @@ static int memory_hugepage_free(struct memtype *m, void *ptr, size_t len)
|
|||
void* memory_managed_alloc(struct memtype *m, size_t len, size_t alignment)
|
||||
{
|
||||
/* Simple first-fit allocation */
|
||||
struct memtype_managed* man = (struct memtype_managed*) m;
|
||||
struct memblock* block;
|
||||
for (block = man->first; block != NULL; block = block->next) {
|
||||
struct memblock *first = (struct memblock*) m->_vd;
|
||||
struct memblock *block;
|
||||
for (block = first; block != NULL; block = block->next) {
|
||||
if (block->flags & MEMBLOCK_USED)
|
||||
continue;
|
||||
char* cptr = (char*) block + sizeof(struct memblock);
|
||||
|
@ -171,10 +171,10 @@ void* memory_managed_alloc(struct memtype *m, size_t len, size_t alignment)
|
|||
|
||||
int memory_managed_free(struct memtype *m, void *ptr, size_t len)
|
||||
{
|
||||
struct memtype_managed* man = (struct memtype_managed*) m;
|
||||
struct memblock *first = m->_vd;
|
||||
struct memblock *block;
|
||||
char* cptr = (char*) ptr;
|
||||
struct memblock* block;
|
||||
for (block = man->first; block != NULL; block = block->next) {
|
||||
for (block = first; block != NULL; block = block->next) {
|
||||
if (!(block->flags & MEMBLOCK_USED))
|
||||
continue;
|
||||
/* since we may waste some memory at the start of a block to ensure
|
||||
|
@ -211,29 +211,28 @@ int memory_managed_free(struct memtype *m, void *ptr, size_t len)
|
|||
|
||||
struct memtype* memtype_managed_init(void *ptr, size_t len)
|
||||
{
|
||||
if (len < sizeof(struct memtype_managed) + sizeof(struct memblock)) {
|
||||
info("memtype_managed: passed region too small");
|
||||
if (len < sizeof(struct memtype) + sizeof(struct memblock)) {
|
||||
info("memtype_managed_init: passed region too small");
|
||||
return NULL;
|
||||
}
|
||||
struct memtype_managed *man = (struct memtype_managed*) ptr;
|
||||
man->mt.name = "managed";
|
||||
man->mt.flags = 0;
|
||||
man->mt.alloc = memory_managed_alloc;
|
||||
man->mt.free = memory_managed_free;
|
||||
man->mt.alignment = 1;
|
||||
man->base = ptr;
|
||||
man->len = len;
|
||||
struct memtype *mt = (struct memtype*) ptr;
|
||||
mt->name = "managed";
|
||||
mt->flags = 0;
|
||||
mt->alloc = memory_managed_alloc;
|
||||
mt->free = memory_managed_free;
|
||||
mt->alignment = 1;
|
||||
|
||||
char *cptr = (char*) ptr;
|
||||
cptr += ALIGN(sizeof(struct memtype_managed), sizeof(void*));
|
||||
man->first = (struct memblock*) ((void*) cptr);
|
||||
man->first->prev = NULL;
|
||||
man->first->next = NULL;
|
||||
cptr += ALIGN(sizeof(struct memtype), sizeof(void*));
|
||||
struct memblock *first = (struct memblock*) ((void*) cptr);
|
||||
first->prev = NULL;
|
||||
first->next = NULL;
|
||||
cptr += ALIGN(sizeof(struct memblock), sizeof(void*));
|
||||
man->first->len = len - (cptr - (char*) ptr);
|
||||
man->first->flags = 0;
|
||||
first->len = len - (cptr - (char*) ptr);
|
||||
first->flags = 0;
|
||||
mt->_vd = (void*) first;
|
||||
|
||||
return (struct memtype*) man;
|
||||
return mt;
|
||||
}
|
||||
|
||||
/* List of available memory types */
|
||||
|
@ -242,7 +241,8 @@ struct memtype memtype_heap = {
|
|||
.flags = MEMORY_HEAP,
|
||||
.alloc = memory_heap_alloc,
|
||||
.free = memory_heap_free,
|
||||
.alignment = 1
|
||||
.alignment = 1,
|
||||
._vd = NULL,
|
||||
};
|
||||
|
||||
struct memtype memtype_hugepage = {
|
||||
|
@ -250,7 +250,8 @@ struct memtype memtype_hugepage = {
|
|||
.flags = MEMORY_MMAP | MEMORY_HUGEPAGE,
|
||||
.alloc = memory_hugepage_alloc,
|
||||
.free = memory_hugepage_free,
|
||||
.alignment = 21 /* 2 MiB hugepage */
|
||||
.alignment = 21, /* 2 MiB hugepage */
|
||||
._vd = NULL,
|
||||
};
|
||||
|
||||
/** @todo */
|
||||
|
|
|
@ -37,7 +37,7 @@ Theory((size_t len, size_t align, struct memtype *m), memory, aligned) {
|
|||
|
||||
Test(memory, manager) {
|
||||
size_t total_size = 1 << 10;
|
||||
size_t max_block = total_size - sizeof(struct memtype_managed) - sizeof(struct memblock);
|
||||
size_t max_block = total_size - sizeof(struct memtype) - sizeof(struct memblock);
|
||||
void *p = memory_alloc(&memtype_heap, total_size);
|
||||
struct memtype *manager = memtype_managed_init(p, total_size);
|
||||
|
||||
|
@ -66,4 +66,5 @@ Test(memory, manager) {
|
|||
p1 = memory_alloc(manager, max_block);
|
||||
cr_assert(p1);
|
||||
cr_assert(memory_free(manager, p1, max_block) == 0);
|
||||
memory_free(&memtype_heap, p, total_size);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue