Loading...
Searching...
No Matches
examples/libs/l4re/c/ma+rm.c

Coarse grained memory allocation, in C.

Coarse grained memory allocation, in C.

/*
* (c) 2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*/
#include <l4/re/c/mem_alloc.h>
#include <l4/re/c/rm.h>
#include <l4/re/c/util/cap_alloc.h>
#include <l4/sys/err.h>
#include <stdio.h>
#include <string.h>
static int allocate_mem(unsigned long size_in_bytes, unsigned long flags,
void **virt_addr)
{
int r;
l4re_ds_t ds;
/* Allocate a free capability index for our data space */
ds = l4re_util_cap_alloc();
if (l4_is_invalid_cap(ds))
return -L4_ENOMEM;
size_in_bytes = l4_trunc_page(size_in_bytes);
/* Allocate memory via a dataspace */
if ((r = l4re_ma_alloc(size_in_bytes, ds, flags)))
return r;
/* Make the dataspace visible in our address space */
*virt_addr = 0;
if ((r = l4re_rm_attach(virt_addr, size_in_bytes,
L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX, ds, 0,
flags & L4RE_MA_SUPER_PAGES
? L4_SUPERPAGESHIFT : L4_PAGESHIFT)))
{
/* Free dataspace again */
l4re_util_cap_free_um(ds);
return r;
}
/* Done, virtual address is in virt_addr */
return 0;
}
static int free_mem(void *virt_addr)
{
int r;
l4re_ds_t ds;
/* Detach memory from our address space */
if ((r = l4re_rm_detach_ds(virt_addr, &ds)))
return r;
/* Free memory at our memory allocator */
l4re_util_cap_free_um(ds);
/* All went ok */
return 0;
}
int main(void)
{
void *virt;
/* Allocate memory: 16k Bytes (usually) */
if (allocate_mem(4 * L4_PAGESIZE, 0, &virt))
return 1;
printf("Allocated memory.\n");
/* Do something with the memory */
memset(virt, 0x12, 4 * L4_PAGESIZE);
printf("Touched memory.\n");
/* Free memory */
if (free_mem(virt))
return 2;
printf("Freed and done. Bye.\n");
return 0;
}
cap_alloc.h
Capability allocator C interface.
err.h
Error codes.
l4re_ds_t
L4_BEGIN_DECLS typedef l4_cap_idx_t l4re_ds_t
Dataspace type.
Definition dataspace.h:29
l4re_ma_alloc
long l4re_ma_alloc(long size, l4re_ds_t const mem, unsigned long flags) L4_NOTHROW
Allocate memory.
Definition mem_alloc.h:136
l4re_rm_attach
int l4re_rm_attach(void **start, unsigned long size, l4re_rm_flags_t flags, l4re_ds_t mem, l4re_rm_offset_t offs, unsigned char align) L4_NOTHROW
Attach a data space to a region.
Definition rm.h:342
l4re_rm_detach_ds
int l4re_rm_detach_ds(void *addr, l4re_ds_t *ds) L4_NOTHROW
Detach and unmap a region and return affected dataspace in the current task.
Definition rm.h:365
L4RE_RM_F_SEARCH_ADDR
@ L4RE_RM_F_SEARCH_ADDR
Search for a suitable address range.
Definition rm.h:59
l4re_util_cap_free_um
void l4re_util_cap_free_um(l4_cap_idx_t cap) L4_NOTHROW
Return capability index to capability allocator, and unmaps the object.
l4re_util_cap_alloc
L4_BEGIN_DECLS l4_cap_idx_t l4re_util_cap_alloc(void) L4_NOTHROW
Get free capability index at capability allocator.
l4_is_invalid_cap
unsigned l4_is_invalid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is the invalid capability.
Definition types.h:389
L4_ENOMEM
@ L4_ENOMEM
No memory.
Definition err.h:39
L4_SUPERPAGESHIFT
#define L4_SUPERPAGESHIFT
Size of a large page, log2-based.
Definition consts.h:31
l4_trunc_page
l4_addr_t l4_trunc_page(l4_addr_t address) L4_NOTHROW
Round an address down to the next lower page boundary.
Definition consts.h:448
L4_PAGESIZE
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition consts.h:391
L4_PAGESHIFT
#define L4_PAGESHIFT
Size of a page, log2-based.
Definition consts.h:26
mem_alloc.h
Memory allocator C interface.
rm.h
Region map interface, C interface.
string.h
String.