Signed-off-by: Nikola Pajkovsky npajkovs@redhat.com --- lib/Utils/Makefile.am | 3 +- lib/Utils/darray.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/Utils/darray.h | 50 +++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletions(-) create mode 100644 lib/Utils/darray.c create mode 100644 lib/Utils/darray.h
diff --git a/lib/Utils/Makefile.am b/lib/Utils/Makefile.am index 13144f6..172596b 100644 --- a/lib/Utils/Makefile.am +++ b/lib/Utils/Makefile.am @@ -26,7 +26,8 @@ libABRTUtils_la_SOURCES = \ ABRTException.cpp \ backtrace.h backtrace.c \ backtrace_parser.y \ - strbuf.h strbuf.c + strbuf.h strbuf.c \ + darray.h darray.c libABRTUtils_la_CPPFLAGS = \ -Wall -Werror \ -I$(srcdir)/../../inc \ diff --git a/lib/Utils/darray.c b/lib/Utils/darray.c new file mode 100644 index 0000000..0ff18a1 --- /dev/null +++ b/lib/Utils/darray.c @@ -0,0 +1,74 @@ +/* + Copyright (C) 2009 RedHat inc. + + 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include <assert.h> + +#include "darray.h" + +#define SIZE 32 + +darray_t* darray_new() +{ + darray_t* da = (darray_t*)xmalloc(sizeof(darray_t)); + + da->size = SIZE; + da->last = 0; + da->data = (void**)xmalloc(da->size * sizeof(void**)); + int ii; + for (ii = 0; ii < da->size; ii++) + { + da->data[ii] = xmalloc(sizeof(void*)); + } + + return da; +} + +static void darray_grow(darray_t* da) +{ + da->size *= 2; + da->data = (void**)xrealloc(da->data, da->size * sizeof(void*)); +} + +void darray_add(darray_t* da, void* item) +{ + if (da->size == da->last) + { + darray_grow(da); + } + + da->data[da->last++] = item; + da->data[da->last] = NULL; +} + +int darray_empty(darray_t* da) +{ + return (da->last == 0 )? 1: 0; +} + +void darray_destroy(darray_t* da) +{ + if (!da) + return; + + int ii; + for (ii = 0; ii < da->size; ii++) + free(da->data[ii]); + + free(da->data); +} + diff --git a/lib/Utils/darray.h b/lib/Utils/darray.h new file mode 100644 index 0000000..1169e08 --- /dev/null +++ b/lib/Utils/darray.h @@ -0,0 +1,50 @@ +/* + Copyright (C) 2009 RedHat inc. + + 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#ifndef ABRT_DARRAY_H +#define ABRT_DARRAY_H + +#include <stdio.h> +#include <stdlib.h> +#include "xfuncs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _darray_t +{ + int size; + int last; + void** data; +} darray_t; + + +extern darray_t* darray_new(); + +extern void darray_add(darray_t* da, void* item); + +extern int darray_empty(darray_t* da); + +extern void darray_destroy(darray_t* da); + +#ifdef __cplusplus +} +#endif + +#endif