LIBUNWIND-SETJMP(3libunwind)

✨ LIBUNWIND-SETJMP πŸ’–

okay bestie, so this is a page from the Programming Library manual, section 3libunwind β€” it's all about libunwind-setjmp, the glow-up version of non-local gotos. sit down, hydrate, let's get into it 🫢


🌸 NAME 🌸

libunwind-setjmp β€” libunwind-based non-local gotos. yeah. the jumpy jumpy but make it ✨libunwind✨.


πŸŽ€ SYNOPSIS πŸŽ€

#include <setjmp.h>

int setjmp(jmp_buf env);
void longjmp(jmp_buf env, int val);
int _setjmp(jmp_buf env);
void _longjmp(jmp_buf env, int val);
int sigsetjmp(sigjmp_buf env, int savemask);
void siglongjmp(sigjmp_buf env, int val);

πŸ’… DESCRIPTION πŸ’…

so the unwind-setjmp library is a libunwind-based implementation of non-local gotos. and the whole point? it's meant to be a drop-in replacement for the normal, system-provided routines with the exact same names. like, it just slides into the spot. no cap.

the main advantage of using the unwind-setjmp library is that setting up a non-local goto via one of the setjmp() routines is very fast. typically, just 2 or 3 words need to be saved in the jump-buffer β€” plus one call to sigprocmask(2) in the case of sigsetjmp. that's it. tiny. cute. minimal. πŸŽ€

on the other hand 😭 executing a non-local goto by calling one of the longjmp() routines tends to be much slower than with the system-provided routines. and it's not a flat cost either β€” the time spent on a longjmp() will be proportional to the number of call frames that exist between the points where setjmp() and longjmp() were called. so the deeper you are, the more it hurts. the way that math works is honestly giving stack traversal πŸ’€

for this reason, the unwind-setjmp library is beneficial primarily in applications that frequently call setjmp() but only rarely call longjmp(). if that's your vibe, this is your library. if not… bestie, maybe reconsider 🧚


⚠️ CAVEATS ⚠️

read this part. i'm serious. this is the "don't text your ex" section of the manpage.
  • the correct operation of this library depends on the presence of correct unwind information. on newer platforms, this is rarely an issue. on older platforms, care needs to be taken to ensure that each of the functions whose stack frames may have to be unwound during a longjmp() have correct unwind information β€” and on those platforms, there is usually a compiler-switch, such as -funwind-tables, to request the generation of unwind information. so like, turn it on. please. πŸ™ˆ
  • the contents of jmp_buf and sigjmp_buf as setup and used by these routines is completely different from the ones used by the system-provided routines. thus, a jump-buffer created by the libunwind-based setjmp()/_setjmp may only be used in a call to the libunwind-based longjmp()/_longjmp(). the analogous applies for sigjmp_buf with sigsetjmp() and siglongjmp(). basically: no mixing brands. keep your jump-buffers in their own lane or it's over πŸ’€

πŸ¦‹ FILES πŸ¦‹

  • -lunwind-setjmp β€” the library an application should be linked against to ensure it uses the libunwind-based non-local goto routines. link this. that's the one. πŸ‘‘

🌈 SEE ALSO 🌈

go read those too, iykyk πŸ“š


πŸ‘‘ AUTHOR πŸ‘‘

David Mosberger-Tang


and that's the whole page, bestie πŸ’– huge shoutout to the Programming Library for publishing this one, dated 29 August 2023, and to LIBUNWIND-SETJMP(3libunwind) for being the moment. periodt. πŸŽ€βœ¨πŸ«Ά

πŸ“¦ where this page comes from