-#ifndef WIN32
-
-void *smpi_shared_malloc(size_t size, const char *file, int line)
-{
- void* mem;
- if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
- int fd;
- smpi_source_location loc(file, line);
- auto res = allocs.insert(std::make_pair(loc, shared_data_t()));
- auto data = res.first;
- if (res.second) {
- // The insertion did not take place.
- // Generate a shared memory name from the address of the shared_data:
- char shmname[32]; // cannot be longer than PSHMNAMLEN = 31 on Mac OS X (shm_open raises ENAMETOOLONG otherwise)
- snprintf(shmname, 31, "/shmalloc%p", &*data);
- fd = shm_open(shmname, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
- if (fd < 0) {
- switch(errno) {
- case EEXIST:
- xbt_die("Please cleanup /dev/shm/%s", shmname);
- default:
- xbt_die("An unhandled error occured while opening %s. shm_open: %s", shmname, strerror(errno));
- }
- }
- data->second.fd = fd;
- data->second.count = 1;
- mem = shm_map(fd, size, &*data);
- if (shm_unlink(shmname) < 0) {
- XBT_WARN("Could not early unlink %s. shm_unlink: %s", shmname, strerror(errno));
- }
- XBT_DEBUG("Mapping %s at %p through %d", shmname, mem, fd);
- } else {
- mem = shm_map(data->second.fd, size, &*data);
- data->second.count++;
- }
- XBT_DEBUG("Shared malloc %zu in %p (metadata at %p)", size, mem, &*data);
- } else {
- mem = xbt_malloc(size);
- XBT_DEBUG("Classic malloc %zu in %p", size, mem);
- }
-
- return mem;
-}
-
-void smpi_shared_free(void *ptr)
-{
- char loc[PTR_STRLEN];
-
- if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
- snprintf(loc, PTR_STRLEN, "%p", ptr);
- auto meta = allocs_metadata.find(ptr);
- if (meta == allocs_metadata.end()) {
- XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
- return;
- }
- shared_data_t* data = &meta->second.data->second;
- if (munmap(ptr, meta->second.size) < 0) {
- XBT_WARN("Unmapping of fd %d failed: %s", data->fd, strerror(errno));
- }
- data->count--;
- XBT_DEBUG("Shared free - no removal - of %p, count = %d", ptr, data->count);
- if (data->count <= 0) {
- close(data->fd);
- allocs.erase(allocs.find(meta->second.data->first));
- XBT_DEBUG("Shared free - with removal - of %p", ptr);
- }
- }else{
- XBT_DEBUG("Classic free of %p", ptr);
- xbt_free(ptr);
- }
-}
-#endif
-
-int smpi_shared_known_call(const char* func, const char* input)
-{
- char* loc = bprintf("%s:%s", func, input);
- xbt_ex_t ex;
- int known = 0;
-
- if (!calls) {
- calls = xbt_dict_new_homogeneous(NULL);
- }
- TRY {
- xbt_dict_get(calls, loc); /* Succeed or throw */
- known = 1;
- }
- TRY_CLEANUP {
- xbt_free(loc);
- }
- CATCH(ex) {
- if (ex.category != not_found_error)
- RETHROW;
- xbt_ex_free(ex);
- }
- return known;
-}
-
-void* smpi_shared_get_call(const char* func, const char* input) {
- char* loc = bprintf("%s:%s", func, input);
- void* data;
-
- if(!calls) {
- calls = xbt_dict_new_homogeneous(NULL);
- }
- data = xbt_dict_get(calls, loc);
- free(loc);
- return data;
-}
-
-void* smpi_shared_set_call(const char* func, const char* input, void* data) {
- char* loc = bprintf("%s:%s", func, input);
-
- if(!calls) {
- calls = xbt_dict_new_homogeneous(NULL);
- }
- xbt_dict_set(calls, loc, data, NULL);
- free(loc);
- return data;
-}
-
-#define TOPAGE(addr) (void *)(((unsigned long)(addr) / xbt_pagesize) * xbt_pagesize)
-
-/** Map a given SMPI privatization segment (make a SMPI process active) */
-void smpi_switch_data_segment(int dest){
- if (smpi_loaded_page==dest)//no need to switch either
- return;
-
- // So the job:
- smpi_really_switch_data_segment(dest);
-}
-
-/** Map a given SMPI privatization segment (make a SMPI process active) even if SMPI thinks it is already active
- *
- * When doing a state restoration, the state of the restored variables might not be consistent with the state of the
- * virtual memory. In this case, we to change the data segment.
- */
-void smpi_really_switch_data_segment(int dest) {
- if(smpi_size_data_exe == 0)//no need to switch
- return;
-
-#ifdef HAVE_PRIVATIZATION
- if(smpi_loaded_page==-1){//initial switch, do the copy from the real page here
- for (int i=0; i< smpi_process_count(); i++){
- memcpy(smpi_privatisation_regions[i].address, TOPAGE(smpi_start_data_exe), smpi_size_data_exe);
- }