home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / desklib / Libraries / Mem / c / CheckHeap next >
Encoding:
Text File  |  1994-05-22  |  1.8 KB  |  65 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Mem.CheckHeap.c
  12.     Author:  Copyright © 1994 Jason Howat
  13.     Version: 1.00 (11 May 1994)
  14.     Purpose: Dynamic memory manager - checks validity of heap
  15. */
  16.  
  17. #include "MemDefs.h"
  18. #include "Error.h"
  19.  
  20. extern BOOL Mem_CheckHeap(void)
  21. /*  Returns TRUE if the heap data structure is valid (i.e. the links are all
  22.  *  intact and anchors are consistent)
  23.  */
  24. {
  25.   mem_header *chunk = (mem_header *) mem__heap,
  26.              *next,
  27.              *end_of_heap = (mem_header *) (mem__heap + mem__heapsize);
  28.  
  29.   while(TRUE)
  30.   {
  31.     if(!ISFREE(chunk) &&
  32.        ((int) *(chunk->handle) != ((int) chunk + sizeof(mem_header))))
  33.     {
  34.       Error_Report(0,"Mem heap corrupt: Inconsistent chunk handle and anchor");
  35.       return(FALSE);
  36.     }
  37.  
  38.     if(chunk->realsize < chunk->datasize + sizeof(mem_header))
  39.     {
  40.       Error_Report(0, "Mem heap corrupt: Data larger than chunk");
  41.       return(FALSE);
  42.     }
  43.  
  44.     next = (mem_header *) ((int)chunk + chunk->realsize);
  45.     if(next >= end_of_heap)
  46.       break;
  47.  
  48.     if(chunk->realsize != next->prevrealsize)
  49.     {
  50.       Error_Report(0, "Mem heap corrupt: Chunk link(s) corrupt");
  51.       return(FALSE);
  52.     }
  53.  
  54.     chunk = next;
  55.   }
  56.  
  57.   if(next != end_of_heap)
  58.   {
  59.     Error_Report(0, "Mem heap corrupt: Last chunk too big");
  60.     return(FALSE);
  61.   }
  62.  
  63.   return(TRUE);
  64. }
  65.