1 /* Ring definitions. */ 2 #define qr(a_type) \ 3 struct { \ 4 a_type *qre_next; \ 5 a_type *qre_prev; \ 6 } 7 8 /* Ring functions. */ 9 #define qr_new(a_qr, a_field) do { \ 10 (a_qr)->a_field.qre_next = (a_qr); \ 11 (a_qr)->a_field.qre_prev = (a_qr); \ 12 } while (0) 13 14 #define qr_next(a_qr, a_field) ((a_qr)->a_field.qre_next) 15 16 #define qr_prev(a_qr, a_field) ((a_qr)->a_field.qre_prev) 17 18 #define qr_before_insert(a_qrelm, a_qr, a_field) do { \ 19 (a_qr)->a_field.qre_prev = (a_qrelm)->a_field.qre_prev; \ 20 (a_qr)->a_field.qre_next = (a_qrelm); \ 21 (a_qr)->a_field.qre_prev->a_field.qre_next = (a_qr); \ 22 (a_qrelm)->a_field.qre_prev = (a_qr); \ 23 } while (0) 24 25 #define qr_after_insert(a_qrelm, a_qr, a_field) \ 26 do \ 27 { \ 28 (a_qr)->a_field.qre_next = (a_qrelm)->a_field.qre_next; \ 29 (a_qr)->a_field.qre_prev = (a_qrelm); \ 30 (a_qr)->a_field.qre_next->a_field.qre_prev = (a_qr); \ 31 (a_qrelm)->a_field.qre_next = (a_qr); \ 32 } while (0) 33 34 #define qr_meld(a_qr_a, a_qr_b, a_field) do { \ 35 void *t; \ 36 (a_qr_a)->a_field.qre_prev->a_field.qre_next = (a_qr_b); \ 37 (a_qr_b)->a_field.qre_prev->a_field.qre_next = (a_qr_a); \ 38 t = (a_qr_a)->a_field.qre_prev; \ 39 (a_qr_a)->a_field.qre_prev = (a_qr_b)->a_field.qre_prev; \ 40 (a_qr_b)->a_field.qre_prev = t; \ 41 } while (0) 42 43 /* qr_meld() and qr_split() are functionally equivalent, so there's no need to 44 * have two copies of the code. */ 45 #define qr_split(a_qr_a, a_qr_b, a_field) \ 46 qr_meld((a_qr_a), (a_qr_b), a_field) 47 48 #define qr_remove(a_qr, a_field) do { \ 49 (a_qr)->a_field.qre_prev->a_field.qre_next \ 50 = (a_qr)->a_field.qre_next; \ 51 (a_qr)->a_field.qre_next->a_field.qre_prev \ 52 = (a_qr)->a_field.qre_prev; \ 53 (a_qr)->a_field.qre_next = (a_qr); \ 54 (a_qr)->a_field.qre_prev = (a_qr); \ 55 } while (0) 56 57 #define qr_foreach(var, a_qr, a_field) \ 58 for ((var) = (a_qr); \ 59 (var) != NULL; \ 60 (var) = (((var)->a_field.qre_next != (a_qr)) \ 61 ? (var)->a_field.qre_next : NULL)) 62 63 #define qr_reverse_foreach(var, a_qr, a_field) \ 64 for ((var) = ((a_qr) != NULL) ? qr_prev(a_qr, a_field) : NULL; \ 65 (var) != NULL; \ 66 (var) = (((var) != (a_qr)) \ 67 ? (var)->a_field.qre_prev : NULL)) 68