/* * Like var_assign_integer, only this routine copies the * supplied "string" into the var named "name". If the var * named "name" cannot be found then it is first allocated * before the copy. Space for the string in the var comes * from interprocess shared memory. If the var "name" * cannot be found or allocated, or the memory for the * var_string copy of "string" cannot be allocated, the * routine returns -1, otherwise it returns 0. */ int var_assign_string(char *name, char *string) { var_t *var; name += 1; if ((var = var_find(name)) == NULL) var = var_alloc(name); if (var == NULL) { filebench_log(LOG_ERROR, "Cannot assign variable %s", name); return (-1); } if ((var->var_string = ipc_stralloc(string)) == NULL) { filebench_log(LOG_ERROR, "Cannot assign variable %s", name); return (-1); } filebench_log(LOG_DEBUG_SCRIPT, "Assign string %s=%s", name, string); return (0); }
/* * Allocates a var (var_t) from interprocess shared memory. * Places the allocated var on the end of the globally shared * var_dyn_list. Finally, the routine allocates a string * containing a copy of the supplied "name" string. If any * allocations fails, returns NULL, otherwise it returns a * pointer to the newly allocated var. */ static var_t * var_alloc_dynamic(char *name) { var_t *var = NULL; var_t *prev = NULL; var_t *newvar; if ((newvar = (var_t *)ipc_malloc(FILEBENCH_VARIABLE)) == NULL) { filebench_log(LOG_ERROR, "Out of memory for variables"); return (NULL); } (void) memset(newvar, 0, sizeof (newvar)); for (var = filebench_shm->var_dyn_list; var != NULL; var = var->var_next) prev = var; /* Find end of list */ if (prev != NULL) prev->var_next = newvar; else filebench_shm->var_dyn_list = newvar; if ((newvar->var_name = ipc_stralloc(name)) == NULL) { filebench_log(LOG_ERROR, "Out of memory for variables"); return (NULL); } return (newvar); }
/* * Searches for the var named "name", and if not found * allocates it. The then extracts the var_string from * the var named "string" and copies it into the var_string * of the var "name", after first allocating a piece of * interprocess shared string memory. If the var "name" * cannot be found or allocated, or the var "string" cannot * be found, the routine returns -1, otherwise it returns 0. */ int var_assign_var(char *name, char *string) { var_t *var; var_string_t str; name += 1; if ((var = var_find(name)) == NULL) var = var_alloc(name); if (var == NULL) { filebench_log(LOG_ERROR, "Cannot assign variable %s", name); return (-1); } if ((str = var_ref_string(string)) == NULL) return (-1); if ((var->var_string = ipc_stralloc(*str)) == NULL) { filebench_log(LOG_ERROR, "Cannot assign variable %s", name); return (-1); } filebench_log(LOG_VERBOSE, "Assign string %s=%s", name, string); return (0); }
/* * Return 0 on success and a non zero error code on failure. */ static int alloc_cvar_lib_info(const char *filename) { int ret = -1; cvar_library_info_t *cli = NULL; cvar_library_info_t *t; cli = (cvar_library_info_t *) ipc_malloc(FILEBENCH_CVAR_LIB_INFO); if (!cli) goto out; cli->filename = ipc_stralloc(filename); if (!cli->filename) goto out; cli->type = ipc_stralloc(gettype(filename)); if (!cli->type) goto out; cli->next = NULL; if (filebench_shm->shm_cvar_lib_info_list) { for (t = filebench_shm->shm_cvar_lib_info_list; t->next != NULL; t = t->next); /* Seek to the last entry. */ cli->index = t->index + 1; t->next = cli; } else { cli->index = 0; filebench_shm->shm_cvar_lib_info_list = cli; } ret = 0; out: if (ret && cli) { /* NOTE: There is no mechanism to free cli->filename and cli->type. */ ipc_free(FILEBENCH_CVAR_LIB_INFO, (char *) cli); } return ret; }
/* * Allocates a string pointer in interprocess shared memory, * then allocates and initializes a piece of shared string memory, * putting the pointer to it into the just allocated string * pointer location. The routine returns a pointer to the * string pointer location or returns NULL on error. */ var_string_t string_alloc(char *string) { char **rtn; if ((rtn = (char **)ipc_malloc(FILEBENCH_STRING)) == NULL) { filebench_log(LOG_ERROR, "Alloc string failed"); return (NULL); } *rtn = ipc_stralloc(string); filebench_log(LOG_DEBUG_IMPL, "Alloc string %s ptr %zx", string, rtn); return (rtn); }