new branch to integrate BlockDevice, BlockCache

git-svn-id: https://profuse.googlecode.com/svn/branches/profuse_interim@333 aa027e90-d47c-11dd-86d7-074df07e0730
This commit is contained in:
ksherlock 2011-02-19 18:07:04 +00:00
parent 7fb0604b76
commit 5427db9990
5 changed files with 389 additions and 0 deletions

105
Endian/Endian.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <Endian/Endian.h>
namespace LittleEndian {
uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return p[0] | (p[1] << 8);
}
uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16);
}
uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
}
void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
}
void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x >> 16) & 0xff;
p[3] = (x >> 24) & 0xff;
}
}
namespace BigEndian {
uint16_t Read16(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 8) | (p[1]);
}
uint32_t Read24(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 16) | (p[1] << 8) | (p[2]);
}
uint32_t Read32(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]);
}
void Write16(void *vp, uint16_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 8) & 0xff;
p[1] = (x) & 0xff;
}
void Write24(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 16) & 0xff;
p[1] = (x >> 8) & 0xff;
p[2] = (x) & 0xff;
}
void Write32(void *vp, uint32_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = (x >> 24) & 0xff;
p[1] = (x >> 16) & 0xff;
p[2] = (x >> 8) & 0xff;
p[3] = (x) & 0xff;
}
}

159
Endian/Endian.h Normal file
View File

@ -0,0 +1,159 @@
#ifndef __ENDIAN_H__
#define __ENDIAN_H__
// utlities to read/write bytes.
#include <stdint.h>
namespace LittleEndian {
inline uint8_t Read8(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return (p[0]);
}
uint16_t Read16(const void *vp);
uint32_t Read24(const void *vp);
uint32_t Read32(const void *vp);
inline uint8_t Read8(const void *vp, unsigned offset)
{
return Read8(offset + (const uint8_t *)vp);
}
inline uint16_t Read16(const void *vp, unsigned offset)
{
return Read16(offset + (const uint8_t *)vp);
}
inline uint32_t Read24(const void *vp, unsigned offset)
{
return Read24(offset + (const uint8_t *)vp);
}
inline uint32_t Read32(const void *vp, unsigned offset)
{
return Read32(offset + (const uint8_t *)vp);
}
// write
inline void Write8(void *vp, uint8_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = x;
}
void Write16(void *vp, uint16_t x);
void Write24(void *vp, uint32_t x);
void Write32(void *vp, uint32_t x);
inline void Write8(void *vp, unsigned offset, uint8_t x)
{
Write8(offset + (uint8_t *)vp, x);
}
inline void Write16(void *vp, unsigned offset, uint16_t x)
{
Write16(offset + (uint8_t *)vp, x);
}
inline void Write24(void *vp, unsigned offset, uint32_t x)
{
Write24(offset + (uint8_t *)vp, x);
}
inline void Write32(void *vp, unsigned offset, uint32_t x)
{
Write32(offset + (uint8_t *)vp, x);
}
}
namespace BigEndian {
inline uint8_t Read8(const void *vp)
{
const uint8_t *p = (const uint8_t *)vp;
return p[0];
}
uint16_t Read16(const void *vp);
uint32_t Read24(const void *vp);
uint32_t Read32(const void *vp);
inline uint8_t Read8(const void *vp, unsigned offset)
{
return Read8(offset + (const uint8_t *)vp);
}
inline uint16_t Read16(const void *vp, unsigned offset)
{
return Read16(offset + (const uint8_t *)vp);
}
inline uint32_t Read24(const void *vp, unsigned offset)
{
return Read24(offset + (const uint8_t *)vp);
}
inline uint32_t Read32(const void *vp, unsigned offset)
{
return Read32(offset + (const uint8_t *)vp);
}
// write
inline void Write8(void *vp, uint8_t x)
{
uint8_t *p = (uint8_t *)vp;
p[0] = x;
}
void Write16(void *vp, uint16_t x);
void Write24(void *vp, uint32_t x);
void Write32(void *vp, uint32_t x);
inline void Write8(void *vp, unsigned offset, uint8_t x)
{
Write8(offset + (uint8_t *)vp, x);
}
inline void Write16(void *vp, unsigned offset, uint16_t x)
{
Write16(offset + (uint8_t *)vp, x);
}
inline void Write24(void *vp, unsigned offset, uint32_t x)
{
Write24(offset + (uint8_t *)vp, x);
}
inline void Write32(void *vp, unsigned offset, uint32_t x)
{
Write32(offset + (uint8_t *)vp, x);
}
}
#endif

103
Endian/IOBuffer.cpp.h Normal file
View File

@ -0,0 +1,103 @@
class IOBuffer {
public:
IOBuffer(void *vp, unsigned size)
{
_buffer = vp;
_size = size;
_offset = 0;
}
void write8(uint8_t value)
{
Write8(_buffer, _offset, value);
_offset += 1;
}
void write16(uint16_t value)
{
Write16(_buffer, _offset, value);
_offset += 2;
}
void write24(uint32_t value)
{
Write24(_buffer, _offset, value);
_offset += 3;
}
void write32(uint32_t value)
{
Write32(_buffer, _offset, value);
_offset += 4;
}
void writeBytes(const void *src, unsigned count)
{
std::memcpy(_offset + (uint8_t *)_buffer, src, count);
_offset += count;
}
void writeZero(unsigned count)
{
std::memset(_offset + (uint8_t *)_buffer, 0, count);
_offset += count;
}
uint8_t read8()
{
uint8_t x = Read8(_buffer, _offset);
_offset += 1;
return x;
}
uint16_t read16()
{
uint16_t x = Read16(_buffer, _offset);
_offset += 2;
return x;
}
uint32_t read24()
{
uint32_t x = Read24(_buffer, _offset);
_offset += 3;
return x;
}
uint32_t read32()
{
uint32_t x = Read32(_buffer, _offset);
_offset += 4;
return x;
}
void readBytes(void *dest, unsigned count)
{
std::memcpy(dest, _offset + (uint8_t *)_buffer, count);
_offset += count;
}
unsigned offset() const { return _offset; }
void setOffset(unsigned offset) { _offset = offset; }
void setOffset(unsigned offset, bool zero)
{
if (zero && offset > _offset)
{
writeZero(offset - _offset);
}
else setOffset(offset);
}
unsigned size() const { return _size; }
void *buffer() const { return _buffer; }
private:
void *_buffer;
unsigned _size;
unsigned _offset;
};

15
Endian/IOBuffer.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __IOBUFFER_H__
#define __IOBUFFER_H__
#include <Endian/Endian.h>
#include <cstring>
namespace LittleEndian {
#include "IOBuffer.cpp.h"
}
namespace BigEndian {
#include "IOBuffer.cpp.h"
}
#endif

7
Endian/Makefile Normal file
View File

@ -0,0 +1,7 @@
CC = g++
CPPFLAGS += -g -Wall -I../
all : Endian.o
Endian.o : Endian.cpp Endian.h