Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / rst / rstzip3 / rstzip_v2 / buffer.H
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* OpenSPARC T2 Processor File: buffer.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// ========== Copyright Header Begin ==========================================
24//
25// OpenSPARC T2 Processor File: buffer.H
26// Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
27// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
28//
29// The above named program is free software; you can redistribute it and/or
30// modify it under the terms of the GNU General Public
31// License version 2 as published by the Free Software Foundation.
32//
33// The above named program is distributed in the hope that it will be
34// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
35// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36// General Public License for more details.
37//
38// You should have received a copy of the GNU General Public
39// License along with this work; if not, write to the Free Software
40// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
41//
42// ========== Copyright Header End ============================================
43#ifndef _BUFFER_H
44#define _BUFFER_H
45
46#include <stdlib.h>
47#include <sys/types.h>
48
49typedef struct {
50 rstf_unionT* rst;
51 unsigned nrecs;
52} RstSplit;
53
54class RstzipBuffer {
55public:
56 rstf_unionT* rstbuf;
57 int nrecs;
58 int curindex;
59
60 RstzipBuffer(int bufsize) {
61 rstbuf = (rstf_unionT*) malloc((bufsize + PADDING) * sizeof(rstf_unionT));
62 nrecs = 0;
63 curindex = 0;
64 buffersize = bufsize;
65 }
66
67 ~RstzipBuffer() {
68 free(rstbuf);
69 }
70
71 int fillBuffer(rstf_unionT* buf, int bufrecs) {
72 if (nrecs + bufrecs < buffersize + PADDING) {
73 memcpy(&rstbuf[nrecs], buf, bufrecs * sizeof(rstf_unionT));
74 nrecs += bufrecs;
75 } else {
76 fprintf(stderr, "Error: in FillRstBuffer::fillBuffer(): nrecs=%d, bufrecs=%d, max buffersize is %d\n",
77 nrecs, bufrecs, buffersize + PADDING);
78 exit(1);
79 }
80
81 return nrecs;
82 }
83
84 int getLastCpuRecIndex() {
85 int i;
86
87 for (i = nrecs - 1; i >= 0; i--) {
88 if (rstbuf[i].proto.rtype == CPU_T) {
89 break;
90 }
91 }
92
93 return i;
94 }
95
96 int shiftBuffer(int index) {
97 if (index > nrecs) {
98 fprintf(stderr, "Error: in RstzipBuffer::shiftBuffer() index (%d) > nrecs (%d)\n", index, nrecs);
99 exit(1);
100 }
101
102 if (index > 0) {
103 // Only do this if we have something to move.
104 // index = 0 when we flush this buffer; index = -1 when we're done decompressing.
105 nrecs -= index;
106 memmove(&rstbuf[0], &rstbuf[index], nrecs * sizeof(rstf_unionT));
107 } else {
108 nrecs = 0;
109 }
110
111 curindex = 0;
112
113 return nrecs;
114 }
115
116protected:
117 enum {
118 PADDING = 1000
119 };
120
121 int buffersize;
122}; // RstzipBuffer
123
124#endif // _BUFFER_H