xref: /haiku/src/kits/debugger/debug_info/ImageDebugLoadingStateHandlerRoster.cpp (revision 5e7964b0a929555415798dea3373db9ac4611caa)
1 /*
2  * Copyright 2014, Rene Gollent, rene@gollent.com.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "ImageDebugLoadingStateHandlerRoster.h"
8 
9 #include <new>
10 
11 #include <AutoDeleter.h>
12 #include <AutoLocker.h>
13 
14 #include "DwarfLoadingStateHandler.h"
15 #include "ImageDebugInfoLoadingState.h"
16 #include "ImageDebugLoadingStateHandler.h"
17 #include "SpecificImageDebugInfoLoadingState.h"
18 
19 
20 /*static*/ ImageDebugLoadingStateHandlerRoster*
21 	ImageDebugLoadingStateHandlerRoster::sDefaultInstance = NULL;
22 
23 
24 ImageDebugLoadingStateHandlerRoster::ImageDebugLoadingStateHandlerRoster()
25 	:
26 	fLock("type handler roster")
27 {
28 }
29 
30 
31 ImageDebugLoadingStateHandlerRoster::~ImageDebugLoadingStateHandlerRoster()
32 {
33 }
34 
35 
36 /*static*/ ImageDebugLoadingStateHandlerRoster*
37 ImageDebugLoadingStateHandlerRoster::Default()
38 {
39 	return sDefaultInstance;
40 }
41 
42 
43 /*static*/ status_t
44 ImageDebugLoadingStateHandlerRoster::CreateDefault()
45 {
46 	if (sDefaultInstance != NULL)
47 		return B_OK;
48 
49 	ImageDebugLoadingStateHandlerRoster* roster
50 		= new(std::nothrow) ImageDebugLoadingStateHandlerRoster;
51 	if (roster == NULL)
52 		return B_NO_MEMORY;
53 	ObjectDeleter<ImageDebugLoadingStateHandlerRoster> rosterDeleter(roster);
54 
55 	status_t error = roster->Init();
56 	if (error != B_OK)
57 		return error;
58 
59 	error = roster->RegisterDefaultHandlers();
60 	if (error != B_OK)
61 		return error;
62 
63 	sDefaultInstance = rosterDeleter.Detach();
64 	return B_OK;
65 }
66 
67 
68 /*static*/ void
69 ImageDebugLoadingStateHandlerRoster::DeleteDefault()
70 {
71 	ImageDebugLoadingStateHandlerRoster* roster = sDefaultInstance;
72 	sDefaultInstance = NULL;
73 	delete roster;
74 }
75 
76 
77 status_t
78 ImageDebugLoadingStateHandlerRoster::Init()
79 {
80 	return fLock.InitCheck();
81 }
82 
83 
84 status_t
85 ImageDebugLoadingStateHandlerRoster::RegisterDefaultHandlers()
86 {
87 	ImageDebugLoadingStateHandler* handler;
88 	BReference<ImageDebugLoadingStateHandler> handlerReference;
89 
90 	handler = new(std::nothrow) DwarfLoadingStateHandler();
91 	if (handler == NULL)
92 		return B_NO_MEMORY;
93 	handlerReference.SetTo(handler, true);
94 
95 	if (!RegisterHandler(handler))
96 		return B_NO_MEMORY;
97 
98 	return B_OK;
99 }
100 
101 
102 status_t
103 ImageDebugLoadingStateHandlerRoster::FindStateHandler(
104 	SpecificImageDebugInfoLoadingState* state,
105 	ImageDebugLoadingStateHandler*& _handler)
106 {
107 	AutoLocker<BLocker> locker(fLock);
108 
109 	bool found = false;
110 	ImageDebugLoadingStateHandler* handler = NULL;
111 	for (int32 i = 0; (handler = fStateHandlers.ItemAt(i)); i++) {
112 		if ((found = handler->SupportsState(state)))
113 			break;
114 	}
115 
116 	if (!found)
117 		return B_ENTRY_NOT_FOUND;
118 
119 	handler->AcquireReference();
120 	_handler = handler;
121 	return B_OK;
122 }
123 
124 
125 bool
126 ImageDebugLoadingStateHandlerRoster::RegisterHandler(
127 	ImageDebugLoadingStateHandler* handler)
128 {
129 	if (!fStateHandlers.AddItem(handler))
130 		return false;
131 
132 	handler->AcquireReference();
133 	return true;
134 }
135 
136 
137 void
138 ImageDebugLoadingStateHandlerRoster::UnregisterHandler(
139 	ImageDebugLoadingStateHandler* handler)
140 {
141 	if (fStateHandlers.RemoveItem(handler))
142 		handler->ReleaseReference();
143 }
144