TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Revring, a circular buffer with zero memory waste

12 pointsby codeheroalmost 10 years ago

5 comments

jhallenworldalmost 10 years ago
A conceptually simpler way to do this is to assign one (or more) extra bits in the head an tail pointers. For example, for a 512 entry ring, use 16-bit indices. Whenever you index the ring, and the index with 511 before performing the index.<p><pre><code> Write to ring: ring[511&amp;(head++)] = data Read from ring: data = ring[511&amp;(tail++)] Ring is empty: head == tail Ring is full: tail + 512 == head</code></pre>
dpc_pwalmost 10 years ago
This post is literally about saving one byte, at the cost of being slower and not producer-consumer friendly. Not very interesting.<p>The flag could have been hidden in any other field as a bit or something. Then it could be at least masked with simple AND operation which is usually faster than branching, especially on pipelined CPUs.<p>Update: Quick implementation: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;dpc&#x2F;a194b7784adfa150a450" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;dpc&#x2F;a194b7784adfa150a450</a><p>This fix for concurrency issue is an ugly hack. I&#x27;m not sure if it&#x27;s even correct in this particular scenario, and definitely not proper for anything that would aspire to be good reusable code. I&#x27;d advise this code to push atomicity requirement onto caller. Irqs should have been disabled by calling code.<p>&quot;register&quot; keyword is obsolete. There&#x27;s no point in using it.
kstenerudalmost 10 years ago
I did something similar back in the DOS era for a serial port library: <a href="https:&#x2F;&#x2F;github.com&#x2F;kstenerud&#x2F;DOS-Serial-Library&#x2F;blob&#x2F;master&#x2F;serial.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kstenerud&#x2F;DOS-Serial-Library&#x2F;blob&#x2F;master&#x2F;...</a>
rhaps0dyalmost 10 years ago
You still have a buffer or size X that may store X-Y items. I fail to see the &quot;zero memory waste&quot; (not that it&#x27;s a good tradeoff).
评论 #9741547 未加载
评论 #9741571 未加载
cpralmost 10 years ago
Gosh, all that to save one byte? ;-)
评论 #9742640 未加载
评论 #9741641 未加载