1 /* Copyright (c) 2014-2015. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_MMU_H
8 #define SIMGRID_MC_MMU_H
13 #include <xbt/asserts.h>
14 #include <xbt/base.h> // xbt_pagesize...
17 #include <simgrid_config.h>
21 /** @brief How many memory pages are necessary to store size bytes?
23 * @param size Byte size
24 * @return Number of memory pages
26 static inline __attribute__ ((always_inline))
27 size_t mc_page_count(size_t size)
29 size_t page_count = size >> xbt_pagebits;
30 if (size & (xbt_pagesize-1)) {
36 /** @brief Get the virtual memory page number of a given address
38 * @param address Address
39 * @return Virtual memory page number of the given address
41 static inline __attribute__ ((always_inline))
42 size_t mc_page_number(const void* base, const void* address)
44 xbt_assert(address>=base, "The address is not in the range");
45 return ((std::uintptr_t) address - (std::uintptr_t) base) >> xbt_pagebits;
48 /** @brief Get the offset of an address within a memory page
50 * @param address Address
51 * @return Offset within the memory page
53 static inline __attribute__ ((always_inline))
54 size_t mc_page_offset(const void* address)
56 return ((std::uintptr_t) address) & (xbt_pagesize-1);
59 /** @brief Get the virtual address of a virtual memory page
61 * @param base Address of the first page
62 * @param page Index of the page
64 static inline __attribute__ ((always_inline))
65 void* mc_page_from_number(const void* base, size_t page)
67 return (void*) ((char*)base + (page << xbt_pagebits));
70 static inline __attribute__ ((always_inline))
71 bool mc_same_page(const void* a, const void* b)
73 return ((std::uintptr_t) a >> xbt_pagebits)
74 == ((std::uintptr_t) b >> xbt_pagebits);