home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff248.lzh / Regex / malloc.c < prev    next >
C/C++ Source or Header  |  1989-09-16  |  2KB  |  91 lines

  1. /*
  2.  *  Support routines for for the GNU regular expression package.
  3.  *  Edwin Hoogerbeets 18/07/89
  4.  *
  5.  *  This file may be copied and distributed under the GNU Public
  6.  *  Licence. See the comment at the top of regex.c for details.
  7.  *
  8.  *  Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
  9.  *  GNU regular expression package by the Free Software Foundation.
  10.  */
  11.  
  12. #include <exec/memory.h>
  13. #define memcpy(dst,src,n) movmem((src),(dst),(n))
  14.  
  15. extern long *AllocMem();
  16. extern void FreeMem();
  17.  
  18. char *malloc(size)
  19. long size;
  20. {
  21.   /* good temp's are hard to come by... */
  22.   long *temp = AllocMem(size + sizeof(long), MEMF_PUBLIC|MEMF_CLEAR);
  23.  
  24.   if ( temp ) {
  25.     temp[0] = size;
  26.     ++temp;
  27.   }
  28.  
  29.   return((char *)temp);
  30. }
  31.  
  32. void free(NelsonMandala)
  33. long *NelsonMandala;
  34. {
  35.   if ( NelsonMandala ) {
  36.     FreeMem((char *) &NelsonMandala[-1], NelsonMandala[-1] + sizeof(long) );
  37.   }
  38. }
  39.  
  40. /* Protect our programming environment! Join a memory realloc program! */
  41. char *realloc(source,size)
  42. char *source;
  43. long size;
  44. {
  45.   char *destination = malloc(size);
  46.  
  47.   if ( destination ) {
  48.     memcpy(destination,source,size);
  49.     free(source);
  50.  
  51.     return(destination);
  52.   } else {
  53.     return(NULL);
  54.   }
  55. }
  56.  
  57. /* define TESTMALLOC to create a stand alone program that tests the
  58.  * above routines
  59.  */
  60.  
  61. #ifdef TESTMALLOC
  62. #include <stdio.h>
  63.  
  64. main()
  65. {
  66.   long *foo;
  67.  
  68.   printf("doing malloc\n");
  69.   foo = (long *) malloc(100);
  70.   printf("malloc of 100 bytes gives pointer: %lx with %ld at -1\n",
  71.          foo,foo[-1]);
  72.  
  73.   foo[42] = 42L;
  74.  
  75.   printf("doing realloc\n");
  76.  
  77.   foo = (long *) realloc((char *) foo, 200);
  78.  
  79.   printf("realloc to 200 bytes gives pointer: %lx with %ld at -1\n",
  80.          foo,foo[-1]);
  81.   printf("and position 42 contains %ld\n",foo[42]);
  82.  
  83.  
  84.   printf("doing free\n");
  85.  
  86.   free(foo);
  87. }
  88. #endif
  89.  
  90.  
  91.