2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 Contains: Private interface file for the BTree Module.
28 Version: xxx put the technology version here xxx
30 Written by: Gordon Sheridan and Bill Bruffey
32 Copyright: © 1992-1999 by Apple Computer, Inc., all rights reserved.
38 Other Contact: Mark Day
40 Technology: File Systems
50 Change History (most recent first):
51 <MacOSX> 3/19/99 djb Disable MoveRecordsLeft/Right macros since bcopy is broken.
53 <MacOSX> 8/10/98 djb Removed unused BTreeIterator from BTreeControlBlock, fixed alignment.
55 <CS5> 9/4/97 djb Convert MoveRecordsLeft and GetLeftSiblingNode to macros.
56 <CS4> 7/24/97 djb Add macro for GetRecordAddress (was a function before).
57 <CS3> 7/21/97 msd GetRecordByIndex now returns an OSStatus.
58 <CS2> 7/16/97 DSH FilesInternal.i renamed FileMgrInternal.i to avoid name
60 <CS1> 4/23/97 djb first checked in
62 <HFS6> 3/17/97 DSH Added a refCon field to BTreeControlBlock, for DFA use, to point
63 to additional data. Fixed Panic macros for use with SC.
64 <HFS5> 2/19/97 djb Add InsertKey struct. Moved on-disk definitions to
66 <HFS4> 1/27/97 djb InsertTree and DeleteTree are now recursive and support variable
68 <HFS3> 1/15/97 djb Move GetFileRefNumFromFCB macro to FilesInternal.h. Added
69 kBTVariableIndexKeysMask.
70 <HFS2> 1/3/97 djb Added support for large keys.
71 <HFS1> 12/19/96 djb first checked in
73 History applicable to original Scarecrow Design:
75 <7> 10/25/96 ser Changing for new VFPI
76 <6> 10/18/96 ser Converting over VFPI changes
77 <5> 9/17/96 dkh More BTree statistics
78 <4> 9/16/96 dkh Revised BTree statistics
79 <3> 6/20/96 dkh Radar #1358740. Switch from using Pools to debug MemAllocators.
80 <2> 12/7/95 dkh D10E2 build. Changed usage of Ref data type to LogicalAddress.
81 <1> 10/18/95 rst Moved from Scarecrow project.
83 <19> 11/22/94 djb Add prototype for GetMapNode
84 <18> 11/16/94 prp Add IsItAHint routine prototype.
85 <17> 9/30/94 prp Get in sync with D2 interface changes.
86 <16> 7/25/94 wjk Eliminate usage of BytePtr in favor of UInt8 *.
87 <15> 7/22/94 wjk Convert to the new set of header files.
88 <14> 5/31/94 srs Moved Btree types to public interface
89 <13> 12/9/93 wjk Add 68k alignment pragma's around persistent structures.
90 <12> 11/30/93 wjk Move from Makefiles to BuildFiles. Fit into the ModernOS and
92 <11> 11/23/93 wjk Changes required to compile on the RS6000.
93 <10> 8/30/93 CH Removed the M_ExitOnError and M_ReturnErrorIf macros which were
94 already defined in FileSystemPriv.h (included here).
95 <9> 8/30/93 CH Added parens around the M_ReturnErrorIf macro.
96 <8> 5/21/93 gs Add kBadClose flag. Add some prototypes for internal routines.
97 <7> 5/10/93 gs Change Ptr to BytePtr. Move BTreeTypes to BTree.h. Add
99 <6> 3/23/93 gs Remove mysterious "flags" field from HeaderRec structure. Move
100 prototypes of private functions to top of respective source
102 <5> 2/8/93 gs Update to use FSAgent.h Get/Release/SetEOF/SetBlockSize
103 procPtrs. Add UpdateNode routine.
104 <4> 12/10/92 gs Add Key Descriptor function declarations.
105 <3> 12/8/92 gs Add HeaderRec structure and incorporate review feedback.
106 <2> 12/2/92 gs Add GetNode and ReleaseNode callback procptrs to BTree CB, and
107 add internal function declarations.
108 <1> 11/15/92 gs first checked in
112 #ifndef __BTREESPRIVATE__
113 #define __BTREESPRIVATE__
115 #include <sys/appleapiopts.h>
118 #ifdef __APPLE_API_PRIVATE
120 #include "../../hfs_macos_defs.h"
122 #ifndef __FILEMGRINTERNAL__
123 #include "FileMgrInternal.h"
126 #ifndef __BTREESINTERNAL__
127 #include "BTreesInternal.h"
131 /////////////////////////////////// Constants ///////////////////////////////////
133 #define kBTreeVersion 1
134 #define kMaxTreeDepth 16
137 #define kHeaderNodeNum 0
138 #define kKeyDescRecord 1
141 // Header Node Record Offsets
143 kHeaderRecOffset
= 0x000E,
144 kKeyDescRecOffset
= 0x0078,
145 kHeaderMapRecOffset
= 0x00F8
148 #define kMinNodeSize 512
150 #define kMinRecordSize 6
151 // where is minimum record size enforced?
153 // miscellaneous BTree constants
164 // illegal string attribute bits set in mask
165 #define kBadStrAttribMask 0xCF
169 //////////////////////////////////// Macros /////////////////////////////////////
171 #define M_NodesInMap(mapSize) ((mapSize) << 3)
173 #define M_ClearBitNum(integer,bitNumber) ((integer) &= (~(1<<(bitNumber))))
174 #define M_SetBitNum(integer,bitNumber) ((integer) |= (1<<(bitNumber)))
175 #define M_IsOdd(integer) (((integer) & 1) != 0)
176 #define M_IsEven(integer) (((integer) & 1) == 0)
177 #define M_BTreeHeaderDirty(btreePtr) btreePtr->flags |= kBTHeaderDirty
179 #define M_MapRecordSize(nodeSize) (nodeSize - sizeof (BTNodeDescriptor) - 6)
180 #define M_HeaderMapRecordSize(nodeSize) (nodeSize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec) - 128 - 8)
182 #define M_SWAP_BE16_ClearBitNum(integer,bitNumber) ((integer) &= SWAP_BE16(~(1<<(bitNumber))))
183 #define M_SWAP_BE16_SetBitNum(integer,bitNumber) ((integer) |= SWAP_BE16(1<<(bitNumber)))
185 ///////////////////////////////////// Types /////////////////////////////////////
187 typedef struct BTreeControlBlock
{ // fields specific to BTree CBs
189 UInt8 keyCompareType
; /* Key string Comparison Type */
192 FileReference fileRefNum
; // refNum of btree file
193 KeyCompareProcPtr keyCompareProc
;
196 UInt32 firstLeafNode
;
203 UInt16 reserved3
; // 4-byte alignment
207 UInt32 flags
; // dynamic flags
208 UInt32 attributes
; // persistent flags
210 UInt32 lastfsync
; /* Last time that this was fsynced */
212 GetBlockProcPtr getBlockProc
;
213 ReleaseBlockProcPtr releaseBlockProc
;
214 SetEndOfForkProcPtr setEndOfForkProc
;
216 // statistical information
218 UInt32 numGetNewNodes
;
219 UInt32 numReleaseNodes
;
220 UInt32 numUpdateNodes
;
221 UInt32 numMapNodesRead
; // map nodes beyond header node
222 UInt32 numHintChecks
;
223 UInt32 numPossibleHints
; // Looks like a formated hint
224 UInt32 numValidHints
; // Hint used to find correct record.
225 UInt32 reservedNodes
;
226 } BTreeControlBlock
, *BTreeControlBlockPtr
;
229 UInt32
CalcKeySize(const BTreeControlBlock
*btcb
, const BTreeKey
*key
);
230 #define CalcKeySize(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? ((key)->length16 + 2) : ((key)->length8 + 1) )
232 UInt32
KeyLength(const BTreeControlBlock
*btcb
, const BTreeKey
*key
);
233 #define KeyLength(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? (key)->length16 : (key)->length8 )
238 kBTHeaderDirty
= 0x00000001
242 typedef SInt8
*NodeBuffer
;
243 typedef BlockDescriptor NodeRec
, *NodePtr
; //\80\80 remove this someday...
248 //// Tree Path Table - constructed by SearchTree, used by InsertTree and DeleteTree
251 UInt32 node
; // node number
253 UInt16 reserved
; // align size to a power of 2
254 } TreePathRecord
, *TreePathRecordPtr
;
256 typedef TreePathRecord TreePathTable
[kMaxTreeDepth
];
259 //// InsertKey - used by InsertTree, InsertLevel and InsertNode
266 Boolean replacingKey
;
270 typedef struct InsertKey InsertKey
;
273 //// For Notational Convenience
275 typedef BTNodeDescriptor
* NodeDescPtr
;
276 typedef UInt8
*RecordPtr
;
277 typedef BTreeKeyPtr KeyPtr
;
280 //////////////////////////////////// Globals ////////////////////////////////////
283 //////////////////////////////////// Macros /////////////////////////////////////
286 #define Panic( message ) DebugStr( (ConstStr255Param) message )
287 #define PanicIf( condition, message ) if ( condition != 0 ) DebugStr( message )
289 #define Panic( message )
290 #define PanicIf( condition, message )
293 // Exit function on error
294 #define M_ExitOnError( result ) if ( ( result ) != noErr ) goto ErrorExit; else ;
296 // Test for passed condition and return if true
297 #define M_ReturnErrorIf( condition, error ) if ( condition ) return( error )
299 //////////////////////////////// Key Operations /////////////////////////////////
301 SInt32
CompareKeys (BTreeControlBlockPtr btreePtr
,
305 //////////////////////////////// Map Operations /////////////////////////////////
307 OSStatus
AllocateNode (BTreeControlBlockPtr btreePtr
,
310 OSStatus
FreeNode (BTreeControlBlockPtr btreePtr
,
313 OSStatus
ExtendBTree (BTreeControlBlockPtr btreePtr
,
316 UInt32
CalcMapBits (BTreeControlBlockPtr btreePtr
);
318 SInt32
BTAvailableNodes (BTreeControlBlock
*btree
);
320 void BTUpdateReserve (BTreeControlBlockPtr btreePtr
,
323 //////////////////////////////// Misc Operations ////////////////////////////////
325 UInt16
CalcKeyRecordSize (UInt16 keySize
,
328 OSStatus
VerifyHeader (FCB
*filePtr
,
329 BTHeaderRec
*header
);
331 OSStatus
UpdateHeader (BTreeControlBlockPtr btreePtr
,
332 Boolean forceWrite
);
334 OSStatus
FindIteratorPosition (BTreeControlBlockPtr btreePtr
,
335 BTreeIteratorPtr iterator
,
336 BlockDescriptor
*left
,
337 BlockDescriptor
*middle
,
338 BlockDescriptor
*right
,
341 Boolean
*foundRecord
);
343 OSStatus
CheckInsertParams (FCB
*filePtr
,
344 BTreeIterator
*iterator
,
345 FSBufferDescriptor
*record
,
348 OSStatus
TrySimpleReplace (BTreeControlBlockPtr btreePtr
,
350 BTreeIterator
*iterator
,
351 FSBufferDescriptor
*record
,
353 Boolean
*recordInserted
);
355 OSStatus
IsItAHint (BTreeControlBlockPtr btreePtr
,
356 BTreeIterator
*iterator
,
359 //////////////////////////////// Node Operations ////////////////////////////////
363 OSStatus
GetNode (BTreeControlBlockPtr btreePtr
,
365 NodeRec
*returnNodePtr
);
367 OSStatus
GetLeftSiblingNode (BTreeControlBlockPtr btreePtr
,
371 #define GetLeftSiblingNode(btree,node,left) GetNode ((btree), ((NodeDescPtr)(node))->bLink, (left))
373 OSStatus
GetRightSiblingNode (BTreeControlBlockPtr btreePtr
,
377 #define GetRightSiblingNode(btree,node,right) GetNode ((btree), ((NodeDescPtr)(node))->fLink, (right))
380 OSStatus
GetNewNode (BTreeControlBlockPtr btreePtr
,
382 NodeRec
*returnNodePtr
);
384 OSStatus
ReleaseNode (BTreeControlBlockPtr btreePtr
,
387 OSStatus
TrashNode (BTreeControlBlockPtr btreePtr
,
391 void ModifyBlockStart(FileReference vp
, BlockDescPtr blockPtr
);
394 OSStatus
UpdateNode (BTreeControlBlockPtr btreePtr
,
396 UInt32 transactionID
,
399 OSStatus
GetMapNode (BTreeControlBlockPtr btreePtr
,
400 BlockDescriptor
*nodePtr
,
404 //// Node Buffer Operations
406 void ClearNode (BTreeControlBlockPtr btreePtr
,
409 UInt16
GetNodeDataSize (BTreeControlBlockPtr btreePtr
,
412 UInt16
GetNodeFreeSize (BTreeControlBlockPtr btreePtr
,
416 //// Record Operations
418 Boolean
InsertRecord (BTreeControlBlockPtr btreePtr
,
424 Boolean
InsertKeyRecord (BTreeControlBlockPtr btreePtr
,
432 void DeleteRecord (BTreeControlBlockPtr btree
,
437 Boolean
SearchNode (BTreeControlBlockPtr btree
,
442 OSStatus
GetRecordByIndex (BTreeControlBlockPtr btree
,
449 UInt8
* GetRecordAddress (BTreeControlBlockPtr btree
,
453 #define GetRecordAddress(btreePtr,node,index) ((UInt8 *)(node) + (*(short *) ((UInt8 *)(node) + (btreePtr)->nodeSize - ((index) << 1) - kOffsetSize)))
456 UInt16
GetRecordSize (BTreeControlBlockPtr btree
,
460 UInt32
GetChildNodeNum (BTreeControlBlockPtr btreePtr
,
464 void MoveRecordsLeft (UInt8
* src
,
466 UInt16 bytesToMove
);
468 #define MoveRecordsLeft(src,dst,bytes) bcopy((src),(dst),(bytes))
470 void MoveRecordsRight (UInt8
* src
,
472 UInt16 bytesToMove
);
474 #define MoveRecordsRight(src,dst,bytes) bcopy((src),(dst),(bytes))
477 //////////////////////////////// Tree Operations ////////////////////////////////
479 OSStatus
SearchTree (BTreeControlBlockPtr btreePtr
,
481 TreePathTable treePathTable
,
483 BlockDescriptor
*nodePtr
,
486 OSStatus
InsertTree (BTreeControlBlockPtr btreePtr
,
487 TreePathTable treePathTable
,
491 BlockDescriptor
*targetNode
,
494 Boolean replacingKey
,
495 UInt32
*insertNode
);
497 OSStatus
DeleteTree (BTreeControlBlockPtr btreePtr
,
498 TreePathTable treePathTable
,
499 BlockDescriptor
*targetNode
,
503 #endif /* __APPLE_API_PRIVATE */
505 #endif //__BTREESPRIVATE__