Logo AND Algorithmique Numérique Distribuée

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