/alps/pcitool

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/pcitool

« back to all changes in this revision

Viewing changes to pcilib/unit.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-09-24 02:28:45 UTC
  • mfrom: (305.1.19 views)
  • Revision ID: csa@suren.me-20150924022845-p7hc8lh8v0q48g0r
Finalyze XML support and provide initial support for views (only descriptions so far)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <strings.h>
 
5
 
 
6
#include "pci.h"
 
7
#include "pcilib.h"
 
8
#include "unit.h"
 
9
#include "error.h"
 
10
 
 
11
 
 
12
pcilib_unit_t pcilib_find_unit_by_name(pcilib_t *ctx, const char *unit) {
 
13
    pcilib_unit_t i;
 
14
 
 
15
    for(i = 0; ctx->units[i].name; i++) {
 
16
        if (!strcasecmp(ctx->units[i].name, unit))
 
17
            return i;
 
18
    }
 
19
    return PCILIB_UNIT_INVALID;
 
20
}
 
21
 
 
22
 
 
23
int pcilib_add_units(pcilib_t *ctx, size_t n, const pcilib_unit_description_t *desc) {
 
24
    if (!n) {
 
25
        for (n = 0; desc[n].name; n++);
 
26
    }
 
27
 
 
28
    if ((ctx->num_units + n + 1) > ctx->alloc_units) {
 
29
        size_t size;
 
30
        pcilib_unit_description_t *units;
 
31
 
 
32
        for (size = ctx->alloc_units; size < 2 * (n + ctx->num_units + 1); size <<= 1);
 
33
 
 
34
        units = (pcilib_unit_description_t*)realloc(ctx->units, size * sizeof(pcilib_unit_description_t));
 
35
        if (!units) return PCILIB_ERROR_MEMORY;
 
36
 
 
37
        ctx->units = units;
 
38
        ctx->alloc_units = size;
 
39
 
 
40
        ctx->model_info.units = units;
 
41
    }
 
42
 
 
43
    memcpy(ctx->units + ctx->num_units, desc, n * sizeof(pcilib_unit_description_t));
 
44
    memset(ctx->units + ctx->num_units + n, 0, sizeof(pcilib_unit_description_t));
 
45
 
 
46
    ctx->num_units += n;
 
47
 
 
48
    return 0;
 
49
}