| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * OpenSPARC T2 Processor File: rz3_rst_array.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 | /* rz3_rst_array.h |
| 24 | * incrementally-growing array of rst records |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <strings.h> |
| 29 | #include <sys/types.h> |
| 30 | |
| 31 | #include "rstf/rstf.h" |
| 32 | |
| 33 | struct rz3_rst_array { |
| 34 | rz3_rst_array(int sz) { |
| 35 | |
| 36 | maxcount = sz; |
| 37 | |
| 38 | nlines = (maxcount + linesize - 1)/linesize; |
| 39 | |
| 40 | lines = new rstf_unionT * [nlines]; |
| 41 | int i; |
| 42 | for (i=0; i<nlines; i++) { |
| 43 | lines[i] = NULL; |
| 44 | } |
| 45 | count = 0; |
| 46 | } |
| 47 | |
| 48 | bool Push(const rstf_unionT * rec) { |
| 49 | if (count >= maxcount) return false; |
| 50 | int lidx = (count / linesize); |
| 51 | int offs = count % linesize; |
| 52 | if (lines[lidx] == NULL) { |
| 53 | assert(offs == 0); |
| 54 | lines[lidx] = new rstf_unionT [linesize]; |
| 55 | } |
| 56 | memcpy(&(lines[lidx][offs]), rec, sizeof(rstf_unionT)); |
| 57 | count++; |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool Get(int idx, rstf_unionT * where) { |
| 62 | if (idx >= count) { |
| 63 | return false; |
| 64 | } |
| 65 | int lidx = idx / linesize; |
| 66 | int offs = idx % linesize; |
| 67 | memcpy(where, &(lines[lidx][offs]), sizeof(rstf_unionT)); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | const rstf_unionT * GetPtr(int idx) { |
| 72 | if (idx >= count) { |
| 73 | return NULL; |
| 74 | } |
| 75 | int lidx = idx/linesize; |
| 76 | int offs = idx % linesize; |
| 77 | return &(lines[lidx][offs]); |
| 78 | } |
| 79 | |
| 80 | int Count() { |
| 81 | return count; |
| 82 | } |
| 83 | |
| 84 | bool CopyFrom(rstf_unionT * membuf, int howmany) { |
| 85 | if (howmany > maxcount) return false; |
| 86 | count = howmany; |
| 87 | int full_line_count = (count / linesize); |
| 88 | int partial_line_size = (count % linesize); |
| 89 | int i; |
| 90 | int recs_copied = 0; |
| 91 | for (i=0; i<full_line_count; i++) { |
| 92 | lines[i] = new rstf_unionT [linesize]; |
| 93 | memcpy(lines[i], membuf+recs_copied, linesize*sizeof(rstf_unionT)); |
| 94 | recs_copied += linesize; |
| 95 | } |
| 96 | if (partial_line_size) { |
| 97 | lines[i] = new rstf_unionT [linesize]; |
| 98 | memcpy(lines[i], membuf+recs_copied, partial_line_size*sizeof(rstf_unionT)); |
| 99 | } |
| 100 | return true; |
| 101 | } // CopyFrom() |
| 102 | |
| 103 | // membuf must accomodate <count> records. count is returned by the Count() function |
| 104 | bool CopyTo(rstf_unionT * membuf) { |
| 105 | int full_line_count = (count/linesize); |
| 106 | int partial_line_size = (count % linesize); |
| 107 | int recs_copied = 0; |
| 108 | int i; |
| 109 | for (i=0; i<full_line_count; i++) { |
| 110 | memcpy(membuf+recs_copied, lines[i], linesize*sizeof(rstf_unionT)); |
| 111 | recs_copied += linesize; |
| 112 | } |
| 113 | if (partial_line_size) { |
| 114 | memcpy(membuf+recs_copied, lines[i], partial_line_size * sizeof(rstf_unionT)); |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | void clear() { |
| 120 | count = 0; |
| 121 | } |
| 122 | |
| 123 | ~rz3_rst_array() { |
| 124 | int i; |
| 125 | for (i=0; i<nlines; i++) { |
| 126 | if (lines[i] != NULL) { |
| 127 | delete [] lines[i]; |
| 128 | } |
| 129 | } |
| 130 | delete [] lines; |
| 131 | } |
| 132 | |
| 133 | enum consts_e { linesize = 512 }; |
| 134 | int maxcount; |
| 135 | int count; |
| 136 | int nlines; |
| 137 | rstf_unionT * *lines; |
| 138 | }; // rz3_rst_array |