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