Backed out changeset b0a4653325cc (bug 941565) for Windows bustage.
[gecko.git] / tools / trace-malloc / adreader.cpp
blob48671465bd147e4691a252197edc293ae37ae2fd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "adreader.h"
7 #include <stdio.h>
8 #include <string.h>
10 ADLog::ADLog()
11 : mEntryCount(0)
13 mEntries.mNext = static_cast<EntryBlock*>(&mEntries);
14 mEntries.mPrev = static_cast<EntryBlock*>(&mEntries);
17 ADLog::~ADLog()
19 for (const_iterator entry = begin(), entry_end = end();
20 entry != entry_end; ++entry) {
21 free((void*) (*entry)->type);
22 free((char*) (*entry)->data);
23 free((char*) (*entry)->allocation_stack);
26 for (EntryBlock *b = mEntries.mNext, *next; b != &mEntries; b = next) {
27 next = b->mNext;
28 delete b;
32 bool
33 ADLog::Read(const char* aFileName)
35 FILE *in = fopen(aFileName, "r");
36 if (!in) {
37 return false;
40 while (!feof(in)) {
41 unsigned int ptr;
42 char typebuf[256];
43 int datasize;
44 int res = fscanf(in, "%x %s (%d)\n", &ptr, typebuf, &datasize);
45 if (res == EOF)
46 break;
47 if (res != 3) {
48 return false;
51 size_t data_mem_size = ((datasize + sizeof(unsigned long) - 1) /
52 sizeof(unsigned long)) * sizeof(unsigned long);
53 char *data = (char*)malloc(data_mem_size);
55 for (unsigned long *cur_data = (unsigned long*) data,
56 *cur_data_end = (unsigned long*) ((char*)data + data_mem_size);
57 cur_data != cur_data_end; ++cur_data) {
58 res = fscanf(in, " %lX\n", cur_data);
59 if (res != 1) {
60 return false;
64 char stackbuf[100000];
65 stackbuf[0] = '\0';
67 char *stack = stackbuf;
68 int len;
69 do {
70 fgets(stack, sizeof(stackbuf) - (stack - stackbuf), in);
71 len = strlen(stack);
72 stack += len;
73 } while (len > 1);
75 if (mEntryCount % ADLOG_ENTRY_BLOCK_SIZE == 0) {
76 EntryBlock *new_block = new EntryBlock();
77 new_block->mNext = static_cast<EntryBlock*>(&mEntries);
78 new_block->mPrev = mEntries.mPrev;
79 mEntries.mPrev->mNext = new_block;
80 mEntries.mPrev = new_block;
83 size_t typelen = strlen(typebuf);
84 char *type = (char*)malloc(typelen-1);
85 strncpy(type, typebuf+1, typelen-2);
86 type[typelen-2] = '\0';
88 Entry *entry =
89 &mEntries.mPrev->entries[mEntryCount % ADLOG_ENTRY_BLOCK_SIZE];
90 entry->address = (Pointer)ptr;
91 entry->type = type;
92 entry->datasize = datasize;
93 entry->data = data;
94 entry->allocation_stack = strdup(stackbuf);
96 ++mEntryCount;
99 return true;
102 ADLog::const_iterator::const_iterator(ADLog::EntryBlock *aBlock,
103 size_t aOffset)
105 SetBlock(aBlock);
106 mCur = mBlockStart + aOffset;
109 ADLog::const_iterator&
110 ADLog::const_iterator::operator++()
112 ++mCur;
113 if (mCur == mBlockEnd) {
114 SetBlock(mBlock->mNext);
115 mCur = mBlockStart;
118 return *this;
121 ADLog::const_iterator&
122 ADLog::const_iterator::operator--()
124 if (mCur == mBlockStart) {
125 SetBlock(mBlock->mPrev);
126 mCur = mBlockEnd;
128 --mCur;
130 return *this;