-#ifndef WIN32
-static void smpi_shared_alloc_free(void *p)
-{
- shared_data_t *data = static_cast<shared_data_t *>(p);
- xbt_free(data->loc);
- xbt_free(data);
-}
-
-static char *smpi_shared_alloc_hash(char *loc)
-{
- char hash[42];
- char s[7];
- unsigned val;
- int i, j;
-
- xbt_sha(loc, hash);
- hash[41] = '\0';
- s[6] = '\0';
- loc = static_cast<char *>(xbt_realloc(loc, 30));
- loc[0] = '/';
- for (i = 0; i < 40; i += 6) { /* base64 encode */
- memcpy(s, hash + i, 6);
- val = strtoul(s, NULL, 16);
- for (j = 0; j < 4; j++) {
- unsigned char x = (val >> (18 - 3 * j)) & 0x3f;
- loc[1 + 4 * i / 6 + j] =
- "ABCDEFGHIJKLMNOPQRSTUVZXYZabcdefghijklmnopqrstuvzxyz0123456789-_"[x];
- }
- }
- loc[29] = '\0';
- return loc;
-}
-
-void *smpi_shared_malloc(size_t size, const char *file, int line)
-{
- void* mem;
- if (sg_cfg_get_boolean("smpi/use_shared_malloc")){
- char *loc = bprintf("/%zu_%s_%d", (size_t)getpid(), file, line);
- int fd;
- shared_data_t *data;
- loc = smpi_shared_alloc_hash(loc); /* hash loc, in order to have something
- * not too long */
- if (!allocs) {
- allocs = xbt_dict_new_homogeneous(smpi_shared_alloc_free);
- }
- data = static_cast<shared_data_t *>(xbt_dict_get_or_null(allocs, loc));
- if (!data) {
- fd = shm_open(loc, 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", loc);
- default:
- xbt_die("An unhandled error occured while opening %s. shm_open: %s", loc, strerror(errno));
- }
- }
- data = xbt_new(shared_data_t, 1);
- data->fd = fd;
- data->count = 1;
- data->loc = loc;
- mem = shm_map(fd, size, data);
- if (shm_unlink(loc) < 0) {
- XBT_WARN("Could not early unlink %s. shm_unlink: %s", loc, strerror(errno));
- }
- xbt_dict_set(allocs, loc, data, NULL);
- XBT_DEBUG("Mapping %s at %p through %d", loc, mem, fd);
- } else {
- xbt_free(loc);
- mem = shm_map(data->fd, size, data);
- data->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];
- 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);