home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / ThreadLib 1.0d1 / Source / ThreadUtil / ThreadUtil.c next >
Encoding:
C/C++ Source or Header  |  1994-02-10  |  3.0 KB  |  156 lines  |  [TEXT/KAHL]

  1. /* See the file Distribution for distribution terms.
  2.     (c) Copyright 1994 Ari Halberstadt */
  3.  
  4. /* Utilities for stand-alone thread library.
  5.     94/02/10 aih - created */
  6.  
  7. #if ! WINTER_SHELL
  8.  
  9. #include <string.h>
  10. #include "ThreadUtil.h"
  11. #include "ExceptionLib.h"
  12.  
  13. Ptr StkLowPt : 0x110;
  14. short _profile_depth;
  15.  
  16. /*----------------------------------------------------------------------------*/
  17. /* assertions */
  18. /*----------------------------------------------------------------------------*/
  19.  
  20. int assertfailed(void)
  21. {
  22.     Debugger();
  23.     return(0);
  24. }
  25.  
  26. /*----------------------------------------------------------------------------*/
  27. /* memory operations */
  28. /*----------------------------------------------------------------------------*/
  29.  
  30. void *memclr(void *p, size_t n)
  31. {
  32.     return(memset(p, 0, n));
  33. }
  34.  
  35. Boolean HandleValid(void *h)
  36. {
  37.     return(h != NULL);
  38. }
  39.  
  40. Boolean HandleValidSize(void *h, size_t n)
  41. {
  42.     return(HandleValid(h) && GetHandleSize(h) >= n);
  43. }
  44.  
  45. void *HandleBeginClear(size_t n)
  46. {
  47.     void *h;
  48.     
  49.     h = NewHandleClear(n);
  50.     FailNIL(h);
  51.     return(h);
  52. }
  53.  
  54. void HandleEnd(void *h)
  55. {
  56.     DisposeHandle(h);
  57. }
  58.  
  59. Boolean PtrValid(void *p)
  60. {
  61.     return(p != NULL);
  62. }
  63.  
  64. size_t PtrSize(void *p)
  65. {
  66.     return(GetPtrSize(p));
  67. }
  68.  
  69. void *PtrBegin(size_t n)
  70. {
  71.     void *p;
  72.     
  73.     p = NewPtr(n);
  74.     FailNIL(p);
  75.     return(p);
  76. }
  77.  
  78. void PtrEnd(void *p)
  79. {
  80.     DisposePtr(p);
  81. }
  82.  
  83. /*----------------------------------------------------------------------------*/
  84. /* low-memory global access (must not move memory) */
  85. /*----------------------------------------------------------------------------*/
  86.  
  87. Ptr GetCurStackBase(void)
  88. {
  89.     return(CurStackBase);
  90. }
  91.  
  92. void SetStkLowPt(Ptr p)
  93. {
  94.     StkLowPt = p;
  95. }
  96.  
  97. /*----------------------------------------------------------------------------*/
  98. /* linked list operations */
  99. /*----------------------------------------------------------------------------*/
  100.  
  101. /* macros for accessing next item */
  102. #define deref(x) (**(LLHandle) (x))
  103. #define next(x) (deref(x).hnext)
  104.  
  105. /* true if a valid item */
  106. Boolean LLHValid(LLObjectType item)
  107. {
  108.     return(HandleValidSize(item, sizeof(LLType)));
  109. }
  110.  
  111. /* return next item in list */
  112. LLObjectType LLHNext(LLObjectType item)
  113. {
  114.     require(! item || LLHValid(item));
  115.     return(item ? next(item) : NULL);
  116. }
  117.  
  118. /* appends the item after the end of the list. Returns head of list. */
  119. LLObjectType LLHAppend(LLObjectType head, LLObjectType item)
  120. {
  121.     LLObjectType tail;
  122.     
  123.     if (! head)
  124.         head = item;
  125.     else {
  126.         for (tail = head; next(tail); tail = next(tail))
  127.             ;
  128.         next(tail) = item;
  129.     }
  130.     return(head);
  131. }
  132.  
  133. /* Removes the item from the list. Does nothing if the item couldn't be found.
  134.     Returns the new head of the list. */
  135. LLObjectType LLHDelete(LLObjectType head, LLObjectType item)
  136. {
  137.     require(! head || LLHValid(head));
  138.     require(! item || LLHValid(item));
  139.     if (head && item) {
  140.         if (head == item)
  141.             head = next(head);
  142.         else {
  143.             LLObjectType prev = head;
  144.             while (prev && next(prev) != item)
  145.                 prev = next(prev);
  146.             check(prev != item);
  147.             if (prev)
  148.                 next(prev) = next(item);
  149.         }
  150.     }
  151.     ensure(! head || LLHValid(head));
  152.     return(head);
  153. }
  154.  
  155. #endif /* WINTER_SHELL */
  156.