Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Better documentation of AddressSpace
[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
20 namespace simgrid {
21 namespace mc {
22 // TODO, do not depend on xbt_pagesize/xbt_pagebits but our own chunk size
23 namespace mmu {
24
25 static int chunkSize()
26 {
27   return xbt_pagesize;
28 }
29
30 /** @brief How many memory pages are necessary to store size bytes?
31  *
32  *  @param size Byte size
33  *  @return Number of memory pages
34  */
35 static inline __attribute__ ((always_inline))
36 std::size_t chunkCount(std::size_t size)
37 {
38   size_t page_count = size >> xbt_pagebits;
39   if (size & (xbt_pagesize-1))
40     page_count ++;
41   return page_count;
42 }
43
44 /** @brief Split into chunk number and remaining offset */
45 static inline __attribute__ ((always_inline))
46 std::pair<std::size_t, std::uintptr_t> split(std::uintptr_t offset)
47 {
48   return {
49     offset >> xbt_pagebits,
50     offset & (xbt_pagesize-1)
51   };
52 }
53
54 /** Merge chunk number and remaining offset info a global offset */
55 static inline __attribute__ ((always_inline))
56 std::uintptr_t join(std::size_t page, std::uintptr_t offset)
57 {
58   return ((std::uintptr_t) page << xbt_pagebits) + offset;
59 }
60
61 static inline __attribute__ ((always_inline))
62 std::uintptr_t join(std::pair<std::size_t,std::uintptr_t> value)
63 {
64   return join(value.first, value.second);
65 }
66
67 static inline __attribute__ ((always_inline))
68 bool sameChunk(std::uintptr_t a, std::uintptr_t b)
69 {
70   return (a >> xbt_pagebits) == (b >> xbt_pagebits);
71 }
72
73 }
74 }
75 }
76
77 #endif