Commit 6ba4abeb authored by Joey Adams's avatar Joey Adams Committed by Rusty Russell

darray: Renamed array module to darray and made several improvements.

 * Removed talloc support.
 * Added a synopsis and rearranged macro definitions.
 * Switched allocation strategy from increments of 64 to powers of 2.
 * Replaced array_for and array_rof with the more useful
   and portable macros array_foreach and array_foreach_reverse.
 * Added typedefs array_* for common types.
parent 460f62ce
#include "array.h"
#include <stdarg.h>
int array_alias_helper(const void *a, const void *b) {
(void)a, (void)b;
return 0;
}
//grows the allocated size to accommodate the size
void array_resize_helper(array_char *a, size_t itemSize) {
a->alloc = (a->size+63)&~63;
#ifndef ARRAY_USE_TALLOC
a->item = realloc(a->item, a->alloc*itemSize);
#else
a->item = talloc_realloc_size(NULL, a->item, a->alloc*itemSize +1);
#endif
}
void array_resize0_helper(array_char *a, size_t itemSize, size_t newSize) {
size_t oldSize = a->size;
a->size = newSize;
if (newSize > oldSize) {
if (newSize > a->alloc)
array_resize_helper(a, itemSize);
memset(a->item + oldSize*itemSize, 0, (newSize-oldSize)*itemSize);
}
}
void array_insert_items_helper(array_char *a, size_t itemSize, size_t pos, const void *items, size_t count, size_t tailSize) {
size_t oldSize = a->size;
size_t newSize = (a->size += count+tailSize);
if (newSize > a->alloc)
array_resize_helper(a, itemSize);
{
char *target = a->item + pos*itemSize;
count *= itemSize;
memmove(target+count, target, (oldSize-pos)*itemSize);
memcpy(target, items, count);
}
}
/*
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
#define ARRAY_USE_TALLOC
#include <stdlib.h>
#include <string.h>
#include "config.h"
#ifdef ARRAY_USE_TALLOC
#include <ccan/talloc/talloc.h>
#endif
//Use the array_alias macro to indicate that a pointer has changed but strict aliasing rules are too stupid to know it
#if HAVE_ATTRIBUTE_MAY_ALIAS
#define array_alias(ptr) /* nothing */
#define array(type) struct {type *item; size_t size; size_t alloc;} __attribute__((__may_alias__))
#else
#define array_alias(ptr) qsort(ptr, 0, 1, array_alias_helper) //hack
#define array(type) struct {type *item; size_t size; size_t alloc;}
#endif
//We call allocator functions directly
#ifndef ARRAY_USE_TALLOC
#define array_new() {0,0,0}
#define array_init(array) do {(array).item=0; (array).size=0; (array).alloc=0;} while(0)
#define array_realloc(array, newAlloc) do {(array).item = realloc((array).item, ((array).alloc = (newAlloc))*sizeof(*(array).item));} while(0)
#define array_free(array) do {free((array).item);} while(0)
#else
//note: the allocations are given an extra byte to prevent free (and thus loss of ctx) on realloc to size 0
#define array_new(ctx) {talloc_size(ctx,1), 0,0}
#define array_init(array, ctx) do {(array).item=talloc_size(ctx,1); (array).size=0; (array).alloc=0;} while(0)
#define array_realloc(array, newAlloc) do {(array).item = talloc_realloc_size(NULL, (array).item, ((array).alloc = (newAlloc))*sizeof(*(array).item) +1);} while(0)
#define array_free(array) do {talloc_free((array).item);} while(0)
#endif
//We call helper functions
#define array_resize(array, newSize) do {(array).size = (newSize); if ((array).size > (array).alloc) { array_resize_helper((array_char*)&(array), sizeof(*(array).item)); array_alias(&(array));}} while(0)
#define array_resize0(array, newSize) do {array_resize0_helper((array_char*)&(array), sizeof(*(array).item), newSize);} while(0)
#define array_prepend_lit(array, stringLiteral) do {array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, stringLiteral, sizeof(stringLiteral)-1, 1); array_alias(&(array)); (array).item[--(array).size] = 0;} while(0)
#define array_prepend_string(array, str) do {const char *__str = (str); size_t __len = strlen(__str); array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, __str, __len, 1); array_alias(&(array)); (array).item[--(array).size] = 0;} while(0)
#define array_prepend_items(array, items, count) do {array_insert_items_helper((array_char*)&(array), sizeof(*(array).item), 0, items, count, 0); array_alias(&(array));} while(0)
//We call other array_* macros
#define array_from_c(array, c_array) array_from_items(array, c_array, sizeof(c_array)/sizeof(*(c_array)))
#define array_from_lit(array, stringLiteral) do {array_from_items(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0)
#define array_from_string(array, str) do {const char *__str = (str); array_from_items(array, __str, strlen(__str)+1); (array).size--;} while(0)
#define array_from_items(array, items, count) do {size_t __count = (count); array_resize(array, __count); memcpy((array).item, items, __count*sizeof(*(array).item));} while(0)
#define array_append(array, ...) do {array_resize(array, (array).size+1); (array).item[(array).size-1] = (__VA_ARGS__);} while(0)
#define array_append_string(array, str) do {const char *__str = (str); array_append_items(array, __str, strlen(__str)+1); (array).size--;} while(0)
#define array_append_lit(array, stringLiteral) do {array_append_items(array, stringLiteral, sizeof(stringLiteral)); (array).size--;} while(0)
#define array_append_items(array, items, count) do {size_t __count = (count); array_resize(array, (array).size+__count); memcpy((array).item+(array).size-__count, items, __count*sizeof(*(array).item));} while(0)
#define array_prepend(array, ...) do {array_resize(array, (array).size+1); memmove((array).item+1, (array).item, ((array).size-1)*sizeof(*(array).item)); *(array).item = (__VA_ARGS__);} while(0)
#define array_push(array, ...) array_append(array, __VA_ARGS__)
#define array_pop_check(array) ((array).size ? array_pop(array) : NULL)
#define array_growalloc(array, newAlloc) do {size_t __newAlloc=(newAlloc); if (__newAlloc > (array).alloc) array_realloc(array, (__newAlloc+63)&~63); } while(0)
#if HAVE_STATEMENT_EXPR==1
#define array_make_room(array, room) ({size_t newAlloc = (array).size+(room); if ((array).alloc<newAlloc) array_realloc(array, newAlloc); (array).item+(array).size; })
#endif
//We do just fine by ourselves
#define array_pop(array) ((array).item[--(array).size])
#define array_for_t(var, array, type, ...) do {type *var=(void*)(array).item; size_t _r=(array).size, _i=0; for (;_r--;_i++, var++) { __VA_ARGS__ ;} } while(0)
#define array_appends_t(array, type, ...) do {type __src[] = {__VA_ARGS__}; array_append_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0)
#if HAVE_TYPEOF==1
#define array_appends(array, ...) array_appends_t(array, typeof((*(array).item)), __VA_ARGS__)
#define array_prepends(array, ...) do {typeof((*(array).item)) __src[] = {__VA_ARGS__}; array_prepend_items(array, __src, sizeof(__src)/sizeof(*__src));} while(0)
#define array_for(var, array, ...) array_for_t(var, array, typeof(*(array).item), __VA_ARGS__)
#define array_rof(var, array, ...) do {typeof(*(array).item) *var=(void*)(array).item; size_t _i=(array).size, _r=0; var += _i; for (;_i--;_r++) { var--; __VA_ARGS__ ;} } while(0)
#endif
typedef array(char) array_char;
void array_resize_helper(array_char *a, size_t itemSize);
void array_resize0_helper(array_char *a, size_t itemSize, size_t newSize);
void array_insert_items_helper(array_char *a, size_t itemSize, size_t pos, const void *items, size_t count, size_t tailSize);
//Note: there is no array_insert_items yet, but it wouldn't be too hard to add.
int array_alias_helper(const void *a, const void *b);
#endif
/*
array_growalloc(array, newAlloc) sees if the array can currently hold newAlloc items;
if not, it increases the alloc to satisfy this requirement, allocating slack
space to avoid having to reallocate for every size increment.
array_from_string(array, str) copies a string to an array_char.
array_push(array, item) pushes an item to the end of the array.
array_pop_nocheck(array) pops it back out. Be sure there is at least one item in the array before calling.
array_pop(array) does the same as array_pop_nocheck, but returns NULL if there are no more items left in the array.
array_make_room(array, room) ensures there's 'room' elements of space after the end of the array, and it returns a pointer to this space.
Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
The following require HAVE_TYPEOF==1 :
array_appends(array, item0, item1...) appends a collection of comma-delimited items to the array.
array_prepends(array, item0, item1...) prepends a collection of comma-delimited items to the array.
array_for(var, array, commands...) iterates forward through the items in the array using var. var is a pointer to an item.
array_rof(var, array, commands...) iterates backward through the items in the array using var. var is a pointer to an item.
Examples:
array(int) array;
array_appends(array, 0,1,2,3,4);
array_appends(array, -5,-4,-3,-2,-1);
array_for(i, array, printf("%d ", *i));
printf("\n");
array_free(array);
array(struct {int n,d;}) fractions;
array_appends(fractions, {3,4}, {3,5}, {2,1});
array_for(i, fractions, printf("%d/%d\n", i->n, i->d));
array_free(fractions);
*/
/*
Direct tests:
array_push
array_prepend
array_from_c
array_for
array_rof
array_from_lit
array_append_lit
array_prepend_lit
array_append_string
array_prepend_string
array_from_string
array_resize0
array_pop_nocheck
array_realloc
array_growalloc
array_make_room
array_pop
array_appends
array_prepends
Indirect tests:
array_append <- array_push
array_resize <- array_append
array_from_items <- array_from_c, array_from_lit, array_from_string
array_append_items <- array_append_string, array_append_lit
array_prepend_items <- array_prepends
Untested:
*/
s/NewArray/array_new/g
s/ASetAllocSize/array_realloc/g
s/ArrayChar/array_char/g
s/Array/array/g
s/ARof/array_rof/g
s/AResize0_helper/array_resize0_helper/g
s/AResize_helper/array_resize_helper/g
s/AResize/array_resize/g
s/APush/array_push/g
s/APrependString/array_prepend_string/g
s/APrepends/array_prepends/g
s/APrependLit/array_prepend_lit/g
s/APrependItems/array_prepend_items/g
s/APrepend/array_prepend/g
s/APopS/array_pop/g
s/APop/array_pop_nocheck/g
s/AMakeRoom/array_make_room/g
s/allocSize/alloc/g
s/AInsertItems_helper/array_insert_items_helper/g
s/AInsertItems/array_insert_items/g
s/AInit/array_init/g
s/AGrowAllocSize/array_growalloc/g
s/AFromString/array_from_string/g
s/AFromLit/array_from_lit/g
s/AFromItems/array_from_items/g
s/AFromC/array_from_c/g
s/AFree/array_free/g
s/AFor/array_for/g
s/AAppendString/array_append_string/g
s/AAppends/array_appends/g
s/AAppendLit/array_append_lit/g
s/AAppendItems/array_append_items/g
s/AAppend/array_append/g
s/Aalias_helper/array_alias_helper/g
s/Aalias/array_alias/g
#include <string.h>
#include "config.h"
#include "ccan/array/array.h"
#include "ccan/darray/darray.h"
/**
* array - A collection of macros for generic dynamic array management.
* darray - Generic resizable arrays
*
* darray is a set of macros for managing dynamically-allocated arrays.
* It removes the tedium of managing realloc'd arrays with pointer, size, and
* allocated size.
*
* 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. It uses the
* talloc library to allocate the memory.
*
* NOTE: The API should be fairly stable now, but do expect small changes
* over time.
*
* Example:
* #include <ccan/array/array.h>
* #include <ccan/darray/darray.h>
* #include <stdio.h>
*
* int main(void) {
* array(int) numbers = array_new(NULL);
* darray(int) numbers = darray_new();
* char buffer[32];
* int add;
*
* for (;;) {
* array_for(i, numbers, printf("%d ", *i));
* if (numbers.size) puts("");
* int *i;
* darray_foreach(i, numbers)
* printf("%d ", *i);
* if (darray_size(numbers) > 0)
* puts("");
*
* printf("array> ");
* printf("darray> ");
* fgets(buffer, sizeof(buffer), stdin);
* if (*buffer==0 || *buffer=='\n')
* if (*buffer == '\0' || *buffer == '\n')
* break;
* add = atoi(buffer);
*
* array_append(numbers, add);
* darray_append(numbers, atoi(buffer));
* }
*
* array_free(numbers);
* darray_free(numbers);
*
* return 0;
* }
*
* Author: Joey Adams
* Version: 0.1.1
* Version: 0.2
* License: BSD
*/
int main(int argc, char *argv[])
......@@ -50,13 +47,10 @@ int main(int argc, char *argv[])
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0)
#ifndef ARRAY_USE_TALLOC
if (strcmp(argv[1], "depends") == 0) {
/* Nothing. */
#else
printf("ccan/talloc\n");
#endif
return 0;
}
return 1;
}
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
static int testarray_from_lit(void);
static int testarray_append_lit(void);
static int testarray_prepend_lit(void);
static int testdarray_from_lit(void);
static int testdarray_append_lit(void);
static int testdarray_prepend_lit(void);
static void testLits(void) {
testing(array_from_lit);
ok1(testarray_from_lit());
testing(darray_from_lit);
ok1(testdarray_from_lit());
testing(testarray_append_lit);
ok1(testarray_append_lit());
testing(testdarray_append_lit);
ok1(testdarray_append_lit());
testing(testarray_prepend_lit);
ok1(testarray_prepend_lit());
testing(testdarray_prepend_lit);
ok1(testdarray_prepend_lit());
}
static int testarray_from_lit(void) {
#ifndef ARRAY_USE_TALLOC
array_char a = array_new();
#else
array_char a = array_new(NULL);
#endif
static int testdarray_from_lit(void) {
darray_char a = darray_new();
size_t testsPassed = 0;
size_t len = 0;
@forEachRandomString
/* Test @i */
array_from_lit(a, @str);
darray_from_lit(a, @str);
len = strlen(@str);
if (len != sizeof(@str)-1)
goto end;
......@@ -34,17 +30,13 @@ static int testarray_from_lit(void) {
goto end;
if (strcmp(a.item, @str))
goto end;
array_free(a);
#ifndef ARRAY_USE_TALLOC
array_init(a);
#else
array_init(a, NULL);
#endif
darray_free(a);
darray_init(a);
testsPassed++;
@end
end:
array_free(a);
darray_free(a);
return testsPassed == @amount;
}
......@@ -53,14 +45,10 @@ typedef struct {
size_t size;
} testLits_string;
static int testarray_append_lit(void) {
#ifndef ARRAY_USE_TALLOC
array_char a = array_new();
array(testLits_string) strings = array_new();
#else
array_char a = array_new(NULL);
array(testLits_string) strings = array_new(NULL);
#endif
static int testdarray_append_lit(void) {
darray_char a = darray_new();
darray(testLits_string) strings = darray_new();
testLits_string *i;
size_t testsPassed = 0;
size_t oldSize;
testLits_string append;
......@@ -70,7 +58,7 @@ static int testarray_append_lit(void) {
/* Test @i */
append.size = sizeof(@str)-1;
oldSize = a.size;
array_append_lit(a, @str);
darray_append_lit(a, @str);
if (a.size != oldSize+append.size)
goto end;
if (a.size > a.alloc)
......@@ -80,19 +68,21 @@ static int testarray_append_lit(void) {
if (memcmp(a.item+oldSize, @str, a.size-oldSize))
goto end;
append.item = strdup(@str);
array_append(strings, append);
darray_append(strings, append);
testsPassed++;
@end
if (strings.size != @amount)
goto end;
array_for(i, strings,
darray_foreach(i, strings) {
if (a.size-offs < i->size)
goto end;
if (memcmp(a.item+offs, i->item, i->size))
goto end;
offs += i->size;
);
};
if (offs != a.size)
goto end;
if (a.item[offs])
......@@ -100,20 +90,17 @@ static int testarray_append_lit(void) {
testsPassed++;
end:
array_free(a);
array_for(i, strings, free(i->item));
array_free(strings);
darray_free(a);
darray_foreach(i, strings)
free(i->item);
darray_free(strings);
return testsPassed == @amount+1;
}
static int testarray_prepend_lit(void) {
#ifndef ARRAY_USE_TALLOC
array_char a = array_new();
array(testLits_string) strings = array_new();
#else
array_char a = array_new(NULL);
array(testLits_string) strings = array_new(NULL);
#endif
static int testdarray_prepend_lit(void) {
darray_char a = darray_new();
darray(testLits_string) strings = darray_new();
testLits_string *i;
size_t testsPassed = 0;
size_t oldSize;
testLits_string append;
......@@ -123,7 +110,7 @@ static int testarray_prepend_lit(void) {
/* Test @i */
append.size = sizeof(@str)-1;
oldSize = a.size;
array_prepend_lit(a, @str);
darray_prepend_lit(a, @str);
if (a.size != oldSize+append.size)
goto end;
if (a.size > a.alloc)
......@@ -133,7 +120,7 @@ static int testarray_prepend_lit(void) {
if (memcmp(a.item, @str, a.size-oldSize))
goto end;
append.item = strdup(@str);
array_append(strings, append);
darray_append(strings, append);
testsPassed++;
@end
......@@ -142,20 +129,21 @@ static int testarray_prepend_lit(void) {
goto end;
if (strings.size != @amount)
goto end;
array_for(i, strings,
darray_foreach(i, strings) {
if (offs < i->size)
goto end;
offs -= i->size;
if (memcmp(a.item+offs, i->item, i->size))
goto end;
);
};
if (offs)
goto end;
testsPassed++;
end:
array_free(a);
array_for(i, strings, free(i->item));
array_free(strings);
darray_free(a);
darray_foreach(i, strings)
free(i->item);
darray_free(strings);
return testsPassed == @amount+1;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment