-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmld.h
199 lines (148 loc) · 5.81 KB
/
mld.h
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* =====================================================================================
*
* Filename: mld.h
*
* Description: This file defines the data structures used for MLD tool
*
* Version: 1.0
* Created: Thursday 28 February 2019 05:14:18 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), [email protected]
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* This file is part of the MLD distribution (https://github.com/sachinites).
* Copyright (c) 2017 Abhishek Sagar.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* =====================================================================================
*/
#ifndef __MLD__
#include <assert.h>
#include <string.h>
/*Structure Data base Definition Begin*/
#define MAX_STRUCTURE_NAME_SIZE 128
#define MAX_FIELD_NAME_SIZE 128
/*Enumeration for data types*/
typedef enum {
UINT8,
UINT32,
INT32,
CHAR,
OBJ_PTR,
VOID_PTR, /*New Data type added to identify void * pointers*/
FLOAT,
DOUBLE,
OBJ_STRUCT
} data_type_t;
typedef enum{
MLD_FALSE,
MLD_TRUE
} mld_boolean_t;
#define OFFSETOF(struct_name, fld_name) \
(unsigned long)&(((struct_name *)0)->fld_name)
#define FIELD_SIZE(struct_name, fld_name) \
sizeof(((struct_name *)0)->fld_name)
typedef struct _struct_db_rec_ struct_db_rec_t;
/*Structure to store the information of one field of a
* C structure*/
typedef struct _field_info_{
char fname [MAX_FIELD_NAME_SIZE]; /*Name of the field*/
data_type_t dtype; /*Data type of the field*/
unsigned int size; /*Size of the field*/
unsigned int offset; /*Offset of the field*/
// Below field is meaningful only if dtype = OBJ_PTR, Or OBJ_STRUCT
char nested_str_name[MAX_STRUCTURE_NAME_SIZE];
} field_info_t;
/*Structure to store the information of one C structure
* which could have 'n_fields' fields*/
struct _struct_db_rec_{
struct_db_rec_t *next; /*Pointer to the next structure in the linked list*/
char struct_name [MAX_STRUCTURE_NAME_SIZE]; // key
unsigned int ds_size; /*Size of the structure*/
unsigned int n_fields; /*No of fields in the structure*/
field_info_t *fields; /*pointer to the array of fields*/
};
/*Finally the head of the linked list representing the structure
* database*/
typedef struct _struct_db_{
struct_db_rec_t *head;
unsigned int count;
} struct_db_t;
/*Structure Data base Definition Ends*/
/* Printing functions*/
void
print_structure_rec (struct_db_rec_t *struct_rec);
void
print_structure_db(struct_db_t *struct_db);
/* Fn to add the structure record in a structure database */
int /*return 0 on success, -1 on failure for some reason*/
add_structure_to_struct_db(struct_db_t *struct_db, struct_db_rec_t *struct_rec);
/*Structure Registration helping APIs*/
#define FIELD_INFO(struct_name, fld_name, dtype, nested_struct_name) \
{#fld_name, dtype, FIELD_SIZE(struct_name, fld_name), \
OFFSETOF(struct_name, fld_name), #nested_struct_name}
#define REG_STRUCT(struct_db, st_name, fields_arr) \
do{ \
struct_db_rec_t *rec = calloc(1, sizeof(struct_db_rec_t)); \
strncpy(rec->struct_name, #st_name, MAX_STRUCTURE_NAME_SIZE); \
rec->ds_size = sizeof(st_name); \
rec->n_fields = sizeof(fields_arr)/sizeof(field_info_t); \
rec->fields = fields_arr; \
if(add_structure_to_struct_db(struct_db, rec)){ \
assert(0); \
} \
}while(0);
/*Structure Data base Definition Ends*/
/*Object Database structure definitions Starts here*/
typedef struct _object_db_rec_ object_db_rec_t;
struct _object_db_rec_{
object_db_rec_t *next;
void *ptr;
unsigned int units;
struct_db_rec_t *struct_rec;
mld_boolean_t is_visited; /*Used for Graph traversal*/
mld_boolean_t is_root; /*Is this object is Root object*/
};
typedef struct _object_db_{
struct_db_t *struct_db;
object_db_rec_t *head;
unsigned int count;
} object_db_t;
/*Dumping functions*/
void
print_object_rec(object_db_rec_t *obj_rec, int i);
void
print_object_db(object_db_t *object_db);
/*API to malloc the object*/
void*
xcalloc(object_db_t *object_db, char *struct_name, int units);
/*APIs to register root objects*/
void mld_register_root_object (object_db_t *object_db,
void *objptr,
char *struct_name,
unsigned int units);
void
set_mld_object_as_global_root(object_db_t *object_db, void *obj_ptr);
/*APIs for MLD Algorithm*/
void
run_mld_algorithm(object_db_t *object_db);
void
report_leaked_objects(object_db_t *object_db);
void
mld_set_dynamic_object_as_root(object_db_t *object_db, void *obj_ptr);
void
mld_init_primitive_data_types_support(struct_db_t *struct_db);
#endif /* __MLD__ */