home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / ace / c / alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  4.4 KB  |  191 lines

  1. /* 
  2. ** Unix-style hassle free memory allocation
  3. ** via Intuition's Alloc/FreeRemember functions.
  4. ** Copyright (C) 1998 David Benn
  5. ** 
  6. ** This program is free software; you can redistribute it and/or
  7. ** modify it under the terms of the GNU General Public License
  8. ** as published by the Free Software Foundation; either version 2
  9. ** of the License, or (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but 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 this program; if not, write to the Free Software
  18. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. **
  20. ** Call alloc/sym_alloc for each memory allocation request.
  21. **
  22. ** Call free_alloc/free_sym_alloc once at the end of a program
  23. ** run or when the symbol table for the current level is finished
  24. ** with.
  25. **
  26. ** This module also contains functions for allocating and freeing 
  27. ** memory for ACE's assembly code generation.
  28. **
  29. ** Author: David J Benn
  30. **   Date: 30th June 1993, 
  31. **       1st July 1993, 
  32. **           16th December 1993,
  33. **       7th,8th January 1994
  34. */
  35.     
  36. #include <acedef.h>
  37. #include <intuition/intuition.h>
  38.  
  39. /* external variables */
  40. extern    int    lev;
  41.  
  42. /* local variables */
  43. struct Remember *GenRememberList = NULL;
  44. struct Remember *SymRememberList[2] = { NULL, NULL };
  45.  
  46. /* protos */
  47. void *malloc();
  48.  
  49. /* functions */
  50. void *alloc(bytes,flags)
  51. ULONG bytes,flags;
  52. {
  53. /* allocate memory as requested */
  54.  
  55.  return((void *)AllocRemember(&GenRememberList,bytes,flags));     
  56. }
  57.  
  58. void *sym_alloc(bytes,flags)
  59. ULONG bytes,flags;
  60. {
  61. /* allocate memory for current level's symbol table as requested */
  62.  
  63.  return((void *)AllocRemember(&SymRememberList[lev],bytes,flags));     
  64. }
  65.  
  66. void free_alloc()
  67. {
  68. /* free all memory allocated by alloc */
  69.  
  70.  if (GenRememberList != NULL) 
  71.  {
  72.     puts("freeing memory...");
  73.     FreeRemember(&GenRememberList,TRUE);
  74.     GenRememberList = NULL;
  75.  }
  76. }
  77.  
  78. void free_sym_alloc()
  79. {
  80. /* free all memory allocated by sym_alloc for current level */
  81.  
  82.  if (SymRememberList[lev] != NULL) 
  83.  {
  84.     FreeRemember(&SymRememberList[lev],TRUE);
  85.     SymRememberList[lev] = NULL;
  86.  }
  87. }
  88.  
  89. CODE  *alloc_code(opcode,srcopr,destopr)
  90. char  *opcode,*srcopr,*destopr;
  91. {
  92. /*
  93. ** Allocate memory for a CODE node plus its 
  94. ** opcode, srcopr and destopr fields.
  95. **
  96. ** If an allocation fails, any memory 
  97. ** allocated is freed and a NULL CODE 
  98. ** pointer is returned.
  99. */
  100. CODE  *cnode;
  101. ULONG opcode_size,srcopr_size,destopr_size;
  102.  
  103.   /* node */
  104.   if ((cnode=(CODE *)malloc(sizeof(CODE))) == NULL) return(NULL);
  105.  
  106.   /* opcode,srcopr,destopr */
  107.   opcode_size  = strlen(opcode)+1;
  108.   srcopr_size  = strlen(srcopr)+1;
  109.   destopr_size = strlen(destopr)+1;
  110.  
  111.   if ((cnode->opcode=(char *)
  112.              malloc((opcode_size+srcopr_size+destopr_size))) == NULL)
  113.   {
  114.     /* unsuccessful -> free node and abort! */
  115.     free(cnode);
  116.     return(NULL);
  117.   }
  118.       
  119.   /* set src and dest operand pointers */
  120.   cnode->srcopr = cnode->opcode + opcode_size;     
  121.   cnode->destopr = cnode->srcopr + srcopr_size;
  122.     
  123.   /* return a pointer to the CODE node! */
  124.   return(cnode);
  125. }
  126.  
  127. void free_code(cnode)
  128. CODE *cnode;
  129. {
  130. /* 
  131. ** Frees all the memory associated with
  132. ** a CODE node, including its members.
  133. */
  134.  
  135.   if (cnode)
  136.   {
  137.     if (cnode->opcode) free(cnode->opcode);
  138.     free(cnode);
  139.   }
  140. }
  141.  
  142. BOOL  alloc_code_members(cnode,opcode,srcopr,destopr)
  143. CODE  *cnode;
  144. char  *opcode,*srcopr,*destopr;
  145. {
  146. /*
  147. ** Allocate memory for a CODE node's
  148. ** opcode, srcopr and destopr fields.
  149. **
  150. ** If the allocation fails, a boolean 
  151. ** FALSE value is returned.
  152. */
  153. ULONG opcode_size,srcopr_size,destopr_size;
  154.  
  155.   /* is the CODE node non-NULL? */
  156.   if (cnode == NULL) return(FALSE);
  157.  
  158.   /* allocate opcode,srcopr,destopr */
  159.   opcode_size  = strlen(opcode)+1;
  160.   srcopr_size  = strlen(srcopr)+1;
  161.   destopr_size = strlen(destopr)+1;
  162.  
  163.   if ((cnode->opcode=(char *)
  164.              malloc((opcode_size+srcopr_size+destopr_size))) == NULL)
  165.   return(FALSE);
  166.       
  167.   /* set src and dest operand pointers */
  168.   cnode->srcopr = cnode->opcode + opcode_size;     
  169.   cnode->destopr = cnode->srcopr + srcopr_size;
  170.      
  171.   /* we got this far so return TRUE to indicate success! */
  172.   return(TRUE);
  173. }
  174.  
  175. void free_code_members(cnode)
  176. CODE *cnode;
  177. {
  178. /* 
  179. ** Frees all the memory associated with
  180. ** a CODE node's members.
  181. */
  182.  
  183.   if (cnode)
  184.   {
  185.     if (cnode->opcode) free(cnode->opcode);
  186.     cnode->opcode  = NULL;
  187.     cnode->srcopr  = NULL;
  188.     cnode->destopr = NULL;
  189.   }
  190. }
  191.