Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--example/use_cases.cpp89
-rw-r--r--include/afio/revision.hpp6
-rw-r--r--include/afio/v2.0/algorithm/mapped_span.hpp25
m---------include/afio/v2.0/quickcpplib0
4 files changed, 110 insertions, 10 deletions
diff --git a/example/use_cases.cpp b/example/use_cases.cpp
index 9cf22891..cd473922 100644
--- a/example/use_cases.cpp
+++ b/example/use_cases.cpp
@@ -189,6 +189,95 @@ void scatter_write()
//! [scatter_write]
}
+void malloc1()
+{
+ //! [malloc1]
+ namespace afio = AFIO_V2_NAMESPACE;
+
+ // Call whatever the equivalent to mmap() is on this platform to fetch
+ // new private memory backed by the swap file. This will be the system
+ // all bits zero page mapped into each page of the allocation. Only on
+ // first write will a page fault allocate a real zeroed page for that
+ // page.
+ afio::map_handle mh = afio::map(4096).value();
+
+ // Fill the newly allocated memory with 'a' C style. For each first write
+ // to a page, it will be page faulted into a private page by the kernel.
+ afio::byte *p = mh.address();
+ size_t len = mh.length();
+ memset(p, 'a', len);
+
+ // Tell the kernel to throw away the contents of any whole pages
+ // by resetting them to the system all zeros page. These pages
+ // will be faulted into existence on first write.
+ mh.zero_memory({ mh.address(), mh.length() }).value();
+
+ // Do not write these pages to the swap file (flip dirty bit to false)
+ mh.do_not_store({mh.address(), mh.length()}).value();
+
+ // Fill the memory with 'b' C++ style, probably faulting new pages into existence
+ afio::algorithm::mapped_span<char> p2(mh);
+ std::fill(p2.begin(), p2.end(), 'b');
+
+ // Kick the contents of the memory out to the swap file so it is no longer cached in RAM
+ // This also remaps the memory to reserved address space.
+ mh.decommit({mh.address(), mh.length()}).value();
+
+ // Map the swap file stored edition back into memory, it will fault on
+ // first read to do the load back into the kernel page cache.
+ mh.commit({ mh.address(), mh.length() }).value();
+
+ // And rather than wait until first page fault read, tell the system we are going to
+ // use this region soon. Most systems will begin an asynchronous population of the
+ // kernel page cache immediately.
+ afio::map_handle::buffer_type pf[] = { { mh.address(), mh.length() } };
+ mh.prefetch(pf).value();
+
+
+ // You can actually save yourself some time and skip manually creating map handles.
+ // Just construct a mapped_span directly, this creates an internal map_handle instance,
+ // so memory is released when the span is destroyed
+ afio::algorithm::mapped_span<float> f(1000); // 1000 floats, allocated used mmap()
+ std::fill(f.begin(), f.end(), 1.23f);
+ //! [malloc1]
+}
+
+void malloc2()
+{
+ //! [malloc2]
+ namespace afio = AFIO_V2_NAMESPACE;
+
+ // Create 4Kb of anonymous shared memory. This will persist
+ // until the last handle to it in the system is destructed.
+ // You can fetch a path to it to give to other processes using
+ // sh.current_path()
+ afio::section_handle sh = afio::section(4096).value();
+
+ {
+ // Map it into memory, and fill it with 'a'
+ afio::algorithm::mapped_span<char> ms1(sh);
+ std::fill(ms1.begin(), ms1.end(), 'a');
+
+ // Destructor unmaps it from memory
+ }
+
+ // Map it into memory again, verify it contains 'a'
+ afio::algorithm::mapped_span<char> ms1(sh);
+ assert(ms1[0] == 'a');
+
+ // Map a *second view* of the same memory
+ afio::algorithm::mapped_span<char> ms2(sh);
+ assert(ms2[0] == 'a');
+
+ // The addresses of the two maps are unique
+ assert(ms1.data() != ms2.data());
+
+ // Yet writes to one map appear in the other map
+ ms2[0] = 'b';
+ assert(ms1[0] == 'b');
+ //! [malloc2]
+}
+
void map_file()
{
//! [map_file]
diff --git a/include/afio/revision.hpp b/include/afio/revision.hpp
index 0318f298..594fb459 100644
--- a/include/afio/revision.hpp
+++ b/include/afio/revision.hpp
@@ -1,4 +1,4 @@
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
-#define AFIO_PREVIOUS_COMMIT_REF 3c096cba2f09986162490ea4cd0ce683af70cebd
-#define AFIO_PREVIOUS_COMMIT_DATE "2018-04-15 12:18:15 +00:00"
-#define AFIO_PREVIOUS_COMMIT_UNIQUE 3c096cba
+#define AFIO_PREVIOUS_COMMIT_REF 8d96200572d8224dd460afe8cc3b7fd55227fa9c
+#define AFIO_PREVIOUS_COMMIT_DATE "2018-04-16 15:39:11 +00:00"
+#define AFIO_PREVIOUS_COMMIT_UNIQUE 8d962005
diff --git a/include/afio/v2.0/algorithm/mapped_span.hpp b/include/afio/v2.0/algorithm/mapped_span.hpp
index 175eb839..1ab2c292 100644
--- a/include/afio/v2.0/algorithm/mapped_span.hpp
+++ b/include/afio/v2.0/algorithm/mapped_span.hpp
@@ -67,11 +67,12 @@ namespace algorithm
public:
//! Default constructor
constexpr mapped_span() {} // NOLINT
- /*! Create a view of new memory.
-
- \param length The number of items to map.
- \param _flag The flags to pass to `map_handle::map()`.
- */
+
+ /*! Create a view of new memory.
+
+ \param length The number of items to map.
+ \param _flag The flags to pass to `map_handle::map()`.
+ */
explicit mapped_span(size_type length, section_handle::flag _flag = section_handle::flag::readwrite)
: _mapping(map_handle::map(length * sizeof(T), _flag).value())
{
@@ -95,14 +96,24 @@ namespace algorithm
byteoffset, sh, (length == (size_type) -1) ? 0 : length * sizeof(T), _flag)) // NOLINT
{
}
+ /*! Construct a mapped view of the given map handle.
+
+ \param mh The map handle to use.
+ \param length The number of items to map, use -1 to mean the length of the map handle divided by `sizeof(T)`.
+ \param byteoffset The byte offset into the map handle, this does not need to be a multiple of the page size.
+ */
+ explicit mapped_span(map_handle &mh, size_type length = (size_type) -1, extent_type byteoffset = 0) // NOLINT
+ : span<T>(reinterpret_cast<T *>(mh.address() + byteoffset), (length == (size_type) -1) ? ((mh.length() - byteoffset) / sizeof(T)) : length) // NOLINT
+ {
+ }
/*! Construct a mapped view of the given mapped file handle.
\param mfh The mapped file handle to use as the data source for creating the map.
\param length The number of items to map, use -1 to mean the length of the section handle divided by `sizeof(T)`.
\param byteoffset The byte offset into the mapped file handle, this does not need to be a multiple of the page size.
*/
- explicit mapped_span(mapped_file_handle &mfh, size_type length = (size_type) -1, extent_type byteoffset = 0) // NOLINT
- : span<T>(reinterpret_cast<T *>(mfh.address() + byteoffset), (length == (size_type) -1) ? (mfh.maximum_extent().value() / sizeof(T)) : length) // NOLINT
+ explicit mapped_span(mapped_file_handle &mfh, size_type length = (size_type) -1, extent_type byteoffset = 0) // NOLINT
+ : span<T>(reinterpret_cast<T *>(mfh.address() + byteoffset), (length == (size_type) -1) ? ((mfh.maximum_extent().value() - byteoffset) / sizeof(T)) : length) // NOLINT
{
}
};
diff --git a/include/afio/v2.0/quickcpplib b/include/afio/v2.0/quickcpplib
-Subproject fae982283648633c89790c42cd4e661cc895dfd
+Subproject 2f20b924a9900a6e82928888a513eb643735996