1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Copyright 2013, Rene Gollent, rene@gollent.com.
4 * Distributed under the terms of the MIT License.
5 */
6
7
8#include "BaseUnit.h"
9
10#include <new>
11
12#include "DebugInfoEntries.h"
13
14
15BaseUnit::BaseUnit(off_t headerOffset, off_t contentOffset,
16	off_t totalSize, off_t abbreviationOffset, uint8 addressSize,
17	bool isBigEndian, bool isDwarf64)
18	:
19	fHeaderOffset(headerOffset),
20	fContentOffset(contentOffset),
21	fTotalSize(totalSize),
22	fAbbreviationOffset(abbreviationOffset),
23	fAbbreviationTable(NULL),
24	fAddressSize(addressSize),
25	fIsBigEndian(isBigEndian),
26	fIsDwarf64(isDwarf64)
27{
28}
29
30
31BaseUnit::~BaseUnit()
32{
33	for (int32 i = 0; i < fEntries.Count(); i++)
34		delete fEntries[i];
35}
36
37
38void
39BaseUnit::SetAbbreviationTable(AbbreviationTable* abbreviationTable)
40{
41	fAbbreviationTable = abbreviationTable;
42}
43
44
45status_t
46BaseUnit::AddDebugInfoEntry(DebugInfoEntry* entry, off_t offset)
47{
48	if (!fEntries.Add(entry))
49		return B_NO_MEMORY;
50	if (!fEntryOffsets.Add(offset)) {
51		fEntries.Remove(fEntries.Count() - 1);
52		return B_NO_MEMORY;
53	}
54
55	return B_OK;
56}
57
58
59bool
60BaseUnit::ContainsAbsoluteOffset(off_t offset) const
61{
62	return fHeaderOffset <= offset && fHeaderOffset + fTotalSize > offset;
63}
64
65
66void
67BaseUnit::SetSourceLanguage(const SourceLanguageInfo* language)
68{
69	fSourceLanguage = language;
70}
71
72
73int
74BaseUnit::CountEntries() const
75{
76	return fEntries.Count();
77}
78
79
80void
81BaseUnit::GetEntryAt(int index, DebugInfoEntry*& entry,
82	off_t& offset) const
83{
84	entry = fEntries[index];
85	offset = fEntryOffsets[index];
86}
87
88
89DebugInfoEntry*
90BaseUnit::EntryForOffset(off_t offset) const
91{
92	if (fEntries.IsEmpty())
93		return NULL;
94
95	// binary search
96	int lower = 0;
97	int upper = fEntries.Count() - 1;
98	while (lower < upper) {
99		int mid = (lower + upper + 1) / 2;
100		if (fEntryOffsets[mid] > offset)
101			upper = mid - 1;
102		else
103			lower = mid;
104	}
105
106	return fEntryOffsets[lower] == offset ? fEntries[lower] : NULL;
107}
108