C 的 UUID 库?

发布于 2024-08-27 03:22:15 字数 1539 浏览 6 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2024-09-03 03:22:15

我两者都用过,而且我绝对更喜欢 util-linux-ng(以前在 e2fsprogs 中)。为了可移植性,我让我的软件支持两者,并使用 autoconf/cmake 宏来检测安装了哪一个。

对我来说,OSSP 的主要问题是它无缘无故地滥用了 C 中的面向对象。 UUID 只是一个 128 位数字,可以用 char[16] 数组表示。 UUID 通常与另一个结构相关联(UUID 用作此类结构的键或标识符),因此如果您可以将该数组内联到结构本身中,那应该会很好。

使用 OSSP UUID,它为您提供一个指向动态分配对象的 uuid_t* 指针,该对象不仅保存 UUID 值,还保存更多状态。如果您处理数以万计的对象,这显然会使程序变慢,使用更多内存并导致更多内存碎片。最后,为了使 OSSP UUID 可用,您必须使用它来生成 UUID(其中涉及 4 个调用:uuid_create、uuid_make、uuid_export 和 uuid_destroy)并自己使用 UUID 值。使用 util-linux-ng,只需一次调用:uuid_generate。

I used both, and I definitely prefer the util-linux-ng (formerly in e2fsprogs) one. For portability, I make my software support both and use autoconf/cmake macros to detect which one is installed.

The main problem with OSSP for me is that it abuses object-orientation in C for no good reason. An UUID is just a 128-bit number, which can be represented with a char[16] array. The UUID is usually associated with another structure (the UUID serves as a key or an identifier for such structure), so it should be good if you could inline that array in the structure itself.

With OSSP UUID, it gives you a uuid_t* pointer to a dynamically allocated object, which holds more state than just the UUID value. If you work with tens of thousands of objects, this sensibly makes the program slower, uses more memory and causes more memory fragmentation. In the end, to make OSSP UUID usable, you have to use it just to generate the UUID (which involves 4 calls: uuid_create, uuid_make, uuid_export and uuid_destroy) and work with the UUID value yourself. With util-linux-ng, it is just a single call: uuid_generate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文