<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">

    <!-- Enable responsiveness on mobile devices-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">

    <title>Typed Chronicles</title>

    <!-- CSS -->
    <!-- <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/favicon.png" /> -->
    <link rel="shortcut icon" href="/favicon.ico" />
    <!-- <link rel="stylesheet" href="https:&#x2F;&#x2F;vincenthz.github.io&#x2F;print.css" media="print"> -->
    <!-- <link rel="stylesheet" href="https:&#x2F;&#x2F;vincenthz.github.io&#x2F;normalize.css"> -->
    <link rel="stylesheet" href="https:&#x2F;&#x2F;vincenthz.github.io&#x2F;typed.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=PT+Sans:400,400italic,700|Abril+Fatface">

    

    

    

    

    <script>function toggle_darkmode() { document.querySelector('body').classList.toggle('dark') }
    </script>

    
    
</head>

<body>
    <div id="page-outer">

        <div id="page">
            
<header>
  <h1>Typed Chronicles</h1>
<p class="lead">Vincent Hanquez's adventures</p>
<!-- <a href="https:&#x2F;&#x2F;vincenthz.github.io">Typed Chronicles</a> -->
<nav id="nav-site">
    <ul>
        <li><a class="icon" href="https:&#x2F;&#x2F;vincenthz.github.io"><img src="/icons/home.svg"></a> </li>
        <li><a class="icon" href="https:&#x2F;&#x2F;vincenthz.github.io&#x2F;rss.xml"><img src="/icons/rss.svg"></a>
        </li>
        <li><a class="icon" href="https://github.com/vincenthz" target="_blank"><img src="/icons/gh.svg"></a></li>
        <li><a class="icon" href="https://twitter.com/vincenthz" target="_blank"><img src="/icons/tw.svg"></a></li>
    </ul>
</nav>

</header>


            <main>
                
<h1 class="page-title">unix memory</h1>
<span class="post-date">Published on 2014-02-25</span>
<p>On unix system, we get access to syscalls that maps files or devices into
memory. The main syscall is mmap, but there's also some others syscalls in the
same family to handle mapped memories like mlock, munlock, mprotect, madvise,
msync.</p>
<span id="continue-reading"></span>
<p>Some limited mmap access is available through the
<a href="http://hackage.haskell.org/package/mmap">mmap</a> or
<a href="http://hackage.haskell.org/package/bytestring-mmap">bytestring-mmap</a> packages,
but both provide a high level access to those API.</p>
<p>To the rescue, I've released
<a href="http://hackage.haskell.org/package/unix-memory">unix-memory</a>.  This provide
low level access to all those syscalls. In some place, the API presented is
slightly better than the raw API.</p>
<p>This package is supposed to be ephemeral; The goal is to fold this package to
the venerable <a href="http://hackage.haskell.org/package/unix">unix</a> package when this
becomes less experimental, more stable and is known to work on different
unixoid platforms.  If and when this happens, then this package will just
provide compatibility for old versions and eventually be deprecated.</p>
<p>Manipulating memory is unsafe in general, so don't expect any safety from this
package, by design. Also if you don't know what you're doing, don't use those
APIs; It's difficult to get right.</p>
<p>But it also allow interesting patterns when you cooperate with the operating system
to efficiently map files, and devices as virtual memory.</p>
<p>A simple example opening the &quot;/dev/zero&quot; device first memory page, and reading 4096 bytes from it:</p>
<pre style="background-color:#2b303b;">
<code class="language-{.haskell .numberLines}" data-lang="{.haskell .numberLines}"><span style="color:#c0c5ce;">import System.Posix.IO
import System.Posix.Memory
import Control.Monad
import Control.Exception (bracket)
import Foreign.C.Types
import Foreign.Storable

bracket (openFd &quot;/dev/zero&quot; ReadWrite Nothing defaultFileFlags) closeFd $ \fd -&gt;
  bracket (memoryMap Nothing 4096 [MemoryProtectionRead] MemoryMapPrivate (Just fd) 0)
          (\mem -&gt; memoryUnmap mem 4096)
          (\mem -&gt; mapM (peekElemOff mem) [0..4095])
</span></code></pre>

            </main>
        </div>

        <footer>
            <div class="inner">
                <div class="about-short">
                    <h2><a href="/about/">About me</a></h2>
                    <p>I've been developping software for the last 20 years, in many different languages and many
                        different projects.
                        My main interests are currently cryptography, security and typed programming</p>
                    <p>I'm the director of <a href="https://typed.io/">Typed IO</a>, a software contracting company</p>
                </div>
                <div class="social">
                    <a class="icon" href="https://github.com/vincenthz" target="_blank"><img src="/icons/gh.svg"></a>
                    <a class="icon" href="https://twitter.com/vincenthz" target="_blank"><img src="/icons/tw.svg"></a>
                    <a class="icon" href="https:&#x2F;&#x2F;vincenthz.github.io&#x2F;rss.xml"><img src="/icons/rss.svg"></a>
                </div>
                <span>&copy; 2014-2021 Vincent Hanquez</span>
            </div>
        </footer>
    </div>

</body>


</html>
