File: vector.h

package info (click to toggle)
multipath-tools 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,992 kB
  • sloc: ansic: 63,788; perl: 1,622; makefile: 729; sh: 647; pascal: 150
file content (83 lines) | stat: -rw-r--r-- 2,715 bytes parent folder | download
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * 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.
 *
 * Version:     $Id: vector.h,v 1.0.3 2003/05/11 02:28:03 acassen Exp $
 *
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
 */

#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED

#include <stdbool.h>

/* vector definition */
struct vector_s {
	int allocated;
	void **slot;
};
typedef struct vector_s *vector;

#define VECTOR_SIZE(V)   ((V) ? (V)->allocated : 0)
#define VECTOR_SLOT(V,E) (((V) && (E) < VECTOR_SIZE(V) && (E) >= 0) ? (V)->slot[(E)] : NULL)
#define VECTOR_LAST_SLOT(V)   (((V) && VECTOR_SIZE(V) > 0) ? (V)->slot[(VECTOR_SIZE(V) - 1)] : NULL)

#define vector_foreach_slot(v,p,i) \
	for (i = 0; (v) && (int)i < VECTOR_SIZE(v) && ((p) = (v)->slot[i]); i++)
#define vector_foreach_slot_after(v,p,i) \
	for (; (v) && (int)i < VECTOR_SIZE(v) && ((p) = (v)->slot[i]); i++)
#define vector_foreach_slot_backwards(v,p,i) \
	for (i = VECTOR_SIZE(v) - 1; (int)i >= 0 && ((p) = (v)->slot[i]); i--)

#define identity(x) (x)
/*
 * Given a vector vec with elements of given type,
 * return a newly allocated vector with elements conv(e) for each element
 * e in vec. "conv" may be a macro or a function.
 * Use "identity" for a simple copy.
 */
#define vector_convert(new, vec, type, conv)				\
	({								\
		const struct vector_s *__v = (vec);			\
		vector __t = (new);					\
		type *__j;						\
		int __i;						\
									\
		if (__t == NULL)					\
			__t = vector_alloc();				\
		if (__t != NULL) {					\
			vector_foreach_slot(__v, __j, __i) {		\
				if (!vector_alloc_slot(__t)) {	\
					vector_free(__t);		\
					__t = NULL;			\
					break;				\
				}					\
				vector_set_slot(__t, conv(__j));	\
			}						\
		}							\
		__t;							\
	})

/* Prototypes */
extern vector vector_alloc(void);
extern bool vector_alloc_slot(vector v);
vector vector_reset(vector v);
extern void vector_free(vector v);
void cleanup_vector(vector *pv);
#define vector_free_const(x) vector_free((vector)(long)(x))
extern void free_strvec(vector strvec);
extern void vector_set_slot(vector v, void *value);
extern void vector_del_slot(vector v, int slot);
extern void *vector_insert_slot(vector v, int slot, void *value);
int find_slot(vector v, void * addr);
int vector_find_or_add_slot(vector v, void *value);
extern void vector_dump(vector v);
extern void dump_strvec(vector strvec);
extern int vector_move_up(vector v, int src, int dest);
void vector_sort(vector v, int (*compar)(const void *, const void *));
#endif