home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / midas / mmem.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-06  |  5KB  |  196 lines

  1. {*      MMEM.PAS
  2.  *
  3.  * MIDAS Sound System memory handling routines
  4.  *
  5.  * Copyright 1994 Petteri Kangaslampi and Jarno Paananen
  6.  *
  7.  * This file is part of the MIDAS Sound System, and may only be
  8.  * used, modified and distributed under the terms of the MIDAS
  9.  * Sound System license, LICENSE.TXT. By continuing to use,
  10.  * modify or distribute this file you indicate that you have
  11.  * read the license and understand and accept it fully.
  12. *}
  13.  
  14. unit mMem;
  15.  
  16.  
  17. interface
  18.  
  19.  
  20. type
  21.     Ppointer = ^pointer;
  22.  
  23.  
  24. {****************************************************************************\
  25. *
  26. * Function:     memAlloc(len : word; blk : Ppointer) : integer;
  27. *
  28. * Description:  Allocates a block of conventional memory
  29. *
  30. * Input:        len : word              Memory block length in bytes
  31. *               blk : Ppointer          Pointer to memory block pointer
  32. *
  33. * Returns:      MIDAS error code.
  34. *               Pointer to allocated block stored in blk^, NULL if error.
  35. *
  36. \****************************************************************************}
  37.  
  38. function memAlloc(len : word; blk : Ppointer) : integer;
  39.  
  40.  
  41.  
  42. {****************************************************************************\
  43. *
  44. * Function:     memFree(blk : pointer) : integer;
  45. *
  46. * Description:  Deallocates a memory block allocated with memAlloc()
  47. *
  48. * Input:        blk : pointer           Memory block pointer
  49. *
  50. * Returns:      MIDAS error code.
  51. *
  52. \****************************************************************************}
  53.  
  54. function memFree(blk : pointer) : integer;
  55.  
  56.  
  57.  
  58.  
  59. implementation
  60.  
  61.  
  62. uses Errors;
  63.  
  64.  
  65.  
  66. {****************************************************************************\
  67. *       enum memFunctIDs
  68. *       ----------------
  69. * Description:  ID numbers for memory handling functions
  70. \****************************************************************************}
  71.  
  72. const
  73.     ID_memAlloc = ID_mem;
  74.     ID_memFree = ID_mem + 1;
  75.  
  76.  
  77.  
  78. {****************************************************************************\
  79. *
  80. * Function:     memAlloc(len : word; blk : Ppointer) : integer;
  81. *
  82. * Description:  Allocates a block of conventional memory
  83. *
  84. * Input:        len : word              Memory block length in bytes
  85. *               blk : Ppointer          Pointer to memory block pointer
  86. *
  87. * Returns:      MIDAS error code.
  88. *               Pointer to allocated block stored in blk^, NULL if error.
  89. *
  90. \****************************************************************************}
  91.  
  92. function memAlloc(len : word; blk : Ppointer) : integer;
  93. var
  94.     wordp : ^word;
  95.  
  96. begin
  97.     { Save the si and di registers, as all assembly routines assume that they
  98.       are preserved: }
  99. asm
  100.         push    si
  101.         push    di
  102. end;
  103.  
  104.     { Check that there is enough free memory left for the block: }
  105.     if MaxAvail >= len then
  106.     begin
  107.         { Check that the block is small enough: }
  108.         if len < 65530 then
  109.         begin
  110.             { Allocate memory for the block plus one word: }
  111.             GetMem(wordp, len+2);
  112.  
  113.             { Store block length to allocated pointer: }
  114.             wordp^ := len;
  115.  
  116.             { Return pointer to the next byte following the stored length: }
  117.             blk^ := ptr(seg(wordp^), ofs(wordp^)+2);
  118.             memAlloc := OK;
  119.         end
  120.         else
  121.         begin
  122.             { block too large: }
  123.             mError(errInvalidBlock, ID_memAlloc);
  124.             memAlloc := errInvalidBlock;
  125.             blk^ := NIL;
  126.         end;
  127.     end
  128.     else
  129.     begin
  130.         { not enough memory left: }
  131.         mError(errOutOfMemory, ID_memAlloc);
  132.         memAlloc := errOutOfMemory;
  133.         blk^ := NIL;
  134.     end;
  135.  
  136.     { Restore si and di: }
  137. asm
  138.         pop     di
  139.         pop     si
  140. end;
  141. end;
  142.  
  143.  
  144.  
  145.  
  146. {****************************************************************************\
  147. *
  148. * Function:     memFree(blk : pointer) : integer;
  149. *
  150. * Description:  Deallocates a memory block allocated with memAlloc()
  151. *
  152. * Input:        blk : pointer           Memory block pointer
  153. *
  154. * Returns:      MIDAS error code.
  155. *
  156. \****************************************************************************}
  157.  
  158. function memFree(blk : pointer) : integer;
  159. var
  160.     wordp : ^word;
  161.  
  162. begin
  163.     { Save the si and di registers, as all assembly routines assume that they
  164.       are preserved: }
  165. asm
  166.         push    si
  167.         push    di
  168. end;
  169.     { Check that block pointer is not NULL: }
  170.     if blk <> NIL then
  171.     begin
  172.         { Point wordp to saved block length: }
  173.         wordp := ptr(seg(blk^), ofs(blk^)-2);
  174.  
  175.         { Deallocate block: }
  176.         FreeMem(wordp, wordp^ + 2);
  177.  
  178.         memFree := OK;
  179.     end
  180.     else
  181.     begin
  182.         mError(errInvalidBlock, ID_memFree);
  183.         memFree := errInvalidBlock;
  184.     end;
  185.  
  186.     { Restore si and di: }
  187. asm
  188.         pop     di
  189.         pop     si
  190. end;
  191. end;
  192.  
  193.  
  194.  
  195. END.
  196.