DFPC源碼解讀

看看newdatablock的頭文件

#ifndef __NVMDATABLOCK_H__
#define __NVMDATABLOCK_H__

#include <vector>
#include <stdint.h>
#include <ostream>

namespace NVM {

class NVMDataBlock
{
  public:
    NVMDataBlock( );
    ~NVMDataBlock( );

    void SetSize( uint64_t s );
    uint64_t GetSize( );
    
    uint8_t GetByte( uint64_t byte );
    void SetByte( uint64_t byte, uint8_t value );

    void SetValid( bool valid );
    bool IsValid( );

    void Print( std::ostream& out ) const;
    
    NVMDataBlock& operator=( const NVMDataBlock& m );

    uint8_t *rawData;
    //EDFPC
    uint8_t *comData;
    void SetComSize( uint64_t s );
    uint64_t GetComSize( );
    uint8_t GetComByte( uint64_t byte );
    void SetComByte( uint64_t byte, uint8_t value );
    void SetComFlag( bool flag );
    bool IsCompressed( );
    void SetHalfFlag( bool flag );
    bool IsHalf( );
  
  private:
    bool isValid;
    uint64_t size;
    
    //EDFPC
    uint64_t comSize;
    bool half;
    bool isCompressed;

    NVMDataBlock( const NVMDataBlock& ) { }
};

};

std::ostream& operator<<( std::ostream& out, const NVM::NVMDataBlock& obj );

#endif

首先是構造函數

NVMDataBlock::NVMDataBlock( )
{
    rawData = NULL;
    isValid = false;
    size = 0;
    //EDFPC
    comData = NULL;
    comSize = 0;
    isCompressed = false;
    half = false;
}

NVMDataBlock::~NVMDataBlock( )
{
    delete[] rawData;
    rawData = NULL;
    
    //EDFPC
    delete[] comData;
    comData = NULL;
}

這裏就銷燬comData

size的操作

和源文件一樣,除了增加了iscompressed這個位


void NVMDataBlock::SetSize( uint64_t s )
{
    assert( rawData == NULL );
    rawData = new uint8_t[s];
    size = s;
    isValid = true;
    isCompressed = false;
}

uint64_t NVMDataBlock::GetSize( )
{
    return size;
}

byte的操作

uint8_t NVMDataBlock::GetByte( uint64_t byte )
{
    uint8_t rv = 0;

    if( isValid && byte <= size )
    {
        rv = *((uint8_t*)(rawData)+byte);
    }

    return rv;
}

void NVMDataBlock::SetByte( uint64_t byte, uint8_t value )
{
    if( byte <= size )
    {
        rawData[byte] = value;
    }
    else
    {
        assert( false );
    }
}


void NVMDataBlock::SetValid( bool valid )
{
    isValid = valid;
}

bool NVMDataBlock::IsValid( )
{
    return isValid;
}

void NVMDataBlock::Print( std::ostream& out ) const
{
    out << std::hex;
    for( uint64_t i = 0; i < size; i++ )
    {
        out << std::setw(2) << std::setfill('0') << (int)rawData[i];
    }
    out << std::dec;
}

=的重載

NVMDataBlock& NVMDataBlock::operator=( const NVMDataBlock& m )
{
    if( m.rawData )
    {
        if( rawData == NULL )
            rawData = new uint8_t[m.size];
        memcpy(rawData, m.rawData, m.size);
        //EDFPC
        if( comData == NULL )
            comData = new uint8_t[m.comSize];
        memcpy(comData, m.comData, m.comSize);
        
    }
    isValid = m.isValid;
    size = m.size;
    //EDFPC
    comSize = m.comSize;
    isCompressed = m.isCompressed;

    return *this;
}

DFPC壓縮函數

void NVMDataBlock::SetComSize( uint64_t s )
{
    if( comData == NULL )
    {
        comData = new uint8_t[64];
        for(int i = 0; i < 64; i++)
            comData[i] = 0;
        
    }
    comSize = s;
    isCompressed = true;
}

//comdata的初始化直接建了64個char型數組

uint64_t NVMDataBlock::GetComSize( )
{
    return comSize;
}

byte的操作

uint8_t NVMDataBlock::GetComByte( uint64_t byte )
{
    uint8_t rv = 0;

    if(byte <= comSize )
    {
        rv = *((uint8_t*)(comData)+byte);
    }

    return rv;
}

void NVMDataBlock::SetComByte( uint64_t byte, uint8_t value )
{
    if( byte < 64 )
    {
        comData[byte] = value;
    }
    else
    {
        assert( false );
    }
}
void NVMDataBlock::SetComFlag( bool flag )
{
    isCompressed = flag;
}

bool NVMDataBlock::IsCompressed( )
{
    return isCompressed;
}

void NVMDataBlock::SetHalfFlag( bool flag )
{
    half = flag;
}

bool NVMDataBlock::IsHalf( )
{
    return half;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章