Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
proper check for the -std=gnu++11 standard, and take in on clang too
[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 SIMGRID_MC_MMU_H
8 #define SIMGRID_MC_MMU_H
9
10 #include <stdint.h>
11 #include <stdbool.h>
12
13 #include <xbt/asserts.h>
14 #include <xbt/misc.h>
15
16 #include <simgrid_config.h>
17
18 SG_BEGIN_DECL()
19
20 /** @brief How many memory pages are necessary to store size bytes?
21  *
22  *  @param size Byte size
23  *  @return Number of memory pages
24  */
25 static inline __attribute__ ((always_inline))
26 size_t mc_page_count(size_t size)
27 {
28   size_t page_count = size >> xbt_pagebits;
29   if (size & (xbt_pagesize-1)) {
30     page_count ++;
31   }
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 ((uintptr_t) address - (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 ((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 ((uintptr_t) a >> xbt_pagebits) == ((uintptr_t) b >> xbt_pagebits);
73 }
74
75 SG_END_DECL()
76
77 #endif