#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <getopt.h>
#include <time.h>
/* The trace file looks like an array of struct __malloc_trace_buffer_s */
#include "mtrace.h"
/* This application is "run once and exit" so there's no cleanup code. */
typedef unsigned char byte;
struct __malloc_trace_buffer_s *trace_records;
size_t num_trace_records;
int verbose = 0;
int use_file_buffers = 0;
//------------------------------------------------------------
// File data buffers
static int tmpfile_idx = 0;
static char *tmpdir;
static int tmpdir_len;
#define BUFFER_SIZE 4096
/* If we're using memory buffers, we chain from first_buffer to
last_buffer as a linked list. If we're using disk buffers, we only
use last_buffer, which points to a fixed buffer. */
typedef struct BufferBlock {
struct BufferBlock *next;
byte buf[BUFFER_SIZE];
} BufferBlock;
typedef struct Buffer {
char *filename;
int fd;
BufferBlock *first_buffer;
BufferBlock *last_buffer;
size_t count_total;
size_t count_last;
} Buffer;
void
Buffer__ctor(Buffer *this)
{
if (use_file_buffers)
{
this->filename = (char *) malloc (tmpdir_len + 7);
sprintf (this->filename, "%s%06d", tmpdir, tmpfile_idx);
tmpfile_idx ++;
this->fd = -1;
}
this->first_buffer = this->last_buffer = (BufferBlock *) malloc (sizeof(BufferBlock));
this->first_buffer->next = NULL;
this->count_total = this->count_last = 0;
}
void
Buffer__add (Buffer *this, char x)
{
if (this->count_last == BUFFER_SIZE)
{
if (use_file_buffers)
{
if (this->fd == -1)
{
this->fd = o