home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / src / amiga_memory.c < prev    next >
C/C++ Source or Header  |  1994-06-29  |  2KB  |  125 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. #ifdef NEED_MEMORY_H
  27. # include <memory.h>
  28. #endif
  29.  
  30. _PR int    initmem(void);
  31. _PR void killmem(void);
  32. _PR void *mymalloc(int);
  33. _PR void *mycalloc(int);
  34. _PR void myfree(void *);
  35.  
  36. #if AMIGA_INCLUDE_VER >= 39
  37. # define PUDDLESIZE 20480
  38. # define THRESHSIZE 4096
  39.   APTR mem_pool;
  40. #endif
  41.  
  42. int
  43. initmem(void)
  44. {
  45. #if AMIGA_INCLUDE_VER >= 39
  46.     if(ami_os_version >= 39)
  47.     {
  48.     if(mem_pool = CreatePool(0, PUDDLESIZE, THRESHSIZE))
  49.         return(TRUE);
  50.     return(FALSE);
  51.     }
  52. #endif
  53.     return(TRUE);
  54. }
  55. void
  56. killmem(void)
  57. {
  58. #if AMIGA_INCLUDE_VER >= 39
  59.     if(mem_pool)
  60.     {
  61.     DeletePool(mem_pool);
  62.     mem_pool = NULL;
  63.     }
  64. #endif
  65. }
  66.  
  67. void *
  68. mymalloc(int size)
  69. {
  70.     int *mem;
  71.     size += sizeof(int);
  72.  
  73. #ifdef DEBUG_MALLOC
  74.     if(size == DEBUG_MALLOC)
  75.     {
  76. loop:
  77.     /* Set a breakpoint here. */
  78.     goto loop;
  79.     }
  80. #endif
  81.  
  82. #if AMIGA_INCLUDE_VER >= 39
  83.     if(!(mem = (mem_pool ? AllocPooled(mem_pool, size) : AllocMem(size, 0))))
  84. #else
  85.     if(!(mem = AllocMem(size, 0)))
  86. #endif
  87.     {
  88.     sm_flush(&main_strmem);
  89. #if AMIGA_INCLUDE_VER >= 39
  90.     if(!(mem = (mem_pool
  91.             ? AllocPooled(mem_pool, size)
  92.             : AllocMem(size, 0))))
  93. #else
  94.     if(!(mem = AllocMem(size, 0)))
  95. #endif
  96.         return(NULL);
  97.     }
  98.     *mem = size;
  99.     return(mem + 1);
  100. }
  101.  
  102. void *
  103. mycalloc(int size)
  104. {
  105.     void *mem;
  106.     if(mem = mymalloc(size))
  107.     memset(mem, 0, size);
  108.     return(mem);
  109. }
  110.  
  111. void
  112. myfree(void *mem)
  113. {
  114.     if(mem)
  115.     {
  116.     int size = *(--((int *)mem));
  117. #if AMIGA_INCLUDE_VER >= 39
  118.     if(mem_pool)
  119.         FreePooled(mem_pool, mem, size);
  120.     else
  121. #endif
  122.         FreeMem(mem, size);
  123.     }
  124. }
  125.