Skip to content

Commit

Permalink
Add dyninstAPI/src/codeRange.C
Browse files Browse the repository at this point in the history
  • Loading branch information
hainest committed Apr 3, 2024
1 parent f2ca692 commit 2834074
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 78 deletions.
163 changes: 161 additions & 2 deletions docs/dyninstAPI/developer/codeRange.h.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ codeRange.h

.. cpp:function:: virtual void *get_local_ptr() const

This returns a local pointer to the "beginning" of the code range - as opposed to get_address,
which returns the "remote" address.
.. warning:: not implemented

.. cpp:function:: func_instance *is_function()

Expand All @@ -21,6 +20,10 @@ codeRange.h

.. cpp:function:: block_instance *is_basicBlock()
.. cpp:function:: block_instance *is_basicBlockInstance()

This is a special case. The multitramp is the thing in the codeRange tree, but people think of
baseTramps. So this is dangerous to use, actually.

.. cpp:function:: image *is_image()
.. cpp:function:: mapped_object *is_mapped_object()
.. cpp:function:: parse_func *is_parse_func()
Expand All @@ -34,3 +37,159 @@ codeRange.h
.. cpp:function:: codeRange() = default
.. cpp:function:: codeRange(const codeRange&) = default
.. cpp:function:: virtual ~codeRange() = default


.. cpp:struct:: entry

Red/black tree node

.. cpp:member:: Dyninst::Address key
.. cpp:member:: codeRange *value
.. cpp:member:: color_t color

color of the node

.. cpp:member:: struct entry* left

left child

.. cpp:member:: struct entry* right

right child

.. cpp:member:: struct entry* parent

parent of the node

.. cpp:function:: entry()

constructor for structure

.. cpp:function:: entry(entry* e)

constructor used for non-nil elements

.. cpp:function:: entry(Dyninst::Address key_, codeRange* value_, entry* e)

.. cpp:function:: entry(const entry& e)


.. cpp:class:: codeRangeTree

.. cpp:member:: private entry* nil

pointer to define the nil element of the tree NULL is not used since some operations need
sentinel nil which may have non-nil parent.

.. cpp:member:: private int setSize

size of the tree

.. cpp:member:: private entry* setData

pointer to the tree structure

.. cpp:function:: private void leftRotate(entry*)

Left rotation used by RB tree for balanced tree construction and keeps the RBtree properties.

.. cpp:function:: private void rightRotate(entry*)

Right rotattion used by RB tree for balanced tree construction and keeps the RBtree properties.

.. cpp:function:: private void deleteFixup(entry*)

Modifies the tree structure after deletion for keeping the RBtree properties.

.. cpp:function:: private entry* treeInsert(Dyninst::Address, codeRange *)

insertion to a binary search tree.

It returns the new element pointer that is inserted. If element is already there it returns NULL

.. cpp:function:: private entry* treeSuccessor(entry* ) const

finds the elemnts in the tree that will be replaced with the element being deleted in the deletion.

That is the element with the largest/smallest value than the element being deleted.

.. cpp:function:: private entry* find_internal(Dyninst::Address) const

method that returns the entry pointer for the element that is searchedfor. If the entry is not found then it retuns NULL

.. cpp:function:: private void traverse(codeRange**, entry*, int&) const

infix traverse of the RB tree. It traverses the tree in ascending order

.. cpp:function:: private void traverse(std::vector<codeRange*>& all, entry*) const

Vector version of above infix traverse of the RB tree. It traverses the tree in ascending order

.. cpp:function:: private void destroy(entry*)

deletes the tree structure for deconstructor.

.. cpp:function:: private codeRangeTree(const codeRangeTree &)

.. cpp:function:: codeRangeTree()

constructor. The default comparison structure is used

.. cpp:function:: ~codeRangeTree()

destructor which deletes all tree structure and allocated entries

.. cpp:function:: int size() const

returns the cardinality of the tree , number of elements

.. cpp:function:: bool empty() const

returns true if tree is empty

.. cpp:function:: void insert(codeRange *r)

Inserts ``r`` into tree.

.. cpp:function:: void remove(Dyninst::Address addr)

Removes the range starting at ``addr``.

.. cpp:function:: bool find(Dyninst::Address addr, codeRange*& r) const

Returns in ``r`` the range starting at address ``r``.

Returns ``true`` if a match was found.

.. cpp:function:: bool precessor(Dyninst::Address, codeRange *&) const

Returns the largest value less than or equal to the key given

.. cpp:function:: bool successor(Dyninst::Address, codeRange *&) const

Returns the smallest value greater than or equal to the key given

.. cpp:function:: codeRange** elements(codeRange**) const

Fill an buffer array with the sorted elements of the codeRangeTree in ascending order according
to comparison function.

Returns ``NULL`` if the tree is empty. Otherwise returns the input argument.

.. cpp:function:: bool elements(std::vector<codeRange *> &) const

And vector-style

.. cpp:function:: entry* replicateTree(entry*,entry*,entry*,entry*)

method that replicates the tree structure of this tree

.. cpp:function:: void clear()

Remove all entries in the tree


.. cpp:enum:: codeRangeTree::color_t

.. cpp:enumerator:: TREE_BLACK
.. cpp:enumerator:: TREE_RED
8 changes: 0 additions & 8 deletions dyninstAPI/src/codeRange.C
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ inferiorRPCinProgress * codeRange::is_inferior_rpc() {
return dynamic_cast< inferiorRPCinProgress * >( this );
}

// This is a special case... the multitramp is the thing in the
// codeRange tree, but people think of baseTramps.
// So this is dangerous to use, actually.
block_instance *codeRange::is_basicBlockInstance() {
return dynamic_cast<block_instance *>(this);
}
Expand Down Expand Up @@ -178,7 +175,6 @@ void codeRangeTree::deleteFixup(entry* x){
}


// fails if the key value is already in the tree (happens for shared code)
codeRangeTree::entry *codeRangeTree::treeInsert(Dyninst::Address key, codeRange *value)
{
entry* y = NULL;
Expand Down Expand Up @@ -209,8 +205,6 @@ codeRangeTree::entry *codeRangeTree::treeInsert(Dyninst::Address key, codeRange
return z;
}

/** finds the minimum value node when x is being deleted */

codeRangeTree::entry *codeRangeTree::treeSuccessor(entry* x) const{
if(!x || (x == nil))
return NULL;
Expand Down Expand Up @@ -266,8 +260,6 @@ void codeRangeTree::traverse(std::vector<codeRange *> &all, entry* node) const{
traverse(all,node->right);
}

//////////////////////////// PUBLIC FUNCTIONS ////////////////////////////////

void codeRangeTree::insert(codeRange *value) {
//assert(value->get_size());
entry* x = treeInsert(value->get_address(), value);
Expand Down
73 changes: 5 additions & 68 deletions dyninstAPI/src/codeRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,150 +85,87 @@ class codeRangeTree {

typedef enum {TREE_BLACK, TREE_RED} color_t;

/** tree implementation structure. Used to implement the RB tree */
typedef struct entry {
Dyninst::Address key;
codeRange *value;
color_t color; /* color of the node */
struct entry* left; /* left child */
struct entry* right; /* right child */
struct entry* parent; /* parent of the node */
color_t color;
struct entry* left;
struct entry* right;
struct entry* parent;

/** constructor for structure */
entry()
: key(0), value(NULL), color(TREE_BLACK),left(NULL),right(NULL),parent(NULL) {}

/** constructor used for non-nil elements
* @param e nil entry
*/
entry(entry* e) //constructor with nil entry
entry(entry* e)
: key(0), value(NULL), color(TREE_RED), left(e), right(e), parent(NULL) {}

/** constructor
* @param d data element
* @param e nill entry
*/
entry(Dyninst::Address key_, codeRange *value_, entry* e)
: key(key_), value(value_), color(TREE_RED), left(e),
right(e), parent(NULL) {}

/** constructor
* @param e the entry structure that will be copied
*/
entry(const entry& e) : key(e.key),value(e.value),color(e.color),
left(NULL),right(NULL),parent(NULL) {}
} entry;

/** pointer to define the nil element of the tree NULL is not used
* since some operations need sentinel nil which may have non-nil
* parent.
*/
entry* nil;

/** size of the tree */
int setSize;

/** pointer to the tree structure */
entry* setData;

// method that implements left rotation used by RB tree for balanced
// tree construction and keeps the RBtree properties.
void leftRotate(entry*);

// method that implements right rotattion used by RB tree for balanced
// tree construction and keeps the RBtree properties.
void rightRotate(entry*);

// method that modifies the tree structure after deletion for keeping
// the RBtree properties.
void deleteFixup(entry*);

// insertion to a binary search tree. It returns the new element pointer
// that is inserted. If element is already there it returns NULL
entry* treeInsert(Dyninst::Address, codeRange *);

// finds the elemnts in the tree that will be replaced with the element
// being deleted in the deletion. That is the element with the largest
// smallest value than the element being deleted.
entry* treeSuccessor(entry* ) const;

// method that returns the entry pointer for the element that is searched
//for. If the entry is not found then it retuns NULL
entry* find_internal(Dyninst::Address) const;

// infix traverse of the RB tree. It traverses the tree in ascending order
void traverse(codeRange **,entry*,int&) const;

// Vector version of above
// infix traverse of the RB tree. It traverses the tree in ascending order
void traverse(std::vector<codeRange *> &all, entry*) const;

// deletes the tree structure for deconstructor.
void destroy(entry*);

/** copy constructor */
codeRangeTree(const codeRangeTree &/* y */) {}

public:

/** constructor. The default comparison structure is used */
codeRangeTree() : setSize(0) {
nil = new entry;
setData = nil;
}


/** destructor which deletes all tree structure and allocated entries */
~codeRangeTree() {
destroy(setData);
delete nil;
}

/** returns the cardinality of the tree , number of elements */
int size() const { return setSize; }

/** returns true if tree is empty */
bool empty() const { return (setData == nil); }

/** inserts the element in the tree
* @param 1 element that will be inserted
*/
void insert(codeRange *);

/** removes the element in the tree
* @param 1 element that will be removed
*/
void remove(Dyninst::Address);

/** returns true if the argument is member of the codeRangeTree
* @param e the element that will be searched for
*/
bool find(Dyninst::Address, codeRange *&) const;

/** Returns the largest value less than or equal to the
* key given
*/
bool precessor(Dyninst::Address, codeRange *&) const;

/** Returns the smallest value greater than or equal to the
* key given
*/
bool successor(Dyninst::Address, codeRange *&) const;

/** fill an buffer array with the sorted
* elements of the codeRangeTree in ascending order according to comparison function
* if the codeRangeTree is empty it retuns NULL, other wise it returns
* the input argument.
*/
codeRange ** elements(codeRange **) const;

// And vector-style
bool elements(std::vector<codeRange *> &) const;

// method that replicates the tree structure of this tree
entry* replicateTree(entry*,entry*,entry*,entry*);

// Remove all entries in the tree
void clear();
};

Expand Down

0 comments on commit 2834074

Please sign in to comment.