2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)queue.3 8.2 (Berkeley) 1/24/94
35 .\" 2007-12-08, mtk, Converted from mdoc to man macros
37 .\" Japanese Version Copyright (c) 2004 Yuichi SATO
38 .\" all rights reserved.
39 .\" Translated Thu Sep 2 04:41:11 JST 2004
40 .\" by Yuichi SATO <ysato444@yahoo.co.jp>
42 .\"WORD: circular queue 循環キュー
44 .TH QUEUE 3 2007-12-28 "Linux" "Linux Programmer's Manual"
47 .\"O LIST_ENTRY, LIST_HEAD, LIST_INIT, LIST_INSERT_AFTER, \
48 .\"O LIST_INSERT_HEAD, LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD, \
49 .\"O TAILQ_INIT, TAILQ_INSERT_AFTER, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, \
50 .\"O TAILQ_REMOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT, \
51 .\"O CIRCLEQ_INSERT_AFTER, CIRCLEQ_INSERT_BEFORE, \
52 .\"O CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL, \
53 .\"O CIRCLEQ_REMOVE \- implementations of lists, tail queues, and circular queues
54 LIST_ENTRY, LIST_HEAD, LIST_INIT, LIST_INSERT_AFTER, \
55 LIST_INSERT_HEAD, LIST_REMOVE, TAILQ_ENTRY, TAILQ_HEAD, \
56 TAILQ_INIT, TAILQ_INSERT_AFTER, TAILQ_INSERT_HEAD, TAILQ_INSERT_TAIL, \
57 TAILQ_REMOVE, CIRCLEQ_ENTRY, CIRCLEQ_HEAD, CIRCLEQ_INIT, \
58 CIRCLEQ_INSERT_AFTER, CIRCLEQ_INSERT_BEFORE, \
59 CIRCLEQ_INSERT_HEAD, CIRCLEQ_INSERT_TAIL, \
60 CIRCLEQ_REMOVE \- リスト・テール (tail) キュー・循環キューの実装
62 .\"Osato: tail queue の良い訳語が分からないので、とりあえずカタカナにする。
67 .B #include <sys/queue.h>
69 .BI "LIST_ENTRY(" TYPE );
70 .BI "LIST_HEAD(" HEADNAME ", " TYPE );
71 .BI "LIST_INIT(LIST_HEAD *" head );
72 .BI "LIST_INSERT_AFTER(LIST_ENTRY *" listelm ", "
73 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
74 .BI "LIST_INSERT_HEAD(LIST_HEAD *" head ", "
75 .BI " TYPE *" elm ", LIST_ENTRY " NAME );
76 .BI "LIST_REMOVE(TYPE *" elm ", LIST_ENTRY " NAME );
78 .BI "TAILQ_ENTRY(" TYPE );
79 .BI "TAILQ_HEAD("HEADNAME ", " TYPE );
80 .BI "TAILQ_INIT(TAILQ_HEAD *" head );
81 .BI "TAILQ_INSERT_AFTER(TAILQ_HEAD *" head ", TYPE *" listelm ", "
82 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
83 .BI "TAILQ_INSERT_HEAD(TAILQ_HEAD *" head ", "
84 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
85 .BI "TAILQ_INSERT_TAIL(TAILQ_HEAD *" head ", "
86 .BI " TYPE *" elm ", TAILQ_ENTRY " NAME );
87 .BI "TAILQ_REMOVE(TAILQ_HEAD *" head ", TYPE *" elm ", TAILQ_ENTRY " NAME );
89 .BI CIRCLEQ_ENTRY( TYPE );
90 .BI "CIRCLEQ_HEAD(" HEADNAME ", " TYPE );
91 .BI "CIRCLEQ_INIT(CIRCLEQ_HEAD *" head );
92 .BI "CIRCLEQ_INSERT_AFTER(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
93 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
94 .BI "CIRCLEQ_INSERT_BEFORE(CIRCLEQ_HEAD *" head ", TYPE *" listelm ", "
95 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
96 .BI "CIRCLEQ_INSERT_HEAD(CIRCLEQ_HEAD *" head ", "
97 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
98 .BI "CIRCLEQ_INSERT_TAIL(CIRCLEQ_HEAD *" head ", "
99 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
100 .BI "CIRCLEQ_REMOVE(CIRCLEQ_HEAD *" head ", "
101 .BI " TYPE *" elm ", CIRCLEQ_ENTRY " NAME );
105 .\"O These macros define and operate on three types of data structures:
106 .\"O lists, tail queues, and circular queues.
107 .\"O All three structures support the following functionality:
108 これらのマクロは、次の 3 つのデータ構造を定義して操作する:
110 3 つのデータ構造すべてにおいて以下の機能がサポートされている:
115 .\"O Insertion of a new entry at the head of the list.
118 .\"O Insertion of a new entry after any element in the list.
119 新たなエントリをリストのどの要素よりも後に挿入する。
121 .\"O Removal of any entry in the list.
124 .\"O Forward traversal through the list.
129 .\"O Lists are the simplest of the three data structures and support
130 .\"O only the above functionality.
131 リストは 3 つのデータ構造の中で最も単純であり、
134 .\"O Tail queues add the following functionality:
138 .\"O Entries can be added at the end of a list.
148 .\"O All list insertions and removals must specify the head of the list.
149 全てのリスト挿入と削除において、リストの先頭を指定しなければならない。
151 .\"O Each head entry requires two pointers rather than one.
152 各先頭エントリは 1 つではなく 2 つのポインタを必要とする。
154 .\"O Code size is about 15% greater and operations run about 20% slower
156 リストと比べて、コードサイズは 15% 大きくなり、操作は 20% 遅くなる。
160 .\"O Circular queues add the following functionality:
166 .\"O Entries can be added at the end of a list.
169 .\"O Entries can be added before another entry.
172 .\"O They may be traversed backward, from tail to head.
183 .\"O All list insertions and removals must specify the head of the list.
184 全てのリスト挿入と削除において、リストの先頭を指定しなければならない。
186 .\"O Each head entry requires two pointers rather than one.
187 各先頭エントリは 1 つではなく 2 つのポインタを必要とする。
189 .\"O The termination condition for traversal is more complex.
192 .\"O Code size is about 40% greater and operations run about 45% slower
194 リストと比べて、コードサイズは 40% 大きくなり、操作は 45% 遅くなる。
198 .\"O In the macro definitions,
200 .\"O is the name of a user-defined structure,
201 .\"O that must contain a field of type
202 .\"O .BR LIST_ENTRY ,
203 .\"O .BR TAILQ_ENTRY ,
205 .\"O .BR CIRCLEQ_ENTRY ,
220 .\"O is the name of a user-defined structure that must be declared
221 .\"O using the macros
223 .\"O .BR TAILQ_HEAD ,
225 .\"O .BR CIRCLEQ_HEAD .
234 .\"O See the examples below for further explanation of how these
235 .\"O macros are used.
236 これらのマクロがどのように使われるかについての更なる説明は、
240 .\"O A list is headed by a structure defined by the
246 .\"O This structure contains a single pointer to the first element
248 この構造体はリストの最初の要素へのポインタを 1 つ含む。
249 .\"O The elements are doubly linked so that an arbitrary element can be
250 .\"O removed without traversing the list.
253 .\"O New elements can be added to the list after an existing element or
254 .\"O at the head of the list.
255 新しい要素は既存の要素の後またはリストの先頭に追加できる。
258 .\"O structure is declared as follows:
264 LIST_HEAD(HEADNAME, TYPE) head;
270 .\"O is the name of the structure to be defined, and
272 .\"O is the type of the elements to be linked into the list.
278 .\"O A pointer to the head of the list can later be declared as:
279 リストの先頭へのポインタは、その後で次のように宣言される:
283 struct HEADNAME *headp;
291 .\"O are user selectable.)
300 .\"O declares a structure that connects the elements in
304 はリストの要素を接続する構造体を宣言する。
308 .\"O initializes the list referenced by
317 .\"O .B LIST_INSERT_HEAD
318 .\"O inserts the new element
320 .\"O at the head of the list.
328 .\"O .B LIST_INSERT_AFTER
329 .\"O inserts the new element
331 .\"O after the element
343 .\"O removes the element
351 .\"O .SS LIST EXAMPLE
355 LIST_HEAD(listhead, entry) head;
356 .\"O struct listhead *headp; /* List head. */
357 struct listhead *headp; /* リストの先頭。*/
360 .\"O LIST_ENTRY(entry) entries; /* List. */
363 LIST_ENTRY(entry) entries; /* リスト。 */
367 .\"O LIST_INIT(&head); /* Initialize the list. */
368 LIST_INIT(&head); /* リストを初期化する。*/
370 .\"O n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
371 n1 = malloc(sizeof(struct entry)); /* 先頭に挿入する。*/
372 LIST_INSERT_HEAD(&head, n1, entries);
374 .\"O n2 = malloc(sizeof(struct entry)); /* Insert after. */
375 n2 = malloc(sizeof(struct entry)); /* 後ろに挿入する。*/
376 LIST_INSERT_AFTER(n1, n2, entries);
377 .\"O /* Forward traversal. */
379 for (np = head.lh_first; np != NULL; np = np\->entries.le_next)
382 .\"O while (head.lh_first != NULL) /* Delete. */
383 while (head.lh_first != NULL) /* 削除する。*/
384 LIST_REMOVE(head.lh_first, entries);
388 .\"O A tail queue is headed by a structure defined by the
394 .\"O This structure contains a pair of pointers,
395 .\"O one to the first element in the tail queue and the other to
396 .\"O the last element in the tail queue.
397 この構造体は 1 組のポインタを含んでいる。
398 1 つはテールキューの最初の要素へのポインタであり、
399 もう 1 つはテールキューの最後の要素へのポインタである。
400 .\"O The elements are doubly linked so that an arbitrary element can be
401 .\"O removed without traversing the tail queue.
403 任意の要素はテールキューを辿らずに削除できる。
404 .\"O New elements can be added to the tail queue after an existing element,
405 .\"O at the head of the tail queue, or at the end of the tail queue.
406 新しい要素は既存の要素の後またはテールキューの先頭または末尾に追加できる。
409 .\"O structure is declared as follows:
415 TAILQ_HEAD(HEADNAME, TYPE) head;
421 .\"O is the name of the structure to be defined, and
423 .\"O is the type of the elements to be linked into the tail queue.
428 はテールキュー内でリンクされる要素の型である。
429 .\"O A pointer to the head of the tail queue can later be declared as:
430 テールキューの先頭へのポインタは、その後で次のように宣言される:
434 struct HEADNAME *headp;
442 .\"O are user selectable.)
451 .\"O declares a structure that connects the elements in
455 はテールキューの要素を接続する構造体を宣言する。
459 .\"O initializes the tail queue referenced by
468 .\"O .B TAILQ_INSERT_HEAD
469 .\"O inserts the new element
471 .\"O at the head of the tail queue.
479 .\"O .B TAILQ_INSERT_TAIL
480 .\"O inserts the new element
482 .\"O at the end of the tail queue.
490 .\"O .B TAILQ_INSERT_AFTER
491 .\"O inserts the new element
493 .\"O after the element
496 .B TAILQ_INSERT_AFTER
505 .\"O removes the element
507 .\"O from the tail queue.
513 .\"O .SS Tail Queue Example
517 TAILQ_HEAD(tailhead, entry) head;
518 .\"O struct tailhead *headp; /* Tail queue head. */
519 struct tailhead *headp; /* テールキューの先頭。*/
522 .\"O TAILQ_ENTRY(entry) entries; /* Tail queue. */
523 TAILQ_ENTRY(entry) entries; /* テールキュー。*/
527 .\"O TAILQ_INIT(&head); /* Initialize the queue. */
528 TAILQ_INIT(&head); /* キューを初期化する。*/
530 .\"O n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
531 n1 = malloc(sizeof(struct entry)); /* 先頭に挿入する。*/
532 TAILQ_INSERT_HEAD(&head, n1, entries);
534 .\"O n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
535 n1 = malloc(sizeof(struct entry)); /* 末尾に挿入する。*/
536 TAILQ_INSERT_TAIL(&head, n1, entries);
538 .\"O n2 = malloc(sizeof(struct entry)); /* Insert after. */
539 n2 = malloc(sizeof(struct entry)); /* 後ろに挿入する。*/
540 TAILQ_INSERT_AFTER(&head, n1, n2, entries);
541 .\"O /* Forward traversal. */
543 for (np = head.tqh_first; np != NULL; np = np\->entries.tqe_next)
547 while (head.tqh_first != NULL)
548 TAILQ_REMOVE(&head, head.tqh_first, entries);
550 .\"O .SS Circular Queues
552 .\"O A circular queue is headed by a structure defined by the
558 .\"O This structure contains a pair of pointers,
559 .\"O one to the first element in the circular queue and the other to the
560 .\"O last element in the circular queue.
561 この構造体は 1 組のポインタを含んでいる。
562 1 つは循環キューの最初の要素へのポインタであり、
563 もう 1 つは循環キューの最後の要素へのポインタである。
564 .\"O The elements are doubly linked so that an arbitrary element can be
565 .\"O removed without traversing the queue.
568 .\"O New elements can be added to the queue after an existing element,
569 .\"O before an existing element, at the head of the queue, or at the end
571 新しい要素は、既存の要素の後または前、またはキューの先頭または末尾に追加できる。
574 .\"O structure is declared as follows:
580 CIRCLEQ_HEAD(HEADNAME, TYPE) head;
586 .\"O is the name of the structure to be defined, and
588 .\"O is the type of the elements to be linked into the circular queue.
593 は循環キュー内でリンクされる要素の型である。
594 .\"O A pointer to the head of the circular queue can later be declared as:
595 循環キューの先頭へのポインタは、その後で次のように宣言される:
599 struct HEADNAME *headp;
607 .\"O are user selectable.)
615 .\"O .B CIRCLEQ_ENTRY
616 .\"O declares a structure that connects the elements in
617 .\"O the circular queue.
620 は循環キューの要素を接続する構造体を宣言する。
624 .\"O initializes the circular queue referenced by
633 .\"O .B CIRCLEQ_INSERT_HEAD
634 .\"O inserts the new element
636 .\"O at the head of the circular queue.
638 .B CIRCLEQ_INSERT_HEAD
644 .\"O .B CIRCLEQ_INSERT_TAIL
645 .\"O inserts the new element
647 .\"O at the end of the circular queue.
649 .B CIRCLEQ_INSERT_TAIL
655 .\"O .B CIRCLEQ_INSERT_AFTER
656 .\"O inserts the new element
658 .\"O after the element
661 .B CIRCLEQ_INSERT_AFTER
669 .\"O .B CIRCLEQ_INSERT_BEFORE
670 .\"O inserts the new element
672 .\"O before the element
675 .B CIRCLEQ_INSERT_AFTER
683 .\"O .B CIRCLEQ_REMOVE
684 .\"O removes the element
686 .\"O from the circular queue.
692 .\"O .SS Circular Queue Example
696 CIRCLEQ_HEAD(circleq, entry) head;
697 .\"O struct circleq *headp; /* Circular queue head. */
698 struct circleq *headp; /* 循環キューの先頭。*/
701 .\"O CIRCLEQ_ENTRY(entry) entries; /* Circular queue. */
702 CIRCLEQ_ENTRY(entry) entries; /* 循環キュー。*/
706 .\"O CIRCLEQ_INIT(&head); /* Initialize the circular queue. */
707 CIRCLEQ_INIT(&head); /* 循環キューを初期化する。*/
709 .\"O n1 = malloc(sizeof(struct entry)); /* Insert at the head. */
710 n1 = malloc(sizeof(struct entry)); /* 先頭に挿入する。*/
711 CIRCLEQ_INSERT_HEAD(&head, n1, entries);
713 .\"O n1 = malloc(sizeof(struct entry)); /* Insert at the tail. */
714 n1 = malloc(sizeof(struct entry)); /* 末尾に挿入する。*/
715 CIRCLEQ_INSERT_TAIL(&head, n1, entries);
717 .\"O n2 = malloc(sizeof(struct entry)); /* Insert after. */
718 n2 = malloc(sizeof(struct entry)); /* 後ろに挿入する。*/
719 CIRCLEQ_INSERT_AFTER(&head, n1, n2, entries);
721 .\"O n2 = malloc(sizeof(struct entry)); /* Insert before. */
722 n2 = malloc(sizeof(struct entry)); /* 前に挿入する。*/
723 CIRCLEQ_INSERT_BEFORE(&head, n1, n2, entries);
724 .\"O /* Forward traversal. */
726 for (np = head.cqh_first; np != (void *)&head; np = np\->entries.cqe_next)
728 .\"O /* Reverse traversal. */
730 for (np = head.cqh_last; np != (void *)&head; np = np\->entries.cqe_prev)
734 while (head.cqh_first != (void *)&head)
735 CIRCLEQ_REMOVE(&head, head.cqh_first, entries);
737 .\"O .SH "CONFORMING TO"
739 .\"O Not in POSIX.1-2001.
740 .\"O Present on the BSDs.
742 .\"O queue functions first appeared in
746 queue 関数は 4.4BSD で初めて登場した。