diff --git a/src/cmd/flux.c b/src/cmd/flux.c index 56e021765b12..765e415a2843 100644 --- a/src/cmd/flux.c +++ b/src/cmd/flux.c @@ -167,12 +167,12 @@ int main (int argc, char *argv[]) if (optparse_hasopt (p, "trace-handle")) { if (setenv ("FLUX_HANDLE_TRACE", "1", 1) < 0) err_exit ("setenv"); - flux_conf_environment_set (cf, "FLUX_HANDLE_TRACE", "1", ""); + flux_conf_environment_set (cf, "FLUX_HANDLE_TRACE", "1", 0); } if (optparse_getopt (p, "uri", &opt)) { if (setenv ("FLUX_URI", opt, 1) < 0) err_exit ("setenv"); - flux_conf_environment_set(cf, "FLUX_URI", opt, ""); + flux_conf_environment_set(cf, "FLUX_URI", opt, 0); } optind = optparse_optind (p); @@ -200,7 +200,7 @@ int main (int argc, char *argv[]) optparse_set_data (p, "flux_t", h); } else { if (flux_conf_load (cf) == 0) { - flux_conf_environment_set (cf, "FLUX_CONF_USEFILE", "1", ""); + flux_conf_environment_set (cf, "FLUX_CONF_USEFILE", "1", 0); } else if (errno != ENOENT) err_exit ("%s", flux_conf_get_directory (cf)); } @@ -210,7 +210,7 @@ int main (int argc, char *argv[]) */ if (!optparse_getopt (p, "secdir", &secdir)) secdir = flux_conf_get_directory (cf); - flux_conf_environment_set (cf, "FLUX_SEC_DIRECTORY", secdir, ""); + flux_conf_environment_set (cf, "FLUX_SEC_DIRECTORY", secdir, 0); /* Add PATH to flux_conf_environment and prepend path to * this executable if necessary. @@ -332,7 +332,7 @@ void setup_path (flux_conf_t cf, const char *argv0) /* If argv[0] was explicitly "flux" then assume PATH is already set */ if (strcmp (argv0, "flux") == 0) return; - flux_conf_environment_from_env (cf, "PATH", "/bin:/usr/bin", ":"); + flux_conf_environment_from_env (cf, "PATH", "/bin:/usr/bin", ':'); selfdir = dir_self (); flux_conf_environment_push (cf, "PATH", selfdir); free (selfdir); diff --git a/src/common/libflux/conf.c b/src/common/libflux/conf.c index 006ddf24488b..8da0473d71f1 100644 --- a/src/common/libflux/conf.c +++ b/src/common/libflux/conf.c @@ -31,13 +31,12 @@ #include #include #include +#include #include "src/common/libutil/log.h" #include "src/common/libutil/xzmalloc.h" #include "src/common/libutil/iterators.h" -#include "src/common/libutil/sds.h" -#include "src/common/libutil/vec.h" -#include "src/common/libutil/usa.h" + #include "conf.h" @@ -96,15 +95,15 @@ static void initialize_environment (flux_conf_t cf) { // Defaults from the environment flux_conf_environment_from_env ( - cf, "LUA_CPATH", "", ";"); /* Lua replaces ;; with the default path */ + cf, "LUA_CPATH", "", ';'); /* Lua replaces ;; with the default path */ flux_conf_environment_no_dedup_push_back (cf, "LUA_CPATH", ";;"); flux_conf_environment_from_env ( - cf, "LUA_PATH", "", ";"); /* use a null separator to keep it intact */ + cf, "LUA_PATH", "", ';'); /* use a null separator to keep it intact */ flux_conf_environment_no_dedup_push_back (cf, "LUA_PATH", ";;"); - flux_conf_environment_from_env (cf, "PYTHONPATH", "", ":"); - flux_conf_environment_from_env (cf, "FLUX_CONNECTOR_PATH", "", ":"); - flux_conf_environment_from_env (cf, "FLUX_EXEC_PATH", "", ":"); - flux_conf_environment_from_env (cf, "FLUX_MODULE_PATH", "", ":"); + flux_conf_environment_from_env (cf, "PYTHONPATH", "", ':'); + flux_conf_environment_from_env (cf, "FLUX_CONNECTOR_PATH", "", ':'); + flux_conf_environment_from_env (cf, "FLUX_EXEC_PATH", "", ':'); + flux_conf_environment_from_env (cf, "FLUX_MODULE_PATH", "", ':'); // Build paths flux_conf_environment_push (cf, "FLUX_CONNECTOR_PATH", CONNECTOR_PATH); @@ -123,7 +122,7 @@ const char *flux_conf_get_directory (flux_conf_t cf) void flux_conf_set_directory (flux_conf_t cf, const char *path) { free (cf->confdir); - flux_conf_environment_set (cf, "FLUX_CONF_DIRECTORY", path, ""); + flux_conf_environment_set (cf, "FLUX_CONF_DIRECTORY", path, ':'); cf->confdir = xstrdup (path); } @@ -290,38 +289,82 @@ const char *flux_conf_next (flux_conf_itr_t itr) return item; } +struct env_item { + char *argz; + size_t argz_len; + char sep; + _Bool clean; + char *str_cache; +}; + +struct env_item * new_env_item(){ + return calloc (1, sizeof(struct env_item)); +} +void free_env_item(struct env_item *i) +{ + free (i->argz); + free (i->str_cache); + free (i); +} + +char *find_env_item(struct env_item *i, const char *s) +{ + char *entry = 0; + while ((entry = argz_next (i->argz, i->argz_len, entry))) { + if (!strcmp(entry, s)) + return entry; + } + return NULL; +} + +static const char *stringify_env_item (struct env_item *item) +{ + if (!item) + return NULL; + if (!item->argz) + return ""; + if (item->clean) + return item->str_cache; + + free(item->str_cache); + item->str_cache = malloc(item->argz_len); + memcpy(item->str_cache, item->argz, item->argz_len); + argz_stringify(item->str_cache, item->argz_len, item->sep); + + return item->str_cache; +} + const char *flux_conf_environment_get (flux_conf_t cf, const char *key) { - return usa_get_joined (zhash_lookup (cf->environment, key)); + struct env_item *item = zhash_lookup (cf->environment, key); + return stringify_env_item(item); } static void flux_conf_environment_set_inner (flux_conf_t cf, const char *key, - const sds value, - const char *separator) + const char *value, + char separator) { - unique_string_array_t *item = usa_new (); - usa_set_separator (item, separator); + struct env_item *item = new_env_item(); item->clean = false; - if (sdslen (item->sep)) - usa_split_and_push (item, value, false); - else - usa_push (item, value); + item->sep = separator; zhash_update (cf->environment, key, (void *)item); - zhash_freefn (cf->environment, key, (zhash_free_fn *)usa_destroy); + zhash_freefn (cf->environment, key, (zhash_free_fn *)free_env_item); + + flux_conf_environment_push_back(cf, key, value); } void flux_conf_environment_set (flux_conf_t cf, const char *key, const char *value, - const char *separator) + char separator) { - flux_conf_environment_set_inner (cf, key, sdsnew (value), separator); + flux_conf_environment_set_inner (cf, key, value, separator); } void flux_conf_environment_unset (flux_conf_t cf, const char *key) { - flux_conf_environment_set_inner (cf, key, sdsempty (), ""); + flux_conf_environment_set_inner (cf, key, NULL, 0); } static void flux_conf_environment_push_inner (flux_conf_t cf, @@ -333,17 +376,30 @@ static void flux_conf_environment_push_inner (flux_conf_t cf, if (!value || strlen (value) == 0) return; - unique_string_array_t *item = zhash_lookup (cf->environment, key); + struct env_item *item = zhash_lookup (cf->environment, key); if (!item) - item = usa_new (); + item = new_env_item(); if (split) { - usa_split_and_push (item, value, before); + char *split_value = NULL; + size_t split_value_len = 0; + argz_create_sep (value, item->sep, &split_value, &split_value_len); + char *entry = 0; + while((entry = argz_next (split_value, split_value_len, entry))) { + if ((!strlen(entry)) || find_env_item(item, entry)) + continue; + if (before) { + argz_insert (&item->argz, &item->argz_len, item->argz, entry); + } else { + argz_add(&item->argz, &item->argz_len, entry); + } + } + free(split_value); } else { if (before) { - usa_push (item, value); + argz_insert (&item->argz, &item->argz_len, item->argz, value); } else { - usa_push_back (item, value); + argz_add (&item->argz, &item->argz_len, value); } } } @@ -379,7 +435,7 @@ void flux_conf_environment_no_dedup_push_back (flux_conf_t cf, void flux_conf_environment_from_env (flux_conf_t cf, const char *key, const char *default_base, - const char *separator) + char separator) { const char *env = getenv (key); if (!env) @@ -389,19 +445,21 @@ void flux_conf_environment_from_env (flux_conf_t cf, void flux_conf_environment_set_separator (flux_conf_t cf, const char *key, - const char *separator) + char separator) { - usa_set_separator (zhash_lookup (cf->environment, key), separator); + struct env_item *item = zhash_lookup (cf->environment, key); + if (item) + item->sep = separator; } const char *flux_conf_environment_first (flux_conf_t cf) { - return usa_get_joined (zhash_first (cf->environment)); + return stringify_env_item (zhash_first (cf->environment)); } const char *flux_conf_environment_next (flux_conf_t cf) { - return usa_get_joined (zhash_next (cf->environment)); + return stringify_env_item (zhash_next (cf->environment)); } const char *flux_conf_environment_cursor (flux_conf_t cf) @@ -412,11 +470,11 @@ const char *flux_conf_environment_cursor (flux_conf_t cf) void flux_conf_environment_apply (flux_conf_t cf) { const char *key, *value; - unique_string_array_t *item; + struct env_item *item; FOREACH_ZHASH (cf->environment, key, item) { - value = usa_get_joined (item); - if (sdslen ((sds)value)) { + value = stringify_env_item (item); + if (value) { if (setenv (key, value, 1) < 0) err_exit ("setenv: %s=%s", key, value); } else { diff --git a/src/common/libflux/conf.h b/src/common/libflux/conf.h index 9a12de0c8b01..ae901d1873cc 100644 --- a/src/common/libflux/conf.h +++ b/src/common/libflux/conf.h @@ -145,7 +145,7 @@ void flux_conf_environment_no_dedup_push_back (flux_conf_t cf, void flux_conf_environment_set (flux_conf_t cf, const char *key, const char *value, - const char *separator); + char separator); /** * @brief Explicitly unset the environment variable "key," this will cause the * variable to be cleared when this environment spec is applied. @@ -167,7 +167,7 @@ void flux_conf_environment_unset (flux_conf_t cf, const char *key); void flux_conf_environment_from_env (flux_conf_t cf, const char *key, const char *default_base, - const char *separator); + char separator); /** * @brief Set the separator for a given environment variable. * @@ -178,7 +178,7 @@ void flux_conf_environment_from_env (flux_conf_t cf, */ void flux_conf_environment_set_separator (flux_conf_t cf, const char *key, - const char *separator); + char separator); /** * @brief Get the value of a given environment variable by name. diff --git a/src/common/libutil/Makefile.am b/src/common/libutil/Makefile.am index bd2c5ebae8b3..326dd8caa4a5 100644 --- a/src/common/libutil/Makefile.am +++ b/src/common/libutil/Makefile.am @@ -51,10 +51,6 @@ libutil_la_SOURCES = \ sds.c \ sds.h \ sdsalloc.h \ - vec.c \ - vec.h \ - usa.c \ - usa.h \ iterators.h \ macros.h \ sha1.h \ diff --git a/src/common/libutil/usa.c b/src/common/libutil/usa.c deleted file mode 100644 index adb0dcadb268..000000000000 --- a/src/common/libutil/usa.c +++ /dev/null @@ -1,212 +0,0 @@ -/*****************************************************************************\ - * Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at - * the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS). - * LLNL-CODE-658032 All rights reserved. - * - * This file is part of the Flux resource manager framework. - * For details, see https://github.com/flux-framework. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the license, or (at your option) - * any later version. - * - * Flux is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - * See also: http://www.gnu.org/licenses/ -\*****************************************************************************/ - -#include "usa.h" - -#include "xzmalloc.h" - -#include - -unique_string_array_t *usa_alloc () -{ - return xzmalloc (sizeof(unique_string_array_t)); -} - -void usa_init (unique_string_array_t *item) -{ - item->clean = false; - item->sep = sdsempty (); - item->string_cache = sdsempty (); - vec_init (&item->components); -} - -unique_string_array_t *usa_new () -{ - unique_string_array_t *item = usa_alloc (); - usa_init (item); - return item; -} - -void usa_clear (unique_string_array_t *item) -{ - int i; - sds to_free; - if (!item) - return; - - sdsclear (item->sep); - sdsclear (item->string_cache); - vec_foreach (&item->components, to_free, i) - { - sdsfree (to_free); - } - vec_clear (&item->components); - item->clean = false; -} - -void usa_destroy (unique_string_array_t *item) -{ - usa_clear (item); - vec_deinit (&item->components); - sdsfree (item->sep); - sdsfree (item->string_cache); - free (item); -} - -void usa_set (unique_string_array_t *item, const char *value) -{ - item->clean = false; - usa_clear (item); - vec_push (&item->components, sdsnew (value)); -} - -void usa_push (unique_string_array_t *item, const char *str) -{ - vec_insert (&item->components, 0, sdsnew (str)); -} - -void usa_push_back (unique_string_array_t *item, const char *str) -{ - vec_push (&item->components, sdsnew (str)); -} - -#if 0 -static void usa_print_all (unique_string_array_t *item) -{ - int i; - sds part; - vec_foreach (&item->components, part, i) - { - printf ("%d: %s\n", i, part); - } -} -#endif - -int usa_remove (unique_string_array_t *item, const char *str) -{ - int idx = usa_find_idx (item, str); - if (idx >= 0) { - vec_splice (&item->components, idx, 1); - } - return idx; -} - -void usa_split_and_set (unique_string_array_t *item, const char *value) -{ - item->clean = false; - usa_clear (item); - usa_split_and_push (item, value, false); -} - -void usa_split_and_push (unique_string_array_t *item, - const char *value, - bool before) -{ - int value_count = 0, i; - - if (!value || strlen (value) == 0) - return; - - sds part; - sds *new_parts = sdssplitlen ( - value, strlen (value), item->sep, strlen (item->sep), &value_count); - - for (i = 0, part = new_parts[i]; i < value_count; - i++, part = new_parts[i]) { - if (!sdslen (part)) - continue; - if (before) { - usa_remove (item, part); - usa_push (item, part); - } else { - if (usa_find_idx (item, part) >= 0) - continue; - usa_push_back (item, part); - } - item->clean = false; - } - - sdsfreesplitres (new_parts, value_count); -} - -void usa_set_separator (unique_string_array_t *item, const char *separator) -{ - if (item) - item->sep = sdscpy (item->sep, separator); -} - -const char *usa_get_joined (unique_string_array_t *item) -{ - if (!item) - return NULL; - - if (usa_length (item) == 1) - return usa_data (item)[0]; - - if (!item->clean) { - sdsfree (item->string_cache); - item->string_cache = sdsjoinsds (item->components.data, - item->components.length, - item->sep, - sdslen (item->sep)); - } - - return item->string_cache; -} - -const char *usa_find (unique_string_array_t *item, const char *str) -{ - int idx = usa_find_idx (item, str); - return idx >= 0 ? item->components.data[idx] : NULL; -} - -int usa_find_idx (unique_string_array_t *item, const char *str) -{ - int i; - sds part; - vec_foreach (&item->components, part, i) - { - if (!strcmp (part, str)) { - return i; - } - } - return -1; -} - -void usa_deduplicate (unique_string_array_t *item) -{ - int i; - sds part; - vec_str_t v = item->components; - vec_init (&item->components); - vec_foreach (&v, part, i) - { - if (!usa_find (item, part)) { - vec_push (&item->components, part); - } else { - sdsfree (part); - } - } - vec_deinit (&v); -} diff --git a/src/common/libutil/usa.h b/src/common/libutil/usa.h deleted file mode 100644 index c5fd94a2d9c1..000000000000 --- a/src/common/libutil/usa.h +++ /dev/null @@ -1,213 +0,0 @@ -/*****************************************************************************\ - * Copyright (c) 2014 Lawrence Livermore National Security, LLC. Produced at - * the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS). - * LLNL-CODE-658032 All rights reserved. - * - * This file is part of the Flux resource manager framework. - * For details, see https://github.com/flux-framework. - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the license, or (at your option) - * any later version. - * - * Flux is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. - * See also: http://www.gnu.org/licenses/ -\*****************************************************************************/ - -/*! \file usa.h - * \brief Unique String Arrays - * - * A unique_string_array_t stores a dense array of strings that can be easily - * generated from output that must be split, joined, and otherwise - * manipulated. This is most useful for generating argv-style argument lists - * and managing paths where input path components may not have been processed - * into individual path components ahead of time. - * - * All strings used herein are sds-based, and can be treated as such with - * confidence. The parameter types do not reflect this for interoperability - * with other code. - */ - -#ifndef FLUX_UNIQUE_STRING_ARRAY_H -#define FLUX_UNIQUE_STRING_ARRAY_H - -#include - -#include "sds.h" -#include "vec.h" - -/** - * @brief Base structure for a usa, may be stack-based, but needs init. - */ -typedef struct unique_string_array -{ - sds sep; - vec_str_t components; - sds string_cache; - bool clean; -} unique_string_array_t; - -/** - * @brief Get the length of the array in number of strings. - * - * @param a The array - * - * @return The number of strings in the array - */ -#define usa_length(a) ((a)->components.length) - -/** - * @brief Get the underlying array, usable as an argv-style parameter. - * - * @param a The array class - * - * @return The underlying C array - */ -#define usa_data(a) ((a)->components.data) - -/** - * @brief Allocate, but do not initialize, a usa object. - * - * @return An un-initialized usa - */ -unique_string_array_t *usa_alloc (); -/** - * @brief Initialize a usa, most useful for stack-allocated objects. - * - * @param item Pointer to the usa to initialize - */ -void usa_init (unique_string_array_t *item); -/** - * @brief Allocate and initialize a new usa in one step. - * - * @return A ready-to-use usa instance, NULL on failure - */ -unique_string_array_t *usa_new (); -/** - * @brief Re-set a usa as though it were new, keeping the storage active. - * - * @param item The usa to clear - */ -void usa_clear (unique_string_array_t *item); -/** - * @brief Clear and deallocate the storage used by a usa. - * - * @param item The usa to be destroyed - */ -void usa_destroy (unique_string_array_t *item); - -/** - * @brief Set a usa to contain only the string "str," clearing the current - * contents, does not split its argument by separator. - * - * @param item The usa to set - * @param str The string to add - */ -void usa_set (unique_string_array_t *item, const char *str); -/** - * @brief Push a new string onto the front of the array, requires a move of - * all other elements, not O(1) and does *not* deduplicate. - * - * @param item The usa to push into - * @param str The string to push - */ -void usa_push (unique_string_array_t *item, const char *str); -/** - * @brief Push a new string onto the back of the array, this is O(1) unless - * an allocation is required and does *not* deduplicate. - * - * @param item The usa to push into - * @param str The string to push - */ -void usa_push_back (unique_string_array_t *item, const char *str); -/** - * @brief Find and remove a string. - * - * @param item The usa to remove the string from - * @param str The string to remove - * - * @return On success, the index that was removed, on failure -1 - */ -int usa_remove (unique_string_array_t *item, const char *str); - -/** - * @brief Set the separator character to be used by the split and join - * functions on this object. - * - * @param item The array object - * @param separator The separator to use - */ -void usa_set_separator (unique_string_array_t *item, const char *separator); -/** - * @brief Split the input string by separator and set the contents of the - * array to the resulting tokens. - * - * This function performs de-duplication as it adds componenets to the array. - * - * @param item The array to act on - * @param str The string to be split and set - */ -void usa_split_and_set (unique_string_array_t *item, const char *str); -/** - * @brief Split the input string by separator and push the resulting tokens - * onto the list at front or back. - * - * This function performs de-duplication as it adds componenets to the array. - * - * @param item The array to act on - * @param str The string to be split and pushed - * @param before Push to the front if true, back if false - */ -void usa_split_and_push (unique_string_array_t *item, - const char *value, - bool before); -/** - * @brief Get the result of joining all strings in the array, in order, by the - * separator character. - * - * The returned value is owned by the array, and should not be freed. It is a - * cached value, so repeated calls to this function are cheap if no - * modifications are made inbetween. - * - * @param item The array to join. - * - * @return The joined string - */ -const char *usa_get_joined (unique_string_array_t *item); -/** - * @brief Find a string in the array matching str, if no such match is found - * return NULL. - * - * @param item The array to search - * @param str The string to search for - * - * @return A pointer to the first match in the array or NULL - */ -const char *usa_find (unique_string_array_t *item, const char *str); -/** - * @brief Find a string in the array matching str and return the index. - * - * @param item The array to search - * @param str The string to search for - * - * @return The array index of the first match or -1 if none is found - */ -int usa_find_idx (unique_string_array_t *item, const char *str); - -/** - * @brief Removes all duplicates in the array, leaving the left-most version - * of each in place. - * - * @param item The array to de-duplicate - */ -void usa_deduplicate (unique_string_array_t *item); - -#endif /* FLUX_UNIQUE_STRING_ARRAY_H */ diff --git a/src/common/libutil/vec.c b/src/common/libutil/vec.c deleted file mode 100644 index ab75cf992a4f..000000000000 --- a/src/common/libutil/vec.c +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Copyright (c) 2014 rxi - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the MIT license. See LICENSE for details. - */ - -#include "vec.h" - - -int vec_expand_(char **data, int *length, int *capacity, int memsz) { - if (*length + 1 > *capacity) { - void *ptr; - int n = (*capacity == 0) ? 1 : *capacity << 1; - ptr = realloc(*data, n * memsz); - if (ptr == NULL) return -1; - *data = ptr; - *capacity = n; - } - return 0; -} - - -int vec_reserve_(char **data, int *length, int *capacity, int memsz, int n) { - (void) length; - if (n > *capacity) { - void *ptr = realloc(*data, n * memsz); - if (ptr == NULL) return -1; - *data = ptr; - *capacity = n; - } - return 0; -} - - -int vec_reserve_po2_( - char **data, int *length, int *capacity, int memsz, int n -) { - int n2 = 1; - if (n == 0) return 0; - while (n2 < n) n2 <<= 1; - return vec_reserve_(data, length, capacity, memsz, n2); -} - - -int vec_compact_(char **data, int *length, int *capacity, int memsz) { - if (*length == 0) { - free(*data); - *data = NULL; - *capacity = 0; - return 0; - } else { - void *ptr; - int n = *length; - ptr = realloc(*data, n * memsz); - if (ptr == NULL) return -1; - *capacity = n; - *data = ptr; - } - return 0; -} - - -int vec_insert_(char **data, int *length, int *capacity, int memsz, - int idx -) { - int err = vec_expand_(data, length, capacity, memsz); - if (err) return err; - memmove(*data + (idx + 1) * memsz, - *data + idx * memsz, - (*length - idx) * memsz); - return 0; -} - - -void vec_splice_(char **data, int *length, int *capacity, int memsz, - int start, int count -) { - (void) capacity; - memmove(*data + start * memsz, - *data + (start + count) * memsz, - (*length - start - count) * memsz); -} - - -void vec_swapsplice_(char **data, int *length, int *capacity, int memsz, - int start, int count -) { - (void) capacity; - memmove(*data + start * memsz, - *data + (*length - count) * memsz, - count * memsz); -} - - -void vec_swap_(char **data, int *length, int *capacity, int memsz, - int idx1, int idx2 -) { - unsigned char *a, *b, tmp; - int count; - (void) length; - (void) capacity; - if (idx1 == idx2) return; - a = (unsigned char*) *data + idx1 * memsz; - b = (unsigned char*) *data + idx2 * memsz; - count = memsz; - while (count--) { - tmp = *a; - *a = *b; - *b = tmp; - a++, b++; - } -} diff --git a/src/common/libutil/vec.h b/src/common/libutil/vec.h deleted file mode 100644 index 32aec970ae6e..000000000000 --- a/src/common/libutil/vec.h +++ /dev/null @@ -1,181 +0,0 @@ -/** - * Copyright (c) 2014 rxi - * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the MIT license. See LICENSE for details. - */ - -#ifndef VEC_H -#define VEC_H - -#include -#include - -#define VEC_VERSION "0.2.1" - - -#define vec_unpack_(v)\ - (char**)&(v)->data, &(v)->length, &(v)->capacity, sizeof(*(v)->data) - - -#define vec_t(T)\ - struct { T *data; int length, capacity; } - - -#define vec_init(v)\ - memset((v), 0, sizeof(*(v))) - - -#define vec_deinit(v)\ - ( free((v)->data),\ - vec_init(v) ) - - -#define vec_push(v, val)\ - ( vec_expand_(vec_unpack_(v)) ? -1 :\ - ((v)->data[(v)->length++] = (val), 0), 0 ) - - -#define vec_pop(v)\ - (v)->data[--(v)->length] - - -#define vec_splice(v, start, count)\ - ( vec_splice_(vec_unpack_(v), start, count),\ - (v)->length -= (count) ) - - -#define vec_swapsplice(v, start, count)\ - ( vec_swapsplice_(vec_unpack_(v), start, count),\ - (v)->length -= (count) ) - - -#define vec_insert(v, idx, val)\ - ( vec_insert_(vec_unpack_(v), idx) ? -1 :\ - ((v)->data[idx] = (val), 0), (v)->length++, 0 ) - - -#define vec_sort(v, fn)\ - qsort((v)->data, (v)->length, sizeof(*(v)->data), fn) - - -#define vec_swap(v, idx1, idx2)\ - vec_swap_(vec_unpack_(v), idx1, idx2) - - -#define vec_truncate(v, len)\ - ((v)->length = (len) < (v)->length ? (len) : (v)->length) - - -#define vec_clear(v)\ - ((v)->length = 0) - - -#define vec_first(v)\ - (v)->data[0] - - -#define vec_last(v)\ - (v)->data[(v)->length - 1] - - -#define vec_reserve(v, n)\ - vec_reserve_(vec_unpack_(v), n) - - -#define vec_compact(v)\ - vec_compact_(vec_unpack_(v)) - - -#define vec_pusharr(v, arr, count)\ - do {\ - int i__, n__ = (count);\ - if (vec_reserve_po2_(vec_unpack_(v), (v)->length + n__) != 0) break;\ - for (i__ = 0; i__ < n__; i__++) {\ - (v)->data[(v)->length++] = (arr)[i__];\ - }\ - } while (0) - - -#define vec_extend(v, v2)\ - vec_pusharr((v), (v2)->data, (v2)->length) - - -#define vec_find(v, val, idx)\ - do {\ - for ((idx) = 0; (idx) < (v)->length; (idx)++) {\ - if ((v)->data[(idx)] == (val)) break;\ - }\ - if ((idx) == (v)->length) (idx) = -1;\ - } while (0) - - -#define vec_remove(v, val)\ - do {\ - int idx__;\ - vec_find(v, val, idx__);\ - if (idx__ != -1) vec_splice(v, idx__, 1);\ - } while (0) - - -#define vec_reverse(v)\ - do {\ - int i__ = (v)->length / 2;\ - while (i__--) {\ - vec_swap((v), i__, (v)->length - (i__ + 1));\ - }\ - } while (0) - - -#define vec_foreach(v, var, iter)\ - if ( (v)->length > 0 )\ - for ( (iter) = 0;\ - (iter) < (v)->length && (((var) = (v)->data[(iter)]), 1);\ - ++(iter)) - - -#define vec_foreach_rev(v, var, iter)\ - if ( (v)->length > 0 )\ - for ( (iter) = (v)->length - 1;\ - (iter) >= 0 && (((var) = (v)->data[(iter)]), 1);\ - --(iter)) - - -#define vec_foreach_ptr(v, var, iter)\ - if ( (v)->length > 0 )\ - for ( (iter) = 0;\ - (iter) < (v)->length && (((var) = &(v)->data[(iter)]), 1);\ - ++(iter)) - - -#define vec_foreach_ptr_rev(v, var, iter)\ - if ( (v)->length > 0 )\ - for ( (iter) = (v)->length - 1;\ - (iter) >= 0 && (((var) = &(v)->data[(iter)]), 1);\ - --(iter)) - - - -int vec_expand_(char **data, int *length, int *capacity, int memsz); -int vec_reserve_(char **data, int *length, int *capacity, int memsz, int n); -int vec_reserve_po2_(char **data, int *length, int *capacity, int memsz, - int n); -int vec_compact_(char **data, int *length, int *capacity, int memsz); -int vec_insert_(char **data, int *length, int *capacity, int memsz, - int idx); -void vec_splice_(char **data, int *length, int *capacity, int memsz, - int start, int count); -void vec_swapsplice_(char **data, int *length, int *capacity, int memsz, - int start, int count); -void vec_swap_(char **data, int *length, int *capacity, int memsz, - int idx1, int idx2); - - -typedef vec_t(void*) vec_void_t; -typedef vec_t(char*) vec_str_t; -typedef vec_t(int) vec_int_t; -typedef vec_t(char) vec_char_t; -typedef vec_t(float) vec_float_t; -typedef vec_t(double) vec_double_t; - -#endif