Renamed Block to MemoryChunk, and got rid of the template
parameter in lieu of using reinterpret_cast, which turns out to be cleaner all in all. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3350 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,42 +0,0 @@
|
|||||||
//----------------------------------------------------------------------
|
|
||||||
// This software is part of the OpenBeOS distribution and is covered
|
|
||||||
// by the OpenBeOS license.
|
|
||||||
//
|
|
||||||
// Copyright (c) 2003 Tyler Dauwalder, [email protected]
|
|
||||||
//---------------------------------------------------------------------
|
|
||||||
|
|
||||||
#ifndef _UDF_BLOCK_H
|
|
||||||
#define _UDF_BLOCK_H
|
|
||||||
|
|
||||||
#include <malloc.h>
|
|
||||||
|
|
||||||
#include "cpp.h"
|
|
||||||
|
|
||||||
namespace UDF {
|
|
||||||
|
|
||||||
template<class DataType>
|
|
||||||
class Block {
|
|
||||||
public:
|
|
||||||
Block(uint32 blockSize)
|
|
||||||
: fSize(blockSize)
|
|
||||||
, fData(malloc(blockSize))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
~Block() { free(Data()); }
|
|
||||||
uint32 Size() { return fSize; }
|
|
||||||
DataType* Data() { return (DataType*)fData; }
|
|
||||||
status_t InitCheck() { return Data() ? B_OK : B_NO_MEMORY; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
Block();
|
|
||||||
Block(const Block&);
|
|
||||||
Block& operator=(const Block&);
|
|
||||||
|
|
||||||
uint32 fSize;
|
|
||||||
void *fData;
|
|
||||||
};
|
|
||||||
|
|
||||||
}; // namespace UDF
|
|
||||||
|
|
||||||
#endif // _UDF_BLOCK_H
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
//----------------------------------------------------------------------
|
||||||
|
// This software is part of the OpenBeOS distribution and is covered
|
||||||
|
// by the OpenBeOS license.
|
||||||
|
//
|
||||||
|
// Copyright (c) 2003 Tyler Dauwalder, [email protected]
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
#ifndef _UDF_MEMORY_CHUNK_H
|
||||||
|
#define _UDF_MEMORY_CHUNK_H
|
||||||
|
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
#include "cpp.h"
|
||||||
|
|
||||||
|
namespace UDF {
|
||||||
|
|
||||||
|
/*! Simple class to encapsulate the boring details of allocating
|
||||||
|
and deallocating a chunk of memory.
|
||||||
|
|
||||||
|
The main use for this class is cleanly and simply allocating
|
||||||
|
arbitrary chunks of data on the stack.
|
||||||
|
*/
|
||||||
|
class MemoryChunk {
|
||||||
|
public:
|
||||||
|
MemoryChunk(uint32 blockSize)
|
||||||
|
: fSize(blockSize)
|
||||||
|
, fData(malloc(blockSize))
|
||||||
|
, fOwnsData(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
MemoryChunk(uint32 blockSize, void *blockData)
|
||||||
|
: fSize(blockSize)
|
||||||
|
, fData(blockData)
|
||||||
|
, fOwnsData(false)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~MemoryChunk()
|
||||||
|
{
|
||||||
|
if (fOwnsData)
|
||||||
|
free(Data());
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32 Size() { return fSize; }
|
||||||
|
void* Data() { return fData; }
|
||||||
|
status_t InitCheck() { return Data() ? B_OK : B_NO_MEMORY; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
MemoryChunk();
|
||||||
|
MemoryChunk(const MemoryChunk&);
|
||||||
|
MemoryChunk& operator=(const MemoryChunk&);
|
||||||
|
|
||||||
|
uint32 fSize;
|
||||||
|
void *fData;
|
||||||
|
bool fOwnsData;
|
||||||
|
};
|
||||||
|
|
||||||
|
}; // namespace UDF
|
||||||
|
|
||||||
|
#endif // _UDF_MEMORY_CHUNK_H
|
||||||
Reference in New Issue
Block a user