~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/bcachefs/darray.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* SPDX-License-Identifier: GPL-2.0 */
  2 #ifndef _BCACHEFS_DARRAY_H
  3 #define _BCACHEFS_DARRAY_H
  4 
  5 /*
  6  * Dynamic arrays:
  7  *
  8  * Inspired by CCAN's darray
  9  */
 10 
 11 #include <linux/slab.h>
 12 
 13 #define DARRAY_PREALLOCATED(_type, _nr)                                 \
 14 struct {                                                                \
 15         size_t nr, size;                                                \
 16         _type *data;                                                    \
 17         _type preallocated[_nr];                                        \
 18 }
 19 
 20 #define DARRAY(_type) DARRAY_PREALLOCATED(_type, 0)
 21 
 22 typedef DARRAY(char)    darray_char;
 23 typedef DARRAY(char *) darray_str;
 24 
 25 int __bch2_darray_resize(darray_char *, size_t, size_t, gfp_t);
 26 
 27 static inline int __darray_resize(darray_char *d, size_t element_size,
 28                                   size_t new_size, gfp_t gfp)
 29 {
 30         return unlikely(new_size > d->size)
 31                 ? __bch2_darray_resize(d, element_size, new_size, gfp)
 32                 : 0;
 33 }
 34 
 35 #define darray_resize_gfp(_d, _new_size, _gfp)                          \
 36         unlikely(__darray_resize((darray_char *) (_d), sizeof((_d)->data[0]), (_new_size), _gfp))
 37 
 38 #define darray_resize(_d, _new_size)                                    \
 39         darray_resize_gfp(_d, _new_size, GFP_KERNEL)
 40 
 41 static inline int __darray_make_room(darray_char *d, size_t t_size, size_t more, gfp_t gfp)
 42 {
 43         return __darray_resize(d, t_size, d->nr + more, gfp);
 44 }
 45 
 46 #define darray_make_room_gfp(_d, _more, _gfp)                           \
 47         __darray_make_room((darray_char *) (_d), sizeof((_d)->data[0]), (_more), _gfp)
 48 
 49 #define darray_make_room(_d, _more)                                     \
 50         darray_make_room_gfp(_d, _more, GFP_KERNEL)
 51 
 52 #define darray_room(_d)         ((_d).size - (_d).nr)
 53 
 54 #define darray_top(_d)          ((_d).data[(_d).nr])
 55 
 56 #define darray_push_gfp(_d, _item, _gfp)                                \
 57 ({                                                                      \
 58         int _ret = darray_make_room_gfp((_d), 1, _gfp);                 \
 59                                                                         \
 60         if (!_ret)                                                      \
 61                 (_d)->data[(_d)->nr++] = (_item);                       \
 62         _ret;                                                           \
 63 })
 64 
 65 #define darray_push(_d, _item)  darray_push_gfp(_d, _item, GFP_KERNEL)
 66 
 67 #define darray_pop(_d)          ((_d)->data[--(_d)->nr])
 68 
 69 #define darray_first(_d)        ((_d).data[0])
 70 #define darray_last(_d)         ((_d).data[(_d).nr - 1])
 71 
 72 #define darray_insert_item(_d, pos, _item)                              \
 73 ({                                                                      \
 74         size_t _pos = (pos);                                            \
 75         int _ret = darray_make_room((_d), 1);                           \
 76                                                                         \
 77         if (!_ret)                                                      \
 78                 array_insert_item((_d)->data, (_d)->nr, _pos, (_item)); \
 79         _ret;                                                           \
 80 })
 81 
 82 #define darray_remove_item(_d, _pos)                                    \
 83         array_remove_item((_d)->data, (_d)->nr, (_pos) - (_d)->data)
 84 
 85 #define __darray_for_each(_d, _i)                                               \
 86         for ((_i) = (_d).data; _i < (_d).data + (_d).nr; _i++)
 87 
 88 #define darray_for_each(_d, _i)                                         \
 89         for (typeof(&(_d).data[0]) _i = (_d).data; _i < (_d).data + (_d).nr; _i++)
 90 
 91 #define darray_for_each_reverse(_d, _i)                                 \
 92         for (typeof(&(_d).data[0]) _i = (_d).data + (_d).nr - 1; _i >= (_d).data; --_i)
 93 
 94 #define darray_init(_d)                                                 \
 95 do {                                                                    \
 96         (_d)->nr = 0;                                                   \
 97         (_d)->size = ARRAY_SIZE((_d)->preallocated);                    \
 98         (_d)->data = (_d)->size ? (_d)->preallocated : NULL;            \
 99 } while (0)
100 
101 #define darray_exit(_d)                                                 \
102 do {                                                                    \
103         if (!ARRAY_SIZE((_d)->preallocated) ||                          \
104             (_d)->data != (_d)->preallocated)                           \
105                 kvfree((_d)->data);                                     \
106         darray_init(_d);                                                \
107 } while (0)
108 
109 #endif /* _BCACHEFS_DARRAY_H */
110 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php