xref: /haiku/src/kits/debugger/dwarf/BaseUnit.cpp (revision 56430ad8002b8fd1ac69b590e9cc130de6d9e852)
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 }
33 
34 
35 void
36 BaseUnit::SetAbbreviationTable(AbbreviationTable* abbreviationTable)
37 {
38 	fAbbreviationTable = abbreviationTable;
39 }
40 
41 
42 status_t
43 BaseUnit::AddDebugInfoEntry(DebugInfoEntry* entry, off_t offset)
44 {
45 	if (!fEntries.Add(entry))
46 		return B_NO_MEMORY;
47 	if (!fEntryOffsets.Add(offset)) {
48 		fEntries.Remove(fEntries.Count() - 1);
49 		return B_NO_MEMORY;
50 	}
51 
52 	return B_OK;
53 }
54 
55 
56 bool
57 BaseUnit::ContainsAbsoluteOffset(off_t offset) const
58 {
59 	return fHeaderOffset <= offset && fHeaderOffset + fTotalSize > offset;
60 }
61 
62 
63 void
64 BaseUnit::SetSourceLanguage(const SourceLanguageInfo* language)
65 {
66 	fSourceLanguage = language;
67 }
68 
69 
70 int
71 BaseUnit::CountEntries() const
72 {
73 	return fEntries.Count();
74 }
75 
76 
77 void
78 BaseUnit::GetEntryAt(int index, DebugInfoEntry*& entry,
79 	off_t& offset) const
80 {
81 	entry = fEntries[index];
82 	offset = fEntryOffsets[index];
83 }
84 
85 
86 DebugInfoEntry*
87 BaseUnit::EntryForOffset(off_t offset) const
88 {
89 	if (fEntries.IsEmpty())
90 		return NULL;
91 
92 	// binary search
93 	int lower = 0;
94 	int upper = fEntries.Count() - 1;
95 	while (lower < upper) {
96 		int mid = (lower + upper + 1) / 2;
97 		if (fEntryOffsets[mid] > offset)
98 			upper = mid - 1;
99 		else
100 			lower = mid;
101 	}
102 
103 	return fEntryOffsets[lower] == offset ? fEntries[lower] : NULL;
104 }
105