1 /* 2 * Copyright (C) 2007 Oracle. All rights reserved. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License v2 as published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public 14 * License along with this program; if not, write to the 15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 * Boston, MA 021110-1307, USA. 17 */ 18 19 /// D translation of list.h from btrfs-progs (v5.9) 20 module btrfs.c.kernel_lib.list; 21 22 import core.stdc.config; 23 24 extern(C): 25 26 enum LIST_POISON1 = cast(list_head *) 0x00100100; 27 enum LIST_POISON2 = cast(list_head *) 0x00200200; 28 29 /* 30 * Simple doubly linked list implementation. 31 * 32 * Some of the internal functions ("__xxx") are useful when 33 * manipulating whole lists rather than single entries, as 34 * sometimes we already know the next/prev entries and we can 35 * generate better code by using them directly rather than 36 * using the generic single-entry routines. 37 */ 38 39 struct list_head { 40 list_head* next, prev; 41 } 42 43 // D: The rest is not yet translated: 44 /+ 45 46 #define LIST_HEAD_INIT(name) { &(name), &(name) } 47 48 #define LIST_HEAD(name) \ 49 struct list_head name = LIST_HEAD_INIT(name) 50 51 static inline void INIT_LIST_HEAD(struct list_head *list) 52 { 53 list->next = list; 54 list->prev = list; 55 } 56 57 /* 58 * Insert a new entry between two known consecutive entries. 59 * 60 * This is only for internal list manipulation where we know 61 * the prev/next entries already! 62 */ 63 #ifndef CONFIG_DEBUG_LIST 64 static inline void __list_add(struct list_head *xnew, 65 struct list_head *prev, 66 struct list_head *next) 67 { 68 next->prev = xnew; 69 xnew->next = next; 70 xnew->prev = prev; 71 prev->next = xnew; 72 } 73 #else 74 extern void __list_add(struct list_head *xnew, 75 struct list_head *prev, 76 struct list_head *next); 77 #endif 78 79 /** 80 * list_add - add a new entry 81 * @new: new entry to be added 82 * @head: list head to add it after 83 * 84 * Insert a new entry after the specified head. 85 * This is good for implementing stacks. 86 */ 87 #ifndef CONFIG_DEBUG_LIST 88 static inline void list_add(struct list_head *xnew, struct list_head *head) 89 { 90 __list_add(xnew, head, head->next); 91 } 92 #else 93 extern void list_add(struct list_head *xnew, struct list_head *head); 94 #endif 95 96 97 /** 98 * list_add_tail - add a new entry 99 * @new: new entry to be added 100 * @head: list head to add it before 101 * 102 * Insert a new entry before the specified head. 103 * This is useful for implementing queues. 104 */ 105 static inline void list_add_tail(struct list_head *xnew, struct list_head *head) 106 { 107 __list_add(xnew, head->prev, head); 108 } 109 110 /* 111 * Delete a list entry by making the prev/next entries 112 * point to each other. 113 * 114 * This is only for internal list manipulation where we know 115 * the prev/next entries already! 116 */ 117 static inline void __list_del(struct list_head * prev, struct list_head * next) 118 { 119 next->prev = prev; 120 prev->next = next; 121 } 122 123 /** 124 * list_del - deletes entry from list. 125 * @entry: the element to delete from the list. 126 * Note: list_empty on entry does not return true after this, the entry is 127 * in an undefined state. 128 */ 129 #ifndef CONFIG_DEBUG_LIST 130 static inline void list_del(struct list_head *entry) 131 { 132 __list_del(entry->prev, entry->next); 133 entry->next = LIST_POISON1; 134 entry->prev = LIST_POISON2; 135 } 136 #else 137 extern void list_del(struct list_head *entry); 138 #endif 139 140 /** 141 * list_replace - replace old entry by new one 142 * @old : the element to be replaced 143 * @new : the new element to insert 144 * Note: if 'old' was empty, it will be overwritten. 145 */ 146 static inline void list_replace(struct list_head *old, 147 struct list_head *xnew) 148 { 149 xnew->next = old->next; 150 xnew->next->prev = xnew; 151 xnew->prev = old->prev; 152 xnew->prev->next = xnew; 153 } 154 155 static inline void list_replace_init(struct list_head *old, 156 struct list_head *xnew) 157 { 158 list_replace(old, xnew); 159 INIT_LIST_HEAD(old); 160 } 161 /** 162 * list_del_init - deletes entry from list and reinitialize it. 163 * @entry: the element to delete from the list. 164 */ 165 static inline void list_del_init(struct list_head *entry) 166 { 167 __list_del(entry->prev, entry->next); 168 INIT_LIST_HEAD(entry); 169 } 170 171 /** 172 * list_move - delete from one list and add as another's head 173 * @list: the entry to move 174 * @head: the head that will precede our entry 175 */ 176 static inline void list_move(struct list_head *list, struct list_head *head) 177 { 178 __list_del(list->prev, list->next); 179 list_add(list, head); 180 } 181 182 /** 183 * list_move_tail - delete from one list and add as another's tail 184 * @list: the entry to move 185 * @head: the head that will follow our entry 186 */ 187 static inline void list_move_tail(struct list_head *list, 188 struct list_head *head) 189 { 190 __list_del(list->prev, list->next); 191 list_add_tail(list, head); 192 } 193 194 /** 195 * list_is_last - tests whether @list is the last entry in list @head 196 * @list: the entry to test 197 * @head: the head of the list 198 */ 199 static inline int list_is_last(const struct list_head *list, 200 const struct list_head *head) 201 { 202 return list->next == head; 203 } 204 205 /** 206 * list_empty - tests whether a list is empty 207 * @head: the list to test. 208 */ 209 static inline int list_empty(const struct list_head *head) 210 { 211 return head->next == head; 212 } 213 214 /** 215 * list_empty_careful - tests whether a list is empty and not being modified 216 * @head: the list to test 217 * 218 * Description: 219 * tests whether a list is empty _and_ checks that no other CPU might be 220 * in the process of modifying either member (next or prev) 221 * 222 * NOTE: using list_empty_careful() without synchronization 223 * can only be safe if the only activity that can happen 224 * to the list entry is list_del_init(). Eg. it cannot be used 225 * if another CPU could re-list_add() it. 226 */ 227 static inline int list_empty_careful(const struct list_head *head) 228 { 229 struct list_head *next = head->next; 230 return (next == head) && (next == head->prev); 231 } 232 233 static inline void __list_splice(const struct list_head *list, 234 struct list_head *prev, 235 struct list_head *next) 236 { 237 struct list_head *first = list->next; 238 struct list_head *last = list->prev; 239 240 first->prev = prev; 241 prev->next = first; 242 243 last->next = next; 244 next->prev = last; 245 } 246 247 /** 248 * list_splice - join two lists 249 * @list: the new list to add. 250 * @head: the place to add it in the first list. 251 */ 252 static inline void list_splice(struct list_head *list, struct list_head *head) 253 { 254 if (!list_empty(list)) 255 __list_splice(list, head, head->next); 256 } 257 258 /** 259 * list_splice_tail - join two lists, each list being a queue 260 * @list: the new list to add. 261 * @head: the place to add it in the first list. 262 */ 263 static inline void list_splice_tail(struct list_head *list, 264 struct list_head *head) 265 { 266 if (!list_empty(list)) 267 __list_splice(list, head->prev, head); 268 } 269 270 /** 271 * list_splice_init - join two lists and reinitialise the emptied list. 272 * @list: the new list to add. 273 * @head: the place to add it in the first list. 274 * 275 * The list at @list is reinitialised 276 */ 277 static inline void list_splice_init(struct list_head *list, 278 struct list_head *head) 279 { 280 if (!list_empty(list)) { 281 __list_splice(list, head, head->next); 282 INIT_LIST_HEAD(list); 283 } 284 } 285 286 /** 287 * list_splice_tail_init - join two lists and reinitialise the emptied list 288 * @list: the new list to add. 289 * @head: the place to add it in the first list. 290 * 291 * Each of the lists is a queue. 292 * The list at @list is reinitialised 293 */ 294 static inline void list_splice_tail_init(struct list_head *list, 295 struct list_head *head) 296 { 297 if (!list_empty(list)) { 298 __list_splice(list, head->prev, head); 299 INIT_LIST_HEAD(list); 300 } 301 } 302 303 /** 304 * list_entry - get the struct for this entry 305 * @ptr: the &struct list_head pointer. 306 * @type: the type of the struct this is embedded in. 307 * @member: the name of the list_struct within the struct. 308 */ 309 #define list_entry(ptr, type, member) \ 310 container_of(ptr, type, member) 311 312 /** 313 * list_first_entry - get the first element from a list 314 * @ptr: the list head to take the element from. 315 * @type: the type of the struct this is embedded in. 316 * @member: the name of the list_struct within the struct. 317 * 318 * Note, that list is expected to be not empty. 319 */ 320 #define list_first_entry(ptr, type, member) \ 321 list_entry((ptr)->next, type, member) 322 323 /** 324 * list_next_entry - get the next element from a list 325 * @ptr: the list head to take the element from. 326 * @member: the name of the list_struct within the struct. 327 * 328 * Note, that next is expected to be not null. 329 */ 330 #define list_next_entry(ptr, member) \ 331 list_entry((ptr)->member.next, typeof(*ptr), member) 332 333 /** 334 * list_for_each - iterate over a list 335 * @pos: the &struct list_head to use as a loop cursor. 336 * @head: the head for your list. 337 */ 338 #define list_for_each(pos, head) \ 339 for (pos = (head)->next; pos != (head); \ 340 pos = pos->next) 341 342 /** 343 * __list_for_each - iterate over a list 344 * @pos: the &struct list_head to use as a loop cursor. 345 * @head: the head for your list. 346 * 347 * This variant differs from list_for_each() in that it's the 348 * simplest possible list iteration code, no prefetching is done. 349 * Use this for code that knows the list to be very short (empty 350 * or 1 entry) most of the time. 351 */ 352 #define __list_for_each(pos, head) \ 353 for (pos = (head)->next; pos != (head); pos = pos->next) 354 355 /** 356 * list_for_each_prev - iterate over a list backwards 357 * @pos: the &struct list_head to use as a loop cursor. 358 * @head: the head for your list. 359 */ 360 #define list_for_each_prev(pos, head) \ 361 for (pos = (head)->prev; pos != (head); \ 362 pos = pos->prev) 363 364 /** 365 * list_for_each_safe - iterate over a list safe against removal of list entry 366 * @pos: the &struct list_head to use as a loop cursor. 367 * @n: another &struct list_head to use as temporary storage 368 * @head: the head for your list. 369 */ 370 #define list_for_each_safe(pos, n, head) \ 371 for (pos = (head)->next, n = pos->next; pos != (head); \ 372 pos = n, n = pos->next) 373 374 /** 375 * list_for_each_entry - iterate over list of given type 376 * @pos: the type * to use as a loop cursor. 377 * @head: the head for your list. 378 * @member: the name of the list_struct within the struct. 379 */ 380 #define list_for_each_entry(pos, head, member) \ 381 for (pos = list_entry((head)->next, typeof(*pos), member); \ 382 &pos->member != (head); \ 383 pos = list_entry(pos->member.next, typeof(*pos), member)) 384 385 /** 386 * list_for_each_entry_reverse - iterate backwards over list of given type. 387 * @pos: the type * to use as a loop cursor. 388 * @head: the head for your list. 389 * @member: the name of the list_struct within the struct. 390 */ 391 #define list_for_each_entry_reverse(pos, head, member) \ 392 for (pos = list_entry((head)->prev, typeof(*pos), member); \ 393 &pos->member != (head); \ 394 pos = list_entry(pos->member.prev, typeof(*pos), member)) 395 396 /** 397 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue 398 * @pos: the type * to use as a start point 399 * @head: the head of the list 400 * @member: the name of the list_struct within the struct. 401 * 402 * Prepares a pos entry for use as a start point in list_for_each_entry_continue. 403 */ 404 #define list_prepare_entry(pos, head, member) \ 405 ((pos) ? : list_entry(head, typeof(*pos), member)) 406 407 /** 408 * list_for_each_entry_continue - continue iteration over list of given type 409 * @pos: the type * to use as a loop cursor. 410 * @head: the head for your list. 411 * @member: the name of the list_struct within the struct. 412 * 413 * Continue to iterate over list of given type, continuing after 414 * the current position. 415 */ 416 #define list_for_each_entry_continue(pos, head, member) \ 417 for (pos = list_entry(pos->member.next, typeof(*pos), member); \ 418 &pos->member != (head); \ 419 pos = list_entry(pos->member.next, typeof(*pos), member)) 420 421 /** 422 * list_for_each_entry_from - iterate over list of given type from the current point 423 * @pos: the type * to use as a loop cursor. 424 * @head: the head for your list. 425 * @member: the name of the list_struct within the struct. 426 * 427 * Iterate over list of given type, continuing from current position. 428 */ 429 #define list_for_each_entry_from(pos, head, member) \ 430 for (; &pos->member != (head); \ 431 pos = list_entry(pos->member.next, typeof(*pos), member)) 432 433 /** 434 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry 435 * @pos: the type * to use as a loop cursor. 436 * @n: another type * to use as temporary storage 437 * @head: the head for your list. 438 * @member: the name of the list_struct within the struct. 439 */ 440 #define list_for_each_entry_safe(pos, n, head, member) \ 441 for (pos = list_entry((head)->next, typeof(*pos), member), \ 442 n = list_entry(pos->member.next, typeof(*pos), member); \ 443 &pos->member != (head); \ 444 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 445 446 /** 447 * list_for_each_entry_safe_continue 448 * @pos: the type * to use as a loop cursor. 449 * @n: another type * to use as temporary storage 450 * @head: the head for your list. 451 * @member: the name of the list_struct within the struct. 452 * 453 * Iterate over list of given type, continuing after current point, 454 * safe against removal of list entry. 455 */ 456 #define list_for_each_entry_safe_continue(pos, n, head, member) \ 457 for (pos = list_entry(pos->member.next, typeof(*pos), member), \ 458 n = list_entry(pos->member.next, typeof(*pos), member); \ 459 &pos->member != (head); \ 460 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 461 462 /** 463 * list_for_each_entry_safe_from 464 * @pos: the type * to use as a loop cursor. 465 * @n: another type * to use as temporary storage 466 * @head: the head for your list. 467 * @member: the name of the list_struct within the struct. 468 * 469 * Iterate over list of given type from current point, safe against 470 * removal of list entry. 471 */ 472 #define list_for_each_entry_safe_from(pos, n, head, member) \ 473 for (n = list_entry(pos->member.next, typeof(*pos), member); \ 474 &pos->member != (head); \ 475 pos = n, n = list_entry(n->member.next, typeof(*n), member)) 476 477 /** 478 * list_for_each_entry_safe_reverse 479 * @pos: the type * to use as a loop cursor. 480 * @n: another type * to use as temporary storage 481 * @head: the head for your list. 482 * @member: the name of the list_struct within the struct. 483 * 484 * Iterate backwards over list of given type, safe against removal 485 * of list entry. 486 */ 487 #define list_for_each_entry_safe_reverse(pos, n, head, member) \ 488 for (pos = list_entry((head)->prev, typeof(*pos), member), \ 489 n = list_entry(pos->member.prev, typeof(*pos), member); \ 490 &pos->member != (head); \ 491 pos = n, n = list_entry(n->member.prev, typeof(*n), member)) 492 493 +/