+ char loc[PTR_STRLEN];
+ shared_metadata_t* meta;
+ shared_data_t* data;
+ if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
+
+ if (!allocs) {
+ XBT_WARN("Cannot free: nothing was allocated");
+ return;
+ }
+ if(!allocs_metadata) {
+ XBT_WARN("Cannot free: no metadata was allocated");
+ }
+ snprintf(loc, PTR_STRLEN, "%p", ptr);
+ meta = (shared_metadata_t*)xbt_dict_get_or_null(allocs_metadata, loc);
+ if (!meta) {
+ XBT_WARN("Cannot free: %p was not shared-allocated by SMPI", ptr);
+ return;
+ }
+ data = meta->data;
+ if(!data) {
+ XBT_WARN("Cannot free: something is broken in the metadata link");
+ return;
+ }
+ if(munmap(ptr, meta->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);
+ xbt_dict_remove(allocs, data->loc);
+ 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) {