Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Move collective algorithms to separate folders
[simgrid.git] / src / mc / mc_mmu.h
1 /* Copyright (c) 2014-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_MMU_H
7 #define SIMGRID_MC_MMU_H
8
9 #include <cstdint>
10 #include <cstddef>
11
12 #include <xbt/asserts.h>
13 #include <xbt/base.h> // xbt_pagesize...
14 #include <xbt/misc.h>
15
16 #include <simgrid_config.h>
17
18
19 namespace simgrid {
20 namespace mc {
21 // TODO, do not depend on xbt_pagesize/xbt_pagebits but our own chunk size
22 namespace mmu {
23
24 static int chunkSize()
25 {
26   return xbt_pagesize;
27 }
28
29 /** @brief How many memory pages are necessary to store size bytes?
30  *
31  *  @param size Byte size
32  *  @return Number of memory pages
33  */
34 static inline __attribute__ ((always_inline))
35 std::size_t chunkCount(std::size_t size)
36 {
37   size_t page_count = size >> xbt_pagebits;
38   if (size & (xbt_pagesize-1))
39     page_count ++;
40   return page_count;
41 }
42
43 /** @brief Split into chunk number and remaining offset */
44 static inline __attribute__ ((always_inline))
45 std::pair<std::size_t, std::uintptr_t> split(std::uintptr_t offset)
46 {
47   return {
48     offset >> xbt_pagebits,
49     offset & (xbt_pagesize-1)
50   };
51 }
52
53 /** Merge chunk number and remaining offset info a global offset */
54 static inline __attribute__ ((always_inline))
55 std::uintptr_t join(std::size_t page, std::uintptr_t offset)
56 {
57   return ((std::uintptr_t) page << xbt_pagebits) + offset;
58 }
59
60 static inline __attribute__ ((always_inline))
61 std::uintptr_t join(std::pair<std::size_t,std::uintptr_t> value)
62 {
63   return join(value.first, value.second);
64 }
65
66 static inline __attribute__ ((always_inline))
67 bool sameChunk(std::uintptr_t a, std::uintptr_t b)
68 {
69   return (a >> xbt_pagebits) == (b >> xbt_pagebits);
70 }
71
72 }
73 }
74 }
75
76 #endif