/* ////////////////////////////////////////////////////////////////////////// */ static inline int list_item_construct(mmu_list_item_t **item, const void *payload_base, size_t payload_extent) { int rc = MMU_FAILURE; mmu_list_item_t *tmp; void *payload; if (NULL == item || NULL == payload_base || 0 == payload_extent) { return MMU_FAILURE_INVALID_ARG; } if (NULL == (tmp = calloc(1, sizeof(*tmp)))) { MMU_OOR_COMPLAIN(); return MMU_FAILURE_OOR; } /* allocate enough space for the payload */ if (NULL == (payload = calloc(1, payload_extent))) { MMU_OOR_COMPLAIN(); rc = MMU_FAILURE_OOR; goto err; } /* copy the payload */ (void)memmove(payload, payload_base, payload_extent); tmp->datap = payload; tmp->size = payload_extent; *item = tmp; return MMU_SUCCESS; err: list_item_destruct(tmp, false); return rc; }
/* ////////////////////////////////////////////////////////////////////////// */ int mmu_args_construct(mmu_args_t **a) { mmu_args_t *tmp = NULL; if (NULL == a) return MMU_FAILURE_INVALID_ARG; tmp = (mmu_args_t *)calloc(1, sizeof(*tmp)); if (NULL == tmp) { MMU_OOR_COMPLAIN(); return MMU_FAILURE_OOR; } set_arg_defaults(tmp); *a = tmp; return MMU_SUCCESS; }
/* ////////////////////////////////////////////////////////////////////////// */ int mmu_list_construct(mmu_list_t **list) { mmu_list_t *tmp; if (NULL == list) return MMU_FAILURE_INVALID_ARG; if (NULL == (tmp = calloc(1, sizeof(*tmp)))) { MMU_OOR_COMPLAIN(); return MMU_FAILURE_OOR; } /* start off with nothing */ tmp->item_ptrs = NULL; tmp->size = 0; tmp->capacity = 0; *list = tmp; return MMU_SUCCESS; }
/* ////////////////////////////////////////////////////////////////////////// */ static inline int list_grow(mmu_list_t *list, size_t new_capacity) { if (NULL == list || new_capacity <= 0) return MMU_FAILURE_INVALID_ARG; /* this is easy. */ if (list->capacity >= new_capacity) return MMU_SUCCESS; /* if we are here, it is safe to realloc to the given size */ if (NULL == (list->item_ptrs = realloc(list->item_ptrs, new_capacity * sizeof(mmu_list_item_t *)))) { MMU_OOR_COMPLAIN(); return MMU_FAILURE_OOR; } /* update list capacity to the new capacity */ list->capacity = new_capacity; return MMU_SUCCESS; }
/* ////////////////////////////////////////////////////////////////////////// */ int mmu_args_process_user_input(mmu_args_t *a, int argc, char **argv) { int c; int rc = MMU_FAILURE; char **tmp_argv = NULL; if (NULL == (tmp_argv = mmu_args_dup_argv(argc, argv))) { MMU_OOR_COMPLAIN(); return MMU_FAILURE_OOR; } while (-1 != (c = getopt_long_only(argc, tmp_argv, opt_string, long_opts, NULL))) { switch (c) { case 's': /* samples-per-sec */ if (MMU_SUCCESS != (rc = handle_s(a, c, optarg))) { goto show_usage; } break; case 't': /* sample-time */ if (MMU_SUCCESS != (rc = handle_t(a, c, optarg))) { goto show_usage; } break; case 'w': /* enable workload */ if (MMU_SUCCESS != (rc = handle_w(a))) { goto show_usage; } break; case 'h': /* help! */ mmu_args_usage(); rc = MMU_SUCCESS_EXIT_SUCCESS; goto cleanup; break; case 'v': /* version, please */ showver(); rc = MMU_SUCCESS_EXIT_SUCCESS; goto cleanup; break; default: goto show_usage; } } if (optind < argc) { fprintf(stderr, MMU_ERR_PREFIX"unrecognized input: \"%s\"\n", tmp_argv[optind]); goto show_usage; } /* all is well */ rc = MMU_SUCCESS; goto cleanup; show_usage: mmu_args_usage(); rc = MMU_FAILURE_INVALID_USER_INPUT; cleanup: if (NULL != tmp_argv) { mmu_args_free_dup_argv(tmp_argv); } return rc; }