xref: /titanic_41/usr/src/lib/libc/port/gen/insque.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * insque() and remque() insert or remove an element from a queue.
35  * The queue is built from a doubly linked list whose elements are
36  * defined by a structure where the first member of the structure
37  * points to the next element in the queue and the second member
38  * of the structure points to the previous element in the queue.
39  */
40 
41 #pragma weak insque = _insque
42 #pragma weak remque = _remque
43 
44 #include "synonyms.h"
45 #include <sys/types.h>
46 #include <stdlib.h>
47 #include <search.h>
48 
49 void
50 insque(void *elem, void *pred)
51 {
52 	if (pred == NULL) {    /* This is the first element being inserted. */
53 		((struct qelem *)elem)->q_forw = NULL;
54 		((struct qelem *)elem)->q_back = NULL;
55 	} else if (((struct qelem *)pred)->q_forw == NULL) {
56 					/* The element is inserted at */
57 					/* the end of the queue. */
58 		((struct qelem *)elem)->q_forw = NULL;
59 		((struct qelem *)elem)->q_back = pred;
60 		((struct qelem *)pred)->q_forw = elem;
61 	} else {		/* The element is inserted in the middle of */
62 				/* the queue. */
63 		((struct qelem *)elem)->q_forw = ((struct qelem *)pred)->q_forw;
64 		((struct qelem *)elem)->q_back = pred;
65 		((struct qelem *)pred)->q_forw->q_back = elem;
66 		((struct qelem *)pred)->q_forw = elem;
67 	}
68 }
69 
70 void
71 remque(void *elem)
72 {
73 	if (((struct qelem *)elem)->q_back == NULL) {
74 					/* The first element is removed. */
75 		if (((struct qelem *)elem)->q_forw == NULL)
76 					/* The only element is removed. */
77 			return;
78 		((struct qelem *)elem)->q_forw->q_back = NULL;
79 	} else if (((struct qelem *)elem)->q_forw == NULL) {
80 					/* The last element is removed */
81 		((struct qelem *)elem)->q_back->q_forw = NULL;
82 	} else {	/* The middle element is removed. */
83 		((struct qelem *)elem)->q_back->q_forw =
84 				((struct qelem *)elem)->q_forw;
85 		((struct qelem *)elem)->q_forw->q_back =
86 				((struct qelem *)elem)->q_back;
87 	}
88 }
89