1 /* Copyright (c) 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
13 #include <simgrid_config.h>
17 extern int xbt_pagesize;
18 extern int xbt_pagebits;
20 /** @brief How many memory pages are necessary to store size bytes?
22 * @param size Byte size
23 * @return Number of memory pages
25 static inline __attribute__ ((always_inline))
26 size_t mc_page_count(size_t size)
28 size_t page_count = size >> xbt_pagebits;
29 if (size & (xbt_pagesize-1)) {
35 /** @brief Get the virtual memory page number of a given address
37 * @param address Address
38 * @return Virtual memory page number of the given address
40 static inline __attribute__ ((always_inline))
41 size_t mc_page_number(const void* base, const void* address)
43 xbt_assert(address>=base, "The address is not in the range");
44 return ((uintptr_t) address - (uintptr_t) base) >> xbt_pagebits;
47 /** @brief Get the offset of an address within a memory page
49 * @param address Address
50 * @return Offset within the memory page
52 static inline __attribute__ ((always_inline))
53 size_t mc_page_offset(const void* address)
55 return ((uintptr_t) address) & (xbt_pagesize-1);
58 /** @brief Get the virtual address of a virtual memory page
60 * @param base Address of the first page
61 * @param page Index of the page
63 static inline __attribute__ ((always_inline))
64 void* mc_page_from_number(const void* base, size_t page)
66 return (void*) ((char*)base + (page << xbt_pagebits));
69 static inline __attribute__ ((always_inline))
70 bool mc_same_page(const void* a, const void* b)
72 return ((uintptr_t) a >> xbt_pagebits) == ((uintptr_t) b >> xbt_pagebits);