home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / nfsd / src / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-24  |  2.2 KB  |  109 lines

  1. /*  Memory handling and a few global symbols
  2.  
  3.     ©1998,1999 Joseph Walton
  4.  
  5.     This software is distributed under the terms of the GNU General Public
  6.     License; either version 2 of the License, or (at your option) any
  7.     later version.
  8. */
  9.  
  10. #include "nfsd.h"
  11. #include "memory.h"
  12. #include "handle_list.h"
  13.  
  14. #include <string.h>
  15.  
  16. #include <dos/dos.h>
  17.  
  18. #include <exec/memory.h>
  19. #include <clib/alib_protos.h>
  20.  
  21. #include <proto/exec.h>
  22.  
  23. /* All memory allocated comes from this pool */
  24. static APTR mem_pool = NULL;
  25.  
  26. /* Lists used by various sections */
  27. struct List cfg_list;
  28.  
  29. /* Buffers for file data */
  30. __aligned struct InfoData infodata;
  31. __aligned struct FileInfoBlock fib;
  32.  
  33. BYTE *recv_buffer = NULL,
  34.     *send_buffer = NULL;
  35.  
  36. BOOL init_memory()
  37. {
  38.     if (mem_pool = CreatePool(MEMF_ANY, POOL_SIZE, (POOL_SIZE / 2))) {
  39.         recv_buffer = AllocVecPooled(RECV_BUFFER_BYTES);
  40.         send_buffer = AllocVecPooled(SEND_BUFFER_BYTES);
  41.         if (recv_buffer && send_buffer) {
  42.             if (init_handle_list()) {
  43.                 NewList(&cfg_list);
  44.                 return TRUE;
  45.             }
  46.         }
  47.     }
  48.  
  49.     /* Something failed */
  50.     free_memory();
  51.     return FALSE;
  52. }
  53.  
  54. /* Just free the whole pool in one go - that's what they're for */
  55. void free_memory()
  56. {
  57.     if (mem_pool) {
  58.         DeletePool(mem_pool);
  59.         mem_pool = NULL;
  60.     }
  61. }
  62.  
  63. /* Oh, I need these, so I might as well write 'em  in C */
  64.  
  65. #define SUL sizeof(ULONG)
  66.  
  67. /* Allocate memory and keep track of its size */
  68. APTR AllocVecPooled(ULONG length)
  69. {
  70.     /* SAS is said to have problems with code like this. I hope
  71.         this variant works */
  72.     ULONG *mem = AllocPooled(mem_pool, length += SUL);
  73.     if (mem) {
  74.         *mem = length;
  75.         mem++;
  76.     }
  77.     return mem;
  78. }
  79.  
  80. /* Free memory allocated by the above method */
  81. void FreeVecPooled(APTR mem)
  82. {
  83.     /* Ditto for here */
  84.     ULONG *mem1 = (ULONG *)mem;
  85.     if (mem1) {
  86.         mem1--;
  87.         FreePooled(mem_pool, mem1, *mem1);
  88.     }
  89. }
  90.  
  91. #undef SUL
  92.  
  93. /* Utility function to duplicate a string */
  94. STRPTR DupString(STRPTR orig)
  95. {
  96.     STRPTR new_str;
  97.  
  98.     if (!orig)
  99.         return NULL;
  100.  
  101.     if (new_str = AllocVecPooled(strlen(orig) + 1)) {
  102.         strcpy(new_str, orig);
  103.     }
  104.  
  105.     return new_str;
  106. }
  107.  
  108.  
  109.