Skip to content

use memory mapped files for the peer buffer - #12

Merged
arvidn merged 4 commits into
bittorrent:masterfrom
arvidn:memory-mapped-file
Aug 14, 2016
Merged

arvidn merged 4 commits into
bittorrent:masterfrom
arvidn:memory-mapped-file

Conversation

@arvidn

@arvidn arvidn commented Aug 13, 2016

Copy link
Copy Markdown
Contributor

to preserve state between restarts with some simple unit tests

Comment thread mapped_file.hpp
enum { header_size = 16 };
mapped_vector(char const* file, size_t const size)
: m_map(file, header_size + size * sizeof(T))
, m_size(*static_cast<size_t*>(m_map.data()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reinterpret_cast since you're transmuting the data rather than downcasting.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I double checked the C++ spec and static_cast can indeed be used to convert void* to T*. Learn something new every day.

@arvidn

arvidn commented Aug 13, 2016

Copy link
Copy Markdown
Contributor Author

one questionable thing, I think, is that the header size is not technically guaranteed to be a multiple of alignof(T) in mapped_vector. That should actually be pretty easy to fix.

Comment thread main.cpp
return;
}

this->threadid = threadid;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the thread id around a bit here, to have it be available in the constructor, in order to create unique names for the mapped files

@ssiloti

ssiloti commented Aug 13, 2016

Copy link
Copy Markdown
Contributor

lgtm, it would probably be a good idea to check the last modified time of the file and discard it if it is too old.

@arvidn

arvidn commented Aug 13, 2016

Copy link
Copy Markdown
Contributor Author

Yeah, I was thinking about that too. I got a bit lazy and thought that the user could always just delete the file :)
maybe in another patch

@arvidn

arvidn commented Aug 13, 2016

Copy link
Copy Markdown
Contributor Author

ok, fixed the alignment of T too

Comment thread mapped_file.hpp Outdated
enum { header_size = 16 };
// the header must be large enough to make the first element still be
// correctly aligned
static constexpr size_t header_size = std::max(size_t(16), alignof(T));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::max isn't constexpr in C++11, I don't think the qualifier is necessary in this case.

@ssiloti

ssiloti commented Aug 14, 2016

Copy link
Copy Markdown
Contributor

lgtm

@arvidn
arvidn force-pushed the memory-mapped-file branch from 771333b to cb2d2cf Compare August 14, 2016 15:34
@arvidn

arvidn commented Aug 14, 2016

Copy link
Copy Markdown
Contributor Author

rebased on top of master again

@arvidn
arvidn merged commit dded993 into bittorrent:master Aug 14, 2016
@arvidn
arvidn deleted the memory-mapped-file branch August 14, 2016 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants