What is memory-pager?
The memory-pager package is a utility for managing and abstracting over pages of memory, allowing for efficient memory allocation and manipulation in a paged manner. It is particularly useful in scenarios where managing large datasets or buffers in a memory-efficient way is crucial, such as in databases or file systems.
What are memory-pager's main functionalities?
Page allocation and retrieval
This feature allows for the allocation and retrieval of memory pages. The code sample demonstrates how to allocate a new page (or retrieve it if it already exists) and then fill it with zeros.
const pager = require('memory-pager')();
let page = pager.get(0, true);
page.fill(0); // Allocate and fill the first page with zeros
Page updates and access
This feature enables updating and accessing data within a page. The code sample shows how to write data to a page and then read it back.
const data = Buffer.from('hello world');
page.set(data, 0); // Write data to the beginning of the page
const readData = page.get(0, data.length); // Read data from the page
Other packages similar to memory-pager
random-access-memory
Similar to memory-pager, random-access-memory provides an abstraction for memory storage, allowing for random access reads and writes. The key difference is that random-access-memory focuses on a single buffer abstraction, whereas memory-pager manages multiple pages of memory.
Access memory using small fixed sized buffers instead of allocating a huge buffer.
Useful if you are implementing sparse data structures (such as large bitfield).
npm install memory-pager
Usage
var pager = require('paged-memory')
var pages = pager(1024)
var page = pages.get(10)
console.log(page.offset)
console.log(page.buffer)
API
Create a new pager. pageSize
defaults to 1024
.
var page = pages.get(pageNumber, [noAllocate])
Get a page. The page will be allocated at first access.
Optionally you can set the noAllocate
flag which will make the
method return undefined if no page has been allocated already
A page looks like this
{
offset: byteOffset,
buffer: bufferWithPageSize
}
pages.set(pageNumber, buffer)
Explicitly set the buffer for a page.
pages.updated(page)
Mark a page as updated.
pages.lastUpdate()
Get the last page that was updated.
var buf = pages.toBuffer()
Concat all pages allocated pages into a single buffer
License
MIT