X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Farray%2Farray.h;fp=ccan%2Farray%2Farray.h;h=0000000000000000000000000000000000000000;hp=007c51b8f97e1095c2046245cbbc536687a7d282;hb=6ba4abebd2fdaba81a0dad774de3cd8fc99304dc;hpb=460f62ce63a6ef4aaa0f8840474c039f7e73399f diff --git a/ccan/array/array.h b/ccan/array/array.h deleted file mode 100644 index 007c51b8..00000000 --- a/ccan/array/array.h +++ /dev/null @@ -1,194 +0,0 @@ -/* - 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 -#include -#include "config.h" - -#ifdef ARRAY_USE_TALLOC -#include -#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).allocn, 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: - -*/