home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gcc / newgccstart.lha / source.lha / libm / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  1.4 KB  |  68 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/nodes.h>
  4. #include <exec/lists.h>
  5.  
  6. extern struct MinList __memorylist;
  7.  
  8. struct memoryentry
  9. {
  10.   struct MinNode node;
  11.   unsigned long size[1]; /* &memoryentry.size[1] gibt dann den Speicher */
  12. };
  13.  
  14. void *malloc(unsigned long size)
  15. {
  16.   struct memoryentry *a;
  17.   a=(struct memoryentry *)AllocMem(sizeof(struct memoryentry)+size,MEMF_ANY);
  18.   if(a==NULL)
  19.     return NULL;
  20.   a->size[0]=size;
  21.   AddHead((struct List *)&__memorylist,(struct Node *)&a->node);
  22.   return (void*)&a->size[1];
  23. }
  24.  
  25. void free(void *ptr)
  26. {
  27.   struct memoryentry *a;
  28.   a=(struct memoryentry *)ptr-1;
  29.   Remove((struct Node *)&a->node);
  30.   FreeMem(a,sizeof(struct memoryentry)+a->size[0]);
  31. }
  32.  
  33. void __freeall(void)
  34. {
  35.   struct memoryentry *a;
  36.   while ((a=(struct memoryentry *)RemHead((struct List *)&__memorylist))!=NULL)
  37.     FreeMem(a,sizeof(struct memoryentry)+a->size[0]); /* Den Speicher freigeben */
  38. }
  39.  
  40. void *calloc(unsigned long nmemb,unsigned long size)
  41. {
  42.   unsigned long l;
  43.   unsigned long *a;
  44.   void *b;
  45.   l=nmemb*size+sizeof(unsigned long)-1&~sizeof(unsigned long);
  46.   a=(unsigned long *)(b=malloc(l));
  47.   if(b==NULL)
  48.     return NULL;
  49.   do
  50.     *a++=0;
  51.   while(l-=sizeof(unsigned long int));
  52.   return b;
  53. }
  54.  
  55. void *realloc(void *ptr,unsigned long size)
  56. {
  57.   void *a;
  58.   unsigned long l;
  59.   a=malloc(size);
  60.   if(a==NULL)
  61.     return NULL;
  62.   l=((unsigned long *)ptr)[-1];
  63.   l=l<size?l:size;
  64.   CopyMem(ptr,a,l);
  65.   return a;
  66. }
  67.  
  68.