home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Wipeout / source / nametag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-16  |  3.6 KB  |  163 lines

  1. /*
  2.  * $Id: nametag.c 1.1 1998/04/16 09:54:38 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Wipeout -- Traces and munges memory and detects memory trashing
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. struct NameTag
  19. {
  20.     ULONG    nt_Checksum;        /* checksum for entire data structure */
  21.     ULONG    nt_Size;            /* size of entire data structure */
  22.     ULONG    nt_Segment;            /* hunk number allocation came from */
  23.     ULONG    nt_Offset;            /* hunk offset allocation came from */
  24.     LONG    nt_ProgramNameLen;    /* name of program making the call */
  25.     LONG    nt_TaskNameLen;        /* name of task/process making the call */
  26.     UBYTE    nt_Names[2];        /* both program and task name */
  27. };
  28.  
  29. /******************************************************************************/
  30.  
  31. STATIC ULONG LocalSegment;
  32. STATIC ULONG LocalOffset;
  33. STATIC UBYTE LocalProgramNameBuffer[MAX_FILENAME_LEN];
  34. STATIC UBYTE LocalTaskNameBuffer[MAX_FILENAME_LEN];
  35.  
  36. /******************************************************************************/
  37.  
  38. LONG
  39. GetNameTagLen(ULONG pc)
  40. {
  41.     LONG bytes = 0;
  42.  
  43.     /* determine the name and the origin of the caller */
  44.     if(GetTaskName(NULL,LocalTaskNameBuffer,sizeof(LocalTaskNameBuffer)))
  45.     {
  46.         if(FindAddress(pc,sizeof(LocalProgramNameBuffer),LocalProgramNameBuffer,&LocalSegment,&LocalOffset))
  47.         {
  48.             /* calculate the size of the data structure to allocate later */
  49.             bytes = ((sizeof(struct NameTag) + strlen(LocalTaskNameBuffer) + strlen(LocalProgramNameBuffer)) + 3) & ~3;
  50.         }
  51.     }
  52.  
  53.     return(bytes);
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. STATIC struct NameTag *
  59. GetNameTag(const APTR mem,LONG size)
  60. {
  61.     struct NameTag * nt = NULL;
  62.  
  63.     ASSERT(th != NULL);
  64.  
  65.     /* return the nametag corresponding to the given memory area */
  66.     if(size > 0)
  67.     {
  68.         nt = (struct NameTag *)(((ULONG)mem) - size);
  69.     }
  70.  
  71.     return(nt);
  72. }
  73.  
  74. /******************************************************************************/
  75.  
  76. VOID
  77. FillNameTag(APTR mem,LONG size)
  78. {
  79.     struct NameTag * nt;
  80.     STRPTR name;
  81.  
  82.     ASSERT(mem != NULL && size > 0);
  83.  
  84.     nt = (struct NameTag *)mem;
  85.  
  86.     /* fill in the name tag header with data */
  87.     nt->nt_Size                = size;
  88.     nt->nt_Segment            = LocalSegment;
  89.     nt->nt_Offset            = LocalOffset;
  90.     nt->nt_ProgramNameLen    = strlen(LocalProgramNameBuffer)+1;
  91.     nt->nt_TaskNameLen        = strlen(LocalTaskNameBuffer)+1;
  92.  
  93.     /* fill in the names */    
  94.     name = nt->nt_Names;
  95.     strcpy(name,LocalProgramNameBuffer);
  96.     
  97.     name += strlen(LocalProgramNameBuffer)+1;
  98.     strcpy(name,LocalTaskNameBuffer);
  99.  
  100.     /* fix the checksum */
  101.     nt->nt_Checksum    = 0;
  102.     nt->nt_Checksum = ~CalculateChecksum((ULONG *)nt,nt->nt_Size);
  103. }
  104.  
  105. /******************************************************************************/
  106.  
  107. BOOL
  108. GetNameTagData(
  109.     const APTR    mem,
  110.     LONG        size,
  111.     STRPTR *    programNamePtr,
  112.     ULONG *        segmentPtr,
  113.     ULONG *        offsetPtr,
  114.     STRPTR *    taskNamePtr)
  115. {
  116.     struct NameTag * nt;
  117.     BOOL found = FALSE;
  118.  
  119.     ASSERT(th != NULL);
  120.  
  121.     /* if a name tag header corresponds to the memory
  122.      * chunk, extract its data
  123.      */
  124.     nt = GetNameTag(mem,size);
  125.     if(nt != NULL)
  126.     {
  127.         /* is the data still intact? */
  128.         if(CalculateChecksum((ULONG *)nt,nt->nt_Size) == (ULONG)-1)
  129.         {
  130.             STRPTR name;
  131.  
  132.             /* fill in the data */
  133.  
  134.             name = nt->nt_Names;
  135.  
  136.             if(programNamePtr != NULL)
  137.             {
  138.                 (*programNamePtr) = name;
  139.             }
  140.  
  141.             if(segmentPtr != NULL)
  142.             {
  143.                 (*segmentPtr) = nt->nt_Segment;
  144.             }
  145.  
  146.             if(offsetPtr != NULL)
  147.             {
  148.                 (*offsetPtr) = nt->nt_Offset;
  149.             }
  150.  
  151.             if(taskNamePtr != NULL)
  152.             {
  153.                 name += nt->nt_ProgramNameLen;
  154.                 (*taskNamePtr) = name;
  155.             }
  156.  
  157.             found = TRUE;
  158.         }
  159.     }
  160.  
  161.     return(found);
  162. }
  163.