OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [netrace-1.0/] [queue.c] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 alirezamon
/*
2
 * Copyright (c) 2010-2011 The University of Texas at Austin
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
7
 * met: redistributions of source code must retain the above copyright
8
 * notice, this list of conditions and the following disclaimer;
9
 * 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
 * neither the name of the copyright holders nor the names of its
13
 * contributors may be used to endorse or promote products derived from
14
 * this software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include "queue.h"
30
 
31
queue_t* queue_new() {
32
        queue_t* to_return = (queue_t*) malloc( sizeof(queue_t) );
33
        if( to_return == NULL ) {
34
                printf( "Failed malloc in queue_new\n" );
35
                exit(0);
36
        }
37
        to_return->head = NULL;
38
        to_return->tail = NULL;
39
        return to_return;
40
}
41
 
42
void queue_delete( queue_t* q ) {
43
        void* elem;
44
        while( ! queue_empty( q ) ) {
45
                elem = queue_pop_front( q );
46
                free( elem );
47
        }
48
        free( q );
49
}
50
 
51
int queue_empty( queue_t* q ) {
52
        return (q == NULL) || (q->head == NULL);
53
}
54
 
55
void queue_push_back( queue_t* q, void* e ) {
56
        if( q != NULL ) {
57
                if( q->head == NULL ) {
58
                        q->head = (node_t*) malloc( sizeof(node_t) );
59
                        if( q->head == NULL ) {
60
                                printf( "Failed malloc in queue_push_back\n" );
61
                                exit(0);
62
                        }
63
                        q->tail = q->head;
64
                        q->head->prev = NULL;
65
                        q->head->next = NULL;
66
                        q->head->elem = e;
67
                        q->head->prio = 0;
68
                } else {
69
                        q->tail->next = (node_t*) malloc( sizeof(node_t) );
70
                        if( q->head == NULL ) {
71
                                printf( "Failed malloc in queue_push_back\n" );
72
                                exit(0);
73
                        }
74
                        q->tail->next->prev = q->tail;
75
                        q->tail = q->tail->next;
76
                        q->tail->next = NULL;
77
                        q->tail->elem = e;
78
                        q->tail->prio = q->tail->prev->prio;
79
                }
80
        } else {
81
                printf( "Must initialize queue with queue_new()\n" );
82
                exit(0);
83
        }
84
}
85
 
86
void queue_push( queue_t* q, void* e, unsigned long long int prio ) {
87
        if( q != NULL ) {
88
                if( q->head == NULL ) {
89
                        q->head = (node_t*) malloc( sizeof(node_t) );
90
                        if( q->head == NULL ) {
91
                                printf( "Failed malloc in queue_push\n" );
92
                                exit(0);
93
                        }
94
                        q->tail = q->head;
95
                        q->head->prev = NULL;
96
                        q->head->next = NULL;
97
                        q->head->elem = e;
98
                        q->head->prio = prio;
99
                } else {
100
                        node_t* to_add = (node_t*) malloc( sizeof(node_t) );
101
                        if( to_add == NULL ) {
102
                                printf( "Failed malloc in queue_push\n" );
103
                                exit(0);
104
                        }
105
                        to_add->prio = prio;
106
                        to_add->elem = e;
107
                        node_t* behind;
108
                        for( behind = q->head; (behind != NULL) && (behind->prio < prio); behind = behind->next );
109
                        to_add->next = behind;
110
                        if( behind == NULL ) {
111
                                to_add->prev = q->tail;
112
                                q->tail->next = to_add;
113
                                q->tail = to_add;
114
                        } else if( behind == q->head ) {
115
                                to_add->prev = behind->prev;
116
                                behind->prev = to_add;
117
                                q->head = to_add;
118
                        } else {
119
                                to_add->prev = behind->prev;
120
                                to_add->prev->next = to_add;
121
                                behind->prev = to_add;
122
                        }
123
                }
124
        } else {
125
                printf( "Must initialize queue with queue_new()\n" );
126
                exit(0);
127
        }
128
}
129
 
130
void* queue_peek_front( queue_t* q ) {
131
        if( (q != NULL) && (q->head != NULL) ) {
132
                return q->head->elem;
133
        } else {
134
                return NULL;
135
        }
136
}
137
 
138
void* queue_pop_front( queue_t* q ) {
139
        void* to_return = NULL;
140
        if( (q != NULL) && (q->head != NULL) ) {
141
                to_return = q->head->elem;
142
                node_t* temp = q->head;
143
                q->head = q->head->next;
144
                if( q->head == NULL ) {
145
                        q->tail = NULL;
146
                }
147
                free( temp );
148
        }
149
        return to_return;
150
}
151
 
152
void queue_remove( queue_t* q, void* e ) {
153
        if( q != NULL ) {
154
                node_t* temp = q->head;
155
                while( temp != NULL ) {
156
                        if( temp->elem == e ) {
157
                                if( temp->prev == NULL ) {
158
                                        if( temp->next == NULL ) {
159
                                                q->head = NULL;
160
                                                q->tail = NULL;
161
                                        } else {
162
                                                q->head = temp->next;
163
                                                temp->next->prev = NULL;
164
                                        }
165
                                } else {
166
                                        temp->prev->next = temp->next;
167
                                        if( temp->next != NULL ) {
168
                                                temp->next->prev = temp->prev;
169
                                        } else {
170
                                                q->tail = temp->prev;
171
                                        }
172
                                }
173
                                free( temp );
174
                                temp = NULL;
175
                        } else {
176
                                temp = temp->next;
177
                        }
178
                }
179
        }
180
}
181
 

powered by: WebSVN 2.1.0

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