Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
common.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
20 #ifndef FACEMGR_COMMON_H
21 #define FACEMGR_COMMON_H
22 
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 
27 #include <hicn/util/ip_address.h>
28 
29 //#define DEBUG
30 
31 #define INT_CMP(x, y) x < y ? -1 : (x == y ? 0 : 1)
32 
33 /* Dump with indent */
34 #define INDENT(n, fmt) "%*s" fmt, n, ""
35 #define printfi(n, fmt, ...) printf(INDENT(n*4, fmt), ##__VA_ARGS__)
36 
37 #define _unused(x) ((void)(x))
38 
39 /* Random strings */
40 
41 static inline
42 void rand_str(char *dest, size_t length) {
43  char charset[] = "0123456789"
44  "abcdefghijklmnopqrstuvwxyz"
45  "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
46 
47  while (length-- > 0) {
48  size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
49  *dest++ = charset[index];
50  }
51  *dest = '\0';
52 }
53 
54 /* Boilerplate code */
55 
56 #define NO_INITIALIZE(NAME) \
57 int \
58 NAME ## _initialize(NAME ## _t * obj) { \
59  return 0; \
60 }
61 
62 #define NO_FINALIZE(NAME) \
63 int \
64 NAME ## _finalize(NAME ## _t * obj) { \
65  return 0; \
66 }
67 
68 #define AUTOGENERATE_CREATE_FREE(NAME) \
69  \
70 NAME ## _t * \
71 NAME ## _create() \
72 { \
73  NAME ## _t * obj = malloc(sizeof(NAME ## _t)); \
74  if (!obj) \
75  goto ERR_MALLOC; \
76  \
77  if (NAME ## _initialize(obj) < 0) \
78  goto ERR_INIT; \
79  \
80  return obj; \
81  \
82 ERR_INIT: \
83  free(obj); \
84 ERR_MALLOC: \
85  return NULL; \
86 } \
87  \
88 void \
89 NAME ## _free(NAME ## _t * obj) \
90 { \
91  if (NAME ## _finalize(obj) < 0) \
92  (void)0; /* XXX */ \
93  free(obj); \
94 } \
95 
96 #define AUTOGENERATE_DEFS(NAME) \
97 int NAME ## _initialize(NAME ## _t *); \
98 int NAME ## _finalize(NAME ## _t *); \
99 NAME ## _t * NAME ## _create(); \
100 void NAME ## _free(NAME ## _t *); \
101 
102 #endif /* FACEMGR_COMMON_H */