2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
26 * 12 Nov 98 sdouglas created.
30 #include <IOKit/IORegistryEntry.h>
31 #include <libkern/c++/OSContainers.h>
32 #include <IOKit/IOService.h>
33 #include <IOKit/IOKitKeys.h>
35 #include <IOKit/IOLib.h>
37 #include <IOKit/assert.h>
39 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
41 #define super OSObject
43 OSDefineMetaClassAndStructors(IORegistryEntry
, OSObject
)
45 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
47 static IORegistryEntry
* gRegistryRoot
;
48 static OSDictionary
* gIORegistryPlanes
;
50 const OSSymbol
* gIONameKey
;
51 const OSSymbol
* gIOLocationKey
;
62 class IORegistryPlane
: public OSObject
{
64 friend IORegistryEntry
;
66 OSDeclareAbstractStructors(IORegistryPlane
)
68 const OSSymbol
* nameKey
;
69 const OSSymbol
* keys
[ kNumSetIndex
];
70 const OSSymbol
* pathNameKey
;
71 const OSSymbol
* pathLocationKey
;
75 virtual bool serialize(OSSerialize
*s
) const;
78 OSDefineMetaClassAndStructors(IORegistryPlane
, OSObject
)
81 static IORecursiveLock
* gPropertiesLock
;
82 static SInt32 gIORegistryGenerationCount
;
84 #define UNLOCK s_lock_done( &gIORegistryLock )
85 #define RLOCK s_lock_read( &gIORegistryLock )
86 #define WLOCK s_lock_write( &gIORegistryLock ); \
87 gIORegistryGenerationCount++
90 #define PUNLOCK IORecursiveLockUnlock( gPropertiesLock )
91 #define PLOCK IORecursiveLockLock( gPropertiesLock )
93 #define IOREGSPLITTABLES
95 #ifdef IOREGSPLITTABLES
96 #define registryTable() fRegistryTable
98 #define registryTable() fPropertyTable
103 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
106 decl_simple_lock_data(,interlock
) /* "hardware" interlock field */
107 volatile unsigned int
108 read_count
:16, /* No. of accepted readers */
109 want_upgrade
:1, /* Read-to-write upgrade waiting */
110 want_write
:1, /* Writer is waiting, or
112 waiting
:1, /* Someone is sleeping on lock */
113 can_sleep
:1; /* Can attempts to lock go to sleep? */
116 static struct s_lock_t gIORegistryLock
;
118 /* Time we loop without holding the interlock.
119 * The former is for when we cannot sleep, the latter
120 * for when our thread can go to sleep (loop less)
121 * we shouldn't retake the interlock at all frequently
122 * if we cannot go to sleep, since it interferes with
123 * any other processors. In particular, 100 is too small
124 * a number for powerpc MP systems because of cache
125 * coherency issues and differing lock fetch times between
128 static unsigned int lock_wait_time
[2] = { (unsigned int)-1, 100 } ;
135 (void) memset((void *) l
, 0, sizeof(s_lock_t
));
137 simple_lock_init(&l
->interlock
, 0);
138 l
->want_write
= FALSE
;
139 l
->want_upgrade
= FALSE
;
141 l
->can_sleep
= can_sleep
;
146 register s_lock_t
* l
)
150 simple_lock(&l
->interlock
);
153 * Try to acquire the want_write bit.
155 while (l
->want_write
) {
157 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
159 simple_unlock(&l
->interlock
);
160 while (--i
!= 0 && l
->want_write
)
162 simple_lock(&l
->interlock
);
165 if (l
->can_sleep
&& l
->want_write
) {
167 thread_sleep_simple_lock((event_t
) l
,
168 simple_lock_addr(l
->interlock
), FALSE
);
169 simple_lock(&l
->interlock
);
172 l
->want_write
= TRUE
;
174 /* Wait for readers (and upgrades) to finish */
176 while ((l
->read_count
!= 0) || l
->want_upgrade
) {
178 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
180 simple_unlock(&l
->interlock
);
181 while (--i
!= 0 && (l
->read_count
!= 0 ||
184 simple_lock(&l
->interlock
);
187 if (l
->can_sleep
&& (l
->read_count
!= 0 || l
->want_upgrade
)) {
189 thread_sleep_simple_lock((event_t
) l
,
190 simple_lock_addr(l
->interlock
), FALSE
);
191 simple_lock(&l
->interlock
);
195 simple_unlock(&l
->interlock
);
200 register s_lock_t
* l
)
202 boolean_t do_wakeup
= FALSE
;
204 simple_lock(&l
->interlock
);
206 if (l
->read_count
!= 0) {
210 if (l
->want_upgrade
) {
211 l
->want_upgrade
= FALSE
;
214 l
->want_write
= FALSE
;
219 * There is no reason to wakeup a waiting thread
220 * if the read-count is non-zero. Consider:
221 * we must be dropping a read lock
222 * threads are waiting only if one wants a write lock
223 * if there are still readers, they can't proceed
225 if (l
->waiting
&& (l
->read_count
== 0)) {
230 simple_unlock(&l
->interlock
);
233 thread_wakeup((event_t
) l
);
238 register s_lock_t
* l
)
242 simple_lock(&l
->interlock
);
244 while ( l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)) {
246 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
249 simple_unlock(&l
->interlock
);
251 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)))
253 simple_lock(&l
->interlock
);
257 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
))) {
259 thread_sleep_simple_lock((event_t
) l
,
260 simple_lock_addr(l
->interlock
), FALSE
);
261 simple_lock(&l
->interlock
);
266 simple_unlock(&l
->interlock
);
270 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
272 IORegistryEntry
* IORegistryEntry::initialize( void )
276 if( !gRegistryRoot
) {
278 s_lock_init( &gIORegistryLock
, true );
279 gRegistryRoot
= new IORegistryEntry
;
280 gPropertiesLock
= IORecursiveLockAlloc();
281 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
283 assert( gRegistryRoot
&& gPropertiesLock
284 && gIORegistryPlanes
);
285 ok
= gRegistryRoot
->init();
287 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
288 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
290 assert( ok
&& gIONameKey
&& gIOLocationKey
);
292 gRegistryRoot
->setName( "Root" );
293 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
296 return( gRegistryRoot
);
299 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
301 return( gRegistryRoot
);
304 SInt32
IORegistryEntry::getGenerationCount( void )
306 return( gIORegistryGenerationCount
);
310 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
312 IORegistryPlane
* plane
;
313 const OSSymbol
* nameKey
;
314 const OSSymbol
* parentKey
;
315 const OSSymbol
* childKey
;
316 const OSSymbol
* pathNameKey
;
317 const OSSymbol
* pathLocationKey
;
318 char key
[ kIOMaxPlaneName
+ 16 ];
321 strncpy( key
, name
, kIOMaxPlaneName
);
322 key
[ kIOMaxPlaneName
] = 0;
323 end
= key
+ strlen( name
);
325 nameKey
= OSSymbol::withCString( key
);
327 strcpy( end
, "ParentLinks" );
328 parentKey
= OSSymbol::withCString( key
);
330 strcpy( end
, "ChildLinks" );
331 childKey
= OSSymbol::withCString( key
);
333 strcpy( end
, "Name" );
334 pathNameKey
= OSSymbol::withCString( key
);
336 strcpy( end
, "Location" );
337 pathLocationKey
= OSSymbol::withCString( key
);
339 plane
= new IORegistryPlane
;
341 if( plane
&& plane
->init()
342 && nameKey
&& parentKey
&& childKey
343 && pathNameKey
&& pathLocationKey
) {
345 plane
->nameKey
= nameKey
;
346 plane
->keys
[ kParentSetIndex
] = parentKey
;
347 plane
->keys
[ kChildSetIndex
] = childKey
;
348 plane
->pathNameKey
= pathNameKey
;
349 plane
->pathLocationKey
= pathLocationKey
;
352 gIORegistryPlanes
->setObject( nameKey
, plane
);
360 pathLocationKey
->release();
362 pathNameKey
->release();
364 parentKey
->release();
375 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
377 const IORegistryPlane
* plane
;
380 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
386 bool IORegistryPlane::serialize(OSSerialize
*s
) const
388 return( nameKey
->serialize(s
) );
391 enum { kIORegCapacityIncrement
= 4 };
393 bool IORegistryEntry::init( OSDictionary
* dict
= 0 )
402 fPropertyTable
= dict
;
405 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
407 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
413 #ifdef IOREGSPLITTABLES
414 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
416 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
418 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
419 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
420 // ok for OSSymbol too
425 #endif /* IOREGSPLITTABLES */
430 bool IORegistryEntry::init( IORegistryEntry
* old
,
431 const IORegistryPlane
* plane
)
434 IORegistryEntry
* next
;
442 fPropertyTable
= old
->getPropertyTable();
443 old
->fPropertyTable
= 0;
444 #ifdef IOREGSPLITTABLES
445 fRegistryTable
= old
->fRegistryTable
;
446 old
->fRegistryTable
= 0;
447 #endif /* IOREGSPLITTABLES */
449 all
= getParentSetReference( plane
);
450 if( all
) for( index
= 0;
451 (next
= (IORegistryEntry
*) all
->getObject(index
));
453 next
->makeLink( this, kChildSetIndex
, plane
);
454 next
->breakLink( old
, kChildSetIndex
, plane
);
457 all
= getChildSetReference( plane
);
458 if( all
) for( index
= 0;
459 (next
= (IORegistryEntry
*) all
->getObject(index
));
461 next
->makeLink( this, kParentSetIndex
, plane
);
462 next
->breakLink( old
, kParentSetIndex
, plane
);
470 void IORegistryEntry::free( void )
474 #define msg ": attached at free()"
475 char buf
[ strlen(msg
) + 40 ];
477 if( registryTable() && gIOServicePlane
) {
478 if( getParentSetReference( gIOServicePlane
)
479 || getChildSetReference( gIOServicePlane
)) {
481 strncpy( buf
, getName(), 32);
489 if( getPropertyTable())
490 getPropertyTable()->release();
492 #ifdef IOREGSPLITTABLES
494 registryTable()->release();
495 #endif /* IOREGSPLITTABLES */
500 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
503 fPropertyTable
->release();
506 fPropertyTable
= dict
;
509 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
511 /* Wrappers to synchronize property table */
513 #define wrap1(func,type,constant) \
515 IORegistryEntry::func ## Property( type * aKey) constant \
520 obj = getPropertyTable()->func ## Object( aKey ); \
526 #define wrap2(type,constant) \
528 IORegistryEntry::copyProperty( type * aKey) constant \
533 obj = getPropertyTable()->getObject( aKey ); \
541 #define wrap3(func,type,constant) \
543 IORegistryEntry::func ## Property( type * aKey) constant \
546 getPropertyTable()->func ## Object( aKey ); \
550 #define wrap4(type,constant) \
552 IORegistryEntry::getProperty( type * aKey, \
553 const IORegistryPlane * plane, \
554 IOOptionBits options ) constant \
556 OSObject * obj = getProperty( aKey ); \
558 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
559 IORegistryEntry * entry = (IORegistryEntry *) this; \
560 IORegistryIterator * iter; \
561 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
564 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
565 obj = entry->getProperty( aKey ); \
574 #define wrap5(type,constant) \
576 IORegistryEntry::copyProperty( type * aKey, \
577 const IORegistryPlane * plane, \
578 IOOptionBits options ) constant \
580 OSObject * obj = copyProperty( aKey ); \
582 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
583 IORegistryEntry * entry = (IORegistryEntry *) this; \
584 IORegistryIterator * iter; \
585 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
588 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
589 obj = entry->copyProperty( aKey ); \
598 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
602 // setProperty( getRetainCount(), 32, "__retain" );
605 ok
= getPropertyTable()->serialize( s
);
611 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
616 dict
= OSDictionary::withDictionary( getPropertyTable(),
617 getPropertyTable()->getCapacity() );
623 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
625 return( kIOReturnUnsupported
);
628 wrap1(get
, const OSSymbol
, const) // getProperty() definition
629 wrap1(get
, const OSString
, const) // getProperty() definition
630 wrap1(get
, const char, const) // getProperty() definition
632 wrap2(const OSSymbol
, const) // copyProperty() definition
633 wrap2(const OSString
, const) // copyProperty() definition
634 wrap2(const char, const) // copyProperty() definition
636 wrap3(remove
, const OSSymbol
,) // removeProperty() definition
637 wrap3(remove
, const OSString
,) // removeProperty() definition
638 wrap3(remove
, const char,) // removeProperty() definition
640 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
641 wrap4(const OSString
, const) // getProperty() w/plane definition
642 wrap4(const char, const) // getProperty() w/plane definition
644 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
645 wrap5(const OSString
, const) // copyProperty() w/plane definition
646 wrap5(const char, const) // copyProperty() w/plane definition
650 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
654 ret
= getPropertyTable()->setObject( aKey
, anObject
);
661 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
665 ret
= getPropertyTable()->setObject( aKey
, anObject
);
672 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
676 ret
= getPropertyTable()->setObject( aKey
, anObject
);
683 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
686 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
690 ret
= getPropertyTable()->setObject( aKey
, aSymbol
);
698 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
701 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
705 ret
= getPropertyTable()->setObject( aKey
, aBooleanObj
);
707 aBooleanObj
->release();
713 IORegistryEntry::setProperty( const char * aKey
,
714 unsigned long long aValue
,
715 unsigned int aNumberOfBits
)
718 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
722 ret
= getPropertyTable()->setObject( aKey
, anOffset
);
730 IORegistryEntry::setProperty( const char * aKey
,
735 OSData
* data
= OSData::withBytes( bytes
, length
);
739 ret
= getPropertyTable()->setObject( aKey
, data
);
746 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
748 /* Name, location, paths */
750 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
= 0 ) const
756 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
758 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
762 return( sym
->getCStringNoCopy());
764 return( (getMetaClass())->getClassName());
767 const OSSymbol
* IORegistryEntry::copyName(
768 const IORegistryPlane
* plane
= 0 ) const
774 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
776 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
784 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
787 const OSSymbol
* IORegistryEntry::copyLocation(
788 const IORegistryPlane
* plane
= 0 ) const
794 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
796 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
804 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
= 0 ) const
806 const OSSymbol
* sym
= copyLocation( plane
);
807 const char * result
= 0;
810 result
= sym
->getCStringNoCopy();
817 void IORegistryEntry::setName( const OSSymbol
* name
,
818 const IORegistryPlane
* plane
= 0 )
820 const OSSymbol
* key
;
824 key
= plane
->pathNameKey
;
829 registryTable()->setObject( key
, (OSObject
*) name
);
834 void IORegistryEntry::setName( const char * name
,
835 const IORegistryPlane
* plane
= 0 )
837 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
839 setName( sym
, plane
);
844 void IORegistryEntry::setLocation( const OSSymbol
* location
,
845 const IORegistryPlane
* plane
= 0 )
847 const OSSymbol
* key
;
851 key
= plane
->pathLocationKey
;
853 key
= gIOLocationKey
;
856 registryTable()->setObject( key
, (OSObject
*) location
);
861 void IORegistryEntry::setLocation( const char * location
,
862 const IORegistryPlane
* plane
= 0 )
864 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
866 setLocation( sym
, plane
);
872 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
= 0 ) const
874 const OSSymbol
* sym
= copyName();
877 isEqual
= sym
->isEqualTo( name
);
879 if( isEqual
&& matched
) {
891 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
= 0 ) const
894 OSCollection
* collection
;
895 OSIterator
* iter
= 0;
898 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
899 iter
= OSCollectionIterator::withCollection( collection
);
902 string
= OSDynamicCast( OSString
, names
);
906 result
= compareName( string
, matched
);
908 } while( (false == result
)
909 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
918 bool IORegistryEntry::getPath( char * path
, int * length
,
919 const IORegistryPlane
* plane
) const
922 IORegistryEntry
* root
;
923 const IORegistryEntry
* entry
;
924 IORegistryEntry
* parent
;
925 const OSSymbol
* alias
;
927 int len
, maxLength
, compLen
;
931 if( !path
|| !length
|| !plane
)
935 maxLength
= *length
- 2;
938 len
= plane
->nameKey
->getLength();
939 if( len
>= maxLength
)
941 strcpy( nextComp
, plane
->nameKey
->getCStringNoCopy());
942 nextComp
[ len
++ ] = ':';
945 if( (alias
= hasAlias( plane
))) {
946 len
+= alias
->getLength();
947 ok
= (maxLength
> len
);
950 strcpy( nextComp
, alias
->getCStringNoCopy());
955 parent
= entry
->getParentEntry( plane
);
957 // Error if not attached in plane
960 stack
= OSArray::withCapacity( getDepth( plane
));
966 root
= gRegistryRoot
->getChildEntry( plane
);
967 while( parent
&& (entry
!= root
)) {
969 stack
->setObject( (OSObject
*) entry
);
971 parent
= entry
->getParentEntry( plane
);
974 index
= stack
->getCount();
983 } else while( ok
&& ((--index
) >= 0)) {
985 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
988 if( (alias
= entry
->hasAlias( plane
))) {
989 len
= plane
->nameKey
->getLength() + 1;
990 nextComp
= path
+ len
;
992 compLen
= alias
->getLength();
993 ok
= (maxLength
> len
+ compLen
);
995 strcpy( nextComp
, alias
->getCStringNoCopy());
997 compLen
= maxLength
- len
;
998 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
1000 if( ok
&& compLen
) {
1008 nextComp
+= compLen
;
1020 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
1021 const IORegistryPlane
* plane
) const
1023 int len
, locLen
, maxLength
;
1024 const char * compName
;
1028 maxLength
= *length
;
1030 compName
= getName( plane
);
1031 len
= strlen( compName
);
1032 if( (loc
= getLocation( plane
)))
1033 locLen
= 1 + strlen( loc
);
1037 ok
= ((len
+ locLen
) < maxLength
);
1039 strcpy( path
, compName
);
1044 strcpy( path
, loc
);
1052 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
1053 const IORegistryPlane
* plane
)
1056 const char * result
= 0;
1057 u_quad_t num1
, num2
;
1060 str
= getLocation( plane
);
1064 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1066 num2
= strtouq( str
, (char **) &str
, 16 );
1076 if( (c2
== ':') && (c2
== c1
)) {
1098 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1099 const IORegistryPlane
* plane
)
1101 IORegistryEntry
* entry
= 0;
1105 const char * cmp
= 0;
1110 set
= getChildSetReference( plane
);
1116 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1122 str
= entry
->getName( plane
);
1123 len
= strlen( str
);
1124 if( strncmp( str
, cmp
, len
))
1129 if( (c
== 0) || (c
== '/') || (c
== ':'))
1135 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1145 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1146 char * opath
= 0, int * length
= 0 ) const
1148 IORegistryEntry
* entry
;
1149 IORegistryEntry
* entry2
;
1150 const OSSymbol
* key
;
1151 const OSSymbol
* bestKey
= 0;
1154 const char * path
= "/aliases";
1156 entry
= IORegistryEntry::fromPath( path
, plane
);
1159 if( (iter
= OSCollectionIterator::withCollection(
1160 entry
->getPropertyTable() ))) {
1162 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1164 data
= (OSData
*) entry
->getProperty( key
);
1165 path
= (const char *) data
->getBytesNoCopy();
1166 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1168 if( this == entry2
) {
1170 || (bestKey
->getLength() > key
->getLength()))
1171 // pick the smallest alias
1185 const char * IORegistryEntry::dealiasPath(
1186 const char ** opath
,
1187 const IORegistryPlane
* plane
)
1189 IORegistryEntry
* entry
;
1191 const char * path
= *opath
;
1192 const char * rpath
= 0;
1195 char temp
[ kIOMaxPlaneName
+ 1 ];
1202 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1205 if( (end
- path
) < kIOMaxPlaneName
) {
1206 strncpy( temp
, path
, end
- path
);
1207 temp
[ end
- path
] = 0;
1210 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1212 data
= (OSData
*) entry
->getProperty( temp
);
1214 rpath
= (const char *) data
->getBytesNoCopy();
1226 IORegistryEntry
* IORegistryEntry::fromPath(
1228 const IORegistryPlane
* plane
= 0,
1231 IORegistryEntry
* fromEntry
= 0 )
1233 IORegistryEntry
* where
= 0;
1234 IORegistryEntry
* next
;
1240 char temp
[ kIOMaxPlaneName
+ 1 ];
1247 end
= strchr( path
, ':' );
1248 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1249 strncpy( temp
, path
, end
- path
);
1250 temp
[ end
- path
] = 0;
1251 plane
= getPlane( temp
);
1260 if( (alias
= dealiasPath( &end
, plane
))) {
1263 where
= IORegistryEntry::fromPath( alias
, plane
,
1264 opath
, &len
, fromEntry
);
1275 if( (0 == fromEntry
) && (*path
++ == '/'))
1276 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1283 if( c
&& (c
!= ':')) // check valid terminator
1288 next
= where
->getChildFromComponent( &path
, plane
);
1294 // check residual path
1295 if( where
!= fromEntry
)
1298 if( opath
&& length
) {
1299 // copy out residual path
1300 len2
= len
+ strlen( path
);
1302 strcpy( opath
+ len
, path
);
1306 // no residual path => must be no tail for success
1318 IORegistryEntry
* IORegistryEntry::childFromPath(
1320 const IORegistryPlane
* plane
= 0,
1324 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1327 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1329 #define IOLinkIterator OSCollectionIterator
1332 #define super OSObject
1334 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1335 const IORegistryEntry
* member
,
1336 unsigned int * index
= 0 ) const
1339 OSObject
* probeObject
;
1341 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1342 if (probeObject
== (OSObject
*) member
) {
1351 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1352 unsigned int relation
,
1353 const IORegistryPlane
* plane
) const
1356 bool result
= false;
1358 if( (links
= (OSArray
*)
1359 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1361 result
= arrayMember( links
, to
);
1363 result
= links
->setObject( to
);
1367 links
= OSArray::withObjects( & (const OSObject
*) to
, 1, 1 );
1368 result
= (links
!= 0);
1370 result
= registryTable()->setObject( plane
->keys
[ relation
],
1379 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1380 unsigned int relation
,
1381 const IORegistryPlane
* plane
) const
1386 if( (links
= (OSArray
*)
1387 registryTable()->getObject( plane
->keys
[ relation
]))) {
1389 if( arrayMember( links
, to
, &index
)) {
1390 links
->removeObject( index
);
1391 if( 0 == links
->getCount())
1392 registryTable()->removeObject( plane
->keys
[ relation
]);
1398 OSArray
* IORegistryEntry::getParentSetReference(
1399 const IORegistryPlane
* plane
) const
1402 return( (OSArray
*) registryTable()->getObject(
1403 plane
->keys
[ kParentSetIndex
]));
1408 OSIterator
* IORegistryEntry::getParentIterator(
1409 const IORegistryPlane
* plane
) const
1418 links
= getParentSetReference( plane
);
1420 links
= OSArray::withCapacity( 1 );
1422 links
= OSArray::withArray( links
, links
->getCount() );
1425 iter
= IOLinkIterator::withCollection( links
);
1433 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1435 IORegistryEntry
* entry
= 0;
1440 if( (links
= getParentSetReference( plane
))) {
1441 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1450 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1452 IORegistryEntry
* entry
;
1454 entry
= copyParentEntry( plane
);
1461 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1464 return( (OSArray
*) registryTable()->getObject(
1465 plane
->keys
[ kChildSetIndex
]));
1470 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1479 links
= getChildSetReference( plane
);
1481 links
= OSArray::withCapacity( 1 );
1483 links
= OSArray::withArray( links
, links
->getCount() );
1486 iter
= IOLinkIterator::withCollection( links
);
1495 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1496 const IORegistryPlane
* plane
) const
1498 IORegistryEntry
* entry
= 0;
1503 if( (links
= getChildSetReference( plane
))) {
1504 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1513 IORegistryEntry
* IORegistryEntry::getChildEntry(
1514 const IORegistryPlane
* plane
) const
1516 IORegistryEntry
* entry
;
1518 entry
= copyChildEntry( plane
);
1525 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1527 const IORegistryPlane
* plane
) const
1531 IORegistryEntry
* next
;
1537 array
= OSArray::withArray( getChildSetReference( plane
));
1541 (next
= (IORegistryEntry
*) array
->getObject( index
));
1543 (*applier
)(next
, context
);
1548 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1550 const IORegistryPlane
* plane
) const
1554 IORegistryEntry
* next
;
1560 array
= OSArray::withArray( getParentSetReference( plane
));
1564 (next
= (IORegistryEntry
*) array
->getObject( index
));
1566 (*applier
)(next
, context
);
1571 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1572 const IORegistryPlane
* plane
,
1573 bool onlyChild
= false ) const
1580 if( (links
= getChildSetReference( plane
))) {
1581 if( (!onlyChild
) || (1 == links
->getCount()))
1582 ret
= arrayMember( links
, child
);
1584 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1585 ret
= arrayMember( links
, this );
1592 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1593 const IORegistryPlane
* plane
,
1594 bool onlyParent
= false ) const
1602 if( (links
= getParentSetReference( plane
))) {
1603 if( (!onlyParent
) || (1 == links
->getCount()))
1604 ret
= arrayMember( links
, parent
);
1606 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1607 ret
= arrayMember( links
, this );
1614 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1620 ret
= (0 != getParentSetReference( plane
));
1627 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1628 const IORegistryPlane
* plane
)
1639 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1641 if( (links
= parent
->getChildSetReference( plane
)))
1642 needParent
= (false == arrayMember( links
, this ));
1646 // ret &= parent->makeLink( this, kChildSetIndex, plane );
1651 ret
&= parent
->attachToChild( this, plane
);
1656 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1657 const IORegistryPlane
* plane
)
1668 ret
= makeLink( child
, kChildSetIndex
, plane
);
1670 if( (links
= child
->getParentSetReference( plane
)))
1671 needChild
= (false == arrayMember( links
, this ));
1678 ret
&= child
->attachToParent( this, plane
);
1683 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1684 const IORegistryPlane
* plane
)
1693 breakLink( parent
, kParentSetIndex
, plane
);
1695 if( (links
= parent
->getChildSetReference( plane
)))
1696 needParent
= arrayMember( links
, this );
1700 // parent->breakLink( this, kChildSetIndex, plane );
1705 parent
->detachFromChild( this, plane
);
1710 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1711 const IORegistryPlane
* plane
)
1720 breakLink( child
, kChildSetIndex
, plane
);
1722 if( (links
= child
->getParentSetReference( plane
)))
1723 needChild
= arrayMember( links
, this );
1730 child
->detachFromParent( this, plane
);
1735 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1737 IORegistryEntry
* parent
;
1740 while( (parent
= getParentEntry( plane
)))
1741 detachFromParent( parent
, plane
);
1745 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1748 IORegistryEntry
* next
;
1749 IORegistryIterator
* regIter
;
1751 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1754 all
= regIter
->iterateAll();
1757 detachAbove( plane
);
1759 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1762 all
->removeObject(next
);
1764 next
->detachAbove( plane
);
1771 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1773 unsigned int depth
= 1;
1775 unsigned int oneDepth
, maxParentDepth
, count
;
1776 IORegistryEntry
* one
;
1777 const IORegistryEntry
* next
;
1783 while( (parents
= next
->getParentSetReference( plane
))) {
1785 count
= parents
->getCount();
1790 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1795 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1797 oneDepth
= one
->getDepth( plane
);
1798 if( oneDepth
> maxParentDepth
)
1799 maxParentDepth
= oneDepth
;
1801 depth
+= maxParentDepth
;
1811 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1814 #define super OSIterator
1816 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1818 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1820 IORegistryIterator
*
1821 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1822 const IORegistryPlane
* plane
,
1823 IOOptionBits options
= 0 )
1825 IORegistryIterator
* create
;
1832 create
= new IORegistryIterator
;
1834 if( create
->init()) {
1837 create
->root
= root
;
1838 create
->where
= &create
->start
;
1839 create
->start
.current
= root
;
1840 create
->plane
= plane
;
1841 create
->options
= options
;
1851 IORegistryIterator
*
1852 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1853 IOOptionBits options
= 0 )
1855 return( iterateOver( gRegistryRoot
, plane
, options
));
1858 bool IORegistryIterator::isValid( void )
1867 while( ok
&& next
) {
1869 ok
= where
->iter
->isValid();
1877 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1882 where
= (IORegCursor
*) IOMalloc( sizeof( IORegCursor
));
1888 where
->current
= prev
->current
;
1893 void IORegistryIterator::enterEntry( void )
1895 enterEntry( plane
);
1898 bool IORegistryIterator::exitEntry( void )
1903 where
->iter
->release();
1905 if( where
->current
)// && (where != &start))
1906 where
->current
->release();
1909 if( where
!= &start
) {
1912 IOFree( gone
, sizeof( IORegCursor
));
1919 void IORegistryIterator::reset( void )
1929 where
->current
= root
;
1932 void IORegistryIterator::free( void )
1943 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1945 IORegistryEntry
* next
= 0;
1946 OSArray
* links
= 0;
1950 if( (0 == where
->iter
)) {
1951 // just entered - create new iter
1954 && (links
= ( (options
& kIORegistryIterateParents
) ?
1955 where
->current
->getParentSetReference( plane
) :
1956 where
->current
->getChildSetReference( plane
) )) )
1958 where
->iter
= OSCollectionIterator::withCollection( links
);
1961 // next sibling - release current
1963 where
->current
->release();
1966 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
1971 where
->current
= next
;
1978 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
1980 IORegistryEntry
* next
;
1983 next
= getNextObjectFlat();
1984 while( (0 == next
) && exitEntry());
1988 done
= OSOrderedSet::withCapacity( 10 );
1989 if( done
->setObject((OSObject
*) next
)) {
1990 // done set didn't contain this one, so recurse
1997 IORegistryEntry
* IORegistryIterator::getNextObject( void )
1999 if( options
& kIORegistryIterateRecursively
)
2000 return( getNextObjectRecursive());
2002 return( getNextObjectFlat());
2005 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2008 return( where
->current
);
2013 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2016 while( getNextObjectRecursive())
2023 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2024 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2025 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2026 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2027 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2029 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2030 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2031 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2032 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2033 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2034 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2035 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2036 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2037 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2038 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2039 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2040 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2041 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2042 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2043 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2044 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2045 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2046 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2047 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2048 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2049 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2050 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2051 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2052 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2053 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2054 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2055 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);