-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRAMDisk.cpp
More file actions
92 lines (73 loc) · 1.49 KB
/
RAMDisk.cpp
File metadata and controls
92 lines (73 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/** @file RAMDisk.cpp
*
* RAM Disk structure implementation
*
* @author Copyright Aniesh Chawla, Sept 2006
**/
#include "RAMDisk.h"
#include <iostream>
RAMDisk::RAMDisk() : size(0), data(NULL)
{ }
RAMDisk::~RAMDisk()
{
if (data)
delete data;
}
int RAMDisk::num_blocks() const
{
return size;
}
int RAMDisk::block_size() const
{
return RAM_BLOCK_SIZE;
}
int RAMDisk::format(int num_blocks)
{
if (num_blocks < 1 || num_blocks > MAX_NUM_BLOCKS)
{
error1("Cannot format RAM disk with %d blocks\n", num_blocks);
return DISK_ERROR;
}
if (data)
delete data;
size = num_blocks;
data = new RAMDiskBlock[size];
if (!data)
{
error("RAM disk allocation failed\n");
size = 0;
return DISK_ERROR;
}
memset(data, 0, size * RAM_BLOCK_SIZE);
return DISK_OK;
}
int RAMDisk::read(int block_num, char *buf) const
{
if (block_num < 0 || block_num >= size)
{
error1("Cannot read RAM disk block %d\n", block_num);
return DISK_ERROR;
}
memcpy(buf, data[block_num], RAM_BLOCK_SIZE);
return DISK_OK;
}
int RAMDisk::write(int block_num, const char *buf)
{
if (block_num < 0 || block_num >= size)
{
error1("Cannot write RAM disk block %d\n", block_num);
return DISK_ERROR;
}
memcpy(data[block_num], buf, RAM_BLOCK_SIZE);
return DISK_OK;
}
/*RAMDisk&
RAMDisk::operator=(RAMDisk& rmdisk){
if(this!=&rmdisk){
std::cout<<"copying rmdisk"<<std::endl;
size = rmdisk.size;
data = (rmdisk.data);
}
return *this;
}
*/