Home | History | Annotate | Download | only in utils
      1 /* Copyright (C) 2009 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 #ifndef _ANDROID_UTILS_REFSET_H
     13 #define _ANDROID_UTILS_REFSET_H
     14 
     15 #include "android/utils/compiler.h"
     16 #include <android/utils/vector.h>
     17 
     18 ANDROID_BEGIN_HEADER
     19 
     20 /* this implements a set of addresses in memory.
     21  * NULL cannot be stored in the set.
     22  */
     23 
     24 typedef struct {
     25     AVECTOR_DECL(void*,buckets);
     26     int iteration;
     27 } ARefSet;
     28 
     29 AINLINED void
     30 arefSet_init( ARefSet*  s )
     31 {
     32     AVECTOR_INIT(s,buckets);
     33 }
     34 
     35 AINLINED void
     36 arefSet_done( ARefSet*  s )
     37 {
     38     AVECTOR_DONE(s,buckets);
     39 }
     40 
     41 AINLINED void
     42 arefSet_clear( ARefSet*  s )
     43 {
     44     AVECTOR_CLEAR(s,buckets);
     45     s->iteration = 0;
     46 }
     47 
     48 AINLINED int
     49 arefSet_count( ARefSet*  s )
     50 {
     51     return (int) AVECTOR_SIZE(s,buckets);
     52 }
     53 
     54 extern ABool  arefSet_has( ARefSet*  s, void*  item );
     55 extern void   arefSet_add( ARefSet*  s, void*  item );
     56 extern void   arefSet_del( ARefSet*  s, void*  item );
     57 
     58 extern void   _arefSet_removeDeferred( ARefSet* s );
     59 
     60 #define  AREFSET_DELETED ((void*)~(size_t)0)
     61 
     62 #define  AREFSET_FOREACH(_set,_item,_statement) \
     63     do { \
     64         int  __refset_nn = 0; \
     65         int  __refset_max = (_set)->max_buckets; \
     66         (_set)->iteration += 2; \
     67         for ( ; __refset_nn < __refset_max; __refset_nn++ ) { \
     68             void*  __refset_item = (_set)->buckets[__refset_nn]; \
     69             if (__refset_item == NULL || __refset_item == AREFSET_DELETED) \
     70                 continue; \
     71             _item = __refset_item; \
     72             _statement; \
     73         } \
     74         (_set)->iteration -= 2; \
     75         if ((_set)->iteration == 1) \
     76             _arefSet_removeDeferred(_set); \
     77     } while (0)
     78 
     79 ANDROID_END_HEADER
     80 
     81 #endif /* _ANDROID_UTILS_REFSET_H */
     82