aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/common.h
blob: 9d6e8ca87d97c0d56025b37bbc8759afa13fc8f7 (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
105
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file common.h
 * \file Common definitions used throughout the code
 */
#ifndef FACEMGR_COMMON_H
#define FACEMGR_COMMON_H

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include <hicn/util/ip_address.h>

//#define DEBUG

/* Useful types and macros for comparisons */
typedef int(*cmp_t)(const void * x, const void * y);

#define INT_CMP(x, y) x < y ? -1 : (x == y ? 0 : 1)

/* Dump with indent */
#define INDENT(n, fmt) "%*s" fmt, n, ""
#define printfi(n, fmt, ...) printf(INDENT(n*4, fmt), ##__VA_ARGS__)

#define _unused(x) ((void)(x))

/* Random strings */

static inline
void rand_str(char *dest, size_t length) {
    char charset[] = "0123456789"
                     "abcdefghijklmnopqrstuvwxyz"
                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    while (length-- > 0) {
        size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
        *dest++ = charset[index];
    }
    *dest = '\0';
}

/* Boilerplate code */

#define NO_INITIALIZE(NAME)                                             \
int                                                                     \
NAME ## _initialize(NAME ## _t * obj) {                                 \
    return 0;                                             \
}

#define NO_FINALIZE(NAME)                                               \
int                                                                     \
NAME ## _finalize(NAME ## _t * obj) {                                   \
    return 0;                                             \
}

#define AUTOGENERATE_CREATE_FREE(NAME)                                  \
                                                                        \
NAME ## _t *                                                            \
NAME ## _create()                                                       \
{                                                                       \
    NAME ## _t * obj  = malloc(sizeof(NAME ## _t));                     \
    if (!obj)                                                           \
        goto ERR_MALLOC;                                                \
                                                                        \
    if (NAME ## _initialize(obj) < 0)                                   \
        goto ERR_INIT;                                                  \
                                                                        \
    return obj;                                                         \
                                                                        \
ERR_INIT:                                                               \
    free(obj);                                                          \
ERR_MALLOC:                                                             \
    return NULL;                                                        \
}                                                                       \
                                                                        \
void                                                                    \
NAME ## _free(NAME ## _t * obj)                                         \
{                                                                       \
    if (NAME ## _finalize(obj) < 0)                                     \
        (void)0; /* XXX */                                              \
    free(obj);                                                          \
}                                                                       \

#define AUTOGENERATE_DEFS(NAME)                                         \
int NAME ## _initialize(NAME ## _t *);                                  \
int NAME ## _finalize(NAME ## _t *);                                    \
NAME ## _t * NAME ## _create();                                         \
void NAME ## _free(NAME ## _t *);                                       \

#endif /* FACEMGR_COMMON_H */