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: A glib-like multi-platform C library

48 pointsby danny0zabout 3 years ago

3 comments

camel-cdrabout 3 years ago
Whenever I see such libraries, the first thing I check out is their PRNG implementation, and this one must be the worst I&#x27;ve seen so far. (The rest of the library is probably quite good, I&#x27;m just talking about the PRNG part)<p>The library has one PRNG, and it is the following:<p><pre><code> tb_spinlock_enter(&amp;g_lock); g_value = (g_value * 10807 + 1) &amp; 0xffffffff; tb_spinlock_leave(&amp;g_lock); </code></pre> Using this is literally worse than using the rand reference implementation.<p>Also, tb_random_range uses a biased and slow implementation with modulo. (see <a href="https:&#x2F;&#x2F;www.pcg-random.org&#x2F;posts&#x2F;bounded-rands.html" rel="nofollow">https:&#x2F;&#x2F;www.pcg-random.org&#x2F;posts&#x2F;bounded-rands.html</a> for the proper way to do this)<p>I might look into adding a proper PRNG implementation, but I&#x27;ll recommend a few resources:<p>A lesson of what not to do: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;LDPMpc-ENqY" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;LDPMpc-ENqY</a> (&quot;rand() Considered Harmful&quot;) Some good modern PRNGs: <a href="https:&#x2F;&#x2F;www.pcg-random.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.pcg-random.org&#x2F;</a>, <a href="https:&#x2F;&#x2F;prng.di.unimi.it&#x2F;" rel="nofollow">https:&#x2F;&#x2F;prng.di.unimi.it&#x2F;</a>, <a href="https:&#x2F;&#x2F;romu-random.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;romu-random.org&#x2F;</a> Generating random numbers in a specific range: <a href="https:&#x2F;&#x2F;www.pcg-random.org&#x2F;posts&#x2F;bounded-rands.html" rel="nofollow">https:&#x2F;&#x2F;www.pcg-random.org&#x2F;posts&#x2F;bounded-rands.html</a>
Rochusabout 3 years ago
Looks like a really interesting set of libraries (<a href="https:&#x2F;&#x2F;github.com&#x2F;tboox" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tboox</a>), available under Apache 2 license; also xmake looks interesting; definitely worth a closer look; thanks for sharing.
评论 #31247147 未加载
评论 #31247319 未加载
nextaccounticabout 3 years ago
How does the memory library detect use after free?