2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
34 * 12 Nov 98 sdouglas created.
38 #include <IOKit/IORegistryEntry.h>
39 #include <libkern/c++/OSContainers.h>
40 #include <IOKit/IOService.h>
41 #include <IOKit/IOKitKeys.h>
43 #include <IOKit/IOLib.h>
45 #include <IOKit/assert.h>
47 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
49 #define super OSObject
51 OSDefineMetaClassAndStructors(IORegistryEntry
, OSObject
)
53 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
55 #define kIORegPlaneParentSuffix "ParentLinks"
56 #define kIORegPlaneChildSuffix "ChildLinks"
57 #define kIORegPlaneNameSuffix "Name"
58 #define kIORegPlaneLocationSuffix "Location"
59 #define kIORegPlaneParentSuffixLen (sizeof(kIORegPlaneParentSuffix) - 1)
61 static IORegistryEntry
* gRegistryRoot
;
62 static OSDictionary
* gIORegistryPlanes
;
64 const OSSymbol
* gIONameKey
;
65 const OSSymbol
* gIOLocationKey
;
76 class IORegistryPlane
: public OSObject
{
78 friend class IORegistryEntry
;
80 OSDeclareAbstractStructors(IORegistryPlane
)
82 const OSSymbol
* nameKey
;
83 const OSSymbol
* keys
[ kNumSetIndex
];
84 const OSSymbol
* pathNameKey
;
85 const OSSymbol
* pathLocationKey
;
89 virtual bool serialize(OSSerialize
*s
) const;
92 OSDefineMetaClassAndStructors(IORegistryPlane
, OSObject
)
95 static IORecursiveLock
* gPropertiesLock
;
96 static SInt32 gIORegistryGenerationCount
;
98 #define UNLOCK lck_rw_done( &gIORegistryLock )
99 #define RLOCK lck_rw_lock_shared( &gIORegistryLock )
100 #define WLOCK lck_rw_lock_exclusive( &gIORegistryLock ); \
101 gIORegistryGenerationCount++
104 #define PUNLOCK IORecursiveLockUnlock( gPropertiesLock )
105 #define PLOCK IORecursiveLockLock( gPropertiesLock )
107 #define IOREGSPLITTABLES
109 #ifdef IOREGSPLITTABLES
110 #define registryTable() fRegistryTable
112 #define registryTable() fPropertyTable
117 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
119 lck_rw_t gIORegistryLock
;
120 lck_grp_t
*gIORegistryLockGrp
;
121 lck_grp_attr_t
*gIORegistryLockGrpAttr
;
122 lck_attr_t
*gIORegistryLockAttr
;
125 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
127 IORegistryEntry
* IORegistryEntry::initialize( void )
131 if( !gRegistryRoot
) {
134 gIORegistryLockGrpAttr
= lck_grp_attr_alloc_init();
135 //lck_grp_attr_setstat(gIORegistryLockGrpAttr);
136 gIORegistryLockGrp
= lck_grp_alloc_init("IORegistryLock", gIORegistryLockGrpAttr
);
137 gIORegistryLockAttr
= lck_attr_alloc_init();
138 lck_attr_rw_shared_priority(gIORegistryLockAttr
);
139 //lck_attr_setdebug(gIORegistryLockAttr);
140 lck_rw_init( &gIORegistryLock
, gIORegistryLockGrp
, gIORegistryLockAttr
);
142 gRegistryRoot
= new IORegistryEntry
;
143 gPropertiesLock
= IORecursiveLockAlloc();
144 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
146 assert( gRegistryRoot
&& gPropertiesLock
147 && gIORegistryPlanes
);
148 ok
= gRegistryRoot
->init();
150 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
151 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
153 assert( ok
&& gIONameKey
&& gIOLocationKey
);
155 gRegistryRoot
->setName( "Root" );
156 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
159 return( gRegistryRoot
);
162 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
164 return( gRegistryRoot
);
167 SInt32
IORegistryEntry::getGenerationCount( void )
169 return( gIORegistryGenerationCount
);
173 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
175 IORegistryPlane
* plane
;
176 const OSSymbol
* nameKey
;
177 const OSSymbol
* parentKey
;
178 const OSSymbol
* childKey
;
179 const OSSymbol
* pathNameKey
;
180 const OSSymbol
* pathLocationKey
;
181 char key
[ kIOMaxPlaneName
+ 16 ];
184 strncpy( key
, name
, kIOMaxPlaneName
);
185 key
[ kIOMaxPlaneName
] = 0;
186 end
= key
+ strlen( name
);
188 nameKey
= OSSymbol::withCString( key
);
190 strcpy( end
, kIORegPlaneParentSuffix
);
191 parentKey
= OSSymbol::withCString( key
);
193 strcpy( end
, kIORegPlaneChildSuffix
);
194 childKey
= OSSymbol::withCString( key
);
196 strcpy( end
, kIORegPlaneNameSuffix
);
197 pathNameKey
= OSSymbol::withCString( key
);
199 strcpy( end
, kIORegPlaneLocationSuffix
);
200 pathLocationKey
= OSSymbol::withCString( key
);
202 plane
= new IORegistryPlane
;
204 if( plane
&& plane
->init()
205 && nameKey
&& parentKey
&& childKey
206 && pathNameKey
&& pathLocationKey
) {
208 plane
->nameKey
= nameKey
;
209 plane
->keys
[ kParentSetIndex
] = parentKey
;
210 plane
->keys
[ kChildSetIndex
] = childKey
;
211 plane
->pathNameKey
= pathNameKey
;
212 plane
->pathLocationKey
= pathLocationKey
;
215 gIORegistryPlanes
->setObject( nameKey
, plane
);
223 pathLocationKey
->release();
225 pathNameKey
->release();
227 parentKey
->release();
238 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
240 const IORegistryPlane
* plane
;
243 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
249 bool IORegistryPlane::serialize(OSSerialize
*s
) const
251 return( nameKey
->serialize(s
) );
254 enum { kIORegCapacityIncrement
= 4 };
256 bool IORegistryEntry::init( OSDictionary
* dict
)
266 fPropertyTable
->release();
267 fPropertyTable
= dict
;
269 } else if( !fPropertyTable
) {
270 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
272 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
278 #ifdef IOREGSPLITTABLES
279 if( !fRegistryTable
) {
280 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
282 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
285 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
286 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
287 // ok for OSSymbol too
292 #endif /* IOREGSPLITTABLES */
297 bool IORegistryEntry::init( IORegistryEntry
* old
,
298 const IORegistryPlane
* plane
)
301 IORegistryEntry
* next
;
309 fPropertyTable
= old
->getPropertyTable();
310 fPropertyTable
->retain();
311 #ifdef IOREGSPLITTABLES
312 fRegistryTable
= old
->fRegistryTable
;
313 old
->fRegistryTable
= OSDictionary::withDictionary( fRegistryTable
);
314 #endif /* IOREGSPLITTABLES */
316 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
317 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
319 all
= getParentSetReference( plane
);
320 if( all
) for( index
= 0;
321 (next
= (IORegistryEntry
*) all
->getObject(index
));
323 next
->makeLink( this, kChildSetIndex
, plane
);
324 next
->breakLink( old
, kChildSetIndex
, plane
);
327 all
= getChildSetReference( plane
);
328 if( all
) for( index
= 0;
329 (next
= (IORegistryEntry
*) all
->getObject(index
));
331 next
->makeLink( this, kParentSetIndex
, plane
);
332 next
->breakLink( old
, kParentSetIndex
, plane
);
340 void IORegistryEntry::free( void )
344 #define msg ": attached at free()"
345 char buf
[ strlen(msg
) + 40 ];
347 if( registryTable() && gIOServicePlane
) {
348 if( getParentSetReference( gIOServicePlane
)
349 || getChildSetReference( gIOServicePlane
)) {
351 strncpy( buf
, getName(), 32);
359 if( getPropertyTable())
360 getPropertyTable()->release();
362 #ifdef IOREGSPLITTABLES
364 registryTable()->release();
365 #endif /* IOREGSPLITTABLES */
370 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
373 fPropertyTable
->release();
376 fPropertyTable
= dict
;
379 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
381 /* Wrappers to synchronize property table */
383 #define wrap2(type, constant) \
385 IORegistryEntry::copyProperty( type * aKey) constant \
390 obj = getProperty( aKey ); \
398 #define wrap4(type,constant) \
400 IORegistryEntry::getProperty( type * aKey, \
401 const IORegistryPlane * plane, \
402 IOOptionBits options ) constant \
404 OSObject * obj = getProperty( aKey ); \
406 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
407 IORegistryEntry * entry = (IORegistryEntry *) this; \
408 IORegistryIterator * iter; \
409 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
412 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
413 obj = entry->getProperty( aKey ); \
422 #define wrap5(type,constant) \
424 IORegistryEntry::copyProperty( type * aKey, \
425 const IORegistryPlane * plane, \
426 IOOptionBits options ) constant \
428 OSObject * obj = copyProperty( aKey ); \
430 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
431 IORegistryEntry * entry = (IORegistryEntry *) this; \
432 IORegistryIterator * iter; \
433 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
436 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
437 obj = entry->copyProperty( aKey ); \
446 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
448 // setProperty( getRetainCount(), 32, "__retain" );
451 OSCollection
*snapshotProperties
= getPropertyTable()->copyCollection();
454 bool ok
= snapshotProperties
->serialize( s
);
455 snapshotProperties
->release();
459 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
464 dict
= OSDictionary::withDictionary( getPropertyTable(),
465 getPropertyTable()->getCapacity() );
471 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
473 return( kIOReturnUnsupported
);
476 wrap2(const OSSymbol
, const) // copyProperty() definition
477 wrap2(const OSString
, const) // copyProperty() definition
478 wrap2(const char, const) // copyProperty() definition
480 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
481 wrap4(const OSString
, const) // getProperty() w/plane definition
482 wrap4(const char, const) // getProperty() w/plane definition
484 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
485 wrap5(const OSString
, const) // copyProperty() w/plane definition
486 wrap5(const char, const) // copyProperty() w/plane definition
490 IORegistryEntry::getProperty( const OSSymbol
* aKey
) const
495 obj
= getPropertyTable()->getObject( aKey
);
502 IORegistryEntry::removeProperty( const OSSymbol
* aKey
)
505 getPropertyTable()->removeObject( aKey
);
510 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
514 // If we are inserting a collection class and the current entry
515 // is attached into the registry (inPlane()) then mark the collection
517 OSCollection
*coll
= OSDynamicCast(OSCollection
, anObject
);
518 bool makeImmutable
= (coll
&& inPlane());
522 coll
->setOptions( OSCollection::kMASK
, OSCollection::kImmutable
);
524 ret
= getPropertyTable()->setObject( aKey
, anObject
);
530 IOReturn
IORegistryEntry::
531 runPropertyAction(Action inAction
, OSObject
*target
,
532 void *arg0
, void *arg1
, void *arg2
, void *arg3
)
536 // closeGate is recursive so don't worry if we already hold the lock.
538 res
= (*inAction
)(target
, arg0
, arg1
, arg2
, arg3
);
545 IORegistryEntry::getProperty( const OSString
* aKey
) const
547 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
548 OSObject
* obj
= getProperty( tmpKey
);
555 IORegistryEntry::getProperty( const char * aKey
) const
557 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
558 OSObject
* obj
= getProperty( tmpKey
);
566 IORegistryEntry::removeProperty( const OSString
* aKey
)
568 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
569 removeProperty( tmpKey
);
574 IORegistryEntry::removeProperty( const char * aKey
)
576 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
577 removeProperty( tmpKey
);
582 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
584 const OSSymbol
* tmpKey
= OSSymbol::withString( aKey
);
585 bool ret
= setProperty( tmpKey
, anObject
);
592 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
594 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
595 bool ret
= setProperty( tmpKey
, anObject
);
602 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
605 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
608 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
609 ret
= setProperty( tmpKey
, aSymbol
);
618 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
621 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
624 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
625 ret
= setProperty( tmpKey
, aBooleanObj
);
628 aBooleanObj
->release();
634 IORegistryEntry::setProperty( const char * aKey
,
635 unsigned long long aValue
,
636 unsigned int aNumberOfBits
)
639 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
642 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
643 ret
= setProperty( tmpKey
, anOffset
);
652 IORegistryEntry::setProperty( const char * aKey
,
657 OSData
* data
= OSData::withBytes( bytes
, length
);
660 const OSSymbol
* tmpKey
= OSSymbol::withCString( aKey
);
661 ret
= setProperty( tmpKey
, data
);
669 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
671 /* Name, location, paths */
673 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
) const
679 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
681 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
685 return( sym
->getCStringNoCopy());
687 return( (getMetaClass())->getClassName());
690 const OSSymbol
* IORegistryEntry::copyName(
691 const IORegistryPlane
* plane
) const
697 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
699 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
707 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
710 const OSSymbol
* IORegistryEntry::copyLocation(
711 const IORegistryPlane
* plane
) const
717 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
719 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
727 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
) const
729 const OSSymbol
* sym
= copyLocation( plane
);
730 const char * result
= 0;
733 result
= sym
->getCStringNoCopy();
740 void IORegistryEntry::setName( const OSSymbol
* name
,
741 const IORegistryPlane
* plane
)
743 const OSSymbol
* key
;
747 key
= plane
->pathNameKey
;
752 registryTable()->setObject( key
, (OSObject
*) name
);
757 void IORegistryEntry::setName( const char * name
,
758 const IORegistryPlane
* plane
)
760 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
762 setName( sym
, plane
);
767 void IORegistryEntry::setLocation( const OSSymbol
* location
,
768 const IORegistryPlane
* plane
)
770 const OSSymbol
* key
;
774 key
= plane
->pathLocationKey
;
776 key
= gIOLocationKey
;
779 registryTable()->setObject( key
, (OSObject
*) location
);
784 void IORegistryEntry::setLocation( const char * location
,
785 const IORegistryPlane
* plane
)
787 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
789 setLocation( sym
, plane
);
795 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
) const
797 const OSSymbol
* sym
= copyName();
800 isEqual
= sym
->isEqualTo( name
);
802 if( isEqual
&& matched
) {
814 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
) const
817 OSCollection
* collection
;
818 OSIterator
* iter
= 0;
821 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
822 iter
= OSCollectionIterator::withCollection( collection
);
825 string
= OSDynamicCast( OSString
, names
);
829 result
= compareName( string
, matched
);
831 } while( (false == result
)
832 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
841 bool IORegistryEntry::getPath( char * path
, int * length
,
842 const IORegistryPlane
* plane
) const
845 IORegistryEntry
* root
;
846 const IORegistryEntry
* entry
;
847 IORegistryEntry
* parent
;
848 const OSSymbol
* alias
;
850 int len
, maxLength
, compLen
;
854 if( !path
|| !length
|| !plane
)
858 maxLength
= *length
- 2;
861 len
= plane
->nameKey
->getLength();
862 if( len
>= maxLength
)
864 strcpy( nextComp
, plane
->nameKey
->getCStringNoCopy());
865 nextComp
[ len
++ ] = ':';
868 if( (alias
= hasAlias( plane
))) {
869 len
+= alias
->getLength();
870 ok
= (maxLength
> len
);
873 strcpy( nextComp
, alias
->getCStringNoCopy());
878 parent
= entry
->getParentEntry( plane
);
880 // Error if not attached in plane
883 stack
= OSArray::withCapacity( getDepth( plane
));
889 root
= gRegistryRoot
->getChildEntry( plane
);
890 while( parent
&& (entry
!= root
)) {
892 stack
->setObject( (OSObject
*) entry
);
894 parent
= entry
->getParentEntry( plane
);
897 index
= stack
->getCount();
906 } else while( ok
&& ((--index
) >= 0)) {
908 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
911 if( (alias
= entry
->hasAlias( plane
))) {
912 len
= plane
->nameKey
->getLength() + 1;
913 nextComp
= path
+ len
;
915 compLen
= alias
->getLength();
916 ok
= (maxLength
> len
+ compLen
);
918 strcpy( nextComp
, alias
->getCStringNoCopy());
920 compLen
= maxLength
- len
;
921 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
943 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
944 const IORegistryPlane
* plane
) const
946 int len
, locLen
, maxLength
;
947 const char * compName
;
953 compName
= getName( plane
);
954 len
= strlen( compName
);
955 if( (loc
= getLocation( plane
)))
956 locLen
= 1 + strlen( loc
);
960 ok
= ((len
+ locLen
) < maxLength
);
962 strcpy( path
, compName
);
975 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
976 const IORegistryPlane
* plane
)
979 const char * result
= 0;
981 char lastPathChar
, lastLocationChar
;
983 str
= getLocation( plane
);
985 lastPathChar
= cmp
[0];
986 lastLocationChar
= str
[0];
989 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
990 lastPathChar
= *cmp
++;
994 if( lastLocationChar
) {
995 num2
= strtouq( str
, (char **) &str
, 16 );
996 lastLocationChar
= *str
++;
1003 if (!lastPathChar
&& !lastLocationChar
) {
1008 if( (',' != lastPathChar
) && (':' != lastPathChar
))
1011 if (lastPathChar
&& lastLocationChar
&& (lastPathChar
!= lastLocationChar
))
1020 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1021 const IORegistryPlane
* plane
)
1023 IORegistryEntry
* entry
= 0;
1027 const char * cmp
= 0;
1032 set
= getChildSetReference( plane
);
1038 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1044 str
= entry
->getName( plane
);
1045 len
= strlen( str
);
1046 if( strncmp( str
, cmp
, len
))
1051 if( (c
== 0) || (c
== '/') || (c
== ':'))
1057 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1067 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1068 char * opath
, int * length
) const
1070 IORegistryEntry
* entry
;
1071 IORegistryEntry
* entry2
;
1072 const OSSymbol
* key
;
1073 const OSSymbol
* bestKey
= 0;
1076 const char * path
= "/aliases";
1078 entry
= IORegistryEntry::fromPath( path
, plane
);
1081 if( (iter
= OSCollectionIterator::withCollection(
1082 entry
->getPropertyTable() ))) {
1084 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1086 data
= (OSData
*) entry
->getProperty( key
);
1087 path
= (const char *) data
->getBytesNoCopy();
1088 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1090 if( this == entry2
) {
1092 || (bestKey
->getLength() > key
->getLength()))
1093 // pick the smallest alias
1107 const char * IORegistryEntry::dealiasPath(
1108 const char ** opath
,
1109 const IORegistryPlane
* plane
)
1111 IORegistryEntry
* entry
;
1113 const char * path
= *opath
;
1114 const char * rpath
= 0;
1117 char temp
[ kIOMaxPlaneName
+ 1 ];
1124 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1127 if( (end
- path
) < kIOMaxPlaneName
) {
1128 strncpy( temp
, path
, end
- path
);
1129 temp
[ end
- path
] = 0;
1132 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1134 data
= (OSData
*) entry
->getProperty( temp
);
1136 rpath
= (const char *) data
->getBytesNoCopy();
1148 IORegistryEntry
* IORegistryEntry::fromPath(
1150 const IORegistryPlane
* plane
,
1153 IORegistryEntry
* fromEntry
)
1155 IORegistryEntry
* where
= 0;
1156 IORegistryEntry
* aliasEntry
= 0;
1157 IORegistryEntry
* next
;
1163 char temp
[ kIOMaxPlaneName
+ 1 ];
1170 end
= strchr( path
, ':' );
1171 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1172 strncpy( temp
, path
, end
- path
);
1173 temp
[ end
- path
] = 0;
1174 plane
= getPlane( temp
);
1183 if( (alias
= dealiasPath( &end
, plane
))) {
1186 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1187 opath
, &len
, fromEntry
);
1199 if( (0 == fromEntry
) && (*path
++ == '/'))
1200 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1207 if( c
&& (c
!= ':')) // check valid terminator
1212 next
= where
->getChildFromComponent( &path
, plane
);
1218 // check residual path
1219 if( where
!= fromEntry
)
1222 if( opath
&& length
) {
1223 // copy out residual path
1224 len2
= len
+ strlen( path
);
1226 strcpy( opath
+ len
, path
);
1230 // no residual path => must be no tail for success
1237 aliasEntry
->release();
1244 IORegistryEntry
* IORegistryEntry::childFromPath(
1246 const IORegistryPlane
* plane
,
1250 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1253 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1255 #define IOLinkIterator OSCollectionIterator
1258 #define super OSObject
1260 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1261 const IORegistryEntry
* member
,
1262 unsigned int * index
) const
1265 OSObject
* probeObject
;
1267 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1268 if (probeObject
== (OSObject
*) member
) {
1277 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1278 unsigned int relation
,
1279 const IORegistryPlane
* plane
) const
1282 bool result
= false;
1284 if( (links
= (OSArray
*)
1285 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1287 result
= arrayMember( links
, to
);
1289 result
= links
->setObject( to
);
1293 links
= OSArray::withObjects( (const OSObject
**) &to
, 1, 1 );
1294 result
= (links
!= 0);
1296 result
= registryTable()->setObject( plane
->keys
[ relation
],
1305 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1306 unsigned int relation
,
1307 const IORegistryPlane
* plane
) const
1312 if( (links
= (OSArray
*)
1313 registryTable()->getObject( plane
->keys
[ relation
]))) {
1315 if( arrayMember( links
, to
, &index
)) {
1316 links
->removeObject( index
);
1317 if( 0 == links
->getCount())
1318 registryTable()->removeObject( plane
->keys
[ relation
]);
1324 OSArray
* IORegistryEntry::getParentSetReference(
1325 const IORegistryPlane
* plane
) const
1328 return( (OSArray
*) registryTable()->getObject(
1329 plane
->keys
[ kParentSetIndex
]));
1334 OSIterator
* IORegistryEntry::getParentIterator(
1335 const IORegistryPlane
* plane
) const
1344 links
= getParentSetReference( plane
);
1346 links
= OSArray::withCapacity( 1 );
1348 links
= OSArray::withArray( links
, links
->getCount() );
1351 iter
= IOLinkIterator::withCollection( links
);
1359 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1361 IORegistryEntry
* entry
= 0;
1366 if( (links
= getParentSetReference( plane
))) {
1367 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1376 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1378 IORegistryEntry
* entry
;
1380 entry
= copyParentEntry( plane
);
1387 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1390 return( (OSArray
*) registryTable()->getObject(
1391 plane
->keys
[ kChildSetIndex
]));
1396 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1405 links
= getChildSetReference( plane
);
1407 links
= OSArray::withCapacity( 1 );
1409 links
= OSArray::withArray( links
, links
->getCount() );
1412 iter
= IOLinkIterator::withCollection( links
);
1421 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1422 const IORegistryPlane
* plane
) const
1424 IORegistryEntry
* entry
= 0;
1429 if( (links
= getChildSetReference( plane
))) {
1430 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1439 IORegistryEntry
* IORegistryEntry::getChildEntry(
1440 const IORegistryPlane
* plane
) const
1442 IORegistryEntry
* entry
;
1444 entry
= copyChildEntry( plane
);
1451 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1453 const IORegistryPlane
* plane
) const
1457 IORegistryEntry
* next
;
1463 array
= OSArray::withArray( getChildSetReference( plane
));
1467 (next
= (IORegistryEntry
*) array
->getObject( index
));
1469 (*applier
)(next
, context
);
1474 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1476 const IORegistryPlane
* plane
) const
1480 IORegistryEntry
* next
;
1486 array
= OSArray::withArray( getParentSetReference( plane
));
1490 (next
= (IORegistryEntry
*) array
->getObject( index
));
1492 (*applier
)(next
, context
);
1497 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1498 const IORegistryPlane
* plane
,
1499 bool onlyChild
) const
1506 if( (links
= getChildSetReference( plane
))) {
1507 if( (!onlyChild
) || (1 == links
->getCount()))
1508 ret
= arrayMember( links
, child
);
1510 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1511 ret
= arrayMember( links
, this );
1518 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1519 const IORegistryPlane
* plane
,
1520 bool onlyParent
) const
1528 if( (links
= getParentSetReference( plane
))) {
1529 if( (!onlyParent
) || (1 == links
->getCount()))
1530 ret
= arrayMember( links
, parent
);
1532 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1533 ret
= arrayMember( links
, this );
1540 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1547 ret
= (0 != getParentSetReference( plane
));
1550 // Check to see if this is in any plane. If it is in a plane
1551 // then the registryTable will contain a key with the ParentLinks
1552 // suffix. When we iterate over the keys looking for that suffix
1555 OSCollectionIterator
*iter
=
1556 OSCollectionIterator::withCollection( registryTable());
1558 const OSSymbol
*key
;
1560 while( (key
= (OSSymbol
*) iter
->getNextObject()) ) {
1561 const char *keysuffix
;
1563 // Get a pointer to this keys suffix
1564 keysuffix
= key
->getCStringNoCopy()
1565 + key
->getLength() - kIORegPlaneParentSuffixLen
;
1566 if( !strcmp(keysuffix
, kIORegPlaneParentSuffix
) ) {
1580 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1581 const IORegistryPlane
* plane
)
1592 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1594 if( (links
= parent
->getChildSetReference( plane
)))
1595 needParent
= (false == arrayMember( links
, this ));
1603 // Mark any collections in the property list as immutable
1604 OSDictionary
*ptable
= getPropertyTable();
1605 OSCollectionIterator
*iter
=
1606 OSCollectionIterator::withCollection( ptable
);
1608 const OSSymbol
*key
;
1610 while( (key
= (OSSymbol
*) iter
->getNextObject( ))) {
1611 // Is object for key a collection?
1612 OSCollection
*coll
=
1613 OSDynamicCast( OSCollection
, ptable
->getObject( key
));
1616 // Yup so mark it as immutable
1617 coll
->setOptions( OSCollection::kMASK
,
1618 OSCollection::kImmutable
);
1627 ret
&= parent
->attachToChild( this, plane
);
1632 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1633 const IORegistryPlane
* plane
)
1644 ret
= makeLink( child
, kChildSetIndex
, plane
);
1646 if( (links
= child
->getParentSetReference( plane
)))
1647 needChild
= (false == arrayMember( links
, this ));
1654 ret
&= child
->attachToParent( this, plane
);
1659 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1660 const IORegistryPlane
* plane
)
1669 breakLink( parent
, kParentSetIndex
, plane
);
1671 if( (links
= parent
->getChildSetReference( plane
)))
1672 needParent
= arrayMember( links
, this );
1676 // parent->breakLink( this, kChildSetIndex, plane );
1681 parent
->detachFromChild( this, plane
);
1686 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1687 const IORegistryPlane
* plane
)
1696 breakLink( child
, kChildSetIndex
, plane
);
1698 if( (links
= child
->getParentSetReference( plane
)))
1699 needChild
= arrayMember( links
, this );
1706 child
->detachFromParent( this, plane
);
1711 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1713 IORegistryEntry
* parent
;
1716 while( (parent
= getParentEntry( plane
)))
1717 detachFromParent( parent
, plane
);
1721 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1724 IORegistryEntry
* next
;
1725 IORegistryIterator
* regIter
;
1727 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1730 all
= regIter
->iterateAll();
1733 detachAbove( plane
);
1735 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1738 all
->removeObject(next
);
1740 next
->detachAbove( plane
);
1747 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1749 unsigned int depth
= 1;
1751 unsigned int oneDepth
, maxParentDepth
, count
;
1752 IORegistryEntry
* one
;
1753 const IORegistryEntry
* next
;
1759 while( (parents
= next
->getParentSetReference( plane
))) {
1761 count
= parents
->getCount();
1766 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1771 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1773 oneDepth
= one
->getDepth( plane
);
1774 if( oneDepth
> maxParentDepth
)
1775 maxParentDepth
= oneDepth
;
1777 depth
+= maxParentDepth
;
1787 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1790 #define super OSIterator
1792 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1794 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1796 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1798 IORegistryIterator
*
1799 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1800 const IORegistryPlane
* plane
,
1801 IOOptionBits options
)
1803 IORegistryIterator
* create
;
1810 create
= new IORegistryIterator
;
1812 if( create
->init()) {
1815 create
->root
= root
;
1816 create
->where
= &create
->start
;
1817 create
->start
.current
= root
;
1818 create
->plane
= plane
;
1819 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1829 IORegistryIterator
*
1830 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1831 IOOptionBits options
)
1833 return( iterateOver( gRegistryRoot
, plane
, options
));
1836 bool IORegistryIterator::isValid( void )
1845 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1847 while( ok
&& next
) {
1849 ok
= where
->iter
->isValid();
1857 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1862 where
= (IORegCursor
*) IOMalloc( sizeof( IORegCursor
));
1868 where
->current
= prev
->current
;
1873 void IORegistryIterator::enterEntry( void )
1875 enterEntry( plane
);
1878 bool IORegistryIterator::exitEntry( void )
1883 where
->iter
->release();
1885 if( where
->current
)// && (where != &start))
1886 where
->current
->release();
1889 if( where
!= &start
) {
1892 IOFree( gone
, sizeof( IORegCursor
));
1899 void IORegistryIterator::reset( void )
1909 where
->current
= root
;
1910 options
&= ~kIORegistryIteratorInvalidFlag
;
1913 void IORegistryIterator::free( void )
1924 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1926 IORegistryEntry
* next
= 0;
1927 OSArray
* links
= 0;
1931 if( (0 == where
->iter
)) {
1932 // just entered - create new iter
1935 && (links
= ( (options
& kIORegistryIterateParents
) ?
1936 where
->current
->getParentSetReference( plane
) :
1937 where
->current
->getChildSetReference( plane
) )) )
1939 where
->iter
= OSCollectionIterator::withCollection( links
);
1942 // next sibling - release current
1944 where
->current
->release();
1948 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
1952 else if( !where
->iter
->isValid())
1953 options
|= kIORegistryIteratorInvalidFlag
;
1956 where
->current
= next
;
1963 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
1965 IORegistryEntry
* next
;
1968 next
= getNextObjectFlat();
1969 while( (0 == next
) && exitEntry());
1973 done
= OSOrderedSet::withCapacity( 10 );
1974 if( done
->setObject((OSObject
*) next
)) {
1975 // done set didn't contain this one, so recurse
1982 IORegistryEntry
* IORegistryIterator::getNextObject( void )
1984 if( options
& kIORegistryIterateRecursively
)
1985 return( getNextObjectRecursive());
1987 return( getNextObjectFlat());
1990 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
1993 return( where
->current
);
1998 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2001 while( getNextObjectRecursive())
2008 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2009 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2010 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2011 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2012 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2013 OSMetaClassDefineReservedUsed(IORegistryEntry
, 5);
2015 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2016 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2017 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2018 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2019 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2020 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2021 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2022 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2023 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2024 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2025 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2026 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2027 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2028 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2029 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2030 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2031 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2032 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2033 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2034 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2035 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2036 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2037 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2038 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2039 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2040 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2042 /* inline function implementation */
2043 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2044 { return(fPropertyTable
); }