OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [mcapi/] [queue.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 * Copyright (c) 2010, Mentor Graphics Corporation
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
 
31
 
32
#include <openmcapi.h>
33
 
34
typedef struct MCAPI_QNode_Struct
35
{
36
    struct MCAPI_QNode_Struct      *flink;
37
    struct MCAPI_QNode_Struct      *blink;
38
} MCAPI_QNODE;
39
 
40
/*************************************************************************
41
*
42
*   FUNCTION
43
*
44
*       mcapi_enqueue
45
*
46
*   DESCRIPTION
47
*
48
*       Enqueues an entry onto a given queue.
49
*
50
*   INPUTS
51
*
52
*       *queue_ptr              Pointer to the queue to which the entry
53
*                               is being enqueued.
54
*       *entry                  Pointer to the new queue entry.
55
*
56
*   OUTPUTS
57
*
58
*       None.
59
*
60
*************************************************************************/
61
void mcapi_enqueue(void *queue_ptr, void *entry)
62
{
63
    MCAPI_QNODE     *hdr = queue_ptr;
64
    MCAPI_QNODE     *item = entry;
65
 
66
    /* Set item's flink to point at NULL */
67
    item->flink = MCAPI_NULL;
68
 
69
    /* If there is currently a node in the linked list, we want add
70
     * item after that node
71
     */
72
    if (hdr->flink)
73
    {
74
        /* Make the last node's flink point to item */
75
        hdr->blink->flink = item;
76
 
77
        /* Make item's blink point to the old last node */
78
        item->blink = hdr->blink;
79
 
80
        /* Make hdr's blink point to the new last node, item */
81
        hdr->blink = item;
82
    }
83
 
84
    /* If the linked list was empty, we want both the hdr's flink and
85
     * the hdr's blink to point to item.  Both of item's links will
86
     * point to NULL, as there are no other nodes in the list
87
     */
88
    else
89
    {
90
        hdr->flink = hdr->blink = item;
91
        item->blink = MCAPI_NULL;
92
    }
93
 
94
}
95
 
96
/*************************************************************************
97
*
98
*   FUNCTION
99
*
100
*       mcapi_remove
101
*
102
*   DESCRIPTION
103
*
104
*       Removes an entry from a given queue.
105
*
106
*   INPUTS
107
*
108
*       *queue_ptr              Pointer to the queue from which the entry
109
*                               is being removed.
110
*       *entry                  Pointer to the queue entry to remove.
111
*
112
*   OUTPUTS
113
*
114
*       A pointer to the item that was removed.
115
*
116
*************************************************************************/
117
void *mcapi_remove(void *queue_ptr, void *entry)
118
{
119
    MCAPI_QNODE     *hdr = queue_ptr;
120
    MCAPI_QNODE     *item = entry;
121
    MCAPI_QNODE     *ent;
122
 
123
    /*  Search the linked list until item is found or the end of the list
124
     *  is reached.
125
     */
126
    for (ent = hdr->flink;( (ent) && (ent != item) ); ent = ent->flink)
127
        ;
128
 
129
    /*  If the item was found. */
130
    if (ent)
131
    {
132
        /* If we are deleting the list head, this is just a dequeue operation */
133
        if (hdr->flink == item)
134
            mcapi_dequeue(hdr);
135
 
136
        /*  If we are deleting the list tail, we need to reset the tail pointer
137
         *  and make the new tail point forward to 0.
138
         */
139
        else if (hdr->blink == item)
140
        {
141
            hdr->blink = item->blink;
142
            hdr->blink->flink = MCAPI_NULL;
143
        }
144
 
145
        /* We are removing this entry from the middle of the list */
146
        else
147
        {
148
            item->blink->flink = item->flink;
149
            item->flink->blink = item->blink;
150
        }
151
    }
152
 
153
    return (ent);
154
 
155
}
156
 
157
/*************************************************************************
158
*
159
*   FUNCTION
160
*
161
*       mcapi_dequeue
162
*
163
*   DESCRIPTION
164
*
165
*       Removes and returns the first entry in a queue.
166
*
167
*   INPUTS
168
*
169
*       *queue_ptr              Pointer to the queue from which the entry
170
*                               is being removed.
171
*
172
*   OUTPUTS
173
*
174
*       Pointer to the entry removed from the queue.
175
*
176
*************************************************************************/
177
void *mcapi_dequeue(void *queue_ptr)
178
{
179
   MCAPI_QNODE *hdr = queue_ptr;
180
   MCAPI_QNODE *ent;
181
 
182
   /* Create a pointer to the first node in the linked list */
183
   ent = hdr->flink;
184
 
185
   /* If there is a node in the list we want to remove it. */
186
   if (ent)
187
   {
188
       /* Make the hdr point the second node in the list */
189
       hdr->flink = ent->flink;
190
 
191
       /*  If there was a second node, we want that node's blink to at 0. */
192
       if (hdr->flink)
193
           hdr->flink->blink = MCAPI_NULL;
194
 
195
       /* Clear the next and previous pointers.*/
196
       ent->flink = ent->blink = MCAPI_NULL;
197
   }
198
 
199
   /* Return a pointer to the removed node */
200
   return (ent);
201
 
202
}  /* mcapi_dequeue */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.