3 #include <util/kernel_cpp.h>
4 #include <ddm_userland_interface.h>
7 #include "UserDataWriter.h"
13 // RelocationEntryList
14 struct UserDataWriter::RelocationEntryList
: Vector
<addr
*> {};
17 UserDataWriter::UserDataWriter()
21 fRelocationEntries(NULL
)
26 UserDataWriter::UserDataWriter(user_disk_device_data
*buffer
,
31 fRelocationEntries(NULL
)
33 SetTo(buffer
, bufferSize
);
37 UserDataWriter::~UserDataWriter()
39 delete fRelocationEntries
;
44 UserDataWriter::SetTo(user_disk_device_data
*buffer
, size_t bufferSize
)
48 fBufferSize
= bufferSize
;
50 if (fBuffer
&& fBufferSize
> 0) {
51 fRelocationEntries
= new(nothrow
) RelocationEntryList
;
52 if (!fRelocationEntries
)
60 UserDataWriter::Unset()
62 delete fRelocationEntries
;
66 fRelocationEntries
= NULL
;
71 UserDataWriter::AllocateData(size_t size
, size_t align
)
73 // handles size == 0 gracefully
74 // get a properly aligned offset
75 size_t offset
= fAllocatedSize
;
77 offset
= (fAllocatedSize
+ align
- 1) / align
* align
;
78 // get the result pointer
80 if (fBuffer
&& offset
+ size
<= fBufferSize
)
81 result
= (uint8
*)fBuffer
+ offset
;
82 // always update the allocated size, even if there wasn't enough space
83 fAllocatedSize
= offset
+ size
;
87 // AllocatePartitionData
89 UserDataWriter::AllocatePartitionData(size_t childCount
)
91 return (user_partition_data
*)AllocateData(
92 sizeof(user_partition_data
)
93 + sizeof(user_partition_data
*) * ((int32
)childCount
- 1),
98 user_disk_device_data
*
99 UserDataWriter::AllocateDeviceData(size_t childCount
)
101 return (user_disk_device_data
*)AllocateData(
102 sizeof(user_disk_device_data
)
103 + sizeof(user_partition_data
*) * ((int32
)childCount
- 1),
109 UserDataWriter::PlaceString(const char *str
)
113 size_t len
= strlen(str
) + 1;
114 char *data
= (char*)AllocateData(len
);
116 memcpy(data
, str
, len
);
122 UserDataWriter::AllocatedSize() const
124 return fAllocatedSize
;
127 // AddRelocationEntry
129 UserDataWriter::AddRelocationEntry(void *address
)
131 if (fRelocationEntries
&& (addr
)address
>= (addr
)fBuffer
132 && (addr
)address
< (addr
)fBuffer
+ fBufferSize
- sizeof(void*)) {
133 return fRelocationEntries
->PushBack((addr
*)address
);
140 UserDataWriter::Relocate(void *address
)
142 if (!fRelocationEntries
|| !fBuffer
)
144 int32 count
= fRelocationEntries
->Count();
145 for (int32 i
= 0; i
< count
; i
++) {
146 addr
*entry
= fRelocationEntries
->ElementAt(i
);
148 *entry
+= (addr
)address
- (addr
)fBuffer
;