- data = xbt_dict_get_or_null(allocs, loc);
- if (!data) {
- data = (shared_data_t *) xbt_malloc0(sizeof(int) + size);
- data->count = 1;
- xbt_dict_set(allocs, loc, data, &free);
+ 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 = 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);