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.

Memory Allocators 101 – Write a simple memory allocator (2016)

271 pointsby DonbunEf7over 6 years ago

11 comments

kevin_thibedeauover 6 years ago
FreeRTOS also has a nice collection of different heap implementations with varying degrees of sophistication. They are a good read for those interested in such things.<p><a href="https:&#x2F;&#x2F;www.freertos.org&#x2F;a00111.html" rel="nofollow">https:&#x2F;&#x2F;www.freertos.org&#x2F;a00111.html</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;aws&#x2F;amazon-freertos&#x2F;tree&#x2F;master&#x2F;lib&#x2F;FreeRTOS&#x2F;portable&#x2F;MemMang" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;aws&#x2F;amazon-freertos&#x2F;tree&#x2F;master&#x2F;lib&#x2F;FreeR...</a>
评论 #18182959 未加载
q3kover 6 years ago
Please don&#x27;t write custom memory allocators for production code without seriously considering the security implications of doing so. The glibc allocator has benefited from years of security hardening against very creative attack vectors.<p>You don&#x27;t want a simple double-free to lead to an RCE bug, do you...
评论 #18184610 未加载
pjc50over 6 years ago
K&amp;R includes an example memory allocator as well: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;13159564&#x2F;explain-this-implementation-of-malloc-from-the-kr-book" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;13159564&#x2F;explain-this-im...</a><p>To me, the K&amp;R one seems much less readable and also doesn&#x27;t include the global malloc lock, since even the updated edition of K&amp;R predates standardised threading.<p>I note it also does the &quot;bp = (Header *)ap - 1;&quot; trick, so if that&#x27;s undefined behaviour then it&#x27;s a good example of how hard it is to write C without relying on UB.
01100011over 6 years ago
Good timing. I literally had to do this on a whiteboard in an interview about 11 hours ago.<p>Interesting things to consider: Fragmentation prevention, real-time performance, minimizing locking(lock-free techniques, or per-thread free lists), and reusing the freed memory to contain the free list structure. I basically started out whiteboarding what the article lays out and by the end of the interview realized everything wrong with it. It&#x27;s a good starting point though.
评论 #18182838 未加载
DivisionSolover 6 years ago
I gotta say this is a very topical read. Was messing around last week or so trying to learn some x86 Assembly from scratch (Linux subsystem on Windows,) and memory was very much a sticking point. Seeing this is helping me grok just what is sorta going on, which is the best way for me to learn I think.
slashvar2701over 6 years ago
While I strongly support the idea that no one should write a generic allocator in production, writing it as an exercise is a very good idea.<p>The article looks at lot like the tutorial I wrote a long time ago ... (Every now and then, I see my old PDF in post about implementing allocators, which is disturbing since I wrote it in a hurry as a quick support for a lecture and I found it very poorly written ... )<p>I think it&#x27;s interesting to note that using sbrk(2) is a bit deprecated, it&#x27;s way easier to implement malloc(3) using mmap(2) ...<p>There&#x27;s also better ways to handle pointers arithmetic and list management. Someday I&#x27;ll put only cleaner version only ... Someday ...
apankratover 6 years ago
It&#x27;s worth noting that<p><pre><code> realloc(ptr, 0) </code></pre> behavior is undefined. The vast majority of modern C libraries will implement it as<p><pre><code> return free(ptr), NULL; </code></pre> and it will be documented on man pages as such, but there <i>are</i> systems where this will be equivalent to<p><pre><code> return free(ptr), malloc(0); </code></pre> Furthermore, in theory, this is also permitted:<p><pre><code> return NULL; </code></pre> so as tempting as realloc() might be as a single override for implementing custom allocators, there are some worms.
评论 #18183608 未加载
rv11over 6 years ago
Knuth in one of his volumes ( I think vol I) has described this very nicely. way better than what was in K&amp;R.
paavoovaover 6 years ago
<p><pre><code> header = (struct header_t*)block - 1; </code></pre> Isn&#x27;t this UB?
评论 #18182621 未加载
评论 #18181812 未加载
评论 #18182137 未加载
anon49124over 6 years ago
Is there any effort to extract ORCA from Pony (which is better than Erlang&#x27;s and Azul&#x27;s C4)?
partycoderover 6 years ago
Good for didactic purposes but in the real world you may want to try an allocator like tcmalloc or jemalloc.
评论 #18183528 未加载
评论 #18182524 未加载
评论 #18182521 未加载
评论 #18183504 未加载