diff --git a/src/kernel/core/disk_device_manager/UserDataWriter.cpp b/src/kernel/core/disk_device_manager/UserDataWriter.cpp new file mode 100644 index 0000000000..aaaf20883f --- /dev/null +++ b/src/kernel/core/disk_device_manager/UserDataWriter.cpp @@ -0,0 +1,83 @@ +// UserDataWriter.cpp + +#include + +#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; +} + diff --git a/src/kernel/core/disk_device_manager/UserDataWriter.h b/src/kernel/core/disk_device_manager/UserDataWriter.h new file mode 100644 index 0000000000..0f312d9116 --- /dev/null +++ b/src/kernel/core/disk_device_manager/UserDataWriter.h @@ -0,0 +1,39 @@ +// KDiskDeviceUserDataWriter.h + +#ifndef _K_DISK_DEVICE_USER_DATA_WRITER_H +#define _K_DISK_DEVICE_USER_DATA_WRITER_H + +#include + +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