home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 4.ddi / TOOLS.4 / TCTSRC1.EXE / ISRESERV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  3.6 KB  |  106 lines

  1. /**
  2. *
  3. * Name        isreserv -- Allocate a block and reserve space for use
  4. *                in an ISR or intervention function.
  5. *
  6. * Synopsis    ercode = isreserv(reserve,blksize,ppblock);
  7. *
  8. *        int ercode      Success code.  Possible values include:
  9. *                  IS_OK -- Success.
  10. *                  IS_NO_MEMORY -- Insufficient memory.
  11. *                  IS_RANGE -- reserve or blksize is
  12. *                    invalid.
  13. *        size_t reserve      Size of space to reserve for use
  14. *                  in an ISR.
  15. *        size_t blksize      Size of block to allocate via
  16. *                  malloc().
  17. *        char **ppblock      Address of pointer to receive
  18. *                  address of block allocated via
  19. *                  malloc(), or to receive NULL if
  20. *                  failure.
  21. *
  22. * Description    This function allocates a block of space via malloc() in
  23. *        such a way that the amount of space specified by
  24. *        "reserve" is available for later allocation from an ISR
  25. *        or intervention function.
  26. *
  27. *        In the T, S, and M memory models of Turbo C, you should
  28. *        use the resulting allocated block as the stack(s) for
  29. *        the stack of the ISR or intervention function in
  30. *        question.  In the C, L, and H memory models of Turbo C,
  31. *        you can place the ISR stack or intervention function
  32. *        stack wherever you wish; "blksize" can be as small as 1.
  33. *
  34. *        "size_t" is defined in the standard header file
  35. *        STDLIB.H.
  36. *
  37. * Method    First allocate the reserved block; then allocate blocks
  38. *        of size "blksize" until one is located at a higher
  39. *        memory address than the reserved block.  This high block
  40. *        is the one returned.  The reserved block and the trial
  41. *        blocks are all freed.
  42. *
  43. * Returns    ercode          Success code.  Possible values include:
  44. *                  IS_OK -- Success.
  45. *                  IS_NO_MEMORY -- Insufficient memory.
  46. *                  IS_RANGE -- reserve or blksize is
  47. *                    invalid.
  48. *        *pblock       Address of block allocated via
  49. *                  malloc(), or NULL if failure.
  50. *
  51. * Version    6.00 (C)Copyright Blaise Computing Inc. 1989
  52. *
  53. **/
  54.  
  55. #include <stdlib.h>          /* For malloc(), free(), and size_t.    */
  56. #include <bintrupt.h>
  57.  
  58. int isreserv(reserve,blksize,ppblock)
  59. size_t reserve,blksize;
  60. char **ppblock;
  61. {
  62.     void  *p_reserve,*ptrial,*pprev;
  63.  
  64.     /* We may allocate many blocks of size "blksize", and in each one */
  65.     /* store the address of its predecessor, so we can clean up.      */
  66.     /* Therefore each block must be large enough to contain a pointer;*/
  67.     /* "trial_size" is the size we must use.                          */
  68.  
  69.     size_t trial_size = max(blksize,sizeof(void *));
  70.  
  71.     *ppblock = NULL;              /* In case of premature error.  */
  72.  
  73.     if (blksize <= 0 || reserve <= 0)      /* Range check.          */
  74.     return IS_RANGE;
  75.  
  76.                       /* Allocate reserved block.     */
  77.     if (NULL == (p_reserve = malloc(reserve)))
  78.     return IS_NO_MEMORY;
  79.  
  80.     /* Search for a block of size "blksize" until we find one at a    */
  81.     /* higher address than the reserved block or until no more memory */
  82.     /* is available.  In each block, save a pointer to predecessor.   */
  83.  
  84.     pprev = NULL;
  85.     while (   NULL          != (ptrial = malloc(trial_size))
  86.        && utplong(ptrial) <= utplong(p_reserve))
  87.     {
  88.     * (void **) ptrial = pprev;
  89.     pprev           = ptrial;
  90.     }
  91.  
  92.     *ppblock = ptrial;              /* Return NULL if no success    */
  93.                       /* before we ran out of memory; */
  94.                       /* otherwise return successful  */
  95.                       /* trial block.              */
  96.  
  97.     while (NULL != (ptrial = pprev))  /* Free the chain of          */
  98.     {                      /* temporary blocks.          */
  99.     pprev = * (void **) ptrial;
  100.     free(ptrial);
  101.     }
  102.     free(p_reserve);              /* Free the reserved block.     */
  103.  
  104.     return (*ppblock ? IS_OK : IS_NO_MEMORY);
  105. }
  106.