xref: /haiku/src/kits/debugger/dwarf/BaseUnit.cpp (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
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 
15 BaseUnit::BaseUnit(off_t headerOffset, off_t contentOffset,
16 	off_t totalSize, off_t abbreviationOffset, uint8 addressSize,
17 	bool isDwarf64)
18 	:
19 	fHeaderOffset(headerOffset),
20 	fContentOffset(contentOffset),
21 	fTotalSize(totalSize),
22 	fAbbreviationOffset(abbreviationOffset),
23 	fAbbreviationTable(NULL),
24 	fAddressSize(addressSize),
25 	fIsDwarf64(isDwarf64)
26 {
27 }
28 
29 
30 BaseUnit::~BaseUnit()
31 {
32 	for (int32 i = 0; i < fEntries.Count(); i++)
33 		delete fEntries[i];
34 }
35 
36 
37 void
38 BaseUnit::SetAbbreviationTable(AbbreviationTable* abbreviationTable)
39 {
40 	fAbbreviationTable = abbreviationTable;
41 }
42 
43 
44 status_t
45 BaseUnit::AddDebugInfoEntry(DebugInfoEntry* entry, off_t offset)
46 {
47 	if (!fEntries.Add(entry))
48 		return B_NO_MEMORY;
49 	if (!fEntryOffsets.Add(offset)) {
50 		fEntries.Remove(fEntries.Count() - 1);
51 		return B_NO_MEMORY;
52 	}
53 
54 	return B_OK;
55 }
56 
57 
58 bool
59 BaseUnit::ContainsAbsoluteOffset(off_t offset) const
60 {
61 	return fHeaderOffset <= offset && fHeaderOffset + fTotalSize > offset;
62 }
63 
64 
65 void
66 BaseUnit::SetSourceLanguage(const SourceLanguageInfo* language)
67 {
68 	fSourceLanguage = language;
69 }
70 
71 
72 int
73 BaseUnit::CountEntries() const
74 {
75 	return fEntries.Count();
76 }
77 
78 
79 void
80 BaseUnit::GetEntryAt(int index, DebugInfoEntry*& entry,
81 	off_t& offset) const
82 {
83 	entry = fEntries[index];
84 	offset = fEntryOffsets[index];
85 }
86 
87 
88 DebugInfoEntry*
89 BaseUnit::EntryForOffset(off_t offset) const
90 {
91 	if (fEntries.IsEmpty())
92 		return NULL;
93 
94 	// binary search
95 	int lower = 0;
96 	int upper = fEntries.Count() - 1;
97 	while (lower < upper) {
98 		int mid = (lower + upper + 1) / 2;
99 		if (fEntryOffsets[mid] > offset)
100 			upper = mid - 1;
101 		else
102 			lower = mid;
103 	}
104 
105 	return fEntryOffsets[lower] == offset ? fEntries[lower] : NULL;
106 }
107