From ee7d1a1c0bcc551acf85df893914f054bc61842d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 30 Mar 2009 09:16:35 +1030 Subject: [PATCH 1/1] Joey's array module. --- ccan/array/_info.c | 52 +++++++ ccan/array/array.h | 69 +++++++++ ccan/array/test/lotsOfNumbers.h | 252 ++++++++++++++++++++++++++++++++ ccan/array/test/run.c | 25 ++++ 4 files changed, 398 insertions(+) create mode 100644 ccan/array/_info.c create mode 100644 ccan/array/array.h create mode 100644 ccan/array/test/lotsOfNumbers.h create mode 100644 ccan/array/test/run.c diff --git a/ccan/array/_info.c b/ccan/array/_info.c new file mode 100644 index 00000000..1c509fbf --- /dev/null +++ b/ccan/array/_info.c @@ -0,0 +1,52 @@ +#include +#include "config.h" + +/** + * array - A collection of macros for generic dynamic array management. + * + * The array module provides generic dynamic array functions via macros. It + * removes the tedium of managing realloc'd arrays with pointer, size, and + * allocated size. It also fits into structures quite well. + * + * NOTE: The API is currently unstable. It will likely change in the near future. + * + * Example: + * #include + * #include + * + * int main(void) { + * Array(int) numbers = NewArray(); + * char buffer[32]; + * int add; + * + * for (;;) { + * AFor(i, numbers, printf("%d ", *i)) + * if (numbers.size) puts(""); + * + * printf("array> "); + * fgets(buffer, sizeof(buffer), stdin); + * if (*buffer==0 || *buffer=='\n') + * break; + * add = atoi(buffer); + * + * AAppend(numbers, add); + * } + * + * AFree(numbers); + * + * return 0; + * } + * + * Licence: BSD + */ +int main(int argc, char *argv[]) +{ + if (argc != 2) + return 1; + + if (strcmp(argv[1], "depends") == 0) + /* Nothing. */ + return 0; + + return 1; +} diff --git a/ccan/array/array.h b/ccan/array/array.h new file mode 100644 index 00000000..5054b297 --- /dev/null +++ b/ccan/array/array.h @@ -0,0 +1,69 @@ +/* + Copyright (c) 2009 Joseph A. Adams + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef CCAN_ARRAY_H +#define CCAN_ARRAY_H + +#include +#include +#include "config.h" + +#define Array(type) struct {type *item; size_t size; size_t allocSize;} +#define NewArray() {0,0,0} +#define AInit(array) do {(array).item=0; (array).size=0; (array).allocSize=0;} while(0) +#define AFree(array) do {free((array).item);} while(0) +#define AResize(array, newSize) do {size_t __newSize=(newSize); if (__newSize > (array).allocSize) {(array).allocSize = (__newSize+63)&~63; (array).item = realloc((array).item, (array).allocSize*sizeof(*(array).item));} (array).size = __newSize; } while(0) +#define AResize0(array, newSize) do {size_t __oldSize=(array).size, __newSize=(newSize); if (__newSize <= __oldSize) (array).size = __newSize; else {AResize(array,newSize); memset((array).item+__oldSize,0,(__newSize-__oldSize)*sizeof(*(array).item));} } while(0) +#define ASetAllocSize(array, newAlloc) do {(array).item = realloc((array).item, ((array).allocSize = (newAlloc))*sizeof(*(array).item));} while(0) +#define AFromC(array, c_array) AFromItems(array, c_array, sizeof(c_array)/sizeof(*(c_array))) +#define AFromLit(array, stringLiteral) do {AFromItems(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0) +#define AFromString(array, str) do {const char *__str = (str); AFromItems(array, __str, strlen(__str)+1); (array).size--;} while(0) +#define AFromItems(array, items, count) do {size_t __count = (count); AResize(array, __count); memcpy((array).item, items, __count*sizeof(*(array).item));} while(0) +#define AAppend(array, newItem...) do {AResize(array, (array).size+1); (array).item[(array).size-1] = (newItem);} while(0) +#define AAppendString(array, str) do {const char *__str = (str); AAppendItems(array, __str, strlen(__str)+1); (array).size--;} while(0) +#define AAppendLit(array, stringLiteral) do {AAppendItems(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0) +#define AAppendItems(array, items, count) do {size_t __count = (count); AResize(array, (array).size+__count); memcpy((array).item+(array).size-__count, items, __count*sizeof(*(array).item));} while(0) +#define APrepend(array, newItem...) do {AResize(array, (array).size+1); memmove((array).item+1, (array).item, ((array).size-1)*sizeof(*(array).item)); *(array).item = (newItem);} while(0) +#define APrependItems(array, items, count) do {AResize(array, (array).size+(count)); memmove((array).item+(count), (array).item, ((array).size-(count))*sizeof(*(array).item)); memcpy((array).item, items, (count)*sizeof(*(array).item)); } while(0) +#define APush(array, value...) AAppend(array, value) +#define APop(array) ((array).item[--(array).size]) + +#if HAVE_STATEMENT_EXPR==1 +#define AMakeRoom(array, room) ({size_t newAlloc = (array).size+(room); if ((array).allocSize +#include +#include "array/array.h" + +#define countof(array...) (sizeof(array)/sizeof(*(array))) +#include "lotsOfNumbers.h" + +int main(void) { + Array(long) array = NewArray(); + size_t i; + + plan_tests(3); + + { + for (i=0; i= array.size); + ok1(!memcmp(array.item, lotsOfNumbers, sizeof(lotsOfNumbers))); + } + AFree(array); + AInit(array); + + return 0; +} -- 2.39.2