Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Bug: MC was reading from the wrong region
[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 extern int xbt_pagesize;
14 extern int xbt_pagebits;
15
16 /** @brief How many memory pages are necessary to store size bytes?
17  *
18  *  @param size Byte size
19  *  @return Number of memory pages
20  */
21 static inline __attribute__ ((always_inline))
22 size_t mc_page_count(size_t size)
23 {
24   size_t page_count = size >> xbt_pagebits;
25   if (size & (xbt_pagesize-1)) {
26     page_count ++;
27   }
28   return page_count;
29 }
30
31 /** @brief Get the virtual memory page number of a given address
32  *
33  *  @param address Address
34  *  @return Virtual memory page number of the given address
35  */
36 static inline __attribute__ ((always_inline))
37 size_t mc_page_number(void* base, void* address)
38 {
39   xbt_assert(address>=base, "The address is not in the range");
40   return ((uintptr_t) address - (uintptr_t) base) >> xbt_pagebits;
41 }
42
43 /** @brief Get the offset of an address within a memory page
44  *
45  *  @param address Address
46  *  @return Offset within the memory page
47  */
48 static inline __attribute__ ((always_inline))
49 size_t mc_page_offset(void* address)
50 {
51   return ((uintptr_t) address) & (xbt_pagesize-1);
52 }
53
54 /** @brief Get the virtual address of a virtual memory page
55  *
56  *  @param base Address of the first page
57  *  @param page Index of the page
58  */
59 static inline __attribute__ ((always_inline))
60 void* mc_page_from_number(void* base, size_t page)
61 {
62   return (void*) ((char*)base + (page << xbt_pagebits));
63 }
64
65 static inline __attribute__ ((always_inline))
66 bool mc_same_page(void* a, void* b)
67 {
68   return ((uintptr_t) a >> xbt_pagebits) == ((uintptr_t) b >> xbt_pagebits);
69 }
70
71 #endif