Little helper class for writing the disk device/partition data into a buffer for userland.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3879 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2003-07-06 22:53:20 +00:00
parent 6bd5253e85
commit b74cd4c9d1
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,83 @@
// UserDataWriter.cpp
#include <ddm_userland_interface.h>
#include "UserDataWriter.h"
// constructor
UserDataWriter::UserDataWriter()
: fBuffer(NULL),
fBufferSize(0),
fAllocatedSize(0)
{
}
// constructor
UserDataWriter::UserDataWriter(user_disk_device_data *buffer,
size_t bufferSize)
: fBuffer(buffer),
fBufferSize(bufferSize),
fAllocatedSize(0)
{
}
// destructor
UserDataWriter::~UserDataWriter()
{
}
// AllocateData
void *
UserDataWriter::AllocateData(size_t size, size_t align = 1)
{
if (size == 0)
return NULL;
if (align > 1)
fAllocatedSize = (fAllocatedSize + align - 1) / align * align;
void *result = NULL;
if (fAllocatedSize + size <= fBufferSize)
result = (uint8*)fBuffer + fAllocatedSize;
fAllocatedSize += size;
return result;
}
// AllocatePartitionData
user_partition_data *
UserDataWriter::AllocatePartitionData(size_t childCount)
{
return (user_partition_data*)AllocateData(
sizeof(user_partition_data)
+ sizeof(user_partition_data*) * ((int32)childCount - 1),
sizeof(int));
}
// AllocateDeviceData
user_disk_device_data *
UserDataWriter::AllocateDeviceData(size_t childCount)
{
return (user_disk_device_data*)AllocateData(
sizeof(user_disk_device_data)
+ sizeof(user_partition_data*) * ((int32)childCount - 1),
sizeof(int));
}
// PlaceString
char *
UserDataWriter::PlaceString(const char *str)
{
if (!str)
return NULL;
size_t len = strlen(str) + 1;
char *data = (char*)AllocateData(len);
if (data)
memcpy(data, str, len);
return data;
}
// AllocatedSize
size_t
UserDataWriter::AllocatedSize() const
{
return fAllocatedSize;
}

View File

@ -0,0 +1,39 @@
// KDiskDeviceUserDataWriter.h
#ifndef _K_DISK_DEVICE_USER_DATA_WRITER_H
#define _K_DISK_DEVICE_USER_DATA_WRITER_H
#include <SupportDefs.h>
struct user_disk_device_data;
struct user_partition_data;
namespace BPrivate {
namespace DiskDevice {
class UserDataWriter {
public:
UserDataWriter();
UserDataWriter(user_disk_device_data *buffer, size_t bufferSize);
~UserDataWriter();
void *AllocateData(size_t size, size_t align = 1);
user_partition_data *AllocatePartitionData(size_t childCount);
user_disk_device_data *AllocateDeviceData(size_t childCount);
char *PlaceString(const char *str);
size_t AllocatedSize() const;
private:
user_disk_device_data *fBuffer;
size_t fBufferSize;
size_t fAllocatedSize;
};
} // namespace DiskDevice
} // namespace BPrivate
using BPrivate::DiskDevice::UserDataWriter;
#endif // _K_DISK_DEVICE_USER_DATA_WRITER_H