Logo AND Algorithmique Numérique Distribuée

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