Skip to content

Commit

Permalink
ByteBuffer: Remove pointer() in favor of data()
Browse files Browse the repository at this point in the history
We had two ways to get the data inside a ByteBuffer. That was silly.
  • Loading branch information
awesomekling committed Sep 30, 2019
1 parent dd696e7 commit 8f45a25
Show file tree
Hide file tree
Showing 30 changed files with 89 additions and 92 deletions.
17 changes: 7 additions & 10 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class ByteBufferImpl : public RefCounted<ByteBufferImpl> {
bool is_empty() const { return !m_size; }
int size() const { return m_size; }

u8* pointer() { return m_data; }
const u8* pointer() const { return m_data; }
u8* data() { return m_data; }
const u8* data() const { return m_data; }

u8* offset_pointer(int offset) { return m_data + offset; }
const u8* offset_pointer(int offset) const { return m_data + offset; }
Expand Down Expand Up @@ -130,11 +130,8 @@ class ByteBuffer {
bool is_empty() const { return !m_impl || m_impl->is_empty(); }
int size() const { return m_impl ? m_impl->size() : 0; }

u8* data() { return pointer(); }
const u8* data() const { return pointer(); }

u8* pointer() { return m_impl ? m_impl->pointer() : nullptr; }
const u8* pointer() const { return m_impl ? m_impl->pointer() : nullptr; }
u8* data() { return m_impl ? m_impl->data() : nullptr; }
const u8* data() const { return m_impl ? m_impl->data() : nullptr; }

u8* offset_pointer(int offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const u8* offset_pointer(int offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
Expand All @@ -146,7 +143,7 @@ class ByteBuffer {
{
if (!m_impl)
return {};
return copy(m_impl->pointer(), m_impl->size());
return copy(m_impl->data(), m_impl->size());
}

// NOTE: trim() does not reallocate.
Expand Down Expand Up @@ -190,7 +187,7 @@ class ByteBuffer {
{
int old_size = size();
grow(size() + data_size);
memcpy(pointer() + old_size, data, data_size);
memcpy(this->data() + old_size, data, data_size);
}

private:
Expand Down Expand Up @@ -249,7 +246,7 @@ inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(int si
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(int size)
{
auto buffer = ::adopt(*new ByteBufferImpl(size));
memset(buffer->pointer(), 0, size);
memset(buffer->data(), 0, size);
return buffer;
}

Expand Down
4 changes: 2 additions & 2 deletions AK/MappedFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class MappedFile {
bool is_valid() const { return m_map != (void*)-1; }
void unmap();

void* pointer() { return m_map; }
const void* pointer() const { return m_map; }
void* data() { return m_map; }
const void* data() const { return m_map; }
size_t size() const { return m_size; }

private:
Expand Down
10 changes: 5 additions & 5 deletions AK/StringBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void StringBuilder::append(const StringView& str)
if (str.is_empty())
return;
will_append(str.length());
memcpy(m_buffer.pointer() + m_length, str.characters_without_null_termination(), str.length());
memcpy(m_buffer.data() + m_length, str.characters_without_null_termination(), str.length());
m_length += str.length();
}

Expand All @@ -30,14 +30,14 @@ void StringBuilder::append(const char* characters, int length)
if (!length)
return;
will_append(length);
memcpy(m_buffer.pointer() + m_length, characters, length);
memcpy(m_buffer.data() + m_length, characters, length);
m_length += length;
}

void StringBuilder::append(char ch)
{
will_append(1);
m_buffer.pointer()[m_length] = ch;
m_buffer.data()[m_length] = ch;
m_length += 1;
}

Expand Down Expand Up @@ -67,14 +67,14 @@ ByteBuffer StringBuilder::to_byte_buffer()

String StringBuilder::to_string()
{
auto string = String((const char*)m_buffer.pointer(), m_length);
auto string = String((const char*)m_buffer.data(), m_length);
clear();
return string;
}

StringView StringBuilder::string_view() const
{
return StringView { (const char*)m_buffer.pointer(), m_length };
return StringView { (const char*)m_buffer.data(), m_length };
}

void StringBuilder::clear()
Expand Down
2 changes: 1 addition & 1 deletion Applications/Downloader/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char** argv)
auto& response = static_cast<const CHttpResponse&>(*job->response());
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job.ptr(), response.code());
//printf("payload:\n");
//printf("%s", response.payload().pointer());
//printf("%s", response.payload().data());
printf("payload was %d bytes\n", response.payload().size());
};

Expand Down
8 changes: 4 additions & 4 deletions Kernel/Devices/PATAChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ void PATAChannel::detect_disks()

ByteBuffer wbuf = ByteBuffer::create_uninitialized(512);
ByteBuffer bbuf = ByteBuffer::create_uninitialized(512);
u8* b = bbuf.pointer();
u16* w = (u16*)wbuf.pointer();
const u16* wbufbase = (u16*)wbuf.pointer();
u8* b = bbuf.data();
u16* w = (u16*)wbuf.data();
const u16* wbufbase = (u16*)wbuf.data();

for (u32 i = 0; i < 256; ++i) {
u16 data = IO::in16(m_io_base + ATA_REG_DATA);
Expand All @@ -228,7 +228,7 @@ void PATAChannel::detect_disks()

kprintf(
"PATAChannel: Name=\"%s\", C/H/Spt=%u/%u/%u\n",
bbuf.pointer() + 54,
bbuf.data() + 54,
cyls,
heads,
spt);
Expand Down
6 changes: 3 additions & 3 deletions Kernel/FileSystem/DiskBackedFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ByteBuffer DiskBackedFS::read_block(unsigned index) const
auto buffer = ByteBuffer::create_uninitialized(block_size());
//kprintf("created block buffer with size %u\n", block_size());
DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
auto* buffer_pointer = buffer.pointer();
auto* buffer_pointer = buffer.data();
bool success = device().read(base_offset, block_size(), buffer_pointer);
ASSERT(success);
ASSERT(buffer.size() == block_size());
Expand All @@ -126,13 +126,13 @@ ByteBuffer DiskBackedFS::read_blocks(unsigned index, unsigned count) const
if (count == 1)
return read_block(index);
auto blocks = ByteBuffer::create_uninitialized(count * block_size());
u8* out = blocks.pointer();
u8* out = blocks.data();

for (unsigned i = 0; i < count; ++i) {
auto block = read_block(index + i);
if (!block)
return nullptr;
memcpy(out, block.pointer(), block.size());
memcpy(out, block.data(), block.size());
out += block_size();
}

Expand Down
38 changes: 19 additions & 19 deletions Kernel/FileSystem/Ext2FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ByteBuffer Ext2FS::read_super_block() const
{
LOCKER(m_lock);
auto buffer = ByteBuffer::create_uninitialized(1024);
bool success = device().read_block(2, buffer.pointer());
bool success = device().read_block(2, buffer.data());
ASSERT(success);
success = device().read_block(3, buffer.offset_pointer(512));
ASSERT(success);
Expand Down Expand Up @@ -79,7 +79,7 @@ const ext2_super_block& Ext2FS::super_block() const
{
if (!m_cached_super_block)
m_cached_super_block = read_super_block();
return *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
return *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
}

const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
Expand All @@ -97,7 +97,7 @@ const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
#endif
m_cached_group_descriptor_table = read_blocks(first_block_of_bgdt, blocks_to_read);
}
return reinterpret_cast<ext2_group_desc*>(m_cached_group_descriptor_table.pointer())[group_index - 1];
return reinterpret_cast<ext2_group_desc*>(m_cached_group_descriptor_table.data())[group_index - 1];
}

bool Ext2FS::initialize()
Expand Down Expand Up @@ -321,10 +321,10 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in

auto dind_block_contents = read_block(e2inode.i_block[EXT2_DIND_BLOCK]);
if (dind_block_new) {
memset(dind_block_contents.pointer(), 0, dind_block_contents.size());
memset(dind_block_contents.data(), 0, dind_block_contents.size());
dind_block_dirty = true;
}
auto* dind_block_as_pointers = (unsigned*)dind_block_contents.pointer();
auto* dind_block_as_pointers = (unsigned*)dind_block_contents.data();

ASSERT(indirect_block_count <= entries_per_block);
for (unsigned i = 0; i < indirect_block_count; ++i) {
Expand All @@ -341,10 +341,10 @@ bool Ext2FS::write_block_list_for_inode(InodeIndex inode_index, ext2_inode& e2in

auto ind_block_contents = read_block(indirect_block_index);
if (ind_block_new) {
memset(ind_block_contents.pointer(), 0, dind_block_contents.size());
memset(ind_block_contents.data(), 0, dind_block_contents.size());
ind_block_dirty = true;
}
auto* ind_block_as_pointers = (unsigned*)ind_block_contents.pointer();
auto* ind_block_as_pointers = (unsigned*)ind_block_contents.data();

unsigned entries_to_write = new_shape.doubly_indirect_blocks - (i * entries_per_block);
if (entries_to_write > entries_per_block)
Expand Down Expand Up @@ -430,7 +430,7 @@ Vector<Ext2FS::BlockIndex> Ext2FS::block_list_for_inode(const ext2_inode& e2inod
callback(array_block_index);
auto array_block = read_block(array_block_index);
ASSERT(array_block);
auto* array = reinterpret_cast<const __u32*>(array_block.pointer());
auto* array = reinterpret_cast<const __u32*>(array_block.data());
unsigned count = min(blocks_remaining, entries_per_block);
for (unsigned i = 0; i < count; ++i) {
if (!array[i]) {
Expand Down Expand Up @@ -646,7 +646,7 @@ ssize_t Ext2FSInode::read_bytes(off_t offset, ssize_t count, u8* buffer, FileDes

int offset_into_block = (bi == first_block_logical_index) ? offset_into_first_block : 0;
int num_bytes_to_copy = min(block_size - offset_into_block, remaining_count);
memcpy(out, block.pointer() + offset_into_block, num_bytes_to_copy);
memcpy(out, block.data() + offset_into_block, num_bytes_to_copy);
remaining_count -= num_bytes_to_copy;
nread += num_bytes_to_copy;
out += num_bytes_to_copy;
Expand Down Expand Up @@ -755,14 +755,14 @@ ssize_t Ext2FSInode::write_bytes(off_t offset, ssize_t count, const u8* data, Fi
} else
block = buffer_block;

memcpy(block.pointer() + offset_into_block, in, num_bytes_to_copy);
memcpy(block.data() + offset_into_block, in, num_bytes_to_copy);
if (bi == last_logical_block_index_in_file && num_bytes_to_copy < block_size) {
int padding_start = new_size % block_size;
int padding_bytes = block_size - padding_start;
#ifdef EXT2_DEBUG
dbgprintf("Ext2FSInode::write_bytes padding last block of file with zero x %u (new_size=%u, offset_into_block=%u, num_bytes_to_copy=%u)\n", padding_bytes, new_size, offset_into_block, num_bytes_to_copy);
#endif
memset(block.pointer() + padding_start, 0, padding_bytes);
memset(block.data() + padding_start, 0, padding_bytes);
}
#ifdef EXT2_DEBUG
dbgprintf("Ext2FSInode::write_bytes: writing block %u (offset_into_block: %u)\n", m_block_list[bi], offset_into_block);
Expand Down Expand Up @@ -799,7 +799,7 @@ bool Ext2FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)

auto buffer = read_entire();
ASSERT(buffer);
auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.pointer());
auto* entry = reinterpret_cast<ext2_dir_entry_2*>(buffer.data());

while (entry < buffer.end_pointer()) {
if (entry->inode != 0) {
Expand Down Expand Up @@ -867,7 +867,7 @@ bool Ext2FSInode::write_directory(const Vector<FS::DirectoryEntry>& entries)

stream.fill_to_end(0);

ssize_t nwritten = write_bytes(0, directory_data.size(), directory_data.pointer(), nullptr);
ssize_t nwritten = write_bytes(0, directory_data.size(), directory_data.data(), nullptr);
return nwritten == directory_data.size();
}

Expand Down Expand Up @@ -1006,7 +1006,7 @@ Ext2FS::BlockIndex Ext2FS::allocate_block(GroupIndex preferred_group_index)

auto bitmap_block = read_block(bgd.bg_block_bitmap);
int blocks_in_group = min(blocks_per_group(), super_block().s_blocks_count);
auto block_bitmap = Bitmap::wrap(bitmap_block.pointer(), blocks_in_group);
auto block_bitmap = Bitmap::wrap(bitmap_block.data(), blocks_in_group);
BlockIndex first_block_in_group = (group_index - 1) * blocks_per_group() + first_block_index();
int first_unset_bit_index = block_bitmap.find_first_unset();
ASSERT(first_unset_bit_index != -1);
Expand Down Expand Up @@ -1136,7 +1136,7 @@ bool Ext2FS::get_inode_allocation_state(InodeIndex index) const
unsigned bit_index = (index_in_group - 1) % inodes_per_group();
auto block = read_block(bgd.bg_inode_bitmap);
ASSERT(block);
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_group());
auto bitmap = Bitmap::wrap(block.data(), inodes_per_group());
return bitmap.get(bit_index);
}

Expand All @@ -1149,7 +1149,7 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
unsigned bit_index = (index_in_group - 1) % inodes_per_group();
auto block = read_block(bgd.bg_inode_bitmap);
ASSERT(block);
auto bitmap = Bitmap::wrap(block.pointer(), inodes_per_group());
auto bitmap = Bitmap::wrap(block.data(), inodes_per_group());
bool current_state = bitmap.get(bit_index);
#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: set_inode_allocation_state(%u) %u -> %u\n", inode_index, current_state, new_state);
Expand All @@ -1165,7 +1165,7 @@ bool Ext2FS::set_inode_allocation_state(InodeIndex inode_index, bool new_state)
ASSERT(success);

// Update superblock
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: superblock free inode count %u -> %u\n", sb.s_free_inodes_count, sb.s_free_inodes_count - 1);
#endif
Expand Down Expand Up @@ -1212,7 +1212,7 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
#endif
auto block = read_block(bgd.bg_block_bitmap);
ASSERT(block);
auto bitmap = Bitmap::wrap(block.pointer(), blocks_per_group());
auto bitmap = Bitmap::wrap(block.data(), blocks_per_group());
bool current_state = bitmap.get(bit_index);
#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: block %u state: %u -> %u\n", block_index, current_state, new_state);
Expand All @@ -1228,7 +1228,7 @@ bool Ext2FS::set_block_allocation_state(BlockIndex block_index, bool new_state)
ASSERT(success);

// Update superblock
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.pointer());
auto& sb = *reinterpret_cast<ext2_super_block*>(m_cached_super_block.data());
#ifdef EXT2_DEBUG
dbgprintf("Ext2FS: superblock free block count %u -> %u\n", sb.s_free_blocks_count, sb.s_free_blocks_count - 1);
#endif
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/FileDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size)
if (size < temp_buffer.size())
return -1;

memcpy(buffer, temp_buffer.pointer(), temp_buffer.size());
memcpy(buffer, temp_buffer.data(), temp_buffer.size());
return stream.offset();
}

Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/ProcFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ static ssize_t write_sys_string(InodeIdentifier inode_id, const ByteBuffer& data
{
auto* lockable_string = reinterpret_cast<Lockable<String>*>(variable.address);
LOCKER(lockable_string->lock());
lockable_string->resource() = String((const char*)data.pointer(), data.size());
lockable_string->resource() = String((const char*)data.data(), data.size());
}
variable.notify();
return data.size();
Expand Down
2 changes: 1 addition & 1 deletion Kernel/FileSystem/VirtualFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ KResultOr<NonnullRefPtr<Custody>> VFS::resolve_path(StringView path, Custody& ba

// FIXME: We should limit the recursion here and return -ELOOP if it goes to deep.
auto symlink_target = resolve_path(
StringView(symlink_contents.pointer(),
StringView(symlink_contents.data(),
symlink_contents.size()),
current_parent,
parent_custody,
Expand Down
2 changes: 1 addition & 1 deletion Kernel/KSyms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void load_ksyms_from_data(const ByteBuffer& buffer)
{
ksym_lowest_address = 0xffffffff;
ksym_highest_address = 0;
auto* bufptr = (const char*)buffer.pointer();
auto* bufptr = (const char*)buffer.data();
auto* start_of_name = bufptr;
u32 address = 0;

Expand Down
4 changes: 2 additions & 2 deletions Kernel/Net/NetworkAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void NetworkAdapter::send(const MACAddress& destination, const ARPPacket& packet
{
int size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(ARPPacket);
auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
auto* eth = (EthernetFrameHeader*)buffer.pointer();
auto* eth = (EthernetFrameHeader*)buffer.data();
eth->set_source(mac_address());
eth->set_destination(destination);
eth->set_ether_type(EtherType::ARP);
Expand All @@ -72,7 +72,7 @@ void NetworkAdapter::send_ipv4(const MACAddress& destination_mac, const IPv4Addr
{
size_t size_in_bytes = sizeof(EthernetFrameHeader) + sizeof(IPv4Packet) + payload_size;
auto buffer = ByteBuffer::create_zeroed(size_in_bytes);
auto& eth = *(EthernetFrameHeader*)buffer.pointer();
auto& eth = *(EthernetFrameHeader*)buffer.data();
eth.set_source(mac_address());
eth.set_destination(destination_mac);
eth.set_ether_type(EtherType::IPv4);
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/NetworkTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void handle_icmp(const EthernetFrameHeader& eth, const IPv4Packet& ipv4_packet)
(u16)request.sequence_number);
size_t icmp_packet_size = ipv4_packet.payload_size();
auto buffer = ByteBuffer::create_zeroed(icmp_packet_size);
auto& response = *(ICMPEchoPacket*)buffer.pointer();
auto& response = *(ICMPEchoPacket*)buffer.data();
response.header.set_type(ICMPType::EchoReply);
response.header.set_code(0);
response.identifier = request.identifier;
Expand Down
Loading

0 comments on commit 8f45a25

Please sign in to comment.