/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/view.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 "view.h"
 
9
#include "error.h"
 
10
 
 
11
 
 
12
pcilib_view_t pcilib_find_view_by_name(pcilib_t *ctx, const char *view) {
 
13
    pcilib_view_t i;
 
14
 
 
15
    for(i = 0; ctx->views[i]; i++) {
 
16
        if (!strcasecmp(ctx->views[i]->name, view))
 
17
            return i;
 
18
    }
 
19
 
 
20
    return PCILIB_VIEW_INVALID;
 
21
}
 
22
 
 
23
 
 
24
 
 
25
int pcilib_add_views(pcilib_t *ctx, size_t n, const pcilib_view_description_t *desc) {
 
26
    size_t i;
 
27
    void *ptr;
 
28
 
 
29
    if (!n) {
 
30
            // No padding between array elements
 
31
        ptr = (void*)desc;
 
32
        while (1) {
 
33
            const pcilib_view_description_t *v = (const pcilib_view_description_t*)ptr;
 
34
            if (v->name) n++;
 
35
            else break;
 
36
            ptr += v->api->description_size;
 
37
        }
 
38
    }
 
39
 
 
40
    if ((ctx->num_views + n + 1) > ctx->alloc_views) {
 
41
        size_t size;
 
42
        pcilib_view_description_t **views;
 
43
        for (size = ctx->alloc_views; size < 2 * (n + ctx->num_views + 1); size<<=1);
 
44
 
 
45
        views = (pcilib_view_description_t**)realloc(ctx->views, size * sizeof(pcilib_view_description_t*));
 
46
        if (!views) return PCILIB_ERROR_MEMORY;
 
47
 
 
48
        ctx->views = views;
 
49
        ctx->alloc_views = size;
 
50
 
 
51
        ctx->model_info.views = (const pcilib_view_description_t**)views;
 
52
    }
 
53
 
 
54
        // No padding between array elements
 
55
    ptr = (void*)desc;
 
56
    for (i = 0; i < n; i++) {
 
57
        const pcilib_view_description_t *v = (const pcilib_view_description_t*)ptr;
 
58
        ctx->views[ctx->num_views + i] = (pcilib_view_description_t*)malloc(v->api->description_size);
 
59
        if (!ctx->views[ctx->num_views + i]) {
 
60
            size_t j;
 
61
            for (j = 0; j < i; j++)
 
62
                free(ctx->views[ctx->num_views + j]);
 
63
            ctx->views[ctx->num_views] = NULL;
 
64
            pcilib_error("Error allocating %zu bytes of memory for the view description", v->api->description_size);
 
65
            return PCILIB_ERROR_MEMORY;
 
66
        }
 
67
        memcpy(ctx->views[ctx->num_views + i], v, v->api->description_size);
 
68
        ptr += v->api->description_size;
 
69
    }
 
70
    ctx->views[ctx->num_views + i] = NULL;
 
71
    ctx->num_views += n;
 
72
 
 
73
    return 0;
 
74
}