setjmp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. .TH SETJMP 2
  2. .SH NAME
  3. setjmp, longjmp, notejmp \- non-local goto
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .PP
  9. .ta \w'\fLvoid 'u
  10. .B
  11. int setjmp(jmp_buf env)
  12. .PP
  13. .B
  14. void longjmp(jmp_buf env, int val)
  15. .PP
  16. .B
  17. void notejmp(void *uregs, jmp_buf env, int val)
  18. .SH DESCRIPTION
  19. These routines are useful for dealing with errors
  20. and interrupts encountered in
  21. a low-level subroutine of a program.
  22. .PP
  23. .I Setjmp
  24. saves its stack environment in
  25. .I env
  26. for later use by
  27. .IR longjmp .
  28. It returns value 0.
  29. .PP
  30. .I Longjmp
  31. restores the environment saved by the last call of
  32. .IR setjmp .
  33. It then causes execution to
  34. continue as if the call of
  35. .I setjmp
  36. had just returned with value
  37. .IR val .
  38. The invoker of
  39. .I setjmp
  40. must not itself have returned in the interim.
  41. All accessible data have values as of the time
  42. .I longjmp
  43. was called.
  44. .PP
  45. .I Notejmp
  46. is the same as
  47. .I longjmp
  48. except that it is to be called from within a note handler (see
  49. .IR notify (2)).
  50. The
  51. .I uregs
  52. argument should be the first argument passed to the note handler.
  53. .PP
  54. .I Setjmp
  55. and
  56. .I longjmp
  57. can also be used to switch stacks.
  58. Several macros are defined in
  59. .B /$objtype/include/u.h
  60. that can be used to build
  61. .B jmp_bufs
  62. by hand. The following code establishes a
  63. .B jmp_buf
  64. .i label
  65. that may be called by
  66. .I longjmp
  67. to begin execution in a function
  68. .BR f
  69. with 1024 bytes of stack:
  70. .IP
  71. .EX
  72. #include <u.h>
  73. #include <libc.h>
  74. jmp_buf label;
  75. #define NSTACK 1024
  76. char stack[NSTACK];
  77. void
  78. setlabel(void)
  79. {
  80. label[JMPBUFPC] = ((ulong)f+JMPBUFDPC);
  81. /* -2 leaves room for old pc and new pc in frame */
  82. label[JMPBUFSP] =
  83. (ulong)(&stack[NSTACK-2*sizeof(ulong*)]);
  84. }
  85. .EE
  86. .SH SOURCE
  87. .B /sys/src/libc/$objtype/setjmp.s
  88. .br
  89. .B /sys/src/libc/$objtype/notejmp.c
  90. .SH SEE ALSO
  91. .IR notify (2)
  92. .SH BUGS
  93. .PP
  94. .I Notejmp
  95. cannot recover from an address trap or bus error (page fault) on the 680x0
  96. architectures.