⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page430.html

📁 Data Structures And Algorithms With Object-Oriented Design Patterns In Python (2003) source code and
💻 HTML
字号:
<HTML><HEAD><TITLE>Handles</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html6123" HREF="page431.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6121" HREF="page429.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6117" HREF="page429.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6125" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H2><A NAME="SECTION0013510000000000000000">Handles</A></H2><P>The Python virtual machine specification does not prescribehow reference variables are implemented.One approach is for a reference variable to be implementedas an index into an array of object <em>handles</em><A NAME=31420>&#160;</A>.Every object has its own handle.The handle for an object typically contains a reference toa <tt>type</tt> object that describes the typeof the object associated with the handleand a pointer to the region in the heap where the object data resides.<P>The advantage of using handles is that when the position in the heapof an object is changed,only the handle for that object needs to be modified.All other references to that object are unaffectedbecause such references actually refer to the handle.The cost of using handles is that the handle must be dereferencedevery time an object is accessed.<P>The mark-and-compact algorithm uses the handles in two ways:First, the <tt>marked</tt> flags which are set during the mark operationare stored in the handles rather than in the objects themselves.Second, compaction is greatly simplified because when an object is movedonly its handle needs to be updated--all other objects are unaffected.<P>Figure&nbsp;<A HREF="page430.html#fighandles"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> illustrates how object referencesare implemented using handles.Figure&nbsp;<A HREF="page430.html#fighandles"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>&nbsp;(a) shows a circular, singly-linked listas it is usually drawnand Figure&nbsp;<A HREF="page430.html#fighandles"><IMG  ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>&nbsp;(b) shows how the listis represented when using handles.Each reference variable actually contains an index into the array of handles.For example, the <tt>head</tt> variable selects the handle at offset 2and that handle points to linked list element <I>A</I>.Similarly, the <tt>next</tt> field of list element <I>A</I> selectsthe handle at offset 5 which refers to list element <I>B</I>.Notice that when an object is moved, only its handle needs to be modified.<P><P><A NAME="31874">&#160;</A><A NAME="fighandles">&#160;</A> <IMG WIDTH=575 HEIGHT=310 ALIGN=BOTTOM ALT="figure31428" SRC="img1697.gif"  ><BR><STRONG>Figure:</STRONG> Representing object references using handles.<BR><P><P>The handle is a convenient place in which to record informationused by the garbage collection algorithm.For example,we add a <tt>bool</tt> field to each handle, called <tt>marked</tt>.The <tt>marked</tt> field is used to mark live objects as follows:<PRE>def mark(p):    if not handle[p].marked:        handle[p].marked = True	for q in successors(p):            mark(q)</PRE>Notice that this version of the <tt>mark</tt> methodmarks the object handles rather than the objects themselves.<P>Once all of the live objects in the heap have been identified,the heap needs to be defragmented.Perhaps the simplest way to defragment the heapis to <em>slide</em><A NAME=31882>&#160;</A> the objects in the heapall to one end, removing the unused memory locations separating them.The following version of the <tt>compact</tt> method does just this:<PRE>def compact():    offset = 0    for q in heap:        if handle[p].marked:            handle[p].obj = heap.move(p, offset)            handle[p].marked = False            offset += sizeof(type(p))</PRE>This algorithm makes a single pass through the objects in the heap,moving the live objects towards the lower heap addresses as it goes.The <tt>compact</tt> method only modifies the object handles--object data remain unchanged.This algorithm also illustrates an important characteristic ofthe sliding compaction algorithm--the relative positions of the objects in the heap remains unchangedafter the compaction operation.Also, when the compaction method has finished,the <tt>marked</tt> fields have all been set back to <tt>False</tt>in preparation for the next garbage collection operation.<P><HR><A NAME="tex2html6123" HREF="page431.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html6121" HREF="page429.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html6117" HREF="page429.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A>  <A NAME="tex2html6125" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright &#169; 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a>  All rights reserved.</ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -