From: Arnaud Giersch Date: Thu, 13 Mar 2014 22:38:37 +0000 (+0100) Subject: Cache the size of a memory page for the current system. X-Git-Tag: v3_11~215^2~12 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/d5aebc3bb0fadaeec6f5205356259c32466047ea?hp=0b2b203572323552d916d5d2c60d100a27ca7f72 Cache the size of a memory page for the current system. --- diff --git a/include/xbt/misc.h b/include/xbt/misc.h index 43fb3474a3..14e48b056b 100644 --- a/include/xbt/misc.h +++ b/include/xbt/misc.h @@ -224,6 +224,9 @@ SG_BEGIN_DECL() +/** Cache the size of a memory page for the current system. */ +XBT_PUBLIC_DATA(int) xbt_pagesize; + XBT_PUBLIC(const char *) xbt_procname(void); #define XBT_BACKTRACE_SIZE 10 /* FIXME: better place? Do document */ diff --git a/src/mc/mc_memory.c b/src/mc/mc_memory.c index aa38112b9b..2253daf402 100644 --- a/src/mc/mc_memory.c +++ b/src/mc/mc_memory.c @@ -30,7 +30,7 @@ void MC_memory_init() raw_heap = NULL; #else /* Create the second region a page after the first one ends + safety gap */ - raw_heap = xbt_mheap_new(-1, (char*)(std_heap) + STD_HEAP_SIZE + getpagesize()); + raw_heap = xbt_mheap_new(-1, (char*)(std_heap) + STD_HEAP_SIZE + xbt_pagesize); xbt_assert(raw_heap != NULL); #endif } diff --git a/src/xbt/mmalloc/mm_diff.c b/src/xbt/mmalloc/mm_diff.c index 276218593d..39cd21cb80 100644 --- a/src/xbt/mmalloc/mm_diff.c +++ b/src/xbt/mmalloc/mm_diff.c @@ -316,7 +316,7 @@ int init_heap_information(xbt_mheap_t heap1, xbt_mheap_t heap2, xbt_dynar_t i1, heaplimit = ((struct mdesc *)heap1)->heaplimit; - s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize(); + s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize; heapbase1 = (char *)heap1 + BLOCKSIZE; heapbase2 = (char *)heap2 + BLOCKSIZE; @@ -1372,7 +1372,7 @@ int mmalloc_linear_compare_heap(xbt_mheap_t heap1, xbt_mheap_t heap2){ /* Heap information */ heaplimit = ((struct mdesc *)heap1)->heaplimit; - s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - getpagesize(); + s_heap = (char *)mmalloc_get_current_heap() - STD_HEAP_SIZE - xbt_pagesize; heapbase1 = (char *)heap1 + BLOCKSIZE; heapbase2 = (char *)heap2 + BLOCKSIZE; diff --git a/src/xbt/mmalloc/mm_module.c b/src/xbt/mmalloc/mm_module.c index 7cfde64607..08f9de28e8 100644 --- a/src/xbt/mmalloc/mm_module.c +++ b/src/xbt/mmalloc/mm_module.c @@ -323,7 +323,7 @@ void *mmalloc_preinit(void) { int res; if (__mmalloc_default_mdp == NULL) { - unsigned long mask = ~((unsigned long)getpagesize() - 1); + unsigned long mask = ~((unsigned long)xbt_pagesize - 1); void *addr = (void*)(((unsigned long)sbrk(0) + HEAP_OFFSET) & mask); __mmalloc_default_mdp = xbt_mheap_new(-1, addr); /* Fixme? only the default mdp in protected against forks */ diff --git a/src/xbt/mmalloc/mmorecore.c b/src/xbt/mmalloc/mmorecore.c index 5ad7cd37af..2182cc4add 100644 --- a/src/xbt/mmalloc/mmorecore.c +++ b/src/xbt/mmalloc/mmorecore.c @@ -22,15 +22,8 @@ #include "mmprivate.h" -/* Cache the pagesize for the current host machine. Note that if the host - does not readily provide a getpagesize() function, we need to emulate it - elsewhere, not clutter up this file with lots of kluges to try to figure - it out. */ - -static size_t pagesize; - -#define PAGE_ALIGN(addr) (void*) (((long)(addr) + pagesize - 1) & \ - ~(pagesize - 1)) +#define PAGE_ALIGN(addr) (void*) (((long)(addr) + xbt_pagesize - 1) & \ + ~((long)xbt_pagesize - 1)) /* Return MAP_PRIVATE if MDP represents /dev/zero. Otherwise, return MAP_SHARED. */ @@ -65,8 +58,6 @@ void *mmorecore(struct mdesc *mdp, ssize_t size) char buf = 0; /* Single byte to write to extend mapped file */ // fprintf(stderr,"increase %p by %u\n",mdp,size); - if (pagesize == 0) - pagesize = getpagesize(); if (size == 0) { /* Just return the current "break" value. */ diff --git a/src/xbt/xbt_main.c b/src/xbt/xbt_main.c index 26e8dd1a71..30c1a8427a 100644 --- a/src/xbt/xbt_main.c +++ b/src/xbt/xbt_main.c @@ -9,7 +9,7 @@ #include "xbt/misc.h" #include "simgrid_config.h" /* _XBT_WIN32 */ #include "internal_config.h" /* MMALLOC_WANT_OVERRIDE_LEGACY */ - +#include "portable.h" #include "xbt/sysdep.h" #include "xbt/log.h" #include "xbt/dynar.h" @@ -35,6 +35,8 @@ xbt_dynar_t xbt_cmdline = NULL; /* all we got in argv */ int xbt_initialized = 0; int _sg_do_clean_atexit = 1; +int xbt_pagesize; + /* Declare xbt_preinit and xbt_postexit as constructor/destructor of the library. * This is crude and rather compiler-specific, unfortunately. */ @@ -84,6 +86,8 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, static void xbt_preinit(void) { unsigned int seed = 2147483647; + xbt_pagesize = sysconf(_SC_PAGESIZE); + #ifdef MMALLOC_WANT_OVERRIDE_LEGACY mmalloc_preinit(); #endif diff --git a/teshsuite/xbt/mmalloc_test.c b/teshsuite/xbt/mmalloc_test.c index a09704de3e..b9a074c648 100644 --- a/teshsuite/xbt/mmalloc_test.c +++ b/teshsuite/xbt/mmalloc_test.c @@ -27,7 +27,7 @@ int main(int argc, char**argv) xbt_init(&argc,argv); XBT_INFO("Allocating a new heap"); - unsigned long mask = ~((unsigned long)getpagesize() - 1); + unsigned long mask = ~((unsigned long)xbt_pagesize - 1); void *addr = (void*)(((unsigned long)sbrk(0) + BUFFSIZE) & mask); heapA = xbt_mheap_new(-1, addr); if (heapA == NULL) {