home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / may94 / util / edit / jade.lha / Jade / src / amiga_memory.c < prev    next >
C/C++ Source or Header  |  1994-04-19  |  2KB  |  119 lines

  1. /* amiga_memory.c -- Memory allocation for AmigaDOS
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4. This file is part of Jade.
  5.  
  6. Jade is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Jade is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Jade; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <clib/exec_protos.h>
  24. #include <string.h>
  25.  
  26. _PR int    initmem(void);
  27. _PR void killmem(void);
  28. _PR void *mymalloc(int);
  29. _PR void *mycalloc(int);
  30. _PR void myfree(void *);
  31.  
  32. #if AMIGA_INCLUDE_VER >= 39
  33. # define PUDDLESIZE 20480
  34. # define THRESHSIZE 4096
  35.   APTR MemPool;
  36. #endif
  37.  
  38. int
  39. initmem(void)
  40. {
  41. #if AMIGA_INCLUDE_VER >= 39
  42.     if(OSVer >= 39)
  43.     {
  44.     if(MemPool = CreatePool(0, PUDDLESIZE, THRESHSIZE))
  45.         return(TRUE);
  46.     return(FALSE);
  47.     }
  48. #endif
  49.     return(TRUE);
  50. }
  51. void
  52. killmem(void)
  53. {
  54. #if AMIGA_INCLUDE_VER >= 39
  55.     if(MemPool)
  56.     {
  57.     DeletePool(MemPool);
  58.     MemPool = NULL;
  59.     }
  60. #endif
  61. }
  62.  
  63. void *
  64. mymalloc(int size)
  65. {
  66.     int *mem;
  67.     size += sizeof(int);
  68.  
  69. #ifdef DEBUG_MALLOC
  70.     if(size == DEBUG_MALLOC)
  71.     {
  72. loop:
  73.     /* Set a breakpoint here. */
  74.     goto loop;
  75.     }
  76. #endif
  77.  
  78. #if AMIGA_INCLUDE_VER >= 39
  79.     if(!(mem = (MemPool ? AllocPooled(MemPool, size) : AllocMem(size, 0))))
  80. #else
  81.     if(!(mem = AllocMem(size, 0)))
  82. #endif
  83.     {
  84.     sm_flush(&MainStrMem);
  85. #if AMIGA_INCLUDE_VER >= 39
  86.     if(!(mem = (MemPool ? AllocPooled(MemPool, size) : AllocMem(size, 0))))
  87. #else
  88.     if(!(mem = AllocMem(size, 0)))
  89. #endif
  90.         return(NULL);
  91.     }
  92.     *mem = size;
  93.     return(mem + 1);
  94. }
  95.  
  96. void *
  97. mycalloc(int size)
  98. {
  99.     void *mem;
  100.     if(mem = mymalloc(size))
  101.     memset(mem, 0, size);
  102.     return(mem);
  103. }
  104.  
  105. void
  106. myfree(void *mem)
  107. {
  108.     if(mem)
  109.     {
  110.     int size = *(--((int *)mem));
  111. #if AMIGA_INCLUDE_VER >= 39
  112.     if(MemPool)
  113.         FreePooled(MemPool, mem, size);
  114.     else
  115. #endif
  116.         FreeMem(mem, size);
  117.     }
  118. }
  119.