GithubHelp home page GithubHelp logo

lua-mmapfile's Introduction

Lua-mmapfile - A simple interface to mmap

Unix Build Status Windows Build status Coverage Status

1. What?

mmapfile uses mmap on unix and MapViewOfFile on Windows to provide a way of quickly storing and loading data that's already in some kind of in-memory binary format.

create creates a new file and maps new memory to that file. You can then write to the memory to write to the file.

open opens an existing file and maps its contents to memory, returning a pointer to the memory and the length of the file.

close syncs memory to the file, closes the file, and deletes the mapping between the memory and the file.

malloc maps anonymous memory (not mapped to a file), and so acts like malloc. The advantage is that we can get memory above 4G, but the BIG disadvantage is that mmap is not a high-perfomance allocator. Only use this infrequently for big blocks of memory. It's a nasty hack.

free frees memory mmapped by malloc

The "gc" variants of create, open and malloc (gccreate, gcopen and gcmalloc) set up a garbage collection callback for the pointer so that the file is correctly closed when the pointer is no longer referenced. Not appropriate if you might be storing the pointer in C, referencing it from unmanaged memory, or casting it to another type!

All memory is mapped above 4G to try to keep away from the memory space LuaJIT uses.

2. How?

local ffi = require"ffi"

ffi.cdef"struct test { int a; double b; };"

local mmapfile = require"mmapfile"

local ptr1 = mmapfile.gccreate("mmapfile-test", 1, "struct test")
ptr1.a = 1
ptr1.b = 1.5
ptr1 = nil
collectgarbage()

local ptr2, size = mmapfile.gcopen("mmapfile-test", "struct test")
assert(size == 1)
assert(ptr2.a == 1)
assert(ptr2.b == 1.5)

For more details make doc or ldoc lua --all.

3. Requirements

4. Issues

  • Should probably have an option for directly mapping existing memory, but I don't know enough about page boundaries.

lua-mmapfile's People

Contributors

geoffleyland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

sysnajar rikxqd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.