1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "FunctionInstance.h"
7
8#include <new>
9
10#include "DisassembledCode.h"
11#include "Function.h"
12#include "FunctionID.h"
13#include "ImageDebugInfo.h"
14#include "LocatableFile.h"
15
16
17FunctionInstance::FunctionInstance(ImageDebugInfo* imageDebugInfo,
18	FunctionDebugInfo* functionDebugInfo)
19	:
20	fImageDebugInfo(imageDebugInfo),
21	fFunction(NULL),
22	fFunctionDebugInfo(functionDebugInfo),
23	fSourceCode(NULL),
24	fSourceCodeState(FUNCTION_SOURCE_NOT_LOADED)
25{
26	fFunctionDebugInfo->AcquireReference();
27	// TODO: What about fImageDebugInfo? We must be careful regarding cyclic
28	// references.
29}
30
31
32FunctionInstance::~FunctionInstance()
33{
34	SetFunction(NULL);
35	SetSourceCode(NULL, FUNCTION_SOURCE_NOT_LOADED);
36	fFunctionDebugInfo->ReleaseReference();
37}
38
39
40FunctionID*
41FunctionInstance::GetFunctionID() const
42{
43	if (LocatableFile* file = SourceFile()) {
44		BString path;
45		file->GetPath(path);
46		return new(std::nothrow) SourceFunctionID(path, Name());
47	}
48
49	return new(std::nothrow) ImageFunctionID(
50		GetImageDebugInfo()->GetImageInfo().Name(), Name());
51}
52
53
54void
55FunctionInstance::SetFunction(Function* function)
56{
57	if (fFunction != NULL)
58		fFunction->ReleaseReference();
59
60	fFunction = function;
61
62	if (fFunction != NULL)
63		fFunction->AcquireReference();
64}
65
66
67void
68FunctionInstance::SetSourceCode(DisassembledCode* source,
69	function_source_state state)
70{
71	if (source == fSourceCode && state == fSourceCodeState)
72		return;
73
74	if (fSourceCode != NULL)
75		fSourceCode->ReleaseReference();
76
77	fSourceCode = source;
78	fSourceCodeState = state;
79
80	if (fSourceCode != NULL)
81		fSourceCode->AcquireReference();
82
83	if (fFunction != NULL)
84		fFunction->NotifySourceCodeChanged();
85}
86