2 * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #define IOKIT_ENABLE_SHARED_PTR
31 #include <IOKit/IOKernelReportStructs.h>
32 #include <IOKit/IOKernelReporters.h>
35 //#define IORDEBUG_LEGEND 1
37 #ifdef IORDEBUG_LEGEND
38 #define IORLEGENDLOG(fmt, args...) \
40 IOLog("IOReportLegend | "); \
45 #define IORLEGENDLOG(fmt, args...)
49 #define super OSObject
50 OSDefineMetaClassAndStructors(IOReportLegend
, OSObject
);
52 OSSharedPtr
<IOReportLegend
>
53 IOReportLegend::with(OSArray
*legend
)
55 OSSharedPtr
<IOReportLegend
> iorLegend
= OSMakeShared
<IOReportLegend
>();
59 if (iorLegend
->initWith(legend
) != kIOReturnSuccess
) {
70 /* must clean up everything if it fails */
72 IOReportLegend::initWith(OSArray
*legend
)
75 _reportLegend
= OSArray::withArray(legend
);
78 if (_reportLegend
== NULL
) {
79 return kIOReturnError
;
81 return kIOReturnSuccess
;
87 IOReportLegend::free(void)
94 IOReportLegend::getLegend(void)
96 return _reportLegend
.get();
100 IOReportLegend::addReporterLegend(IOService
*reportingService
,
101 IOReporter
*reporter
,
102 const char *groupName
,
103 const char *subGroupName
)
105 IOReturn res
= kIOReturnError
;
106 OSSharedPtr
<IOReportLegend
> legend
;
107 OSSharedPtr
<OSObject
> curLegend
;
109 // No need to check groupName and subGroupName because optional params
110 if (!reportingService
|| !reporter
) {
114 // It's fine if the legend doesn't exist (IOReportLegend::with(NULL)
115 // is how you make an empty legend). If it's not an array, then
116 // we're just going to replace it.
117 curLegend
= reportingService
->copyProperty(kIOReportLegendKey
);
118 legend
= IOReportLegend::with(OSDynamicCast(OSArray
, curLegend
.get()));
123 // Add the reporter's entries and update the service property.
124 // The overwrite triggers a release of the old legend array.
125 legend
->addReporterLegend(reporter
, groupName
, subGroupName
);
126 reportingService
->setProperty(kIOReportLegendKey
, legend
->getLegend());
127 reportingService
->setProperty(kIOReportLegendPublicKey
, true);
129 res
= kIOReturnSuccess
;
137 IOReportLegend::addLegendEntry(IOReportLegendEntry
*legendEntry
,
138 const char *groupName
,
139 const char *subGroupName
)
141 kern_return_t res
= kIOReturnError
;
142 OSSharedPtr
<const OSSymbol
> tmpGroupName
;
143 OSSharedPtr
<const OSSymbol
> tmpSubGroupName
;
150 tmpGroupName
= OSSymbol::withCString(groupName
);
154 tmpSubGroupName
= OSSymbol::withCString(subGroupName
);
157 // It is ok to call appendLegendWith() if tmpGroups are NULL
158 res
= organizeLegend(legendEntry
, tmpGroupName
.get(), tmpSubGroupName
.get());
165 IOReportLegend::addReporterLegend(IOReporter
*reporter
,
166 const char *groupName
,
167 const char *subGroupName
)
169 IOReturn res
= kIOReturnError
;
170 OSSharedPtr
<IOReportLegendEntry
> legendEntry
;
173 legendEntry
= reporter
->createLegend();
176 res
= addLegendEntry(legendEntry
.get(), groupName
, subGroupName
);
185 IOReportLegend::organizeLegend(IOReportLegendEntry
*legendEntry
,
186 const OSSymbol
*groupName
,
187 const OSSymbol
*subGroupName
)
190 return kIOReturnBadArgument
;
193 if (!groupName
&& subGroupName
) {
194 return kIOReturnBadArgument
;
197 IORLEGENDLOG("IOReportLegend::organizeLegend");
198 // Legend is empty, enter first node
199 if (_reportLegend
== NULL
) {
200 IORLEGENDLOG("IOReportLegend::new legend creation");
201 _reportLegend
= OSArray::withCapacity(1);
203 if (!_reportLegend
) {
204 return kIOReturnNoMemory
;
209 legendEntry
->setObject(kIOReportLegendGroupNameKey
, groupName
);
213 legendEntry
->setObject(kIOReportLegendSubGroupNameKey
, subGroupName
);
216 _reportLegend
->setObject(legendEntry
);
218 // callers can now safely release legendEntry (it is part of _reportLegend)
220 return kIOReturnSuccess
;