Path: blob/main/contrib/elftoolchain/common/utarray.h
39536 views
/*1Copyright (c) 2008-2013, Troy D. Hanson http://uthash.sourceforge.net2All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:67* Redistributions of source code must retain the above copyright8notice, this list of conditions and the following disclaimer.910THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS11IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED12TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A13PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER14OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,15EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,16PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR17PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF18LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING19NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS20SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.21*/2223/* $Id: utarray.h 2694 2012-11-24 17:11:58Z kaiwang27 $ */2425/* a dynamic array implementation using macros26* see http://uthash.sourceforge.net/utarray27*/28#ifndef UTARRAY_H29#define UTARRAY_H3031#define UTARRAY_VERSION 1.9.73233#ifdef __GNUC__34#define _UNUSED_ __attribute__ ((__unused__))35#else36#define _UNUSED_37#endif3839#include <stddef.h> /* size_t */40#include <string.h> /* memset, etc */41#include <stdlib.h> /* exit */4243#ifndef oom44#define oom() exit(-1)45#endif4647typedef void (ctor_f)(void *dst, const void *src);48typedef void (dtor_f)(void *elt);49typedef void (init_f)(void *elt);50typedef struct {51size_t sz;52init_f *init;53ctor_f *copy;54dtor_f *dtor;55} UT_icd;5657typedef struct {58unsigned i,n;/* i: index of next available slot, n: num slots */59UT_icd icd; /* initializer, copy and destructor functions */60char *d; /* n slots of size icd->sz*/61} UT_array;6263#define utarray_init(a,_icd) do { \64memset(a,0,sizeof(UT_array)); \65(a)->icd=*_icd; \66} while(0)6768#define utarray_done(a) do { \69if ((a)->n) { \70if ((a)->icd.dtor) { \71size_t _ut_i; \72for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \73(a)->icd.dtor(utarray_eltptr(a,_ut_i)); \74} \75} \76free((a)->d); \77} \78(a)->n=0; \79} while(0)8081#define utarray_new(a,_icd) do { \82a=(UT_array*)malloc(sizeof(UT_array)); \83utarray_init(a,_icd); \84} while(0)8586#define utarray_free(a) do { \87utarray_done(a); \88free(a); \89} while(0)9091#define utarray_reserve(a,by) do { \92if (((a)->i+by) > ((a)->n)) { \93while(((a)->i+by) > ((a)->n)) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \94if ( ((a)->d=(char*)realloc((a)->d, (a)->n*(a)->icd.sz)) == NULL) oom(); \95} \96} while(0)9798#define utarray_push_back(a,p) do { \99utarray_reserve(a,1); \100if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \101else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \102} while(0)103104#define utarray_pop_back(a) do { \105if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \106else { (a)->i--; } \107} while(0)108109#define utarray_extend_back(a) do { \110utarray_reserve(a,1); \111if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \112else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \113(a)->i++; \114} while(0)115116#define utarray_len(a) ((a)->i)117118#define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL)119#define _utarray_eltptr(a,j) ((char*)((a)->d + ((a)->icd.sz*(j) )))120121#define utarray_insert(a,p,j) do { \122utarray_reserve(a,1); \123if (j > (a)->i) break; \124if ((j) < (a)->i) { \125memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \126((a)->i - (j))*((a)->icd.sz)); \127} \128if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \129else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \130(a)->i++; \131} while(0)132133#define utarray_inserta(a,w,j) do { \134if (utarray_len(w) == 0) break; \135if (j > (a)->i) break; \136utarray_reserve(a,utarray_len(w)); \137if ((j) < (a)->i) { \138memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \139_utarray_eltptr(a,j), \140((a)->i - (j))*((a)->icd.sz)); \141} \142if ((a)->icd.copy) { \143size_t _ut_i; \144for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \145(a)->icd.copy(_utarray_eltptr(a,j+_ut_i), _utarray_eltptr(w,_ut_i)); \146} \147} else { \148memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \149utarray_len(w)*((a)->icd.sz)); \150} \151(a)->i += utarray_len(w); \152} while(0)153154#define utarray_resize(dst,num) do { \155size_t _ut_i; \156if (dst->i > (size_t)(num)) { \157if ((dst)->icd.dtor) { \158for(_ut_i=num; _ut_i < dst->i; _ut_i++) { \159(dst)->icd.dtor(utarray_eltptr(dst,_ut_i)); \160} \161} \162} else if (dst->i < (size_t)(num)) { \163utarray_reserve(dst,num-dst->i); \164if ((dst)->icd.init) { \165for(_ut_i=dst->i; _ut_i < num; _ut_i++) { \166(dst)->icd.init(utarray_eltptr(dst,_ut_i)); \167} \168} else { \169memset(_utarray_eltptr(dst,dst->i),0,(dst)->icd.sz*(num-dst->i)); \170} \171} \172dst->i = num; \173} while(0)174175#define utarray_concat(dst,src) do { \176utarray_inserta((dst),(src),utarray_len(dst)); \177} while(0)178179#define utarray_erase(a,pos,len) do { \180if ((a)->icd.dtor) { \181size_t _ut_i; \182for(_ut_i=0; _ut_i < len; _ut_i++) { \183(a)->icd.dtor(utarray_eltptr((a),pos+_ut_i)); \184} \185} \186if ((a)->i > (pos+len)) { \187memmove( _utarray_eltptr((a),pos), _utarray_eltptr((a),pos+len), \188(((a)->i)-(pos+len))*((a)->icd.sz)); \189} \190(a)->i -= (len); \191} while(0)192193#define utarray_renew(a,u) do { \194if (a) utarray_clear(a); \195else utarray_new((a),(u)); \196} while(0)197198#define utarray_clear(a) do { \199if ((a)->i > 0) { \200if ((a)->icd.dtor) { \201size_t _ut_i; \202for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \203(a)->icd.dtor(utarray_eltptr(a,_ut_i)); \204} \205} \206(a)->i = 0; \207} \208} while(0)209210#define utarray_sort(a,cmp) do { \211qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \212} while(0)213214#define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp)215216#define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL)217#define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : (((int)((a)->i) > (utarray_eltidx(a,e)+1)) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL))218#define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) > 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL))219#define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL)220#define utarray_eltidx(a,e) (((char*)(e) >= (char*)((a)->d)) ? (int)(((char*)(e) - (char*)((a)->d))/(a)->icd.sz) : -1)221222/* last we pre-define a few icd for common utarrays of ints and strings */223static void utarray_str_cpy(void *dst, const void *src) {224char *const*_src = (char*const*)src, **_dst = (char**)dst;225*_dst = (*_src == NULL) ? NULL : strdup(*_src);226}227static void utarray_str_dtor(void *elt) {228char **eltc = (char**)elt;229if (*eltc) free(*eltc);230}231static const UT_icd ut_str_icd _UNUSED_ = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor};232static const UT_icd ut_int_icd _UNUSED_ = {sizeof(int),NULL,NULL,NULL};233static const UT_icd ut_ptr_icd _UNUSED_ = {sizeof(void*),NULL,NULL,NULL};234235236#endif /* UTARRAY_H */237238239