diff --git a/include/villas/memory.h b/include/villas/memory.h index a7d370dac..a82991fc2 100644 --- a/include/villas/memory.h +++ b/include/villas/memory.h @@ -73,9 +73,9 @@ int memory_lock(size_t lock); * @retval nullptr If allocation failed. * @retval <>0 If allocation was successful. */ -void * memory_alloc(struct memory_type *m, size_t len); +void * memory_alloc(size_t len, struct memory_type *m = memory_default); -void * memory_alloc_aligned(struct memory_type *m, size_t len, size_t alignment); +void * memory_alloc_aligned(size_t len, size_t alignment, struct memory_type *m = memory_default); int memory_free(void *ptr); diff --git a/include/villas/memory_type.h b/include/villas/memory_type.h index 8e3a97e49..b1167992b 100644 --- a/include/villas/memory_type.h +++ b/include/villas/memory_type.h @@ -30,8 +30,8 @@ struct memory_type; struct node; -typedef struct memory_allocation * (*memory_allocator_t)(struct memory_type *mem, size_t len, size_t alignment); -typedef int (*memory_deallocator_t)(struct memory_type *mem, struct memory_allocation * ma); +typedef struct memory_allocation * (*memory_allocator_t)(size_t len, size_t alignment, struct memory_type *mem); +typedef int (*memory_deallocator_t)(struct memory_allocation * ma, struct memory_type *mem); enum class MemoryFlags { MMAP = (1 << 0), diff --git a/lib/memory.cpp b/lib/memory.cpp index f649ef872..fe5f0ccd2 100644 --- a/lib/memory.cpp +++ b/lib/memory.cpp @@ -105,16 +105,16 @@ int memory_lock(size_t lock) return 0; } -void * memory_alloc(struct memory_type *m, size_t len) +void * memory_alloc(size_t len, struct memory_type *m) { - return memory_alloc_aligned(m, len, sizeof(void *)); + return memory_alloc_aligned(len, sizeof(void *), m); } -void * memory_alloc_aligned(struct memory_type *m, size_t len, size_t alignment) +void * memory_alloc_aligned(size_t len, size_t alignment, struct memory_type *m) { - struct memory_allocation *ma = m->alloc(m, len, alignment); + struct memory_allocation *ma = m->alloc(len, alignment, m); if (ma == nullptr) { - warning("Memory allocation of type %s failed. reason=%s", m->name, strerror(errno) ); + warning("Memory allocation of type %s failed. reason=%s", m->name, strerror(errno)); return nullptr; } @@ -136,7 +136,7 @@ int memory_free(void *ptr) debug(LOG_MEM | 5, "Releasing %#zx bytes of %s memory: %p", ma->length, ma->type->name, ma->address); - ret = ma->type->free(ma->type, ma); + ret = ma->type->free(ma, ma->type); if (ret) return ret; diff --git a/lib/memory/heap.cpp b/lib/memory/heap.cpp index c376a8cde..75c249f9c 100644 --- a/lib/memory/heap.cpp +++ b/lib/memory/heap.cpp @@ -27,7 +27,7 @@ using namespace villas::utils; -static struct memory_allocation * memory_heap_alloc(struct memory_type *m, size_t len, size_t alignment) +static struct memory_allocation * memory_heap_alloc(size_t len, size_t alignment, struct memory_type *m) { int ret; @@ -51,7 +51,7 @@ static struct memory_allocation * memory_heap_alloc(struct memory_type *m, size_ return ma; } -static int memory_heap_free(struct memory_type *m, struct memory_allocation *ma) +static int memory_heap_free(struct memory_allocation *ma, struct memory_type *m) { free(ma->address); diff --git a/lib/memory/ib.cpp b/lib/memory/ib.cpp index ddeaf4b09..add545d1c 100644 --- a/lib/memory/ib.cpp +++ b/lib/memory/ib.cpp @@ -40,7 +40,7 @@ struct ibv_mr * memory_ib_get_mr(void *ptr) return mr; } -static struct memory_allocation * memory_ib_alloc(struct memory_type *m, size_t len, size_t alignment) +static struct memory_allocation * memory_ib_alloc(size_t len, size_t alignment, struct memory_type *m) { struct memory_ib *mi = (struct memory_ib *) m->_vd; @@ -52,7 +52,7 @@ static struct memory_allocation * memory_ib_alloc(struct memory_type *m, size_t ma->length = len; ma->alignment = alignment; - ma->parent = mi->parent->alloc(mi->parent, len + sizeof(struct ibv_mr *), alignment); + ma->parent = mi->parent->alloc(len + sizeof(struct ibv_mr *), alignment, mi->parent); ma->address = ma->parent->address; if (!mi->pd) @@ -60,7 +60,7 @@ static struct memory_allocation * memory_ib_alloc(struct memory_type *m, size_t ma->ib.mr = ibv_reg_mr(mi->pd, ma->address, ma->length, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE); if (!ma->ib.mr) { - mi->parent->free(mi->parent, ma->parent); + mi->parent->free(ma->parent, mi->parent); free(ma); return nullptr; } @@ -68,14 +68,14 @@ static struct memory_allocation * memory_ib_alloc(struct memory_type *m, size_t return ma; } -static int memory_ib_free(struct memory_type *m, struct memory_allocation *ma) +static int memory_ib_free(struct memory_allocation *ma, struct memory_type *m) { int ret; struct memory_ib *mi = (struct memory_ib *) m->_vd; ibv_dereg_mr(ma->ib.mr); - ret = mi->parent->free(mi->parent, ma->parent); + ret = mi->parent->free(ma->parent, mi->parent); if (ret) return ret; diff --git a/lib/memory/managed.cpp b/lib/memory/managed.cpp index def48ca27..42e17409d 100644 --- a/lib/memory/managed.cpp +++ b/lib/memory/managed.cpp @@ -36,7 +36,7 @@ using namespace villas::utils; -static struct memory_allocation * memory_managed_alloc(struct memory_type *m, size_t len, size_t alignment) +static struct memory_allocation * memory_managed_alloc(size_t len, size_t alignment, struct memory_type *m) { /* Simple first-fit allocation */ struct memory_block *first = (struct memory_block *) m->_vd; @@ -124,7 +124,7 @@ static struct memory_allocation * memory_managed_alloc(struct memory_type *m, si return nullptr; } -static int memory_managed_free(struct memory_type *m, struct memory_allocation *ma) +static int memory_managed_free(struct memory_allocation *ma, struct memory_type *m) { struct memory_block *block = ma->managed.block; diff --git a/lib/pool.cpp b/lib/pool.cpp index cb10128a0..eb854530a 100644 --- a/lib/pool.cpp +++ b/lib/pool.cpp @@ -36,7 +36,7 @@ int pool_init(struct pool *p, size_t cnt, size_t blocksz, struct memory_type *m) p->blocksz = p->alignment * CEIL(blocksz, p->alignment); p->len = cnt * p->blocksz; - void *buffer = memory_alloc_aligned(m, p->len, p->alignment); + void *buffer = memory_alloc_aligned(p->len, p->alignment, m); if (!buffer) serror("Failed to allocate memory for memory pool"); else diff --git a/lib/queue.cpp b/lib/queue.cpp index 2647acc3f..4d9539f77 100644 --- a/lib/queue.cpp +++ b/lib/queue.cpp @@ -48,7 +48,7 @@ int queue_init(struct queue *q, size_t size, struct memory_type *m) } q->buffer_mask = size - 1; - struct queue_cell *buffer = (struct queue_cell *) memory_alloc(m, sizeof(struct queue_cell) * size); + struct queue_cell *buffer = (struct queue_cell *) memory_alloc(sizeof(struct queue_cell) * size, m); if (!buffer) return -2; diff --git a/lib/shmem.cpp b/lib/shmem.cpp index cba1e4d90..4f45c7ad2 100644 --- a/lib/shmem.cpp +++ b/lib/shmem.cpp @@ -94,7 +94,7 @@ retry: fd = shm_open(wname, O_RDWR|O_CREAT|O_EXCL, 0600); close(fd); manager = memory_managed(base, len); - shared = (struct shmem_shared *) memory_alloc(manager, sizeof(struct shmem_shared)); + shared = (struct shmem_shared *) memory_alloc(sizeof(struct shmem_shared), manager); if (!shared) { errno = ENOMEM; return -5; diff --git a/tests/unit/memory.cpp b/tests/unit/memory.cpp index ca03dc409..6b09cd719 100644 --- a/tests/unit/memory.cpp +++ b/tests/unit/memory.cpp @@ -46,7 +46,7 @@ Theory((size_t len, size_t align, enum MemoryFlags memory_type), memory, aligned struct memory_type *mt = memory_type_lookup(memory_type); - ptr = memory_alloc_aligned(mt, len, align); + ptr = memory_alloc_aligned(len, align, mt); cr_assert_not_null(ptr, "Failed to allocate memory"); cr_assert(IS_ALIGNED(ptr, align), "Memory at %p is not alligned to %#zx byte bounary", ptr, align); @@ -72,26 +72,26 @@ Test(memory, manager, .init = init_memory) { total_size = 1 << 10; max_block = total_size - sizeof(struct memory_type) - sizeof(struct memory_block); - p = memory_alloc(&memory_heap, total_size); + p = memory_alloc(total_size, &memory_heap); cr_assert_not_null(p); m = memory_managed(p, total_size); cr_assert_not_null(m); - p1 = memory_alloc(m, 16); + p1 = memory_alloc(16, m); cr_assert_not_null(p1); - p2 = memory_alloc(m, 32); + p2 = memory_alloc(32, m); cr_assert_not_null(p2); ret = memory_free(p1); cr_assert(ret == 0); - p1 = memory_alloc_aligned(m, 128, 128); + p1 = memory_alloc_aligned(128, 128, m); cr_assert_not_null(p1); cr_assert(IS_ALIGNED(p1, 128)); - p3 = memory_alloc_aligned(m, 128, 256); + p3 = memory_alloc_aligned(128, 256, m); cr_assert(p3); cr_assert(IS_ALIGNED(p3, 256)); @@ -104,7 +104,7 @@ Test(memory, manager, .init = init_memory) { ret = memory_free(p3); cr_assert(ret == 0); - p1 = memory_alloc(m, max_block); + p1 = memory_alloc(max_block, m); cr_assert_not_null(p1); ret = memory_free(p1);