/// Adds a string to an array of [size, string] /// structures in shared memory /// /// Inserts a string and its corresponding /// length value into a list. Intended to /// be used for parent<->child exchange of /// data via shared memory /// /// @param[in] str Pointer to a C string /// @param[in] sz Size of the string int MathildaFork::shm_store_string(const char *str, size_t sz) { RET_ERR_IF_PTR_INVALID(get_shm_ptr()); if(sz == 0) { return 0; } if(sz > 16384) { sz = 16384; } StringEntry *tmp = (StringEntry *) get_shm_ptr(); uint8_t *string = NULL; while(tmp->length != 0) { tmp = tmp + tmp->length; } if(tmp > (StringEntry *) get_shm_ptr() + get_shm_size()) { return ERR; } string = (uint8_t *) tmp + sizeof(StringEntry); tmp->length = sz; memcpy(string, str, sz); return OK; }
int main(int argc, char **argv){ char c; char *cores = NULL; char *nodes = NULL; int shuffle = 0; struct shared_state s = {}; while ((c = getopt(argc, argv, "+vVsc:n:SN")) != -1) { switch (c) { case 'c': if(cores) { fprintf(stderr, "-c or -n already used !\n"); exit(EXIT_FAILURE); } cores = strdup(optarg); break; case 'n': if(nodes || cores) { fprintf(stderr, "-c or -n already used !\n"); exit(EXIT_FAILURE); } nodes = strdup(optarg); break; case 'N': s.per_node = 1; break; case 's': shuffle = 1; break; case 'S': s.server = 1; break; case 'v': s.verbose = 1; break; case 'V': s.verbose = 1; s.verbose_err = 1; break; default: usage(argv[0]); } } if(!cores && !nodes) cores = build_default_affinity_string(shuffle); if(optind == argc) usage(argv[0]); argv += optind; char *lib = get_lib_path(); setenv("LD_PRELOAD", lib, 1); free(lib); int *cores_array; if(cores) { parse_cores(cores, &cores_array, &s.nr_entries_in_cores, 0); } else { parse_cores(nodes, &cores_array, &s.nr_entries_in_cores, 1); } char *uniq_shm_name = NULL; assert(asprintf(&uniq_shm_name, "%s_%d", "/tmp/shm", gettid())); assert(create_shm(uniq_shm_name, &s, cores_array)); setenv("PINTHREADS_SHMID", uniq_shm_name, 1); setenv("PINTHREADS_SHMSIZE", get_shm_size(), 1); free(uniq_shm_name); execvp(argv[0], argv); perror("execvp"); fprintf(stderr,"failed to execute %s\n", argv[0]); cleanup_shm(uniq_shm_name); return EXIT_SUCCESS; }