home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / stdlib / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.7 KB  |  66 lines

  1. /* 10-Apr-94 bug fix M. Fleischer
  2.  * 11-Apr-94 bug fix & readjastment G. Nikl
  3.  * 14-Apr-94 readjustment M. Fleischer
  4.  * 24-Apr-94 cleanup for malloc changed
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #ifdef __GNUC__
  12. #include <inline/exec.h>
  13. #endif
  14. #include <stabs.h>
  15.  
  16. extern ULONG _MSTEP;
  17.  
  18. struct MinList __memorylist= /* memorylist (empty): free needs also access */
  19. {
  20.   (struct MinNode *)&__memorylist.mlh_Tail,
  21.   NULL,
  22.   (struct MinNode *)&__memorylist.mlh_Head
  23. };
  24.  
  25. void *malloc(unsigned long size)
  26. {
  27.   struct MinNode *node=__memorylist.mlh_Head;
  28.   struct MemHeader *b;
  29.   ULONG size2,*a;
  30.  
  31.   size+=sizeof(ULONG);
  32.   while(node->mln_Succ) /* yet some memory in my list ? */
  33.   {
  34.     if((a=Allocate((struct MemHeader *)node,size))!=NULL)
  35.     { 
  36.       *a++=size;
  37.       return a;
  38.     }
  39.     node=node->mln_Succ;
  40.   }
  41.   size2=sizeof(struct MemHeader)+sizeof(ULONG)+size; /* Total memory needed */
  42.   if(size2<=_MSTEP)
  43.     size2=_MSTEP; /* Allocate a _MSTEP bytes large block if possible */
  44.   size2=(size2+4095)&~4095; /* Blow up to full MMU Page */
  45.   if((b=(struct MemHeader *)AllocMem(size2,MEMF_ANY))!=NULL)
  46.   {
  47.     b->mh_Lower=b->mh_First=(struct MemChunk *)(b+1);
  48.     b->mh_First->mc_Next=NULL;
  49.     b->mh_Free=b->mh_First->mc_Bytes=size2-sizeof(struct MemHeader);
  50.     b->mh_Upper=(char *)b+size2;
  51.     AddHead((struct List *)&__memorylist,&b->mh_Node);
  52.     a=Allocate(b,size); /* It has to work this time */
  53.     *a++=size;
  54.     return a;
  55.   }
  56.   return NULL;
  57. }
  58.  
  59. void __exitmalloc(void)
  60. { struct MemHeader *a;
  61.   while((a=(struct MemHeader *)RemHead((struct List *)&__memorylist))!=NULL)
  62.     FreeMem(a,(char *)a->mh_Upper-(char *)a); /* free all memory */
  63. }
  64.  
  65. ADD2EXIT(__exitmalloc,-50);
  66.