Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove useless braces
[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   return page_count;
33 }
34
35 /** @brief Get the virtual memory page number of a given address
36  *
37  *  @param address Address
38  *  @return Virtual memory page number of the given address
39  */
40 static inline __attribute__ ((always_inline))
41 size_t mc_page_number(const void* base, const void* address)
42 {
43   xbt_assert(address>=base, "The address is not in the range");
44   return ((std::uintptr_t) address - (std::uintptr_t) base) >> xbt_pagebits;
45 }
46
47 /** @brief Get the offset of an address within a memory page
48  *
49  *  @param address Address
50  *  @return Offset within the memory page
51  */
52 static inline __attribute__ ((always_inline))
53 size_t mc_page_offset(const void* address)
54 {
55   return ((std::uintptr_t) address) & (xbt_pagesize-1);
56 }
57
58 /** @brief Get the virtual address of a virtual memory page
59  *
60  *  @param base Address of the first page
61  *  @param page Index of the page
62  */
63 static inline __attribute__ ((always_inline))
64 void* mc_page_from_number(const void* base, size_t page)
65 {
66   return (void*) ((char*)base + (page << xbt_pagebits));
67 }
68
69 static inline __attribute__ ((always_inline))
70 bool mc_same_page(const void* a, const void* b)
71 {
72   return ((std::uintptr_t) a >> xbt_pagebits)
73     == ((std::uintptr_t) b >> xbt_pagebits);
74 }
75
76 SG_END_DECL()
77
78 #endif