<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 10 May 2026 05:46:23 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>a3f - 2017-03-01 15:56:29</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>As Some Guy said, the code doesn't have to be racy. Here's an example of using VM_FLAGS_OVERWRITE:
&lt;br /&gt;
&lt;br /&gt;&lt;a href="https://github.com/a3f/libvas/blob/master/mach/ringbuf.c"&gt;https://github.com/a3f/libvas/blob/master/mach/ringbuf.c&lt;/a&gt;</description><guid isPermaLink="true">667935bdd794e6785932cf03fe370d7c</guid><pubDate>Wed, 01 Mar 2017 15:56:29 GMT</pubDate></item><item><title>Some Guy - 2014-10-06 14:38:48</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>actually vm_remap has acces to VM_FLAGS_OVERWRITE and will do what you want, I just tested it does the right thing (in the 'flags' arguments) and is non racy.
&lt;br /&gt;</description><guid isPermaLink="true">dc50167848651979412515272b7ad6bb</guid><pubDate>Mon, 06 Oct 2014 14:38:48 GMT</pubDate></item><item><title>Magnus Reftel - 2012-02-06 06:42:21</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>&lt;div class="blogcommentquote"&gt;&lt;div class="blogcommentquoteinner"&gt;Instead, you either have to complicate things with multiple reads, or read into a separate buffer and then copy the memory over.&lt;/div&gt;&lt;/div&gt;
&lt;br /&gt;Well, you could of course also set up a  scatter/gather operation via readv. Still, it's nowhere near as cool as the mirroring trick.</description><guid isPermaLink="true">efe3cd05cbc03d9dab27a69ed5d9356e</guid><pubDate>Mon, 06 Feb 2012 06:42:21 GMT</pubDate></item><item><title>mikeash - 2012-02-04 02:15:01</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>&lt;b&gt;Jean-Daniel:&lt;/b&gt; Nifty. It seems unfortunate that vm_remap doesn't have access to those flags. I'm not sure whether your version or mine is better or less complicated. Good to know other ways of doing it, anyway.
&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;Rich Pollock:&lt;/b&gt; Having a coffee API in mach would simplify some things, certainly. Fixed the typo, thanks.</description><guid isPermaLink="true">c1b3cac9a2f6be185ff0d32f77f38f41</guid><pubDate>Sat, 04 Feb 2012 02:15:01 GMT</pubDate></item><item><title>Rich Pollock - 2012-02-03 23:28:45</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>$article ~= s/locatino/location/
&lt;br /&gt;
&lt;br /&gt;Otherwise, it sounds like passing 0 as the fifth argument of vm_remap forces it to use a fancy (if fictitious) coffee rather than choosing its own address.</description><guid isPermaLink="true">87e70796109c2ee87e2aa6f245ebcb82</guid><pubDate>Fri, 03 Feb 2012 23:28:45 GMT</pubDate></item><item><title>Jean-Daniel - 2012-02-03 20:43:08</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>@Mike: You're right about mmap implemented above mach API. I dislike too having to use the filesystem for such task, and before your comment, I didn't realized mmap was based on mach.
&lt;br /&gt;
&lt;br /&gt;But knowing that, I just managed to write a working mach based version using the following functions:
&lt;br /&gt;- vm_allocate(capacity*2, anywhere) to create a large block.
&lt;br /&gt;- vm_allocate(capacity, VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE) to create a region in the first half of the block.
&lt;br /&gt;- make_memory_entry() to get an port representing this region, and vm_map(VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE) to remap it in the second half of the block.
&lt;br /&gt;</description><guid isPermaLink="true">0c8072080cd6bc8554f4800e71b54124</guid><pubDate>Fri, 03 Feb 2012 20:43:08 GMT</pubDate></item><item><title>John McLaughlin - 2012-02-03 20:06:46</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>Thanks Mike,
&lt;br /&gt;
&lt;br /&gt;As usual a very nicely done article on an interesting problem.
&lt;br /&gt;
&lt;br /&gt;-John</description><guid isPermaLink="true">89a1b95a146cde9a1c7befcad5ceba60</guid><pubDate>Fri, 03 Feb 2012 20:06:46 GMT</pubDate></item><item><title>mikeash - 2012-02-03 17:05:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>That's interesting, I'll have to see how mmap is implemented that lets it atomically replace an existing mapping, since as far as I know it's implemented on top of the mach APIs. I dislike how the POSIX version requires hitting the filesystem, but obviously that's a better choice if you can't assume the existence of mach.</description><guid isPermaLink="true">e3ae1400023ca119f869ba1d5484647a</guid><pubDate>Fri, 03 Feb 2012 17:05:07 GMT</pubDate></item><item><title>Jean-Daniel - 2012-02-03 16:57:34</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2012-02-03-ring-buffers-and-mirrored-memory-part-i.html#comments</link><description>While using mach API is fun, it's also good to know that this trick can also be written in a more portable way using only POSIX functions (for instance mmap), and moreover it does not suffer from the race condition you have using the mach API.
&lt;br /&gt;
&lt;br /&gt;An implementation using mmap can be found here: 
&lt;br /&gt;
&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/Circular_buffer#Optimized_POSIX_Implementation"&gt;http://en.wikipedia.org/wiki/Circular_buffer#Optimized_POSIX_Implementation&lt;/a&gt;
&lt;br /&gt;</description><guid isPermaLink="true">4ca35eca888b22c868e40f87eb92b5a1</guid><pubDate>Fri, 03 Feb 2012 16:57:34 GMT</pubDate></item></channel></rss>
