gem5 v24.0.0.0
Loading...
Searching...
No Matches
disk_image.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2001-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
33#ifndef __DEV_STORAGE_DISK_IMAGE_HH__
34#define __DEV_STORAGE_DISK_IMAGE_HH__
35
36#include <fstream>
37#include <unordered_map>
38
39#include "params/CowDiskImage.hh"
40#include "params/DiskImage.hh"
41#include "params/RawDiskImage.hh"
42#include "sim/sim_object.hh"
43
44#define SectorSize (512)
45
46namespace gem5
47{
48
52class DiskImage : public SimObject
53{
54 protected:
56
57 public:
58 typedef DiskImageParams Params;
59 DiskImage(const Params &p) : SimObject(p), initialized(false) {}
60 virtual ~DiskImage() {}
61
62 virtual std::streampos size() const = 0;
63
64 virtual std::streampos read(uint8_t *data,
65 std::streampos offset) const = 0;
66 virtual std::streampos write(const uint8_t *data,
67 std::streampos offset) = 0;
68};
69
73class RawDiskImage : public DiskImage
74{
75 protected:
76 mutable std::fstream stream;
77 std::string file;
79 mutable std::streampos disk_size;
80
81 public:
82 typedef RawDiskImageParams Params;
83 RawDiskImage(const Params &p);
85
86 void notifyFork() override;
87
88 void close();
89 void open(const std::string &filename, bool rd_only = false);
90
91 std::streampos size() const override;
92
93 std::streampos read(uint8_t *data, std::streampos offset) const override;
94 std::streampos write(const uint8_t *data, std::streampos offset) override;
95};
96
108{
109 public:
110 static const uint32_t VersionMajor;
111 static const uint32_t VersionMinor;
112
113 protected:
114 struct Sector
115 {
116 uint8_t data[SectorSize];
117 };
118 typedef std::unordered_map<uint64_t, Sector *> SectorTable;
119
120 protected:
121 std::string filename;
124
125 public:
126 typedef CowDiskImageParams Params;
127 CowDiskImage(const Params &p);
129
130 void notifyFork() override;
131
132 void initSectorTable(int hash_size);
133 bool open(const std::string &file);
134 void save() const;
135 void save(const std::string &file) const;
136 void writeback();
137
138 void serialize(CheckpointOut &cp) const override;
139 void unserialize(CheckpointIn &cp) override;
140
141 std::streampos size() const override;
142
143 std::streampos read(uint8_t *data, std::streampos offset) const override;
144 std::streampos write(const uint8_t *data, std::streampos offset) override;
145};
146
147void SafeRead(std::ifstream &stream, void *data, int count);
148
149template<class T>
150void SafeRead(std::ifstream &stream, T &data);
151
152template<class T>
153void SafeReadSwap(std::ifstream &stream, T &data);
154
155void SafeWrite(std::ofstream &stream, const void *data, int count);
156
157template<class T>
158void SafeWrite(std::ofstream &stream, const T &data);
159
160template<class T>
161void SafeWriteSwap(std::ofstream &stream, const T &data);
162
163} // namespace gem5
164
165
166#endif // __DEV_STORAGE_DISK_IMAGE_HH__
const char data[]
Specialization for accessing a copy-on-write disk image layer.
void notifyFork() override
Notify a child process of a fork.
std::streampos write(const uint8_t *data, std::streampos offset) override
std::streampos size() const override
void unserialize(CheckpointIn &cp) override
Unserialize an object.
static const uint32_t VersionMajor
std::streampos read(uint8_t *data, std::streampos offset) const override
void serialize(CheckpointOut &cp) const override
Serialize an object.
std::unordered_map< uint64_t, Sector * > SectorTable
void save() const
bool open(const std::string &file)
CowDiskImageParams Params
SectorTable * table
static const uint32_t VersionMinor
void initSectorTable(int hash_size)
CowDiskImage(const Params &p)
std::string filename
DiskImage * child
Basic interface for accessing a disk image.
Definition disk_image.hh:53
virtual std::streampos write(const uint8_t *data, std::streampos offset)=0
DiskImageParams Params
Definition disk_image.hh:58
virtual std::streampos size() const =0
virtual ~DiskImage()
Definition disk_image.hh:60
virtual std::streampos read(uint8_t *data, std::streampos offset) const =0
DiskImage(const Params &p)
Definition disk_image.hh:59
Specialization for accessing a raw disk image.
Definition disk_image.hh:74
void notifyFork() override
Notify a child process of a fork.
Definition disk_image.cc:72
void open(const std::string &filename, bool rd_only=false)
Definition disk_image.cc:83
std::streampos size() const override
std::fstream stream
Definition disk_image.hh:76
std::streampos disk_size
Definition disk_image.hh:79
RawDiskImage(const Params &p)
Definition disk_image.cc:60
std::streampos read(uint8_t *data, std::streampos offset) const override
std::streampos write(const uint8_t *data, std::streampos offset) override
RawDiskImageParams Params
Definition disk_image.hh:82
std::string file
Definition disk_image.hh:77
Abstract superclass for simulation objects.
#define SectorSize
Definition disk_image.hh:44
Bitfield< 23, 0 > offset
Definition types.hh:144
Bitfield< 0 > p
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
void SafeWriteSwap(std::ofstream &stream, const T &data)
std::ostream CheckpointOut
Definition serialize.hh:66
void SafeRead(std::ifstream &stream, void *data, int count)
void SafeWrite(std::ofstream &stream, const void *data, int count)
void SafeReadSwap(std::ifstream &stream, T &data)
uint8_t data[SectorSize]

Generated on Tue Jun 18 2024 16:24:03 for gem5 by doxygen 1.11.0