3 * \brief Example of coarse grained memory allocation, in C++.
6 * (c) 2009 Technische Universität Dresden
7 * This file is part of TUD:OS and distributed under the terms of the
8 * GNU General Public License 2.
9 * Please see the COPYING-GPL-2 file for details.
12 #include <l4/re/mem_alloc>
15 #include <l4/re/dataspace>
16 #include <l4/re/util/cap_alloc>
17 #include <l4/sys/err.h>
22 * \brief Allocate memory, given in bytes in the granularity of pages.
24 * \param size_in_bytes Size to allocate, in bytes, will be truncates to
25 * whole pages (L4_PAGESIZE).
26 * \param flags Flags to control memory allocation:
27 * L4Re::Mem_alloc::Continuous: Physically continuous memory
28 * L4Re::Mem_alloc::Pinned: Pinned memory
29 * L4Re::Mem_alloc::Super_pages: Use big pages
30 * \retval virt_addr Virtual address the memory is accessible under,
31 * undefined if return code != 0
33 * \return 0 on success, error code otherwise
35 static int allocate_mem(unsigned long size_in_bytes, unsigned long flags,
39 L4::Cap<L4Re::Dataspace> d;
41 /* Allocate a free capability index for our data space */
42 d = L4Re::Util::cap_alloc.alloc<L4Re::Dataspace>();
46 size_in_bytes = l4_trunc_page(size_in_bytes);
48 /* Allocate memory via a dataspace */
49 if ((r = L4Re::Env::env()->mem_alloc()->alloc(size_in_bytes, d, flags)))
52 /* Make the dataspace visible in our address space */
54 if ((r = L4Re::Env::env()->rm()->attach(virt_addr, size_in_bytes,
55 L4Re::Rm::Search_addr, d, 0,
56 flags & L4Re::Mem_alloc::Super_pages
57 ? L4_SUPERPAGESHIFT : L4_PAGESHIFT)))
60 /* Done, virtual address is in virt_addr */
65 * \brief Free previously allocated memory.
67 * \param virt_addr Virtual address return by allocate_mem
69 * \return 0 on success, error code otherwise
71 static int free_mem(void *virt_addr)
74 L4::Cap<L4Re::Dataspace> ds;
76 /* Detach memory from our address space */
77 if ((r = L4Re::Env::env()->rm()->detach(virt_addr, &ds)))
80 /* Free memory at our memory allocator, this is optional */
81 if ((r = L4Re::Env::env()->mem_alloc()->free(ds)))
84 /* Release and return capability slot to allocator */
85 L4Re::Util::cap_alloc.free(ds, L4Re::Env::env()->task().cap());
95 /* Allocate memory: 16k Bytes (usually) */
96 if (allocate_mem(4 * L4_PAGESIZE, 0, &virt))
99 printf("Allocated memory.\n");
101 /* Do something with the memory */
102 memset(virt, 0x12, 4 * L4_PAGESIZE);
104 printf("Touched memory.\n");
110 printf("Freed and done. Bye.\n");