Subversion Repositories Kolibri OS

Rev

Rev 3391 | Rev 5056 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * include/linux/idr.h
  3.  *
  4.  * 2002-10-18  written by Jim Houston jim.houston@ccur.com
  5.  *      Copyright (C) 2002 by Concurrent Computer Corporation
  6.  *      Distributed under the GNU GPL license version 2.
  7.  *
  8.  * Small id to pointer translation service avoiding fixed sized
  9.  * tables.
  10.  */
  11.  
  12. #ifndef __IDR_H__
  13. #define __IDR_H__
  14.  
  15. #include <syscall.h>
  16. #include <linux/types.h>
  17. #include <errno-base.h>
  18. #include <linux/bitops.h>
  19. //#include <linux/init.h>
  20. //#include <linux/rcupdate.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/bitmap.h>
  23. #include <linux/bug.h>
  24.  
  25.  
  26. /*
  27.  * We want shallower trees and thus more bits covered at each layer.  8
  28.  * bits gives us large enough first layer for most use cases and maximum
  29.  * tree depth of 4.  Each idr_layer is slightly larger than 2k on 64bit and
  30.  * 1k on 32bit.
  31.  */
  32. #define IDR_BITS 8
  33. #define IDR_SIZE (1 << IDR_BITS)
  34. #define IDR_MASK ((1 << IDR_BITS)-1)
  35.  
  36. struct idr_layer {
  37.         int                     prefix; /* the ID prefix of this idr_layer */
  38.         DECLARE_BITMAP(bitmap, IDR_SIZE); /* A zero bit means "space here" */
  39.         struct idr_layer __rcu  *ary[1<<IDR_BITS];
  40.         int                      count;  /* When zero, we can release it */
  41.         int                      layer;  /* distance from leaf */
  42.     struct rcu_head      rcu_head;
  43. };
  44.  
  45. struct idr {
  46.         struct idr_layer __rcu  *hint;  /* the last layer allocated from */
  47.         struct idr_layer __rcu *top;
  48.         struct idr_layer *id_free;
  49.         int                     layers; /* only valid w/o concurrent changes */
  50.         int               id_free_cnt;
  51.         int                     cur;    /* current pos for cyclic allocation */
  52.         spinlock_t              lock;
  53. };
  54.  
  55. #define IDR_INIT(name)                                          \
  56. {                                                               \
  57.         .lock                   = __SPIN_LOCK_UNLOCKED(name.lock),      \
  58. }
  59. #define DEFINE_IDR(name)        struct idr name = IDR_INIT(name)
  60.  
  61. /**
  62.  * DOC: idr sync
  63.  * idr synchronization (stolen from radix-tree.h)
  64.  *
  65.  * idr_find() is able to be called locklessly, using RCU. The caller must
  66.  * ensure calls to this function are made within rcu_read_lock() regions.
  67.  * Other readers (lock-free or otherwise) and modifications may be running
  68.  * concurrently.
  69.  *
  70.  * It is still required that the caller manage the synchronization and
  71.  * lifetimes of the items. So if RCU lock-free lookups are used, typically
  72.  * this would mean that the items have their own locks, or are amenable to
  73.  * lock-free access; and that the items are freed by RCU (or only freed after
  74.  * having been deleted from the idr tree *and* a synchronize_rcu() grace
  75.  * period).
  76.  */
  77.  
  78. /*
  79.  * This is what we export.
  80.  */
  81.  
  82. void *idr_find_slowpath(struct idr *idp, int id);
  83. void idr_preload(gfp_t gfp_mask);
  84. int idr_alloc(struct idr *idp, void *ptr, int start, int end, gfp_t gfp_mask);
  85. int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask);
  86. int idr_for_each(struct idr *idp,
  87.                  int (*fn)(int id, void *p, void *data), void *data);
  88. void *idr_get_next(struct idr *idp, int *nextid);
  89. void *idr_replace(struct idr *idp, void *ptr, int id);
  90. void idr_remove(struct idr *idp, int id);
  91. void idr_free(struct idr *idp, int id);
  92. void idr_destroy(struct idr *idp);
  93. void idr_init(struct idr *idp);
  94.  
  95. /**
  96.  * idr_preload_end - end preload section started with idr_preload()
  97.  *
  98.  * Each idr_preload() should be matched with an invocation of this
  99.  * function.  See idr_preload() for details.
  100.  */
  101. static inline void idr_preload_end(void)
  102. {
  103. //      preempt_enable();
  104. }
  105.  
  106. /**
  107.  * idr_find - return pointer for given id
  108.  * @idr: idr handle
  109.  * @id: lookup key
  110.  *
  111.  * Return the pointer given the id it has been registered with.  A %NULL
  112.  * return indicates that @id is not valid or you passed %NULL in
  113.  * idr_get_new().
  114.  *
  115.  * This function can be called under rcu_read_lock(), given that the leaf
  116.  * pointers lifetimes are correctly managed.
  117.  */
  118. static inline void *idr_find(struct idr *idr, int id)
  119. {
  120.         struct idr_layer *hint = rcu_dereference_raw(idr->hint);
  121.  
  122.         if (hint && (id & ~IDR_MASK) == hint->prefix)
  123.                 return rcu_dereference_raw(hint->ary[id & IDR_MASK]);
  124.  
  125.         return idr_find_slowpath(idr, id);
  126. }
  127.  
  128. /**
  129.  * idr_for_each_entry - iterate over an idr's elements of a given type
  130.  * @idp:     idr handle
  131.  * @entry:   the type * to use as cursor
  132.  * @id:      id entry's key
  133.  *
  134.  * @entry and @id do not need to be initialized before the loop, and
  135.  * after normal terminatinon @entry is left with the value NULL.  This
  136.  * is convenient for a "not found" value.
  137.  */
  138. #define idr_for_each_entry(idp, entry, id)                      \
  139.         for (id = 0; ((entry) = idr_get_next(idp, &(id))) != NULL; ++id)
  140.  
  141. /*
  142.  * Don't use the following functions.  These exist only to suppress
  143.  * deprecated warnings on EXPORT_SYMBOL()s.
  144.  */
  145. int __idr_pre_get(struct idr *idp, gfp_t gfp_mask);
  146. int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id);
  147. void __idr_remove_all(struct idr *idp);
  148.  
  149. /**
  150.  * idr_pre_get - reserve resources for idr allocation
  151.  * @idp:        idr handle
  152.  * @gfp_mask:   memory allocation flags
  153.  *
  154.  * Part of old alloc interface.  This is going away.  Use
  155.  * idr_preload[_end]() and idr_alloc() instead.
  156.  */
  157. static inline int __deprecated idr_pre_get(struct idr *idp, gfp_t gfp_mask)
  158. {
  159.         return __idr_pre_get(idp, gfp_mask);
  160. }
  161.  
  162. /**
  163.  * idr_get_new_above - allocate new idr entry above or equal to a start id
  164.  * @idp: idr handle
  165.  * @ptr: pointer you want associated with the id
  166.  * @starting_id: id to start search at
  167.  * @id: pointer to the allocated handle
  168.  *
  169.  * Part of old alloc interface.  This is going away.  Use
  170.  * idr_preload[_end]() and idr_alloc() instead.
  171.  */
  172. static inline int __deprecated idr_get_new_above(struct idr *idp, void *ptr,
  173.                                                  int starting_id, int *id)
  174. {
  175.         return __idr_get_new_above(idp, ptr, starting_id, id);
  176. }
  177.  
  178. /**
  179.  * idr_get_new - allocate new idr entry
  180.  * @idp:     idr handle
  181.  * @ptr: pointer you want associated with the id
  182.  * @id: pointer to the allocated handle
  183.  *
  184.  * Part of old alloc interface.  This is going away.  Use
  185.  * idr_preload[_end]() and idr_alloc() instead.
  186.  */
  187. static inline int __deprecated idr_get_new(struct idr *idp, void *ptr, int *id)
  188. {
  189.         return __idr_get_new_above(idp, ptr, 0, id);
  190. }
  191.  
  192. /**
  193.  * idr_remove_all - remove all ids from the given idr tree
  194.  * @idp: idr handle
  195.  *
  196.  * If you're trying to destroy @idp, calling idr_destroy() is enough.
  197.  * This is going away.  Don't use.
  198.  */
  199. static inline void __deprecated idr_remove_all(struct idr *idp)
  200. {
  201.         __idr_remove_all(idp);
  202. }
  203.  
  204. /*
  205.  * IDA - IDR based id allocator, use when translation from id to
  206.  * pointer isn't necessary.
  207.  *
  208.  * IDA_BITMAP_LONGS is calculated to be one less to accommodate
  209.  * ida_bitmap->nr_busy so that the whole struct fits in 128 bytes.
  210.  */
  211. #define IDA_CHUNK_SIZE          128     /* 128 bytes per chunk */
  212. #define IDA_BITMAP_LONGS        (IDA_CHUNK_SIZE / sizeof(long) - 1)
  213. #define IDA_BITMAP_BITS         (IDA_BITMAP_LONGS * sizeof(long) * 8)
  214.  
  215. struct ida_bitmap {
  216.         long                    nr_busy;
  217.         unsigned long           bitmap[IDA_BITMAP_LONGS];
  218. };
  219.  
  220. struct ida {
  221.         struct idr              idr;
  222.         struct ida_bitmap       *free_bitmap;
  223. };
  224.  
  225. #define IDA_INIT(name)          { .idr = IDR_INIT((name).idr), .free_bitmap = NULL, }
  226. #define DEFINE_IDA(name)        struct ida name = IDA_INIT(name)
  227.  
  228. int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
  229. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
  230. void ida_remove(struct ida *ida, int id);
  231. void ida_destroy(struct ida *ida);
  232. void ida_init(struct ida *ida);
  233.  
  234. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  235.                    gfp_t gfp_mask);
  236. void ida_simple_remove(struct ida *ida, unsigned int id);
  237.  
  238. /**
  239.  * ida_get_new - allocate new ID
  240.  * @ida:        idr handle
  241.  * @p_id:       pointer to the allocated handle
  242.  *
  243.  * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
  244.  */
  245. static inline int ida_get_new(struct ida *ida, int *p_id)
  246. {
  247.         return ida_get_new_above(ida, 0, p_id);
  248. }
  249.  
  250. void __init idr_init_cache(void);
  251.  
  252. #endif /* __IDR_H__ */
  253.