forked from acassen/keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
88 lines (78 loc) · 3.34 KB
/
vector.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
/*
* Soft: Keepalived is a failover program for the LVS project
* <www.linuxvirtualserver.org>. It monitor & manipulate
* a loadbalanced server pool using multi-layer checks.
*
* Part: vector.c include file.
*
* Author: Alexandre Cassen, <[email protected]>
*
* 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.
*
* 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; either version
* 2 of the License, or (at your option) any later version.
*
* Copyright (C) 2001-2017 Alexandre Cassen, <[email protected]>
*/
#ifndef _VECTOR_H
#define _VECTOR_H
#include <sys/types.h>
#include <stdio.h>
/* vector definition */
typedef struct _vector {
unsigned int active;
unsigned int allocated;
void **slot;
} vector_t;
typedef void (*null_strvec_handler_t)(const vector_t *, size_t);
/* Some defines */
#define VECTOR_DEFAULT_SIZE 1
/* Some useful macros */
#define vector_size(V) ((V)->allocated)
#define vector_slot(V,E) ((V)->slot[(E)])
//#define vector_slot(V,E) (vector_lookup(V,E))
#define vector_active(V) ((V)->active)
#define vector_foreach_slot(v,p,i) \
for (i = 0; i < (v)->allocated && ((p) = (v)->slot[i]); i++)
#ifdef _MEM_CHECK_
#define vector_alloc() (memcheck_log("vector_alloc", NULL, (__FILE__), (__func__), (__LINE__)), \
vector_alloc_r())
#define vector_alloc_slot(v) (memcheck_log("vector_alloc_slot", NULL, (__FILE__), (__func__), (__LINE__)), \
vector_alloc_slot_r(v))
#define vector_free(v) (memcheck_log("vector_free", NULL, (__FILE__), (__func__), (__LINE__)), \
vector_free_r(v))
#define vector_compact(v) (memcheck_log("vector_compact", NULL, (__FILE__), (__func__), (__LINE__)), \
vector_compact_r(v))
#define vector_copy(v) (memcheck_log("vector_copy", NULL, (__FILE__), (__func__), (__LINE__)), \
vector_copy_r(v))
#else
#define vector_alloc() (vector_alloc_r())
#define vector_alloc_slot(v) (vector_alloc_slot_r(v))
#define vector_free(v) (vector_free_r(v))
#define vector_compact(v) (vector_compact_r(v))
#define vector_copy(v) (vector_copy_r(v))
#endif
/* Prototypes */
extern null_strvec_handler_t register_null_strvec_handler(null_strvec_handler_t);
extern null_strvec_handler_t unregister_null_strvec_handler(void);
extern const char *strvec_slot(const vector_t *, size_t);
extern vector_t *vector_alloc_r(void) __attribute__ ((malloc));
extern void vector_alloc_slot_r(vector_t *);
extern vector_t *vector_copy_r(const vector_t *v);
extern void vector_set_slot(vector_t *, void *);
extern void vector_unset(vector_t *, unsigned int);
extern unsigned int vector_count(const vector_t *) __attribute__ ((pure));
extern void vector_free_r(const vector_t *);
extern vector_t *vector_compact_r(const vector_t *);
#ifdef _INCLUDE_UNUSED_CODE_
extern void vector_dump(FILE *, const vector_t *);
#endif
extern char *make_strvec_str(const vector_t *, unsigned);
extern void free_strvec(const vector_t *);
extern vector_t *strvec_remove_slot(vector_t *, unsigned);
#endif