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.

Writing a Simple Garbage Collector in C

142 pointsby mbroncanoover 5 years ago

8 comments

pcwaltonover 5 years ago
Issues I found at a glance:<p>1. This uses unsigned int for the chunk size, so the allocator will overflow on requests of 4GB or more despite taking a size_t. It seems that this is 32-bit only.<p>2. Even on 32-bit, the num_units calculation will overflow if you request (for example) 0xffffffff bytes of memory instead of returning an error.<p>3. None of this is thread-safe. It needs a global mutex lock.<p>4. EBP cannot be relied upon to yield anything sensible with -fomit-frame-pointer, which is common on 32-bit x86 as it brings the number of GPRs from 6 to 7.
评论 #21795726 未加载
评论 #21795011 未加载
评论 #21794851 未加载
stevefan1999over 5 years ago
This really reminds me of a project called TinyGC&#x2F;tgc[0], made by Daniel Holden who is currently a Ubisoft researcher. I have also tried his Cello[1] framework for C99 which also incorporated a garbage collector similar to tgc. Cello is pretty fun to use but the syntax was still limiting.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;tgc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;tgc</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;Cello" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;orangeduck&#x2F;Cello</a>
RodgerTheGreatover 5 years ago
As another example, here&#x27;s a simple GC I wrote quite a while ago in Forth: <a href="https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Mako&#x2F;blob&#x2F;master&#x2F;lib&#x2F;Algorithms&#x2F;Garbage.fs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JohnEarnest&#x2F;Mako&#x2F;blob&#x2F;master&#x2F;lib&#x2F;Algorith...</a><p>This one is a precise, copying GC, with a reserved arena for persistent references into garbage-collected objects (in addition to the stacks). Pointers are identified by reserving a high bit in words.
评论 #21796245 未加载
giomasceover 5 years ago
This GC does not probably survive to pointer scrambling. If believe in C I can validly do something like<p><pre><code> int *ptr = ...; intptr_t iptr = (intptr_t) ptr; ptr = NULL; iptr ^= MAGIC; &#x2F;&#x2F; do something else ptr = (int*) (iptr ^ MAGIC); </code></pre> At the end of this ptr is again a valid pointer to the same thing it was pointing at the beginning. However, if a GC scan will happen during the &quot;do something else&quot; block, it won&#x27;t see the actual pointer value and it might free the pointed object.<p>I don&#x27;t think it is possible to write a GC for C if the program is allowed to do this kind of things, because there is too little structure at runtime. And in any case, this kind of GC is not a GC &quot;for C&quot;, as it heavily relies on knowing the compiler internals.<p>EDIT: Re-reading, I didn&#x27;t mean to be harsh. This is still interesting to read, I am just noting a weakness that is not mentioned in the article. BTW, I know that glibc actually does some pointer scrambling like I said to mitigate some types of attack.
评论 #21795279 未加载
INTPenisover 5 years ago
I had a co-worker who was a sysadmin and was writing a GC on his own time. Just for fun. The guy was seriously over qualified but I guess he chose to work with simple stuff for his own sanity.
评论 #21795390 未加载
stevedekorteover 5 years ago
It&#x27;s great to see tutorials like this. FWIW, here&#x27;s a tiny incremental (baker treadmill) collector[0] library I wrote. It was used in the implementation of the Io programming language.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;stevedekorte&#x2F;garbagecollector" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;stevedekorte&#x2F;garbagecollector</a>
jimbob45over 5 years ago
Would love to see a Windows-based version of this article. No sbrk() or mmap() in Windows makes the implementation a bit different.
评论 #21794589 未加载
评论 #21795584 未加载
matheusmoreiraover 5 years ago
Another great article on the subject:<p><a href="http:&#x2F;&#x2F;journal.stuffwithstuff.com&#x2F;2013&#x2F;12&#x2F;08&#x2F;babys-first-garbage-collector&#x2F;" rel="nofollow">http:&#x2F;&#x2F;journal.stuffwithstuff.com&#x2F;2013&#x2F;12&#x2F;08&#x2F;babys-first-gar...</a>