pktfifo.cc revision 2186
1/*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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 "base/misc.hh"
30#include "dev/pktfifo.hh"
31
32using namespace std;
33
34bool
35PacketFifo::copyout(void *dest, int offset, int len)
36{
37    char *data = (char *)dest;
38    if (offset + len >= size())
39        return false;
40
41    list<PacketPtr>::iterator p = fifo.begin();
42    list<PacketPtr>::iterator end = fifo.end();
43    while (len > 0) {
44        while (offset >= (*p)->length) {
45            offset -= (*p)->length;
46            ++p;
47        }
48
49        if (p == end)
50            panic("invalid fifo");
51
52        int size = min((*p)->length - offset, len);
53        memcpy(data, (*p)->data, size);
54        offset = 0;
55        len -= size;
56        data += size;
57        ++p;
58    }
59
60    return true;
61}
62
63
64void
65PacketFifo::serialize(const string &base, ostream &os)
66{
67    paramOut(os, base + ".size", _size);
68    paramOut(os, base + ".maxsize", _maxsize);
69    paramOut(os, base + ".reserved", _reserved);
70    paramOut(os, base + ".packets", fifo.size());
71
72    int i = 0;
73    list<PacketPtr>::iterator p = fifo.begin();
74    list<PacketPtr>::iterator end = fifo.end();
75    while (p != end) {
76        (*p)->serialize(csprintf("%s.packet%d", base, i), os);
77        ++p;
78        ++i;
79    }
80}
81
82void
83PacketFifo::unserialize(const string &base, Checkpoint *cp,
84                        const string &section)
85{
86    paramIn(cp, section, base + ".size", _size);
87//  paramIn(cp, section, base + ".maxsize", _maxsize);
88    paramIn(cp, section, base + ".reserved", _reserved);
89    int fifosize;
90    paramIn(cp, section, base + ".packets", fifosize);
91
92    fifo.clear();
93
94    for (int i = 0; i < fifosize; ++i) {
95        PacketPtr p = new PacketData(16384);
96        p->unserialize(csprintf("%s.packet%d", base, i), cp, section);
97        fifo.push_back(p);
98    }
99}
100