Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move surf::As to s4u::As
[simgrid.git] / src / mc / mc_mmu.h
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_MMU_H
8 #define SIMGRID_MC_MMU_H
9
10 #include <cstdint>
11 #include <cstddef>
12
13 #include <xbt/asserts.h>
14 #include <xbt/base.h> // xbt_pagesize...
15 #include <xbt/misc.h>
16
17 #include <simgrid_config.h>
18
19 SG_BEGIN_DECL()
20
21 /** @brief How many memory pages are necessary to store size bytes?
22  *
23  *  @param size Byte size
24  *  @return Number of memory pages
25  */
26 static inline __attribute__ ((always_inline))
27 size_t mc_page_count(size_t size)
28 {
29   size_t page_count = size >> xbt_pagebits;
30   if (size & (xbt_pagesize-1)) {
31     page_count ++;
32   }
33   return page_count;
34 }
35
36 /** @brief Get the virtual memory page number of a given address
37  *
38  *  @param address Address
39  *  @return Virtual memory page number of the given address
40  */
41 static inline __attribute__ ((always_inline))
42 size_t mc_page_number(const void* base, const void* address)
43 {
44   xbt_assert(address>=base, "The address is not in the range");
45   return ((std::uintptr_t) address - (std::uintptr_t) base) >> xbt_pagebits;
46 }
47
48 /** @brief Get the offset of an address within a memory page
49  *
50  *  @param address Address
51  *  @return Offset within the memory page
52  */
53 static inline __attribute__ ((always_inline))
54 size_t mc_page_offset(const void* address)
55 {
56   return ((std::uintptr_t) address) & (xbt_pagesize-1);
57 }
58
59 /** @brief Get the virtual address of a virtual memory page
60  *
61  *  @param base Address of the first page
62  *  @param page Index of the page
63  */
64 static inline __attribute__ ((always_inline))
65 void* mc_page_from_number(const void* base, size_t page)
66 {
67   return (void*) ((char*)base + (page << xbt_pagebits));
68 }
69
70 static inline __attribute__ ((always_inline))
71 bool mc_same_page(const void* a, const void* b)
72 {
73   return ((std::uintptr_t) a >> xbt_pagebits)
74     == ((std::uintptr_t) b >> xbt_pagebits);
75 }
76
77 SG_END_DECL()
78
79 #endif