From ad3105b7fb9db1bb03df94b62c2b774aa0f96192 Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Mon, 4 Apr 2016 20:20:01 +0100 Subject: [PATCH] Moving cbs tree functions on nodes to node.c. Copied from Perforce Change: 190677 ServerID: perforce.ravenbrook.com --- mps/code/cbs.c | 49 ++++--------------------------------------------- mps/code/node.c | 33 +++++++++++++++++++++++++++++++++ mps/code/node.h | 17 +++++++++++++++++ 3 files changed, 54 insertions(+), 45 deletions(-) diff --git a/mps/code/cbs.c b/mps/code/cbs.c index 770aacdb098..2698bb88cf1 100644 --- a/mps/code/cbs.c +++ b/mps/code/cbs.c @@ -34,14 +34,6 @@ SRCID(cbs, "$Id$"); #define cbsZonedBlockNode(_block) cbsFastBlockNode(&(_block)->cbsFastBlockStruct) #define cbsBlockPool(cbs) RVALUE((cbs)->blockPool) -/* We pass the block base directly as a TreeKey (void *) assuming that - Addr can be encoded, and possibly breaking . - On an exotic platform where this isn't true, pass the address of base. - i.e. add an & */ -#define cbsBlockKey(block) ((TreeKey)NodeBase(block)) -#define keyOfBaseVar(baseVar) ((TreeKey)(baseVar)) -#define baseOfKey(key) ((Addr)(key)) - /* CBSCheck -- Check CBS */ @@ -63,39 +55,6 @@ Bool CBSCheck(CBS cbs) } -/* cbsCompare -- Compare key to [base,limit) - * - * See - */ - -static Compare cbsCompare(Tree tree, TreeKey key) -{ - Addr base1, base2, limit2; - Node block; - - AVERT_CRITICAL(Tree, tree); - AVER_CRITICAL(tree != TreeEMPTY); - AVER_CRITICAL(key != NULL); - - base1 = baseOfKey(key); - block = NodeOfTree(tree); - base2 = NodeBase(block); - limit2 = NodeLimit(block); - - if (base1 < base2) - return CompareLESS; - else if (base1 >= limit2) - return CompareGREATER; - else - return CompareEQUAL; -} - -static TreeKey cbsKey(Tree tree) -{ - return cbsBlockKey(NodeOfTree(tree)); -} - - /* cbsTestNode, cbsTestTree -- test for nodes larger than the S parameter */ static Bool cbsTestNode(SplayTree splay, Tree tree, void *closure) @@ -216,7 +175,7 @@ static Res cbsInitComm(Land land, ArgList args, SplayUpdateNodeFunction update, blockPool = arg.val.pool; cbs = cbsOfLand(land); - SplayTreeInit(cbsSplay(cbs), cbsCompare, cbsKey, update); + SplayTreeInit(cbsSplay(cbs), NodeCompare, NodeKey, update); if (blockPool != NULL) { cbs->blockPool = blockPool; @@ -448,7 +407,7 @@ static Res cbsInsert(Range rangeReturn, Land land, Range range) METER_ACC(cbs->treeSearch, cbs->treeSize); b = SplayTreeNeighbours(&leftSplay, &rightSplay, - cbsSplay(cbs), keyOfBaseVar(base)); + cbsSplay(cbs), NodeKeyOfBaseVar(base)); if (!b) { res = ResFAIL; goto fail; @@ -457,7 +416,7 @@ static Res cbsInsert(Range rangeReturn, Land land, Range range) /* .insert.overlap: The two cases below are not quite symmetrical, because base was passed into the call to SplayTreeNeighbours(), but limit was not. So we know that if there is a left neighbour, - then leftBlock's limit <= base (this is ensured by cbsCompare, + then leftBlock's limit <= base (this is ensured by NodeCompare, which is the comparison method on the tree). But if there is a right neighbour, all we know is that base < rightBlock's base. But for the range to fit, we need limit <= rightBlock's @@ -553,7 +512,7 @@ static Res cbsDelete(Range rangeReturn, Land land, Range range) limit = RangeLimit(range); METER_ACC(cbs->treeSearch, cbs->treeSize); - if (!SplayTreeFind(&tree, cbsSplay(cbs), keyOfBaseVar(base))) { + if (!SplayTreeFind(&tree, cbsSplay(cbs), NodeKeyOfBaseVar(base))) { res = ResFAIL; goto failSplayTreeSearch; } diff --git a/mps/code/node.c b/mps/code/node.c index 339ace62a54..7e1428a4b2c 100644 --- a/mps/code/node.c +++ b/mps/code/node.c @@ -36,6 +36,39 @@ void NodeFinish(Node node) } +/* NodeCompare -- Compare key to [base,limit) + * + * See + */ + +Compare NodeCompare(Tree tree, TreeKey key) +{ + Addr base1, base2, limit2; + Node block; + + AVERT_CRITICAL(Tree, tree); + AVER_CRITICAL(tree != TreeEMPTY); + AVER_CRITICAL(key != NULL); + + base1 = NodeBaseOfKey(key); + block = NodeOfTree(tree); + base2 = NodeBase(block); + limit2 = NodeLimit(block); + + if (base1 < base2) + return CompareLESS; + else if (base1 >= limit2) + return CompareGREATER; + else + return CompareEQUAL; +} + +TreeKey NodeKey(Tree tree) +{ + return NodeKeyOfBaseVar(NodeBase(NodeOfTree(tree))); +} + + /* C. COPYRIGHT AND LICENSE * * Copyright (C) 2013 Ravenbrook Limited . diff --git a/mps/code/node.h b/mps/code/node.h index d236304a1db..d53136a1ade 100644 --- a/mps/code/node.h +++ b/mps/code/node.h @@ -8,6 +8,7 @@ #define node_h #include "mpmtypes.h" +#include "tree.h" #define NodeTree(node) (&(node)->treeStruct) #define NodeRange(node) (&(node)->rangeStruct) @@ -24,6 +25,22 @@ extern void NodeInit(Node node, Addr base, Addr limit); extern Bool NodeCheck(Node node); extern void NodeFinish(Node node); + +/* Functions for nodes in trees + * + * We pass the ndoe base directly as a TreeKey (void *) assuming that + * Addr can be encoded, and possibly breaking . + * On an exotic platform where this isn't true, pass the address of + * base. i.e. add an & + */ + +#define NodeKeyOfBaseVar(baseVar) ((TreeKey)(baseVar)) +#define NodeBaseOfKey(key) ((Addr)(key)) + +extern Compare NodeCompare(Tree tree, TreeKey key); +extern TreeKey NodeKey(Tree tree); + + #endif /* node_h */ /* C. COPYRIGHT AND LICENSE