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 / free.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.0 KB  |  51 lines

  1. #include <debuglib.h>
  2. #include <stdlib.h>
  3. #ifdef __GNUC__
  4. #include <inline/exec.h>
  5. #endif
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/nodes.h>
  9. #include <exec/lists.h>
  10.  
  11. extern struct MinList __memorylist;
  12.  
  13. void free(void *ptr)
  14. {
  15.   struct MemHeader *a;
  16.  
  17.   if(ptr==NULL) /* What does that mean ????? */
  18.   {
  19. #ifdef DEBUG_LIB
  20.     FATALERROR("NULL pointer free'd\n");
  21. #endif
  22.     return;
  23.   }
  24.  
  25.   a=(struct MemHeader *)__memorylist.mlh_Head;
  26.   for(;;)
  27.   {
  28.     if(((struct MinNode *)a)->mln_Succ==NULL) /* Is not in list ????? */
  29.     {
  30. #ifdef DEBUG_LIB
  31.       FATALERROR("Fake memory free'd\n");
  32. #endif
  33.       return;
  34.     }
  35.  
  36.     if(ptr>=a->mh_Lower&&ptr<a->mh_Upper) /* Entry found */
  37.       break;
  38.  
  39.     a=(struct MemHeader *)((struct MinNode *)a)->mln_Succ;
  40.   }
  41.  
  42. #ifdef DEBUG_LIB
  43.   memset(ptr,0xcc,((ULONG *)ptr)[-1]); /* Destroy contents */
  44. #endif
  45.  
  46.   Deallocate(a,(ULONG *)ptr-1,((ULONG *)ptr)[-1]);
  47.   if(a->mh_Free==(char *)a->mh_Upper-(char *)a->mh_Lower) /* All free ? */
  48.   { Remove(&a->mh_Node);
  49.     FreeMem(a,(char *)a->mh_Upper-(char *)a); }
  50. }
  51.