/* 
 * $RCSfile: datatrace_ext.c,v $
 *
 * x-kernel v3.3
 *
 * Copyright (c) 1993,1991,1990,1996  Arizona Board of Regents
 *
 * $Log: datatrace_ext.c,v $
 * Revision 1.3  1997/06/26 07:09:47  venkat
 * fixed problem with multiple log buffers in file. venkat
 *
 * Revision 1.2  1996/01/29  20:00:56  slm
 * Updated copyright and version.
 *
 * Revision 1.1  1995/07/28  21:45:22  slm
 * Initial revision
 *
 * Revision 1.1  1994/10/27  20:57:08  hkaram
 * Initial revision
 */

#include <stdio.h>
#include "xkernel.h"

#include "datatrace.h"

/*
 * dtPostAmbleLocation
 *      Returns the offset from the beginning of the file
 *      to the beginning of the postamble information.
 */
unsigned long
dtPostAmbleLocation(dthdr *fileHdr)
{
  unsigned long off;

  off = sizeof(dthdr);
  if (fileHdr->lastBufferIdx % fileHdr->numberBuffers == 0) {
    off += (fileHdr->numberBuffers - 1) * fileHdr->bufferSize;
    off += fileHdr->lastBufferSize;
  }
  else {
    off += fileHdr->numberBuffers * fileHdr->bufferSize;
  }
  return off;
}


dttracemapentry *dtGetTraceMap(dthdr *fileHdr)
{
  int i, j, num;
  dttracemapentry *map;

  num = fileHdr->numberBuffers;
  map = (dttracemapentry *)malloc((num+1)*sizeof(dttracemapentry));
  if (! map) return NULL;

  for (j = num, i = fileHdr->lastBufferIdx; j > 0; j--, i--) {
    if (j == num)
      map[j-1].size = fileHdr->lastBufferSize;
    else
      map[j-1].size = fileHdr->bufferSize;
    /* venkat 6/25/97: Fixed seek's here */
    /* Old version always got the seek for last the traceMap entry */
    /* commented out version is the old one */
    /* fixed it to be relative to j-1 */
    /* map[j-1].seek = (((fileHdr->lastBufferIdx -1) % num)*/
     map[j-1].seek = (((j-1) % num)
                                * fileHdr->bufferSize) + sizeof(dthdr);
  }
  map[num].size = map[num].seek = -1;
  return map;
}
