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
|
/*
* 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 structure manipulation.
*
* Version: $Id: vector.c,v 1.1.15 2007/09/15 04:07:41 acassen Exp $
*
* Author: Alexandre Cassen, <acassen@linux-vs.org>
*
* 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-2007 Alexandre Cassen, <acassen@freebox.fr>
*/
#include "vector.h"
#include "memory.h"
/*
* Initialize vector struct.
* allocalted 'size' slot elements then return vector.
*/
vector
vector_alloc(void)
{
vector v = (vector) MALLOC(sizeof (struct _vector));
return v;
}
/* allocated one slot */
void
vector_alloc_slot(vector v)
{
v->allocated += VECTOR_DEFAULT_SIZE;
if (v->slot)
v->slot = REALLOC(v->slot, sizeof (void *) * v->allocated);
else
v->slot = (void *) MALLOC(sizeof (void *) * v->allocated);
}
/* Insert a value into a specific slot */
void
vector_insert_slot(vector v, int slot, void *value)
{
int i;
vector_alloc_slot(v);
for (i = (v->allocated / VECTOR_DEFAULT_SIZE) - 2; i >= slot; i--)
v->slot[i + 1] = v->slot[i];
v->slot[slot] = value;
}
/* Del a slot */
void
vector_del_slot(vector v, int slot)
{
int i;
if (!v->allocated)
return;
for (i = slot + 1; i < (v->allocated / VECTOR_DEFAULT_SIZE); i++)
v->slot[i - 1] = v->slot[i];
v->allocated -= VECTOR_DEFAULT_SIZE;
if (!v->allocated)
v->slot = NULL;
else
v->slot = (void *) MALLOC(sizeof (void *) * v->allocated);
v = REALLOC(v->slot, sizeof (void *) * v->allocated);
}
/* Free memory vector allocation */
void
vector_free(vector v)
{
FREE(v->slot);
FREE(v);
}
void
free_strvec(vector strvec)
{
int i;
char *str;
if (!strvec)
return;
for (i = 0; i < VECTOR_SIZE(strvec); i++)
if ((str = VECTOR_SLOT(strvec, i)) != NULL)
FREE(str);
vector_free(strvec);
}
/* Set a vector slot value */
void
vector_set_slot(vector v, void *value)
{
unsigned int i = v->allocated - 1;
v->slot[i] = value;
}
/* dump vector slots */
void
vector_dump(vector v)
{
int i;
printf("Vector Size : %d\n", v->allocated);
for (i = 0; i < v->allocated; i++)
if (v->slot[i] != NULL)
printf(" Slot [%d]: %p\n", i, VECTOR_SLOT(v, i));
}
void
dump_strvec(vector strvec)
{
int i;
char *str;
if (!strvec)
return;
printf("String Vector : ");
for (i = 0; i < VECTOR_SIZE(strvec); i++) {
str = VECTOR_SLOT(strvec, i);
printf("[%i]=%s ", i, str);
}
printf("\n");
}
|