2 * Copyright (c) 1999-2008 Apple 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-1998 by Apple Computer, Inc., all rights reserved.
35 #ifndef __BTREEPRIVATE__
36 #define __BTREEPRIVATE__
40 /////////////////////////////////// Constants ///////////////////////////////////
42 #define SupportsKeyDescriptors 0
44 #define kBTreeVersion 1
45 #define kMaxTreeDepth 16
48 #define kHeaderNodeNum 0
49 #define kKeyDescRecord 1
52 // Header Node Record Offsets
54 kHeaderRecOffset
= 0x000E,
55 kKeyDescRecOffset
= 0x0078,
56 kHeaderMapRecOffset
= 0x00F8
59 #define kMinNodeSize 512
61 #define kMinRecordSize 6
62 //¥¥ where is minimum record size enforced?
64 // miscellaneous BTree constants
75 // illegal string attribute bits set in mask
76 #define kBadStrAttribMask 0xCF
80 //////////////////////////////////// Macros /////////////////////////////////////
82 #define M_NodesInMap(mapSize) ((mapSize) << 3)
84 #define M_ClearBitNum(integer,bitNumber) ((integer) &= (~(1<<(bitNumber))))
85 #define M_SetBitNum(integer,bitNumber) ((integer) |= (1<<(bitNumber)))
86 #define M_IsOdd(integer) (((integer) & 1) != 0)
87 #define M_IsEven(integer) (((integer) & 1) == 0)
88 #define M_BTreeHeaderDirty(btreePtr) btreePtr->flags |= kBTHeaderDirty
90 #define M_MapRecordSize(nodeSize) (nodeSize - sizeof (BTNodeDescriptor) - 6)
91 #define M_HeaderMapRecordSize(nodeSize) (nodeSize - sizeof(BTNodeDescriptor) - sizeof(BTHeaderRec) - 128 - 8)
93 #define M_SWAP_BE16_ClearBitNum(integer,bitNumber) ((integer) &= SWAP_BE16(~(1<<(bitNumber))))
94 #define M_SWAP_BE16_SetBitNum(integer,bitNumber) ((integer) |= SWAP_BE16(1<<(bitNumber)))
97 #define Panic( message ) DebugStr( (ConstStr255Param) message )
98 #define PanicIf( condition, message ) if ( (condition) != 0 ) DebugStr( message )
100 #define Panic( message ) do { ; } while (0)
101 #define PanicIf( condition, message ) do { ; } while (0)
104 ///////////////////////////////////// Types /////////////////////////////////////
105 struct BTreeExtensionRec
;
107 typedef struct BTreeControlBlock
{ // fields specific to BTree CBs
109 UInt8 keyCompareType
; /* Key string Comparison Type */
111 SInt16 obsolete_fileRefNum
; // refNum of btree file
112 KeyCompareProcPtr keyCompareProc
;
113 UInt8 reserved2
[16]; // keep for alignment with old style fields
117 UInt32 firstLeafNode
;
123 UInt32 reserved3
[16]; /* reserved*/
127 UInt32 flags
; // dynamic flags
128 UInt32 attributes
; // persistent flags
129 KeyDescriptorPtr keyDescPtr
;
132 GetBlockProcPtr getBlockProc
;
133 ReleaseBlockProcPtr releaseBlockProc
;
134 SetEndOfForkProcPtr setEndOfForkProc
;
135 BTreeIterator lastIterator
; // needed for System 7 iteration context
137 // statistical information
139 UInt32 numGetNewNodes
;
140 UInt32 numReleaseNodes
;
141 UInt32 numUpdateNodes
;
142 UInt32 numMapNodesRead
; // map nodes beyond header node
143 UInt32 numHintChecks
;
144 UInt32 numPossibleHints
; // Looks like a formated hint
145 UInt32 numValidHints
; // Hint used to find correct record.
147 struct BTreeExtensionsRec
*refCon
; // Used by DFA to point to private data.
148 SFCB
*fcbPtr
; // fcb of btree file
150 } BTreeControlBlock
, *BTreeControlBlockPtr
;
153 UInt32
CalcKeySize(const BTreeControlBlock
*btcb
, const BTreeKey
*key
);
154 #define CalcKeySize(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? ((key)->length16 + 2) : ((key)->length8 + 1) )
156 UInt32
MaxKeySize(const BTreeControlBlock
*btcb
);
157 #define MaxKeySize(btcb) ( (btcb)->maxKeyLength + ((btcb)->attributes & kBTBigKeysMask ? 2 : 1))
159 UInt32
KeyLength(const BTreeControlBlock
*btcb
, const BTreeKey
*key
);
160 #define KeyLength(btcb, key) ( ((btcb)->attributes & kBTBigKeysMask) ? (key)->length16 : (key)->length8 )
164 kBTHeaderDirty
= 0x00000001
168 typedef SInt8
*NodeBuffer
;
169 typedef BlockDescriptor NodeRec
, *NodePtr
; //¥¥ remove this someday...
174 //// Tree Path Table - constructed by SearchTree, used by InsertTree and DeleteTree
177 UInt32 node
; // node number
179 UInt16 reserved
; // align size to a power of 2
180 } TreePathRecord
, *TreePathRecordPtr
;
182 typedef TreePathRecord TreePathTable
[kMaxTreeDepth
];
185 //// InsertKey - used by InsertTree, InsertLevel and InsertNode
192 Boolean replacingKey
;
196 typedef struct InsertKey InsertKey
;
199 //// For Notational Convenience
201 typedef BTNodeDescriptor
* NodeDescPtr
;
202 typedef UInt8
*RecordPtr
;
203 typedef BTreeKeyPtr KeyPtr
;
206 //////////////////////////////////// Globals ////////////////////////////////////
209 //////////////////////////////////// Macros /////////////////////////////////////
211 // Exit function on error
212 #define M_ExitOnError( result ) if ( ( result ) != noErr ) goto ErrorExit; else ;
214 // Test for passed condition and return if true
215 #define M_ReturnErrorIf( condition, error ) if ( condition ) return( error )
218 #define Panic( message ) DebugStr( (ConstStr255Param) message )
219 #define PanicIf( condition, message ) if ( (condition) != 0 ) DebugStr( message )
221 #define Panic( message ) do { ; } while (0)
222 #define PanicIf( condition, message ) do { ; } while (0)
225 //////////////////////////////// Key Operations /////////////////////////////////
227 SInt32
CompareKeys (BTreeControlBlockPtr btreePtr
,
231 OSStatus
GetKeyDescriptor (BTreeControlBlockPtr btreePtr
,
232 NodeDescPtr headerNode
);
234 OSStatus
CheckKeyDescriptor (KeyDescriptorPtr keyDescPtr
,
235 UInt16 maxKeyLength
);
237 OSStatus
CheckKey (KeyPtr keyPtr
,
238 KeyDescriptorPtr keyDescPtr
,
239 UInt16 maxKeyLength
);
243 //////////////////////////////// Map Operations /////////////////////////////////
245 OSStatus
AllocateNode (BTreeControlBlockPtr btreePtr
,
248 OSStatus
FreeNode (BTreeControlBlockPtr btreePtr
,
251 OSStatus
ExtendBTree (BTreeControlBlockPtr btreePtr
,
254 UInt32
CalcMapBits (BTreeControlBlockPtr btreePtr
);
257 //////////////////////////////// Misc Operations ////////////////////////////////
259 UInt16
CalcKeyRecordSize (UInt16 keySize
,
262 OSStatus
VerifyHeader (SFCB
*filePtr
,
263 BTHeaderRec
*header
);
265 OSStatus
UpdateHeader (BTreeControlBlockPtr btreePtr
);
267 OSStatus
FindIteratorPosition (BTreeControlBlockPtr btreePtr
,
268 BTreeIteratorPtr iterator
,
269 BlockDescriptor
*left
,
270 BlockDescriptor
*middle
,
271 BlockDescriptor
*right
,
274 Boolean
*foundRecord
);
276 OSStatus
CheckInsertParams (SFCB
*filePtr
,
277 BTreeIterator
*iterator
,
278 FSBufferDescriptor
*record
,
281 OSStatus
TrySimpleReplace (BTreeControlBlockPtr btreePtr
,
283 BTreeIterator
*iterator
,
284 FSBufferDescriptor
*record
,
286 Boolean
*recordInserted
);
288 OSStatus
IsItAHint (BTreeControlBlockPtr btreePtr
,
289 BTreeIterator
*iterator
,
292 //////////////////////////////// Node Operations ////////////////////////////////
296 OSStatus
GetNode (BTreeControlBlockPtr btreePtr
,
298 NodeRec
*returnNodePtr
);
300 OSStatus
GetLeftSiblingNode (BTreeControlBlockPtr btreePtr
,
304 #define GetLeftSiblingNode(btree,node,left) GetNode ((btree), ((NodeDescPtr)(node))->bLink, (left))
306 OSStatus
GetRightSiblingNode (BTreeControlBlockPtr btreePtr
,
310 #define GetRightSiblingNode(btree,node,right) GetNode ((btree), ((NodeDescPtr)(node))->fLink, (right))
313 OSStatus
GetNewNode (BTreeControlBlockPtr btreePtr
,
315 NodeRec
*returnNodePtr
);
317 OSStatus
ReleaseNode (BTreeControlBlockPtr btreePtr
,
319 OSStatus
TrashNode (BTreeControlBlockPtr btreePtr
,
322 OSStatus
UpdateNode (BTreeControlBlockPtr btreePtr
,
325 OSStatus
GetMapNode (BTreeControlBlockPtr btreePtr
,
326 BlockDescriptor
*nodePtr
,
330 //// Node Buffer Operations
332 void ClearNode (BTreeControlBlockPtr btreePtr
,
335 UInt16
GetNodeDataSize (BTreeControlBlockPtr btreePtr
,
338 UInt16
GetNodeFreeSize (BTreeControlBlockPtr btreePtr
,
342 //// Record Operations
344 Boolean
InsertRecord (BTreeControlBlockPtr btreePtr
,
350 Boolean
InsertKeyRecord (BTreeControlBlockPtr btreePtr
,
358 void DeleteRecord (BTreeControlBlockPtr btree
,
363 Boolean
SearchNode (BTreeControlBlockPtr btree
,
368 OSStatus
GetRecordByIndex (BTreeControlBlockPtr btree
,
375 UInt8
* GetRecordAddress (BTreeControlBlockPtr btree
,
379 #define GetRecordAddress(btreePtr,node,index) ((UInt8 *)(node) + (*(short *) ((UInt8 *)(node) + (btreePtr)->nodeSize - ((index) << 1) - kOffsetSize)))
382 UInt16
GetRecordSize (BTreeControlBlockPtr btree
,
386 UInt32
GetChildNodeNum (BTreeControlBlockPtr btreePtr
,
390 void MoveRecordsLeft (UInt8
* src
,
392 UInt16 bytesToMove
);
394 #define MoveRecordsLeft(src,dst,bytes) CopyMemory((src),(dst),(bytes))
396 void MoveRecordsRight (UInt8
* src
,
398 UInt16 bytesToMove
);
400 #define MoveRecordsRight(src,dst,bytes) CopyMemory((src),(dst),(bytes))
404 //////////////////////////////// Tree Operations ////////////////////////////////
406 OSStatus
SearchTree (BTreeControlBlockPtr btreePtr
,
408 TreePathTable treePathTable
,
410 BlockDescriptor
*nodePtr
,
413 OSStatus
InsertTree (BTreeControlBlockPtr btreePtr
,
414 TreePathTable treePathTable
,
418 BlockDescriptor
*targetNode
,
421 Boolean replacingKey
,
422 UInt32
*insertNode
);
424 OSStatus
DeleteTree (BTreeControlBlockPtr btreePtr
,
425 TreePathTable treePathTable
,
426 BlockDescriptor
*targetNode
,
430 #endif //__BTREEPRIVATE__