home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / p / pcrte224.zip / SOURCE.ZIP / BUFFER.INC < prev    next >
Text File  |  1992-06-09  |  5KB  |  134 lines

  1. ;;****************************************************************************
  2. ;;                     buffer.inc         buffer.inc
  3. ;;****************************************************************************
  4. ;;
  5. ;;  Copyright (C) 1989 Northwestern University, Vance Morrison
  6. ;;
  7. ;;
  8. ;; Permission to view, compile, and modify for LOCAL (intra-organization) 
  9. ;; USE ONLY is hereby granted, provided that this copyright and permission 
  10. ;; notice appear on all copies.  Any other use by permission only.
  11. ;;
  12. ;; Northwestern University makes no representations about the suitability 
  13. ;; of this software for any purpose.  It is provided "as is" without expressed 
  14. ;; or implied warranty.  See the copywrite notice file for complete details.
  15. ;;
  16. ;;*************************************************************************
  17. ;;
  18. ;;  buffer.inc implements a simple buffer alocator for use in a packet 
  19. ;;  queue.
  20. ;;
  21. ;; AUTHOR: Vance Morrison
  22. ;; DATE:   5/24/89
  23. ;; ADDRESS: morrison@accuvax.nwu.edu
  24. ;;****************************************************************************
  25.  
  26. buff_data STRUC 
  27.     start_free      DW  ?        ;; free space between these
  28.     end_free        DW  ?     
  29. buff_data ENDS 
  30.  
  31.  
  32. ;;**************************************************************************
  33. ;; buff declare declares a new buffer manager.  The buffer to be managed is
  34. ;; 'buff' of length 'len'.  If 'buff' is blank, then BUFF_DEFINE allocates
  35. ;; the buffer space.  Note that the total buffer space needs AT LEAST
  36. ;; 2-3 times greater than the biggest allocation for these routines to work
  37. ;; well.
  38. ;;
  39. BUFF_DECLARE MACRO name, len, buff
  40.  
  41.     .DATA
  42.     buff_&name&_len = len
  43.  
  44.     ifnb <buff>
  45.         buff_&name&_buff = buff 
  46.     else
  47.         global buff_&name&_my_space:byte
  48.         buff_&name&_buff = buff_&name&_my_space
  49.         buff_&name&_allocate = 1
  50.     endif
  51.     buff_&name&_buff_end = (buff_&name&_buff + len)
  52.  
  53.     global buff_&name&_data:buff_data
  54.     .CODE
  55. ENDM
  56.  
  57. ;;**************************************************************************
  58. ;; BUFF_DEFINE allocates memory and initializes the buffer manager
  59. ;;
  60. BUFF_DEFINE MACRO name
  61. ifdef buff_&name&_buff
  62.     .DATA
  63.     buff_&name&_data buff_data <>
  64.     ifdef buff_&name&_allocate
  65.         buff_&name&_my_space DB buff_&name&_len DUP (0)
  66.     endif
  67.  
  68.     .CODE
  69.     mov buff_&name&_data.start_free, offset buff_&name&_buff
  70.     mov buff_&name&_data.end_free, offset buff_&name&_buff_end
  71. endif
  72. ENDM
  73.  
  74.  
  75. ;;**************************************************************************
  76. ;; BUFF_CHECK checks to see if CX bytes of contiguous memory is available
  77. ;; in the buffer.  If it is it sets SI to the begining and DI to the end
  78. ;; of the buffer.  Othersize it jumps to 'fail'.  Note this routine does
  79. ;; NOT allocate the space (that is if BUFF_CHECK is called again it will
  80. ;; return the same space) BUFF_GET does the allocation
  81. ;; note that this behavior is guarenteed (successive BUFF_CHECKS will
  82. ;; return the same value even if BUFF_FREEs are done in between)
  83. ;;
  84. BUFF_CHECK_in_CX_out_SI_DI_const_BX_CX_DX_BP_ES MACRO name, fail
  85.     local done
  86.  
  87.     mov SI, word ptr buff_&name&_data.start_free
  88.     mov AX, word ptr buff_&name&_data.end_free
  89.     mov DI, SI
  90.     add DI, CX                              ;; first guess at SI:DI output
  91.     cmp DI, AX
  92.     jbe done
  93.         ;; try wrap around
  94.         cmp SI, AX
  95.         jbe fail
  96.  
  97.         cmp DI, offset buff_&name&_buff_end
  98.         jbe done
  99.             mov SI, offset buff_&name&_buff
  100.             mov DI, SI
  101.             add DI, CX                      ;; first guess at SI:DI output
  102.             cmp DI, AX
  103.             ja fail
  104.     done:
  105. ENDM
  106.  
  107.  
  108. ;;**************************************************************************
  109. ;; BUFF_GET allocates the space that was checked with BUFF_CHECK.  DI
  110. ;; is assumed to be initialized by BUFF_CHECK.
  111. ;;
  112. BUFF_GET_in_DI_const_AX_BX_CX_DX_BP_SI_DI_ES MACRO name
  113.  
  114.     mov word ptr buff_&name&_data.start_free, DI
  115. ENDM
  116.  
  117. ;;**************************************************************************
  118. ;; BUFF_FREE releases the buffer allocated by BUFF_GET.  DI is assumed to
  119. ;; be the same value as returned by BUFF_CHECK.  IT ALSO RELEASES ALL 
  120. ;; ALLOCATION MADE PREVIOUSLY TO THE ALLOCATION ASSOCIATED WITH DI (that
  121. ;; is we asume that if Buffer 1 was allocated before Buffer 2, then 
  122. ;; buffer 1 will be freed before buffer 2. (this is true for queue allocations)
  123. ;;
  124. BUFF_FREE_in_DI_const_AX_BX_CX_DX_BP_SI_DI_ES MACRO name
  125.     local done
  126.  
  127.     mov word ptr buff_&name&_data.end_free, DI
  128.     cmp DI, word ptr buff_&name&_data.start_free    ;; is the buffer all free?
  129.     jnz done
  130.         mov word ptr buff_&name&_data.start_free, offset buff_&name&_buff
  131.         mov word ptr buff_&name&_data.end_free, offset buff_&name&_buff_end
  132.     done:
  133. ENDM
  134.