summaryrefslogtreecommitdiffstats
path: root/src/util/profile/testmod/testmod_main.c
blob: 126b199dca3dea606e88192d27f3079d5e2c63a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* util/profile/proftest/test.c - Test dynamic profile module */
/*
 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 */

/*
 * This file implements a very simple profile module which just returns the
 * residual string and the number of copies in response to any query.  The full
 * range of vtable profile operations is tested elsewhere.
 */

#include "k5-platform.h"
#include "profile.h"

struct data {
    char *residual;
    int gen;
};

static long
get_values(void *cbdata, const char *const *names, char ***ret_values)
{
    struct data *d = cbdata;

    *ret_values = calloc(3, sizeof(*ret_values));
    (*ret_values)[0] = strdup(d->residual);
    asprintf(&(*ret_values)[1], "%d", d->gen);
    (*ret_values)[2] = NULL;
    return 0;
}

static void
free_values(void *cbdata, char **values)
{
    char **v;

    for (v = values; *v; v++)
        free(*v);
    free(values);
}

static void
cleanup(void *cbdata)
{
    struct data *d = cbdata;

    free(d->residual);
    free(d);
}

static long
copy(void *cbdata, void **ret_cbdata)
{
    struct data *old_data = cbdata, *new_data;

    new_data = malloc(sizeof(*new_data));
    new_data->residual = strdup(old_data->residual);
    new_data->gen = old_data->gen + 1;
    *ret_cbdata = new_data;
    return 0;
}

long
profile_module_init(const char *residual, struct profile_vtable *vtable,
                    void **cb_ret);

long
profile_module_init(const char *residual, struct profile_vtable *vtable,
                    void **cb_ret)
{
    struct data *d;

    d = malloc(sizeof(*d));
    d->residual = strdup(residual);
    d->gen = 0;
    *cb_ret = d;

    vtable->get_values = get_values;
    vtable->free_values = free_values;
    vtable->cleanup = cleanup;
    vtable->copy = copy;
    return 0;
}