1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* 3 * Copyright 2014 Couchbase, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef LCB_SLIST_H 19 #define LCB_SLIST_H 20 21 struct slist_node_st; 22 typedef struct slist_node_st { 23 struct slist_node_st *next; 24 } sllist_node; 25 26 typedef struct { 27 sllist_node first_prev; 28 sllist_node *last; 29 } sllist_root; 30 31 /** 32 * Indicates whether the list is empty or not 33 */ 34 #define SLLIST_FIRST(list) (list)->first_prev.next 35 #define SLLIST_LAST(list) (list)->last 36 37 #define SLLIST_IS_EMPTY(list) (SLLIST_LAST(list) == NULL) 38 #define SLLIST_IS_ONE(list) (SLLIST_FIRST(list) && SLLIST_FIRST(list) == SLLIST_LAST(list)) 39 40 /** 41 * Iterator for list. This can be used as the 'for' statement; as such this 42 * macro should look like such: 43 * 44 * slist_node *ii; 45 * SLIST_FOREACH(list, ii) { 46 * my_item *item = LCB_LIST_ITEM(my_item, ii, slnode); 47 * } 48 * 49 * @param list the list to iterate 50 * @param pos a local variable to use as the iterator 51 */ 52 #define SLLIST_FOREACH(list, pos) \ 53 for (pos = SLLIST_FIRST(list); pos; pos = pos->next) 54 55 56 typedef struct sllist_iterator_st { 57 sllist_node *cur; 58 sllist_node *prev; 59 sllist_node *next; 60 int removed; 61 } sllist_iterator; 62 63 #define sllist_iter_end(list, iter) ((iter)->cur == NULL) 64 65 #define SLLIST_ITEM(ptr, type, member) \ 66 ((type *) (void *) ((char *)(ptr) - offsetof(type, member))) 67 68 #define SLLIST_ITERFOR(list, iter) \ 69 for (slist_iter_init(list, iter); \ 70 !sllist_iter_end(list, iter); \ 71 slist_iter_incr(list, iter)) 72 73 #define SLLIST_ITERBASIC(list, elem) \ 74 for (elem = SLLIST_FIRST(list); elem; elem = elem->next) 75 76 #endif 77