Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / system / util / include / blaze-data.h
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: blaze-data.h
5* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
6* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
7*
8* The above named program is free software; you can redistribute it and/or
9* modify it under the terms of the GNU General Public
10* License version 2 as published by the Free Software Foundation.
11*
12* The above named program is distributed in the hope that it will be
13* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15* General Public License for more details.
16*
17* You should have received a copy of the GNU General Public
18* License along with this work; if not, write to the Free Software
19* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*
21* ========== Copyright Header End ============================================
22*/
23#ifndef _BLAZE_DATA_H_
24#define _BLAZE_DATA_H_
25
26#pragma ident "@(#)1.1 09/19/03 blaze-data.h"
27
28#include <thread.h>
29
30template <class T> class List1Node
31{
32 T data;
33 List1Node* next;
34
35public:
36
37///////////////////////////////////////
38
39 static List1Node* CreateInstance (const char *from)
40 {
41 List1Node * p = (List1Node*) calloc (1, sizeof(List1Node));
42 if (p == NULL) {
43 if (from) {
44 fprintf (stderr, "<%s>: unable to allocate %d bytes \n",
45 from, sizeof(List1Node));
46 }
47 else {
48 fprintf (stderr, "<LIST1>: unable to allocate %d bytes \n",
49 sizeof(List1Node));
50 }
51 return NULL;
52 }
53 return p;
54 }
55
56///////////////////////////////////////
57
58 static void AddTailProtected (List1Node **head, List1Node **tail, List1Node *instance, mutex_t *lock)
59 {
60
61 List1Node *ph;
62 List1Node *pt;
63
64 mutex_lock(lock);
65
66 ph = *head;
67 pt = *tail;
68
69 if (ph == NULL) {
70 *head = *tail = instance;
71 }
72 else {
73 pt->next = instance;
74 *tail = instance;
75 }
76
77 mutex_unlock(lock);
78 }
79
80///////////////////////////////////////
81
82 static void AddTail (List1Node **head, List1Node **tail, List1Node *instance)
83 {
84 List1Node *ph = *head;
85 List1Node *pt = *tail;
86
87 if (ph == NULL) {
88 *head = *tail = instance;
89 }
90 else {
91 pt->next = instance;
92 *tail = instance;
93 }
94 }
95 static void AddTail (List1Node **head, List1Node *instance)
96 {
97 List1Node *ph = *head;
98
99 if (ph == NULL) {
100 *head = instance;
101 }
102 else {
103 List1Node * tail = *head;
104 while(tail->next)
105 tail = tail->next;
106
107 tail->next = instance;
108 instance->next = 0;
109 }
110 }
111///////////////////////////////////////
112
113 static void AddHeadProtected (List1Node **head, List1Node *instance, mutex_t *lock )
114 {
115 List1Node *ph;
116
117 mutex_lock(lock);
118
119 ph = *head;
120
121 if (ph == NULL) {
122 *head = instance;
123 }
124 else {
125 instance->next = *head;
126 *head = instance;
127 }
128
129 mutex_unlock(lock);
130 }
131
132///////////////////////////////////////
133
134 static void AddHead (List1Node **head, List1Node *instance)
135 {
136 List1Node *ph = *head;
137 if (ph == NULL) {
138 *head = instance;
139 }
140 else {
141 instance->next = *head;
142 *head = instance;
143 }
144 }
145
146
147///////////////////////////////////////
148
149 static void DeleteHead (List1Node **head)
150 {
151 List1Node *ph = *head;
152 if (ph == NULL) {
153 return;
154 }
155 else {
156 *head = ph->next;
157 delete ph;
158 }
159 }
160
161///////////////////////////////////////
162
163 static List1Node* GetHeadProtected (List1Node **head, mutex_t *lock)
164 {
165 List1Node *ph;
166
167 mutex_lock(lock);
168 ph = *head;
169
170 if (ph != NULL) {
171 *head = ph->next;
172 }
173 mutex_unlock(lock);
174 return ph;
175 }
176
177///////////////////////////////////////
178
179 static List1Node* GetHead (List1Node **head)
180 {
181 List1Node *ph = *head;
182 if (ph != NULL) {
183 *head = ph->next;
184 }
185 return ph;
186 }
187
188///////////////////////////////////////
189
190 static void DeleteNode (List1Node **head, List1Node *prv)
191 {
192 if (prv == NULL) {
193 List1Node *pinstance = (*head);
194 (*head) = pinstance->next;
195 delete pinstance;
196 return;
197 }
198 else {
199 List1Node *pinstance = prv->next;
200 prv->next = pinstance->next;
201 delete pinstance;
202 }
203 }
204
205///////////////////////////////////////
206
207 T* GetData ()
208 {
209 return &data;
210 }
211
212///////////////////////////////////////
213
214 List1Node *Next ()
215 {
216 return next;
217 }
218
219///////////////////////////////////////
220
221 List1Node **GetNext ()
222 {
223 return &next;
224 }
225
226///////////////////////////////////////
227
228 static void LinktoTail (List1Node *tail, List1Node *head)
229 {
230 tail->next = head;
231 }
232
233};
234
235#endif // _BLAZE_DATA_H_