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
*
* 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.
* 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.
*
* 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;
}
/*
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_DARRAY_H
#define CCAN_DARRAY_H
#include <stdlib.h>
#include <string.h>
#include "config.h"
/*
* SYNOPSIS
*
* Life cycle of a darray (dynamically-allocated array):
*
* darray(int) a = darray_new();
* darray_free(a);
*
* struct {darray(int) a;} foo;
* darray_init(foo.a);
* darray_free(foo.a);
*
* Typedefs for darrays of common types:
*
* darray_char, darray_schar, darray_uchar
* darray_short, darray_int, darray_long
* darray_ushort, darray_uint, darray_ulong
*
* Access:
*
* T darray_item(darray(T) arr, size_t index);
* size_t darray_size(darray(T) arr);
* size_t darray_alloc(darray(T) arr);
* bool darray_empty(darray(T) arr);
*
* Insertion (single item):
*
* void darray_append(darray(T) arr, T item);
* void darray_prepend(darray(T) arr, T item);
* void darray_push(darray(T) arr, T item); // same as darray_append
*
* Insertion (multiple items):
*
* void darray_append_items(darray(T) arr, T *items, size_t count);
* void darray_prepend_items(darray(T) arr, T *items, size_t count);
*
* void darray_appends(darray(T) arr, [T item, [...]]);
* void darray_prepends(darray(T) arr, [T item, [...]]);
*
* // Same functionality as above, but does not require typeof.
* void darray_appends_t(darray(T) arr, #T, [T item, [...]]);
* void darray_prepends_t(darray(T) arr, #T, [T item, [...]]);
*
* Removal:
*
* T darray_pop(darray(T) arr | darray_size(arr) != 0);
* T* darray_pop_check(darray(T*) arr);
*
* Replacement:
*
* void darray_from_items(darray(T) arr, T *items, size_t count);
* void darray_from_c(darray(T) arr, T c_array[N]);
*
* String buffer:
*
* void darray_append_string(darray(char) arr, const char *str);
* void darray_append_lit(darray(char) arr, char stringLiteral[N+1]);
*
* void darray_prepend_string(darray(char) arr, const char *str);
* void darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]);
*
* void darray_from_string(darray(T) arr, const char *str);
* void darray_from_lit(darray(char) arr, char stringLiteral[N+1]);
*
* Size management:
*
* void darray_resize(darray(T) arr, size_t newSize);
* void darray_resize0(darray(T) arr, size_t newSize);
*
* void darray_realloc(darray(T) arr, size_t newAlloc);
* void darray_growalloc(darray(T) arr, size_t newAlloc);
*
* void darray_make_room(darray(T) arr, size_t room);
*
* Traversal:
*
* darray_foreach(T *&i, darray(T) arr) {...}
* darray_foreach_reverse(T *&i, darray(T) arr) {...}
*
* Except for darray_foreach and darray_foreach_reverse,
* all macros evaluate their non-darray arguments only once.
*/
/*** Life cycle ***/
#define darray(type) struct {type *item; size_t size; size_t alloc;}
#define darray_new() {0,0,0}
#define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0)
#define darray_free(arr) do {free((arr).item);} while(0)
/*
* Typedefs for darrays of common types. These are useful
* when you want to pass a pointer to an darray(T) around.
*
* The following will produce an incompatible pointer warning:
*
* void foo(darray(int) *arr);
* darray(int) arr = darray_new();
* foo(&arr);
*
* The workaround:
*
* void foo(darray_int *arr);
* darray_int arr = darray_new();
* foo(&arr);
*/
typedef darray(char) darray_char;
typedef darray(signed char) darray_schar;
typedef darray(unsigned char) darray_uchar;
typedef darray(short) darray_short;
typedef darray(int) darray_int;
typedef darray(long) darray_long;
typedef darray(unsigned short) darray_ushort;
typedef darray(unsigned int) darray_uint;
typedef darray(unsigned long) darray_ulong;
/*** Access ***/
#define darray_item(arr, i) ((arr).item[i])
#define darray_size(arr) ((arr).size)
#define darray_alloc(arr) ((arr).alloc)
#define darray_empty(arr) ((arr).size == 0)
/*** Insertion (single item) ***/
#define darray_append(arr, ...) do { \
darray_resize(arr, (arr).size+1); \
(arr).item[(arr).size-1] = (__VA_ARGS__); \
} while(0)
#define darray_prepend(arr, ...) do { \
darray_resize(arr, (arr).size+1); \
memmove((arr).item+1, (arr).item, ((arr).size-1)*sizeof(*(arr).item)); \
(arr).item[0] = (__VA_ARGS__); \
} while(0)
#define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
/*** Insertion (multiple items) ***/
#define darray_append_items(arr, items, count) do { \
size_t __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __oldSize + __count); \
memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
} while(0)
#define darray_prepend_items(arr, items, count) do { \
size_t __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __count + __oldSize); \
memmove((arr).item + __count, (arr).item, __oldSize * sizeof(*(arr).item)); \
memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
} while(0)
#define darray_append_items_nullterminate(arr, items, count) do { \
size_t __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __oldSize + __count + 1); \
memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
(arr).item[--(arr).size] = 0; \
} while(0)
#define darray_prepend_items_nullterminate(arr, items, count) do { \
size_t __count = (count), __oldSize = (arr).size; \
darray_resize(arr, __count + __oldSize + 1); \
memmove((arr).item + __count, (arr).item, __oldSize * sizeof(*(arr).item)); \
memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
(arr).item[--(arr).size] = 0; \
} while(0)
#if HAVE_TYPEOF
#define darray_appends(arr, ...) darray_appends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
#define darray_prepends(arr, ...) darray_prepends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
#endif
#define darray_appends_t(arr, type, ...) do { \
type __src[] = {__VA_ARGS__}; \
darray_append_items(arr, __src, sizeof(__src)/sizeof(*__src)); \
} while(0)
#define darray_prepends_t(arr, type, ...) do { \
type __src[] = {__VA_ARGS__}; \
darray_prepend_items(arr, __src, sizeof(__src)/sizeof(*__src)); \
} while(0)
/*** Removal ***/
/* Warning: Do not call darray_pop on an empty darray. */
#define darray_pop(arr) ((arr).item[--(arr).size])
#define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
/*** Replacement ***/
#define darray_from_items(arr, items, count) do {size_t __count = (count); darray_resize(arr, __count); memcpy((arr).item, items, __count*sizeof(*(arr).item));} while(0)
#define darray_from_c(arr, c_array) darray_from_items(arr, c_array, sizeof(c_array)/sizeof(*(c_array)))
/*** String buffer ***/
#define darray_append_string(arr, str) do {const char *__str = (str); darray_append_items(arr, __str, strlen(__str)+1); (arr).size--;} while(0)
#define darray_append_lit(arr, stringLiteral) do {darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); (arr).size--;} while(0)
#define darray_prepend_string(arr, str) do { \
const char *__str = (str); \
darray_prepend_items_nullterminate(arr, __str, strlen(__str)); \
} while(0)
#define darray_prepend_lit(arr, stringLiteral) \
darray_prepend_items_nullterminate(arr, stringLiteral, sizeof(stringLiteral) - 1)
#define darray_from_string(arr, str) do {const char *__str = (str); darray_from_items(arr, __str, strlen(__str)+1); (arr).size--;} while(0)
#define darray_from_lit(arr, stringLiteral) do {darray_from_items(arr, stringLiteral, sizeof(stringLiteral)); (arr).size--;} while(0)
/*** Size management ***/
#define darray_resize(arr, newSize) darray_growalloc(arr, (arr).size = (newSize))
#define darray_resize0(arr, newSize) do { \
size_t __oldSize = (arr).size, __newSize = (newSize); \
(arr).size = __newSize; \
if (__newSize > __oldSize) { \
darray_growalloc(arr, __newSize); \
memset(&(arr).item[__oldSize], 0, (__newSize - __oldSize) * sizeof(*(arr).item)); \
} \
} while(0)
#define darray_realloc(arr, newAlloc) do { \
(arr).item = realloc((arr).item, ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
} while(0)
#define darray_growalloc(arr, need) do { \
size_t __need = (need); \
if (__need > (arr).alloc) \
darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
} while(0)
#if HAVE_STATEMENT_EXPR==1
#define darray_make_room(arr, room) ({size_t newAlloc = (arr).size+(room); if ((arr).alloc<newAlloc) darray_realloc(arr, newAlloc); (arr).item+(arr).size; })
#endif
static inline size_t darray_next_alloc(size_t alloc, size_t need)
{
if (alloc == 0)
alloc = 1;
while (alloc < need)
alloc *= 2;
return alloc;
}
/*** Traversal ***/
/*
* darray_foreach(T *&i, darray(T) arr) {...}
*
* Traverse a darray. `i` must be declared in advance as a pointer to an item.
*/
#define darray_foreach(i, arr) \
for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
/*
* darray_foreach_reverse(T *&i, darray(T) arr) {...}
*
* Like darray_foreach, but traverse in reverse order.
*/
#define darray_foreach_reverse(i, arr) \
for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
#endif /* CCAN_DARRAY_H */
/*
darray_growalloc(arr, newAlloc) sees if the darray 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.
darray_from_string(arr, str) copies a string to an darray_char.
darray_push(arr, item) pushes an item to the end of the darray.
darray_pop(arr) pops it back out. Be sure there is at least one item in the darray before calling.
darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
darray_make_room(arr, room) ensures there's 'room' elements of space after the end of the darray, 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 :
darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
Examples:
darray(int) arr;
int *i;
darray_appends(arr, 0,1,2,3,4);
darray_appends(arr, -5,-4,-3,-2,-1);
darray_foreach(i, arr)
printf("%d ", *i);
printf("\n");
darray_free(arr);
typedef struct {int n,d;} Fraction;
darray(Fraction) fractions;
Fraction *i;
darray_appends(fractions, {3,4}, {3,5}, {2,1});
darray_foreach(i, fractions)
printf("%d/%d\n", i->n, i->d);
darray_free(fractions);
*/
#include <stdio.h>
#include <ccan/tap/tap.h>
#include <ccan/array/array.h>
#include <ccan/array/array.c>
#include <ccan/darray/darray.h>
#include <stdio.h>
#define countof(array...) (sizeof(array)/sizeof(*(array)))
#include "lotsOfNumbers.h"
#include "lotsOfStrings.h"
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr)))
struct {
char *stringsF, *stringsB;
//items of lotsOfStrings glued together
......@@ -30,15 +30,9 @@ static void memtile(void *dest, size_t destWidth, const void *src, size_t srcWid
#include "testLits.h"
int main(void) {
#ifndef ARRAY_USE_TALLOC
array(long) arr = array_new();
array_char str = array_new();
#define reset(array) do {array_free(array); array_init(array);} while(0)
#else
array(long) arr = array_new(NULL);
array_char str = array_new(NULL);
#define reset(array) do {array_free(array); array_init(array, NULL);} while(0)
#endif
darray(long) arr = darray_new();
darray_char str = darray_new();
#define reset(arr) do {darray_free(arr); darray_init(arr);} while(0)
size_t i;
trace("Generating amalgams (internal)");
......@@ -48,82 +42,71 @@ int main(void) {
testLits();
testing(array_push);
testing(darray_push);
{
for (i=0; i<countof(lotsOfNumbers); i++)
array_push(arr, lotsOfNumbers[i]);
ok1(arr.size == countof(lotsOfNumbers));
ok1(arr.alloc >= arr.size);
for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++)
darray_push(arr, lotsOfNumbers[i]);
ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers));
ok1(darray_alloc(arr) >= darray_size(arr));
ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)));
}
reset(arr);
testing(array_prepend, array_pop);
testing(darray_prepend, darray_pop);
{
for (i=countof(lotsOfNumbers); i;)
array_prepend(arr, lotsOfNumbers[--i]);
ok1(arr.size == countof(lotsOfNumbers));
ok1(arr.alloc >= arr.size);
for (i = ARRAY_SIZE(lotsOfNumbers); i;)
darray_prepend(arr, lotsOfNumbers[--i]);
ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers));
ok1(darray_alloc(arr) >= darray_size(arr));
ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)));
for (i=countof(lotsOfNumbers); i;) {
if (array_pop(arr) != (long)lotsOfNumbers[--i]) {
for (i = ARRAY_SIZE(lotsOfNumbers); i;) {
if (darray_pop(arr) != (long)lotsOfNumbers[--i]) {
i++;
break;
}
}
ok1(i==0);
ok1(arr.size == 0);
ok1(darray_size(arr) == 0);
}
reset(arr);
testing(array_from_c, array_for, array_rof);
testing(darray_from_c, darray_foreach, darray_foreach_reverse);
{
size_t i_correct, r_correct;
long *i;
size_t j;
array_from_c(arr, lotsOfNumbers);
ok1(arr.size == countof(lotsOfNumbers));
ok1(arr.alloc >= arr.size);
ok1(!memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)));
darray_from_c(arr, lotsOfNumbers);
ok1(darray_size(arr) == ARRAY_SIZE(lotsOfNumbers));
ok1(darray_alloc(arr) >= darray_size(arr));
ok1(memcmp(arr.item, lotsOfNumbers, sizeof(lotsOfNumbers)) == 0);
i_correct = 0;
r_correct = countof(lotsOfNumbers)-1;
array_for(i, arr,
if (i_correct != _i)
break;
if (r_correct != _r)
j = 0;
darray_foreach(i, arr) {
if (i - arr.item != j)
break;
if (i != arr.item+_i)
if (*i != (long)lotsOfNumbers[j])
break;
if (*i != (long)lotsOfNumbers[_i])
break;
i_correct++;
r_correct--;
);
ok1(i_correct == countof(lotsOfNumbers));
i_correct = countof(lotsOfNumbers)-1;
r_correct = 0;
array_rof(i, arr,
if (i_correct != _i)
break;
if (r_correct != _r)
break;
if (i != arr.item+_i)
j++;
};
ok1(j == ARRAY_SIZE(lotsOfNumbers));
j = 0;
darray_foreach_reverse(i, arr) {
if (i - arr.item != darray_size(arr)-j-1)
break;
if (*i != (long)lotsOfNumbers[_i])
if (*i != (long)lotsOfNumbers[darray_size(arr)-j-1])
break;
i_correct--;
r_correct++;
);
ok1(r_correct == countof(lotsOfNumbers));
j++;
};
ok1(j == ARRAY_SIZE(lotsOfNumbers));
}
reset(arr);
testing(array_append_string);
testing(darray_append_string);
{
for (i=0; i<countof(lotsOfStrings); i++)
array_append_string(str, lotsOfStrings[i]);
for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++)
darray_append_string(str, lotsOfStrings[i]);
ok1(str.size == amalgams.stringsSize);
ok1(str.alloc > str.size);
ok1(str.item[str.size] == 0);
......@@ -131,10 +114,10 @@ int main(void) {
}
reset(str);
testing(array_prepend_string);
testing(darray_prepend_string);
{
for (i=0; i<countof(lotsOfStrings); i++)
array_prepend_string(str, lotsOfStrings[i]);
for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++)
darray_prepend_string(str, lotsOfStrings[i]);
ok1(str.size == amalgams.stringsSize);
ok1(str.alloc > str.size);
ok1(str.item[str.size] == 0);
......@@ -142,10 +125,10 @@ int main(void) {
}
reset(str);
testing(array_from_string);
testing(darray_from_string);
{
for (i=0; i<countof(lotsOfStrings); i++) {
array_from_string(str, lotsOfStrings[i]);
for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) {
darray_from_string(str, lotsOfStrings[i]);
if (str.size != strlen(lotsOfStrings[i]))
break;
if (str.alloc < strlen(lotsOfStrings[i])+1)
......@@ -153,88 +136,88 @@ int main(void) {
if (strcmp(str.item, lotsOfStrings[i]))
break;
}
ok1(i == countof(lotsOfStrings));
ok1(i == ARRAY_SIZE(lotsOfStrings));
}
reset(str);
testing(array_resize0);
testing(darray_resize0);
{
size_t prevSize=0, size;
for (i=0; i<countof(lotsOfNumbers); i++, prevSize=size) {
for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++, prevSize=size) {
size = lotsOfNumbers[i] & 0xFFFF;
array_resize0(arr, size);
if (arr.size != size)
darray_resize0(arr, size);
if (darray_size(arr) != size)
break;
if (arr.alloc < size)
if (darray_alloc(arr) < size)
break;
if (size>prevSize) {
if (!isZeros(arr.item+prevSize, (size-prevSize)*sizeof(*arr.item)))
break;
}
//fill the array with lotsOfNumbers garbage
memtile(arr.item, arr.size*sizeof(*arr.item), lotsOfNumbers, sizeof(lotsOfNumbers));
//fill the darray with lotsOfNumbers garbage
memtile(arr.item, darray_size(arr)*sizeof(*arr.item), lotsOfNumbers, sizeof(lotsOfNumbers));
}
ok1(i==countof(lotsOfNumbers));
ok1(i == ARRAY_SIZE(lotsOfNumbers));
}
reset(arr);
testing(array_realloc);
testing(darray_realloc);
{
size_t s,a;
for (i=0; i<countof(lotsOfNumbers); i++) {
for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++) {
arr.size = (s = lotsOfNumbers[i] >> 16);
//give size a nonsense value to make sure array_realloc doesn't care about it
//give size a nonsense value to make sure darray_realloc doesn't care about it
a = amalgams.stringsSize/sizeof(*arr.item)+2;
array_realloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1));
darray_realloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1));
if (a*sizeof(*arr.item) > amalgams.stringsSize)
break;
if (arr.alloc != a)
if (darray_alloc(arr) != a)
break;
if (arr.size != s)
if (darray_size(arr) != s)
break;
memtile(arr.item, a*sizeof(*arr.item), amalgams.stringsF, a*sizeof(*arr.item));
if (memcmp(arr.item, amalgams.stringsF, a*sizeof(*arr.item)))
break;
}
ok1(i==countof(lotsOfNumbers));
ok1(i == ARRAY_SIZE(lotsOfNumbers));
}
reset(arr);
testing(array_growalloc);
testing(darray_growalloc);
{
size_t prevA, s, a;
for (i=0; i<countof(lotsOfNumbers); i++) {
for (i=0; i < ARRAY_SIZE(lotsOfNumbers); i++) {
arr.size = (s = lotsOfNumbers[i] >> 16);
//give size a nonsense value to make sure array_growalloc doesn't care about it
//give size a nonsense value to make sure darray_growalloc doesn't care about it
a = amalgams.stringsSize/sizeof(*arr.item)+2;
prevA = arr.alloc;
array_growalloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1));
prevA = darray_alloc(arr);
darray_growalloc(arr, a = lotsOfNumbers[i] % ((amalgams.stringsSize/sizeof(*arr.item))+1));
if (a*sizeof(*arr.item) > amalgams.stringsSize)
break;
if (arr.alloc < a)
if (darray_alloc(arr) < a)
break;
if (arr.alloc < prevA)
if (darray_alloc(arr) < prevA)
break;
if (arr.size != s)
if (darray_size(arr) != s)
break;
memtile(arr.item, a*sizeof(*arr.item), amalgams.stringsF, a*sizeof(*arr.item));
if (memcmp(arr.item, amalgams.stringsF, a*sizeof(*arr.item)))
break;
//clear the array every now and then
//clear the darray every now and then
if (!(lotsOfNumbers[i] & 15)) {
reset(arr);
}
}
ok1(i==countof(lotsOfNumbers));
ok1(i == ARRAY_SIZE(lotsOfNumbers));
}
reset(arr);
testing(array_make_room);
testing(darray_make_room);
{
for (i=0; i<countof(lotsOfStrings); i++) {
char *dest = array_make_room(str, strlen(lotsOfStrings[i]));
for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++) {
char *dest = darray_make_room(str, strlen(lotsOfStrings[i]));
if (str.alloc < str.size+strlen(lotsOfStrings[i]))
break;
if (dest != str.item+str.size)
......@@ -243,54 +226,50 @@ int main(void) {
memcpy(dest, lotsOfStrings[i], strlen(lotsOfStrings[i]));
str.size += strlen(lotsOfStrings[i]);
}
ok1(i==countof(lotsOfStrings));
ok1(i == ARRAY_SIZE(lotsOfStrings));
ok1(str.size == amalgams.stringsSize);
array_append(str, 0);
darray_append(str, 0);
ok1(!strcmp(str.item, amalgams.stringsF));
}
reset(str);
testing(array_appends, array_prepends, array_pop_check);
testing(darray_appends, darray_prepends, darray_pop_check);
{
#ifndef ARRAY_USE_TALLOC
array(const char*) array = array_new();
#else
array(const char*) array = array_new(NULL);
#endif
darray(const char*) arr = darray_new();
const char *n[9] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight"};
array_appends(array, n[5], n[6], n[7], n[8]);
ok1(array.size==4 && array.alloc>=4);
array_prepends(array, n[0], n[1], n[2], n[3], n[4]);
ok1(array.size==9 && array.alloc>=9);
ok1(array.item[0]==n[0] &&
array.item[1]==n[1] &&
array.item[2]==n[2] &&
array.item[3]==n[3] &&
array.item[4]==n[4] &&
array.item[5]==n[5] &&
array.item[6]==n[6] &&
array.item[7]==n[7] &&
array.item[8]==n[8]);
ok1(array_pop_check(array)==n[8] &&
array_pop_check(array)==n[7] &&
array_pop_check(array)==n[6] &&
array_pop_check(array)==n[5] &&
array_pop_check(array)==n[4] &&
array_pop_check(array)==n[3] &&
array_pop_check(array)==n[2] &&
array_pop_check(array)==n[1] &&
array_pop_check(array)==n[0]);
ok1(array.size==0);
ok1(array_pop_check(array)==NULL && array_pop_check(array)==NULL && array_pop_check(array)==NULL);
array_free(array);
darray_appends(arr, n[5], n[6], n[7], n[8]);
ok1(darray_size(arr)==4 && darray_alloc(arr)>=4);
darray_prepends(arr, n[0], n[1], n[2], n[3], n[4]);
ok1(darray_size(arr)==9 && darray_alloc(arr)>=9);
ok1(arr.item[0]==n[0] &&
arr.item[1]==n[1] &&
arr.item[2]==n[2] &&
arr.item[3]==n[3] &&
arr.item[4]==n[4] &&
arr.item[5]==n[5] &&
arr.item[6]==n[6] &&
arr.item[7]==n[7] &&
arr.item[8]==n[8]);
ok1(darray_pop_check(arr)==n[8] &&
darray_pop_check(arr)==n[7] &&
darray_pop_check(arr)==n[6] &&
darray_pop_check(arr)==n[5] &&
darray_pop_check(arr)==n[4] &&
darray_pop_check(arr)==n[3] &&
darray_pop_check(arr)==n[2] &&
darray_pop_check(arr)==n[1] &&
darray_pop_check(arr)==n[0]);
ok1(darray_size(arr)==0);
ok1(darray_pop_check(arr)==NULL && darray_pop_check(arr)==NULL && darray_pop_check(arr)==NULL);
darray_free(arr);
}
trace("Freeing amalgams (internal)");
......@@ -305,14 +284,14 @@ static void generateAmalgams(void) {
const char *src;
char *p;
for (i=0; i<countof(lotsOfStrings); i++)
for (i=0; i < ARRAY_SIZE(lotsOfStrings); i++)
lotsOfStringsLen += strlen(lotsOfStrings[i]);
amalgams.stringsSize = lotsOfStringsLen;
amalgams.stringsF = malloc(lotsOfStringsLen+1);
amalgams.stringsB = malloc(lotsOfStringsLen+1);
for (i=0,p=amalgams.stringsF; i<countof(lotsOfStrings); i++) {
for (i=0,p=amalgams.stringsF; i < ARRAY_SIZE(lotsOfStrings); i++) {
size_t len = strlen(src=lotsOfStrings[i]);
memcpy(p, src, len);
p += len;
......@@ -321,7 +300,7 @@ static void generateAmalgams(void) {
ok1(p-amalgams.stringsF == (long)lotsOfStringsLen);
ok1(strlen(amalgams.stringsF) == lotsOfStringsLen);
for (i=countof(lotsOfStrings),p=amalgams.stringsB; i--;) {
for (i = ARRAY_SIZE(lotsOfStrings), p = amalgams.stringsB; i--;) {
size_t len = strlen(src=lotsOfStrings[i]);
memcpy(p, src, len);
p += len;
......
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