mspdebug/util/vector.c

110 lines
2.2 KiB
C
Raw Normal View History

2010-04-20 23:18:56 +00:00
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "vector.h"
void vector_init(struct vector *v, int elemsize)
{
memset(v, 0, sizeof(*v));
v->elemsize = elemsize;
}
void vector_destroy(struct vector *v)
{
if (v->ptr)
free(v->ptr);
memset(v, 0, sizeof(*v));
}
int vector_realloc(struct vector *v, int capacity)
{
assert (capacity >= 0);
if (capacity) {
void *new_ptr = realloc(v->ptr, capacity * v->elemsize);
if (!new_ptr)
return -1;
v->ptr = new_ptr;
} else {
v->ptr = NULL;
}
v->capacity = capacity;
if (v->size > capacity)
v->size = capacity;
return 0;
}
2011-04-04 05:09:11 +00:00
static int size_for(struct vector *v, int needed)
2010-04-20 23:18:56 +00:00
{
2011-04-04 05:09:11 +00:00
int cap = needed;
2010-04-20 23:18:56 +00:00
2011-04-04 05:09:11 +00:00
/* Find the smallest power of 2 which is greater than the
* necessary capacity.
*/
while (cap & (cap - 1))
cap &= (cap - 1);
if (cap < needed)
cap <<= 1;
2010-04-20 23:18:56 +00:00
2011-04-04 05:09:11 +00:00
/* Don't allocate fewer than 8 elements */
if (cap < 8)
cap = 8;
2010-04-20 23:18:56 +00:00
2011-04-04 05:09:11 +00:00
if (v->capacity >= cap && v->capacity <= cap * 2)
return 0;
2010-04-20 23:18:56 +00:00
2011-04-04 05:09:11 +00:00
if (vector_realloc(v, cap) < 0)
return -1;
2010-04-20 23:18:56 +00:00
2011-04-04 05:09:11 +00:00
return 0;
}
int vector_push(struct vector *v, const void *data, int count)
{
int needed = v->size + count;
assert (count >= 0);
if (size_for(v, needed) < 0)
return -1;
2010-04-20 23:18:56 +00:00
memcpy((char *)v->ptr + v->size * v->elemsize,
data,
count * v->elemsize);
v->size += count;
return 0;
}
2011-04-04 05:09:11 +00:00
void vector_pop(struct vector *v)
{
if (v->size <= 0)
return;
size_for(v, v->size - 1);
v->size--;
}