diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2015-10-09 03:11:34 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2015-10-09 03:11:34 +0200 |
commit | 3b8e32c9bbe0d909c34303da0ad36ef0ef5be852 (patch) | |
tree | ed22378979b2889a84615cf9f24de76359a95244 /pcilib/unit.c | |
parent | 21812f8d763fac8ee9bb3fdc593642b06f405a2b (diff) | |
download | pcitool-3b8e32c9bbe0d909c34303da0ad36ef0ef5be852.tar.gz pcitool-3b8e32c9bbe0d909c34303da0ad36ef0ef5be852.tar.bz2 pcitool-3b8e32c9bbe0d909c34303da0ad36ef0ef5be852.tar.xz pcitool-3b8e32c9bbe0d909c34303da0ad36ef0ef5be852.zip |
Introduce hashes
Diffstat (limited to 'pcilib/unit.c')
-rw-r--r-- | pcilib/unit.c | 96 |
1 files changed, 65 insertions, 31 deletions
diff --git a/pcilib/unit.c b/pcilib/unit.c index 6817afc..0295120 100644 --- a/pcilib/unit.c +++ b/pcilib/unit.c @@ -8,16 +8,79 @@ #include "unit.h" #include "error.h" - static pcilib_unit_transform_t pcilib_unit_transform_null = { NULL, NULL }; +int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc) { + size_t i; + int err = 0; + + if (!n) { + for (n = 0; desc[n].name; n++); + } + + if ((ctx->num_units + n + 1) > ctx->alloc_units) { + size_t size; + pcilib_unit_description_t *units; + + for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size <<= 1); + + units = (pcilib_unit_description_t*)realloc(ctx->units, size * sizeof(pcilib_unit_description_t)); + if (!units) return PCILIB_ERROR_MEMORY; + + ctx->units = units; + ctx->alloc_units = size; + + ctx->model_info.units = units; + } + + // ToDo: Check if exists... + for (i = 0; i < n; i++) { + pcilib_unit_context_t *unit_ctx = (pcilib_unit_context_t*)malloc(sizeof(pcilib_unit_context_t)); + if (!unit_ctx) { + err = PCILIB_ERROR_MEMORY; + break; + } + memset(unit_ctx, 0, sizeof(pcilib_unit_context_t)); + unit_ctx->unit = ctx->num_units + i; + unit_ctx->name = desc[i].name; + + HASH_ADD_KEYPTR(hh, ctx->unit_hash, unit_ctx->name, strlen(unit_ctx->name), unit_ctx); + memcpy(ctx->units + ctx->num_units + i, &desc[i], sizeof(pcilib_unit_description_t)); + } + + memset(ctx->units + ctx->num_units + i, 0, sizeof(pcilib_unit_description_t)); + ctx->num_units += i; + + return err; +} + +void pcilib_clean_units(pcilib_t *ctx) { + pcilib_unit_context_t *s, *tmp; + + if (ctx->unit_hash) { + HASH_ITER(hh, ctx->unit_hash, s, tmp) { + HASH_DEL(ctx->unit_hash, s); + free(s); + } + } + + memset(ctx->units, 0, sizeof(pcilib_unit_description_t)); + ctx->num_units = 0; +} + pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *name) { - pcilib_unit_t i; + pcilib_unit_context_t *unit_ctx = NULL; + HASH_FIND_STR(ctx->unit_hash, name, unit_ctx); + if (unit_ctx) return unit_ctx->unit; + +/* + pcilib_unit_t i; for(i = 0; ctx->units[i].name; i++) { if (!strcasecmp(ctx->units[i].name, name)) return i; } +*/ return PCILIB_UNIT_INVALID; } @@ -42,35 +105,6 @@ pcilib_unit_transform_t *pcilib_find_transform_by_unit_names(pcilib_t *ctx, cons return NULL; } -int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc) { - if (!n) { - for (n = 0; desc[n].name; n++); - } - - if ((ctx->num_units + n + 1) > ctx->alloc_units) { - size_t size; - pcilib_unit_description_t *units; - - for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size <<= 1); - - units = (pcilib_unit_description_t*)realloc(ctx->units, size * sizeof(pcilib_unit_description_t)); - if (!units) return PCILIB_ERROR_MEMORY; - - ctx->units = units; - ctx->alloc_units = size; - - ctx->model_info.units = units; - } - - memcpy(ctx->units + ctx->num_units, desc, n * sizeof(pcilib_unit_description_t)); - memset(ctx->units + ctx->num_units + n, 0, sizeof(pcilib_unit_description_t)); - - ctx->num_units += n; - - return 0; -} - - int pcilib_transform_unit(pcilib_t *ctx, pcilib_unit_transform_t *trans, pcilib_value_t *value) { int err; |