2016-09-19 20:58:41 -04:00
|
|
|
/** Memory allocators.
|
2016-09-15 21:25:32 -04:00
|
|
|
*
|
2016-09-19 20:58:41 -04:00
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2016-09-19 20:58:41 -04:00
|
|
|
#include <stddef.h>
|
2016-10-30 18:39:22 -04:00
|
|
|
#include <stdint.h>
|
2016-09-19 20:58:41 -04:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2016-10-30 19:40:31 -04:00
|
|
|
#define HUGEPAGESIZE (1 << 21)
|
|
|
|
|
2017-03-27 13:22:54 +02:00
|
|
|
struct memtype;
|
|
|
|
|
|
|
|
typedef void *(*memzone_allocator_t)(struct memtype *mem, size_t len, size_t alignment);
|
|
|
|
typedef int (*memzone_deallocator_t)(struct memtype *mem, void *ptr, size_t len);
|
2016-09-15 21:25:32 -04:00
|
|
|
|
|
|
|
enum memtype_flags {
|
|
|
|
MEMORY_MMAP = (1 << 0),
|
|
|
|
MEMORY_DMA = (1 << 1),
|
|
|
|
MEMORY_HUGEPAGE = (1 << 2),
|
|
|
|
MEMORY_HEAP = (1 << 3)
|
|
|
|
};
|
|
|
|
|
|
|
|
struct memtype {
|
|
|
|
const char *name;
|
|
|
|
int flags;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-09-15 21:25:32 -04:00
|
|
|
size_t alignment;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-09-15 21:25:32 -04:00
|
|
|
memzone_allocator_t alloc;
|
2016-09-19 20:58:41 -04:00
|
|
|
memzone_deallocator_t free;
|
2017-03-29 11:29:53 +02:00
|
|
|
|
2017-03-29 12:05:38 +02:00
|
|
|
void *_vd; /**<Virtual data for possible state */
|
2016-09-15 21:25:32 -04:00
|
|
|
};
|
|
|
|
|
2017-03-27 13:22:54 +02:00
|
|
|
enum memblock_flags {
|
2017-03-29 12:05:38 +02:00
|
|
|
MEMBLOCK_USED = 1,
|
2017-03-27 13:22:54 +02:00
|
|
|
};
|
|
|
|
|
2017-03-29 11:29:53 +02:00
|
|
|
/** Descriptor of a memory block. Associated block always starts at
|
|
|
|
* &m + sizeof(struct memblock). */
|
2017-03-27 13:22:54 +02:00
|
|
|
struct memblock {
|
2017-03-29 12:05:38 +02:00
|
|
|
struct memblock* prev;
|
|
|
|
struct memblock* next;
|
|
|
|
size_t len; /**<Length of the block; doesn't include the descriptor itself */
|
|
|
|
int flags;
|
2016-09-15 21:25:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @todo Unused for now */
|
|
|
|
struct memzone {
|
|
|
|
struct memtype * const type;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-09-15 21:25:32 -04:00
|
|
|
void *addr;
|
|
|
|
uintptr_t physaddr;
|
2017-05-05 19:24:16 +00:00
|
|
|
size_t len;
|
2016-09-15 21:25:32 -04:00
|
|
|
};
|
|
|
|
|
2016-11-20 02:45:16 -05:00
|
|
|
/** Initilialize memory subsystem */
|
2017-03-11 23:39:00 -03:00
|
|
|
int memory_init(int hugepages);
|
2016-11-20 02:45:16 -05:00
|
|
|
|
2016-10-30 16:54:39 -04:00
|
|
|
/** Allocate \p len bytes memory of type \p m.
|
|
|
|
*
|
|
|
|
* @retval NULL If allocation failed.
|
|
|
|
* @retval <>0 If allocation was successful.
|
|
|
|
*/
|
2017-03-27 13:22:54 +02:00
|
|
|
void * memory_alloc(struct memtype *m, size_t len);
|
|
|
|
|
|
|
|
void * memory_alloc_aligned(struct memtype *m, size_t len, size_t alignment);
|
2016-09-19 20:58:41 -04:00
|
|
|
|
2017-03-27 13:22:54 +02:00
|
|
|
int memory_free(struct memtype *m, void *ptr, size_t len);
|
2016-09-19 20:58:41 -04:00
|
|
|
|
2017-03-31 18:04:08 +02:00
|
|
|
struct memtype * memtype_managed_init(void *ptr, size_t len);
|
2016-09-15 21:25:32 -04:00
|
|
|
|
2017-03-27 13:22:54 +02:00
|
|
|
extern struct memtype memtype_heap;
|
2017-03-31 18:14:31 +02:00
|
|
|
extern struct memtype memtype_hugepage;
|