Logo AND Algorithmique Numérique Distribuée

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