#ifndef SIMGRID_MC_MMU_H
#define SIMGRID_MC_MMU_H
-#include <stdint.h>
-#include <stdbool.h>
+#include <cstdint>
+#include <cstddef>
#include <xbt/asserts.h>
+#include <xbt/base.h> // xbt_pagesize...
#include <xbt/misc.h>
#include <simgrid_config.h>
-SG_BEGIN_DECL()
+
+namespace simgrid {
+namespace mc {
+// TODO, do not depend on xbt_pagesize/xbt_pagebits but our own chunk size
+namespace mmu {
+
+static int chunkSize()
+{
+ return xbt_pagesize;
+}
/** @brief How many memory pages are necessary to store size bytes?
*
* @return Number of memory pages
*/
static inline __attribute__ ((always_inline))
-size_t mc_page_count(size_t size)
+std::size_t chunkCount(std::size_t size)
{
size_t page_count = size >> xbt_pagebits;
- if (size & (xbt_pagesize-1)) {
+ if (size & (xbt_pagesize-1))
page_count ++;
- }
return page_count;
}
-/** @brief Get the virtual memory page number of a given address
- *
- * @param address Address
- * @return Virtual memory page number of the given address
- */
+/** @brief Split into chunk number and remaining offset */
static inline __attribute__ ((always_inline))
-size_t mc_page_number(const void* base, const void* address)
+std::pair<std::size_t, std::uintptr_t> split(std::uintptr_t offset)
{
- xbt_assert(address>=base, "The address is not in the range");
- return ((uintptr_t) address - (uintptr_t) base) >> xbt_pagebits;
+ return {
+ offset >> xbt_pagebits,
+ offset & (xbt_pagesize-1)
+ };
}
-/** @brief Get the offset of an address within a memory page
- *
- * @param address Address
- * @return Offset within the memory page
- */
+/** Merge chunk number and remaining offset info a global offset */
static inline __attribute__ ((always_inline))
-size_t mc_page_offset(const void* address)
+std::uintptr_t join(std::size_t page, std::uintptr_t offset)
{
- return ((uintptr_t) address) & (xbt_pagesize-1);
+ return ((std::uintptr_t) page << xbt_pagebits) + offset;
}
-/** @brief Get the virtual address of a virtual memory page
- *
- * @param base Address of the first page
- * @param page Index of the page
- */
static inline __attribute__ ((always_inline))
-void* mc_page_from_number(const void* base, size_t page)
+std::uintptr_t join(std::pair<std::size_t,std::uintptr_t> value)
{
- return (void*) ((char*)base + (page << xbt_pagebits));
+ return join(value.first, value.second);
}
static inline __attribute__ ((always_inline))
-bool mc_same_page(const void* a, const void* b)
+bool sameChunk(std::uintptr_t a, std::uintptr_t b)
{
- return ((uintptr_t) a >> xbt_pagebits) == ((uintptr_t) b >> xbt_pagebits);
+ return (a >> xbt_pagebits) == (b >> xbt_pagebits);
}
-SG_END_DECL()
+}
+}
+}
#endif