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 class 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
),
170 /* interlock relocked */
173 l
->want_write
= TRUE
;
175 /* Wait for readers (and upgrades) to finish */
177 while ((l
->read_count
!= 0) || l
->want_upgrade
) {
179 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
181 simple_unlock(&l
->interlock
);
182 while (--i
!= 0 && (l
->read_count
!= 0 ||
185 simple_lock(&l
->interlock
);
188 if (l
->can_sleep
&& (l
->read_count
!= 0 || l
->want_upgrade
)) {
190 thread_sleep_simple_lock((event_t
) l
,
191 simple_lock_addr(l
->interlock
),
193 /* interlock relocked */
197 simple_unlock(&l
->interlock
);
202 register s_lock_t
* l
)
204 boolean_t do_wakeup
= FALSE
;
206 simple_lock(&l
->interlock
);
208 if (l
->read_count
!= 0) {
212 if (l
->want_upgrade
) {
213 l
->want_upgrade
= FALSE
;
216 l
->want_write
= FALSE
;
221 * There is no reason to wakeup a waiting thread
222 * if the read-count is non-zero. Consider:
223 * we must be dropping a read lock
224 * threads are waiting only if one wants a write lock
225 * if there are still readers, they can't proceed
227 if (l
->waiting
&& (l
->read_count
== 0)) {
232 simple_unlock(&l
->interlock
);
235 thread_wakeup((event_t
) l
);
240 register s_lock_t
* l
)
244 simple_lock(&l
->interlock
);
246 while ( l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)) {
248 i
= lock_wait_time
[l
->can_sleep
? 1 : 0];
251 simple_unlock(&l
->interlock
);
253 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
)))
255 simple_lock(&l
->interlock
);
259 (l
->want_upgrade
|| ((0 == l
->read_count
) && l
->want_write
))) {
261 thread_sleep_simple_lock((event_t
) l
,
262 simple_lock_addr(l
->interlock
),
264 /* interlock relocked */
269 simple_unlock(&l
->interlock
);
273 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
275 IORegistryEntry
* IORegistryEntry::initialize( void )
279 if( !gRegistryRoot
) {
281 s_lock_init( &gIORegistryLock
, true );
282 gRegistryRoot
= new IORegistryEntry
;
283 gPropertiesLock
= IORecursiveLockAlloc();
284 gIORegistryPlanes
= OSDictionary::withCapacity( 1 );
286 assert( gRegistryRoot
&& gPropertiesLock
287 && gIORegistryPlanes
);
288 ok
= gRegistryRoot
->init();
290 gIONameKey
= OSSymbol::withCStringNoCopy( "IOName" );
291 gIOLocationKey
= OSSymbol::withCStringNoCopy( "IOLocation" );
293 assert( ok
&& gIONameKey
&& gIOLocationKey
);
295 gRegistryRoot
->setName( "Root" );
296 gRegistryRoot
->setProperty( kIORegistryPlanesKey
, gIORegistryPlanes
);
299 return( gRegistryRoot
);
302 IORegistryEntry
* IORegistryEntry::getRegistryRoot( void )
304 return( gRegistryRoot
);
307 SInt32
IORegistryEntry::getGenerationCount( void )
309 return( gIORegistryGenerationCount
);
313 const IORegistryPlane
* IORegistryEntry::makePlane( const char * name
)
315 IORegistryPlane
* plane
;
316 const OSSymbol
* nameKey
;
317 const OSSymbol
* parentKey
;
318 const OSSymbol
* childKey
;
319 const OSSymbol
* pathNameKey
;
320 const OSSymbol
* pathLocationKey
;
321 char key
[ kIOMaxPlaneName
+ 16 ];
324 strncpy( key
, name
, kIOMaxPlaneName
);
325 key
[ kIOMaxPlaneName
] = 0;
326 end
= key
+ strlen( name
);
328 nameKey
= OSSymbol::withCString( key
);
330 strcpy( end
, "ParentLinks" );
331 parentKey
= OSSymbol::withCString( key
);
333 strcpy( end
, "ChildLinks" );
334 childKey
= OSSymbol::withCString( key
);
336 strcpy( end
, "Name" );
337 pathNameKey
= OSSymbol::withCString( key
);
339 strcpy( end
, "Location" );
340 pathLocationKey
= OSSymbol::withCString( key
);
342 plane
= new IORegistryPlane
;
344 if( plane
&& plane
->init()
345 && nameKey
&& parentKey
&& childKey
346 && pathNameKey
&& pathLocationKey
) {
348 plane
->nameKey
= nameKey
;
349 plane
->keys
[ kParentSetIndex
] = parentKey
;
350 plane
->keys
[ kChildSetIndex
] = childKey
;
351 plane
->pathNameKey
= pathNameKey
;
352 plane
->pathLocationKey
= pathLocationKey
;
355 gIORegistryPlanes
->setObject( nameKey
, plane
);
363 pathLocationKey
->release();
365 pathNameKey
->release();
367 parentKey
->release();
378 const IORegistryPlane
* IORegistryEntry::getPlane( const char * name
)
380 const IORegistryPlane
* plane
;
383 plane
= (const IORegistryPlane
*) gIORegistryPlanes
->getObject( name
);
389 bool IORegistryPlane::serialize(OSSerialize
*s
) const
391 return( nameKey
->serialize(s
) );
394 enum { kIORegCapacityIncrement
= 4 };
396 bool IORegistryEntry::init( OSDictionary
* dict
= 0 )
406 fPropertyTable
->release();
407 fPropertyTable
= dict
;
409 } else if( !fPropertyTable
) {
410 fPropertyTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
412 fPropertyTable
->setCapacityIncrement( kIORegCapacityIncrement
);
418 #ifdef IOREGSPLITTABLES
419 if( !fRegistryTable
) {
420 fRegistryTable
= OSDictionary::withCapacity( kIORegCapacityIncrement
);
422 fRegistryTable
->setCapacityIncrement( kIORegCapacityIncrement
);
425 if( (prop
= OSDynamicCast( OSString
, getProperty( gIONameKey
)))) {
426 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withString( prop
);
427 // ok for OSSymbol too
432 #endif /* IOREGSPLITTABLES */
437 bool IORegistryEntry::init( IORegistryEntry
* old
,
438 const IORegistryPlane
* plane
)
441 IORegistryEntry
* next
;
449 fPropertyTable
= old
->getPropertyTable();
450 fPropertyTable
->retain();
451 #ifdef IOREGSPLITTABLES
452 fRegistryTable
= old
->fRegistryTable
;
453 old
->fRegistryTable
= OSDictionary::withDictionary( fRegistryTable
);
454 #endif /* IOREGSPLITTABLES */
456 old
->registryTable()->removeObject( plane
->keys
[ kParentSetIndex
] );
457 old
->registryTable()->removeObject( plane
->keys
[ kChildSetIndex
] );
459 all
= getParentSetReference( plane
);
460 if( all
) for( index
= 0;
461 (next
= (IORegistryEntry
*) all
->getObject(index
));
463 next
->makeLink( this, kChildSetIndex
, plane
);
464 next
->breakLink( old
, kChildSetIndex
, plane
);
467 all
= getChildSetReference( plane
);
468 if( all
) for( index
= 0;
469 (next
= (IORegistryEntry
*) all
->getObject(index
));
471 next
->makeLink( this, kParentSetIndex
, plane
);
472 next
->breakLink( old
, kParentSetIndex
, plane
);
480 void IORegistryEntry::free( void )
484 #define msg ": attached at free()"
485 char buf
[ strlen(msg
) + 40 ];
487 if( registryTable() && gIOServicePlane
) {
488 if( getParentSetReference( gIOServicePlane
)
489 || getChildSetReference( gIOServicePlane
)) {
491 strncpy( buf
, getName(), 32);
499 if( getPropertyTable())
500 getPropertyTable()->release();
502 #ifdef IOREGSPLITTABLES
504 registryTable()->release();
505 #endif /* IOREGSPLITTABLES */
510 void IORegistryEntry::setPropertyTable( OSDictionary
* dict
)
513 fPropertyTable
->release();
516 fPropertyTable
= dict
;
519 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
521 /* Wrappers to synchronize property table */
523 #define wrap1(func, type, constant) \
525 IORegistryEntry::func ## Property( type * aKey) constant \
530 obj = getPropertyTable()->func ## Object( aKey ); \
536 #define wrap2(type, constant) \
538 IORegistryEntry::copyProperty( type * aKey) constant \
543 obj = getProperty( aKey ); \
551 #define wrap3(func,type,constant) \
553 IORegistryEntry::func ## Property( type * aKey) constant \
556 getPropertyTable()->func ## Object( aKey ); \
560 #define wrap4(type,constant) \
562 IORegistryEntry::getProperty( type * aKey, \
563 const IORegistryPlane * plane, \
564 IOOptionBits options ) constant \
566 OSObject * obj = getProperty( aKey ); \
568 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
569 IORegistryEntry * entry = (IORegistryEntry *) this; \
570 IORegistryIterator * iter; \
571 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
574 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
575 obj = entry->getProperty( aKey ); \
584 #define wrap5(type,constant) \
586 IORegistryEntry::copyProperty( type * aKey, \
587 const IORegistryPlane * plane, \
588 IOOptionBits options ) constant \
590 OSObject * obj = copyProperty( aKey ); \
592 if ( (0 == obj) && plane && (options & kIORegistryIterateRecursively) ) { \
593 IORegistryEntry * entry = (IORegistryEntry *) this; \
594 IORegistryIterator * iter; \
595 iter = IORegistryIterator::iterateOver( entry, plane, options ); \
598 while ( (0 == obj) && (entry = iter->getNextObject()) ) { \
599 obj = entry->copyProperty( aKey ); \
608 bool IORegistryEntry::serializeProperties( OSSerialize
* s
) const
612 // setProperty( getRetainCount(), 32, "__retain" );
615 ok
= getPropertyTable()->serialize( s
);
621 OSDictionary
* IORegistryEntry::dictionaryWithProperties( void ) const
626 dict
= OSDictionary::withDictionary( getPropertyTable(),
627 getPropertyTable()->getCapacity() );
633 IOReturn
IORegistryEntry::setProperties( OSObject
* properties
)
635 return( kIOReturnUnsupported
);
638 wrap1(get
, const OSSymbol
, const) // getProperty() definition
639 wrap1(get
, const OSString
, const) // getProperty() definition
640 wrap1(get
, const char, const) // getProperty() definition
642 wrap2(const OSSymbol
, const) // copyProperty() definition
643 wrap2(const OSString
, const) // copyProperty() definition
644 wrap2(const char, const) // copyProperty() definition
646 wrap3(remove
, const OSSymbol
,) // removeProperty() definition
647 wrap3(remove
, const OSString
,) // removeProperty() definition
648 wrap3(remove
, const char,) // removeProperty() definition
650 wrap4(const OSSymbol
, const) // getProperty() w/plane definition
651 wrap4(const OSString
, const) // getProperty() w/plane definition
652 wrap4(const char, const) // getProperty() w/plane definition
654 wrap5(const OSSymbol
, const) // copyProperty() w/plane definition
655 wrap5(const OSString
, const) // copyProperty() w/plane definition
656 wrap5(const char, const) // copyProperty() w/plane definition
660 IORegistryEntry::setProperty( const OSSymbol
* aKey
, OSObject
* anObject
)
664 ret
= getPropertyTable()->setObject( aKey
, anObject
);
671 IORegistryEntry::setProperty( const OSString
* aKey
, OSObject
* anObject
)
675 ret
= getPropertyTable()->setObject( aKey
, anObject
);
682 IORegistryEntry::setProperty( const char * aKey
, OSObject
* anObject
)
686 ret
= getPropertyTable()->setObject( aKey
, anObject
);
693 IORegistryEntry::setProperty(const char * aKey
, const char * aString
)
696 OSSymbol
* aSymbol
= (OSSymbol
*) OSSymbol::withCString( aString
);
700 ret
= getPropertyTable()->setObject( aKey
, aSymbol
);
708 IORegistryEntry::setProperty(const char * aKey
, bool aBoolean
)
711 OSBoolean
* aBooleanObj
= OSBoolean::withBoolean( aBoolean
);
715 ret
= getPropertyTable()->setObject( aKey
, aBooleanObj
);
717 aBooleanObj
->release();
723 IORegistryEntry::setProperty( const char * aKey
,
724 unsigned long long aValue
,
725 unsigned int aNumberOfBits
)
728 OSNumber
* anOffset
= OSNumber::withNumber( aValue
, aNumberOfBits
);
732 ret
= getPropertyTable()->setObject( aKey
, anOffset
);
740 IORegistryEntry::setProperty( const char * aKey
,
745 OSData
* data
= OSData::withBytes( bytes
, length
);
749 ret
= getPropertyTable()->setObject( aKey
, data
);
756 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
758 /* Name, location, paths */
760 const char * IORegistryEntry::getName( const IORegistryPlane
* plane
= 0 ) const
766 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
768 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
772 return( sym
->getCStringNoCopy());
774 return( (getMetaClass())->getClassName());
777 const OSSymbol
* IORegistryEntry::copyName(
778 const IORegistryPlane
* plane
= 0 ) const
784 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathNameKey
);
786 sym
= (OSSymbol
*) registryTable()->getObject( gIONameKey
);
794 return( OSSymbol::withCString((getMetaClass())->getClassName()) );
797 const OSSymbol
* IORegistryEntry::copyLocation(
798 const IORegistryPlane
* plane
= 0 ) const
804 sym
= (OSSymbol
*) registryTable()->getObject( plane
->pathLocationKey
);
806 sym
= (OSSymbol
*) registryTable()->getObject( gIOLocationKey
);
814 const char * IORegistryEntry::getLocation( const IORegistryPlane
* plane
= 0 ) const
816 const OSSymbol
* sym
= copyLocation( plane
);
817 const char * result
= 0;
820 result
= sym
->getCStringNoCopy();
827 void IORegistryEntry::setName( const OSSymbol
* name
,
828 const IORegistryPlane
* plane
= 0 )
830 const OSSymbol
* key
;
834 key
= plane
->pathNameKey
;
839 registryTable()->setObject( key
, (OSObject
*) name
);
844 void IORegistryEntry::setName( const char * name
,
845 const IORegistryPlane
* plane
= 0 )
847 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( name
);
849 setName( sym
, plane
);
854 void IORegistryEntry::setLocation( const OSSymbol
* location
,
855 const IORegistryPlane
* plane
= 0 )
857 const OSSymbol
* key
;
861 key
= plane
->pathLocationKey
;
863 key
= gIOLocationKey
;
866 registryTable()->setObject( key
, (OSObject
*) location
);
871 void IORegistryEntry::setLocation( const char * location
,
872 const IORegistryPlane
* plane
= 0 )
874 OSSymbol
* sym
= (OSSymbol
*)OSSymbol::withCString( location
);
876 setLocation( sym
, plane
);
882 IORegistryEntry::compareName( OSString
* name
, OSString
** matched
= 0 ) const
884 const OSSymbol
* sym
= copyName();
887 isEqual
= sym
->isEqualTo( name
);
889 if( isEqual
&& matched
) {
901 IORegistryEntry::compareNames( OSObject
* names
, OSString
** matched
= 0 ) const
904 OSCollection
* collection
;
905 OSIterator
* iter
= 0;
908 if( (collection
= OSDynamicCast( OSCollection
, names
))) {
909 iter
= OSCollectionIterator::withCollection( collection
);
912 string
= OSDynamicCast( OSString
, names
);
916 result
= compareName( string
, matched
);
918 } while( (false == result
)
919 && iter
&& (string
= OSDynamicCast( OSString
, iter
->getNextObject())));
928 bool IORegistryEntry::getPath( char * path
, int * length
,
929 const IORegistryPlane
* plane
) const
932 IORegistryEntry
* root
;
933 const IORegistryEntry
* entry
;
934 IORegistryEntry
* parent
;
935 const OSSymbol
* alias
;
937 int len
, maxLength
, compLen
;
941 if( !path
|| !length
|| !plane
)
945 maxLength
= *length
- 2;
948 len
= plane
->nameKey
->getLength();
949 if( len
>= maxLength
)
951 strcpy( nextComp
, plane
->nameKey
->getCStringNoCopy());
952 nextComp
[ len
++ ] = ':';
955 if( (alias
= hasAlias( plane
))) {
956 len
+= alias
->getLength();
957 ok
= (maxLength
> len
);
960 strcpy( nextComp
, alias
->getCStringNoCopy());
965 parent
= entry
->getParentEntry( plane
);
967 // Error if not attached in plane
970 stack
= OSArray::withCapacity( getDepth( plane
));
976 root
= gRegistryRoot
->getChildEntry( plane
);
977 while( parent
&& (entry
!= root
)) {
979 stack
->setObject( (OSObject
*) entry
);
981 parent
= entry
->getParentEntry( plane
);
984 index
= stack
->getCount();
993 } else while( ok
&& ((--index
) >= 0)) {
995 entry
= (IORegistryEntry
*) stack
->getObject((unsigned int) index
);
998 if( (alias
= entry
->hasAlias( plane
))) {
999 len
= plane
->nameKey
->getLength() + 1;
1000 nextComp
= path
+ len
;
1002 compLen
= alias
->getLength();
1003 ok
= (maxLength
> len
+ compLen
);
1005 strcpy( nextComp
, alias
->getCStringNoCopy());
1007 compLen
= maxLength
- len
;
1008 ok
= entry
->getPathComponent( nextComp
+ 1, &compLen
, plane
);
1010 if( ok
&& compLen
) {
1018 nextComp
+= compLen
;
1030 bool IORegistryEntry::getPathComponent( char * path
, int * length
,
1031 const IORegistryPlane
* plane
) const
1033 int len
, locLen
, maxLength
;
1034 const char * compName
;
1038 maxLength
= *length
;
1040 compName
= getName( plane
);
1041 len
= strlen( compName
);
1042 if( (loc
= getLocation( plane
)))
1043 locLen
= 1 + strlen( loc
);
1047 ok
= ((len
+ locLen
) < maxLength
);
1049 strcpy( path
, compName
);
1054 strcpy( path
, loc
);
1062 const char * IORegistryEntry::matchPathLocation( const char * cmp
,
1063 const IORegistryPlane
* plane
)
1066 const char * result
= 0;
1067 u_quad_t num1
, num2
;
1070 str
= getLocation( plane
);
1074 num1
= strtouq( cmp
, (char **) &cmp
, 16 );
1076 num2
= strtouq( str
, (char **) &str
, 16 );
1086 if( (c2
== ':') && (c2
== c1
)) {
1108 IORegistryEntry
* IORegistryEntry::getChildFromComponent( const char ** opath
,
1109 const IORegistryPlane
* plane
)
1111 IORegistryEntry
* entry
= 0;
1115 const char * cmp
= 0;
1120 set
= getChildSetReference( plane
);
1126 (entry
= (IORegistryEntry
*) set
->getObject(index
));
1132 str
= entry
->getName( plane
);
1133 len
= strlen( str
);
1134 if( strncmp( str
, cmp
, len
))
1139 if( (c
== 0) || (c
== '/') || (c
== ':'))
1145 if( (cmp
= entry
->matchPathLocation( cmp
, plane
)))
1155 const OSSymbol
* IORegistryEntry::hasAlias( const IORegistryPlane
* plane
,
1156 char * opath
= 0, int * length
= 0 ) const
1158 IORegistryEntry
* entry
;
1159 IORegistryEntry
* entry2
;
1160 const OSSymbol
* key
;
1161 const OSSymbol
* bestKey
= 0;
1164 const char * path
= "/aliases";
1166 entry
= IORegistryEntry::fromPath( path
, plane
);
1169 if( (iter
= OSCollectionIterator::withCollection(
1170 entry
->getPropertyTable() ))) {
1172 while( (key
= (OSSymbol
*) iter
->getNextObject())) {
1174 data
= (OSData
*) entry
->getProperty( key
);
1175 path
= (const char *) data
->getBytesNoCopy();
1176 if( (entry2
= IORegistryEntry::fromPath( path
, plane
,
1178 if( this == entry2
) {
1180 || (bestKey
->getLength() > key
->getLength()))
1181 // pick the smallest alias
1195 const char * IORegistryEntry::dealiasPath(
1196 const char ** opath
,
1197 const IORegistryPlane
* plane
)
1199 IORegistryEntry
* entry
;
1201 const char * path
= *opath
;
1202 const char * rpath
= 0;
1205 char temp
[ kIOMaxPlaneName
+ 1 ];
1212 while( (c
= *end
++) && (c
!= '/') && (c
!= ':'))
1215 if( (end
- path
) < kIOMaxPlaneName
) {
1216 strncpy( temp
, path
, end
- path
);
1217 temp
[ end
- path
] = 0;
1220 entry
= IORegistryEntry::fromPath( "/aliases", plane
);
1222 data
= (OSData
*) entry
->getProperty( temp
);
1224 rpath
= (const char *) data
->getBytesNoCopy();
1236 IORegistryEntry
* IORegistryEntry::fromPath(
1238 const IORegistryPlane
* plane
= 0,
1241 IORegistryEntry
* fromEntry
= 0 )
1243 IORegistryEntry
* where
= 0;
1244 IORegistryEntry
* aliasEntry
= 0;
1245 IORegistryEntry
* next
;
1251 char temp
[ kIOMaxPlaneName
+ 1 ];
1258 end
= strchr( path
, ':' );
1259 if( end
&& ((end
- path
) < kIOMaxPlaneName
)) {
1260 strncpy( temp
, path
, end
- path
);
1261 temp
[ end
- path
] = 0;
1262 plane
= getPlane( temp
);
1271 if( (alias
= dealiasPath( &end
, plane
))) {
1274 aliasEntry
= IORegistryEntry::fromPath( alias
, plane
,
1275 opath
, &len
, fromEntry
);
1287 if( (0 == fromEntry
) && (*path
++ == '/'))
1288 fromEntry
= gRegistryRoot
->getChildEntry( plane
);
1295 if( c
&& (c
!= ':')) // check valid terminator
1300 next
= where
->getChildFromComponent( &path
, plane
);
1306 // check residual path
1307 if( where
!= fromEntry
)
1310 if( opath
&& length
) {
1311 // copy out residual path
1312 len2
= len
+ strlen( path
);
1314 strcpy( opath
+ len
, path
);
1318 // no residual path => must be no tail for success
1325 aliasEntry
->release();
1332 IORegistryEntry
* IORegistryEntry::childFromPath(
1334 const IORegistryPlane
* plane
= 0,
1338 return( IORegistryEntry::fromPath( path
, plane
, opath
, len
, this ));
1341 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1343 #define IOLinkIterator OSCollectionIterator
1346 #define super OSObject
1348 inline bool IORegistryEntry::arrayMember( OSArray
* set
,
1349 const IORegistryEntry
* member
,
1350 unsigned int * index
= 0 ) const
1353 OSObject
* probeObject
;
1355 for( i
= 0; (probeObject
= set
->getObject(i
)); i
++) {
1356 if (probeObject
== (OSObject
*) member
) {
1365 bool IORegistryEntry::makeLink( IORegistryEntry
* to
,
1366 unsigned int relation
,
1367 const IORegistryPlane
* plane
) const
1370 bool result
= false;
1372 if( (links
= (OSArray
*)
1373 registryTable()->getObject( plane
->keys
[ relation
] ))) {
1375 result
= arrayMember( links
, to
);
1377 result
= links
->setObject( to
);
1381 links
= OSArray::withObjects( & (const OSObject
*) to
, 1, 1 );
1382 result
= (links
!= 0);
1384 result
= registryTable()->setObject( plane
->keys
[ relation
],
1393 void IORegistryEntry::breakLink( IORegistryEntry
* to
,
1394 unsigned int relation
,
1395 const IORegistryPlane
* plane
) const
1400 if( (links
= (OSArray
*)
1401 registryTable()->getObject( plane
->keys
[ relation
]))) {
1403 if( arrayMember( links
, to
, &index
)) {
1404 links
->removeObject( index
);
1405 if( 0 == links
->getCount())
1406 registryTable()->removeObject( plane
->keys
[ relation
]);
1412 OSArray
* IORegistryEntry::getParentSetReference(
1413 const IORegistryPlane
* plane
) const
1416 return( (OSArray
*) registryTable()->getObject(
1417 plane
->keys
[ kParentSetIndex
]));
1422 OSIterator
* IORegistryEntry::getParentIterator(
1423 const IORegistryPlane
* plane
) const
1432 links
= getParentSetReference( plane
);
1434 links
= OSArray::withCapacity( 1 );
1436 links
= OSArray::withArray( links
, links
->getCount() );
1439 iter
= IOLinkIterator::withCollection( links
);
1447 IORegistryEntry
* IORegistryEntry::copyParentEntry( const IORegistryPlane
* plane
) const
1449 IORegistryEntry
* entry
= 0;
1454 if( (links
= getParentSetReference( plane
))) {
1455 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1464 IORegistryEntry
* IORegistryEntry::getParentEntry( const IORegistryPlane
* plane
) const
1466 IORegistryEntry
* entry
;
1468 entry
= copyParentEntry( plane
);
1475 OSArray
* IORegistryEntry::getChildSetReference( const IORegistryPlane
* plane
) const
1478 return( (OSArray
*) registryTable()->getObject(
1479 plane
->keys
[ kChildSetIndex
]));
1484 OSIterator
* IORegistryEntry::getChildIterator( const IORegistryPlane
* plane
) const
1493 links
= getChildSetReference( plane
);
1495 links
= OSArray::withCapacity( 1 );
1497 links
= OSArray::withArray( links
, links
->getCount() );
1500 iter
= IOLinkIterator::withCollection( links
);
1509 IORegistryEntry
* IORegistryEntry::copyChildEntry(
1510 const IORegistryPlane
* plane
) const
1512 IORegistryEntry
* entry
= 0;
1517 if( (links
= getChildSetReference( plane
))) {
1518 entry
= (IORegistryEntry
*) links
->getObject( 0 );
1527 IORegistryEntry
* IORegistryEntry::getChildEntry(
1528 const IORegistryPlane
* plane
) const
1530 IORegistryEntry
* entry
;
1532 entry
= copyChildEntry( plane
);
1539 void IORegistryEntry::applyToChildren( IORegistryEntryApplierFunction applier
,
1541 const IORegistryPlane
* plane
) const
1545 IORegistryEntry
* next
;
1551 array
= OSArray::withArray( getChildSetReference( plane
));
1555 (next
= (IORegistryEntry
*) array
->getObject( index
));
1557 (*applier
)(next
, context
);
1562 void IORegistryEntry::applyToParents( IORegistryEntryApplierFunction applier
,
1564 const IORegistryPlane
* plane
) const
1568 IORegistryEntry
* next
;
1574 array
= OSArray::withArray( getParentSetReference( plane
));
1578 (next
= (IORegistryEntry
*) array
->getObject( index
));
1580 (*applier
)(next
, context
);
1585 bool IORegistryEntry::isChild( IORegistryEntry
* child
,
1586 const IORegistryPlane
* plane
,
1587 bool onlyChild
= false ) const
1594 if( (links
= getChildSetReference( plane
))) {
1595 if( (!onlyChild
) || (1 == links
->getCount()))
1596 ret
= arrayMember( links
, child
);
1598 if( ret
&& (links
= child
->getParentSetReference( plane
)))
1599 ret
= arrayMember( links
, this );
1606 bool IORegistryEntry::isParent( IORegistryEntry
* parent
,
1607 const IORegistryPlane
* plane
,
1608 bool onlyParent
= false ) const
1616 if( (links
= getParentSetReference( plane
))) {
1617 if( (!onlyParent
) || (1 == links
->getCount()))
1618 ret
= arrayMember( links
, parent
);
1620 if( ret
&& (links
= parent
->getChildSetReference( plane
)))
1621 ret
= arrayMember( links
, this );
1628 bool IORegistryEntry::inPlane( const IORegistryPlane
* plane
) const
1634 ret
= (0 != getParentSetReference( plane
));
1641 bool IORegistryEntry::attachToParent( IORegistryEntry
* parent
,
1642 const IORegistryPlane
* plane
)
1653 ret
= makeLink( parent
, kParentSetIndex
, plane
);
1655 if( (links
= parent
->getChildSetReference( plane
)))
1656 needParent
= (false == arrayMember( links
, this ));
1660 // ret &= parent->makeLink( this, kChildSetIndex, plane );
1665 ret
&= parent
->attachToChild( this, plane
);
1670 bool IORegistryEntry::attachToChild( IORegistryEntry
* child
,
1671 const IORegistryPlane
* plane
)
1682 ret
= makeLink( child
, kChildSetIndex
, plane
);
1684 if( (links
= child
->getParentSetReference( plane
)))
1685 needChild
= (false == arrayMember( links
, this ));
1692 ret
&= child
->attachToParent( this, plane
);
1697 void IORegistryEntry::detachFromParent( IORegistryEntry
* parent
,
1698 const IORegistryPlane
* plane
)
1707 breakLink( parent
, kParentSetIndex
, plane
);
1709 if( (links
= parent
->getChildSetReference( plane
)))
1710 needParent
= arrayMember( links
, this );
1714 // parent->breakLink( this, kChildSetIndex, plane );
1719 parent
->detachFromChild( this, plane
);
1724 void IORegistryEntry::detachFromChild( IORegistryEntry
* child
,
1725 const IORegistryPlane
* plane
)
1734 breakLink( child
, kChildSetIndex
, plane
);
1736 if( (links
= child
->getParentSetReference( plane
)))
1737 needChild
= arrayMember( links
, this );
1744 child
->detachFromParent( this, plane
);
1749 void IORegistryEntry::detachAbove( const IORegistryPlane
* plane
)
1751 IORegistryEntry
* parent
;
1754 while( (parent
= getParentEntry( plane
)))
1755 detachFromParent( parent
, plane
);
1759 void IORegistryEntry::detachAll( const IORegistryPlane
* plane
)
1762 IORegistryEntry
* next
;
1763 IORegistryIterator
* regIter
;
1765 regIter
= IORegistryIterator::iterateOver( this, plane
, true );
1768 all
= regIter
->iterateAll();
1771 detachAbove( plane
);
1773 while( (next
= (IORegistryEntry
*) all
->getLastObject())) {
1776 all
->removeObject(next
);
1778 next
->detachAbove( plane
);
1785 unsigned int IORegistryEntry::getDepth( const IORegistryPlane
* plane
) const
1787 unsigned int depth
= 1;
1789 unsigned int oneDepth
, maxParentDepth
, count
;
1790 IORegistryEntry
* one
;
1791 const IORegistryEntry
* next
;
1797 while( (parents
= next
->getParentSetReference( plane
))) {
1799 count
= parents
->getCount();
1804 next
= (IORegistryEntry
*) parents
->getObject( 0 );
1809 (one
= (IORegistryEntry
*) parents
->getObject( index
));
1811 oneDepth
= one
->getDepth( plane
);
1812 if( oneDepth
> maxParentDepth
)
1813 maxParentDepth
= oneDepth
;
1815 depth
+= maxParentDepth
;
1825 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1828 #define super OSIterator
1830 OSDefineMetaClassAndStructors(IORegistryIterator
, OSIterator
)
1832 enum { kIORegistryIteratorInvalidFlag
= 0x80000000 };
1834 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1836 IORegistryIterator
*
1837 IORegistryIterator::iterateOver( IORegistryEntry
* root
,
1838 const IORegistryPlane
* plane
,
1839 IOOptionBits options
= 0 )
1841 IORegistryIterator
* create
;
1848 create
= new IORegistryIterator
;
1850 if( create
->init()) {
1853 create
->root
= root
;
1854 create
->where
= &create
->start
;
1855 create
->start
.current
= root
;
1856 create
->plane
= plane
;
1857 create
->options
= options
& ~kIORegistryIteratorInvalidFlag
;
1867 IORegistryIterator
*
1868 IORegistryIterator::iterateOver( const IORegistryPlane
* plane
,
1869 IOOptionBits options
= 0 )
1871 return( iterateOver( gRegistryRoot
, plane
, options
));
1874 bool IORegistryIterator::isValid( void )
1883 ok
= (0 == (kIORegistryIteratorInvalidFlag
& options
));
1885 while( ok
&& next
) {
1887 ok
= where
->iter
->isValid();
1895 void IORegistryIterator::enterEntry( const IORegistryPlane
* enterPlane
)
1900 where
= (IORegCursor
*) IOMalloc( sizeof( IORegCursor
));
1906 where
->current
= prev
->current
;
1911 void IORegistryIterator::enterEntry( void )
1913 enterEntry( plane
);
1916 bool IORegistryIterator::exitEntry( void )
1921 where
->iter
->release();
1923 if( where
->current
)// && (where != &start))
1924 where
->current
->release();
1927 if( where
!= &start
) {
1930 IOFree( gone
, sizeof( IORegCursor
));
1937 void IORegistryIterator::reset( void )
1947 where
->current
= root
;
1948 options
&= ~kIORegistryIteratorInvalidFlag
;
1951 void IORegistryIterator::free( void )
1962 IORegistryEntry
* IORegistryIterator::getNextObjectFlat( void )
1964 IORegistryEntry
* next
= 0;
1965 OSArray
* links
= 0;
1969 if( (0 == where
->iter
)) {
1970 // just entered - create new iter
1973 && (links
= ( (options
& kIORegistryIterateParents
) ?
1974 where
->current
->getParentSetReference( plane
) :
1975 where
->current
->getChildSetReference( plane
) )) )
1977 where
->iter
= OSCollectionIterator::withCollection( links
);
1980 // next sibling - release current
1982 where
->current
->release();
1986 next
= (IORegistryEntry
*) where
->iter
->getNextObject();
1990 else if( !where
->iter
->isValid())
1991 options
|= kIORegistryIteratorInvalidFlag
;
1994 where
->current
= next
;
2001 IORegistryEntry
* IORegistryIterator::getNextObjectRecursive( void )
2003 IORegistryEntry
* next
;
2006 next
= getNextObjectFlat();
2007 while( (0 == next
) && exitEntry());
2011 done
= OSOrderedSet::withCapacity( 10 );
2012 if( done
->setObject((OSObject
*) next
)) {
2013 // done set didn't contain this one, so recurse
2020 IORegistryEntry
* IORegistryIterator::getNextObject( void )
2022 if( options
& kIORegistryIterateRecursively
)
2023 return( getNextObjectRecursive());
2025 return( getNextObjectFlat());
2028 IORegistryEntry
* IORegistryIterator::getCurrentEntry( void )
2031 return( where
->current
);
2036 OSOrderedSet
* IORegistryIterator::iterateAll( void )
2039 while( getNextObjectRecursive())
2046 OSMetaClassDefineReservedUsed(IORegistryEntry
, 0);
2047 OSMetaClassDefineReservedUsed(IORegistryEntry
, 1);
2048 OSMetaClassDefineReservedUsed(IORegistryEntry
, 2);
2049 OSMetaClassDefineReservedUsed(IORegistryEntry
, 3);
2050 OSMetaClassDefineReservedUsed(IORegistryEntry
, 4);
2052 OSMetaClassDefineReservedUnused(IORegistryEntry
, 5);
2053 OSMetaClassDefineReservedUnused(IORegistryEntry
, 6);
2054 OSMetaClassDefineReservedUnused(IORegistryEntry
, 7);
2055 OSMetaClassDefineReservedUnused(IORegistryEntry
, 8);
2056 OSMetaClassDefineReservedUnused(IORegistryEntry
, 9);
2057 OSMetaClassDefineReservedUnused(IORegistryEntry
, 10);
2058 OSMetaClassDefineReservedUnused(IORegistryEntry
, 11);
2059 OSMetaClassDefineReservedUnused(IORegistryEntry
, 12);
2060 OSMetaClassDefineReservedUnused(IORegistryEntry
, 13);
2061 OSMetaClassDefineReservedUnused(IORegistryEntry
, 14);
2062 OSMetaClassDefineReservedUnused(IORegistryEntry
, 15);
2063 OSMetaClassDefineReservedUnused(IORegistryEntry
, 16);
2064 OSMetaClassDefineReservedUnused(IORegistryEntry
, 17);
2065 OSMetaClassDefineReservedUnused(IORegistryEntry
, 18);
2066 OSMetaClassDefineReservedUnused(IORegistryEntry
, 19);
2067 OSMetaClassDefineReservedUnused(IORegistryEntry
, 20);
2068 OSMetaClassDefineReservedUnused(IORegistryEntry
, 21);
2069 OSMetaClassDefineReservedUnused(IORegistryEntry
, 22);
2070 OSMetaClassDefineReservedUnused(IORegistryEntry
, 23);
2071 OSMetaClassDefineReservedUnused(IORegistryEntry
, 24);
2072 OSMetaClassDefineReservedUnused(IORegistryEntry
, 25);
2073 OSMetaClassDefineReservedUnused(IORegistryEntry
, 26);
2074 OSMetaClassDefineReservedUnused(IORegistryEntry
, 27);
2075 OSMetaClassDefineReservedUnused(IORegistryEntry
, 28);
2076 OSMetaClassDefineReservedUnused(IORegistryEntry
, 29);
2077 OSMetaClassDefineReservedUnused(IORegistryEntry
, 30);
2078 OSMetaClassDefineReservedUnused(IORegistryEntry
, 31);
2080 /* inline function implementation */
2081 OSDictionary
* IORegistryEntry::getPropertyTable( void ) const
2082 { return(fPropertyTable
); }