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

📄 acis_tutorials_(memory_management).htm

📁 acis说明文档
💻 HTM
📖 第 1 页 / 共 3 页
字号:
</td><td style="padding:0.5em; background-color:#e3e5e7; font-size:90%; line-height:0.95em; border:1px solid #A3B1BF; border-bottom:solid 2px #A3B1BF" width="20"> <a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28Options%29" title="Tutorial:ACIS Tutorials (Options)">Options</a></td><td style="border-bottom:2px solid #A3B1BF" width="3">&nbsp;</td><td style="padding:0.5em; background-color:white; line-height:0.95em; border:solid 2px #A3B1BF; border-bottom:0; font-weight:bold;" width="20"> <strong class="selflink">Memory&nbsp;Management</strong></td><td style="border-bottom:2px solid #A3B1BF" width="3"> &nbsp;</td><td style="padding:0.5em; background-color:#e3e5e7; font-size:90%; line-height:0.95em; border:1px solid #A3B1BF; border-bottom:solid 2px #A3B1BF" width="20"> <a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28Exception_Handling%29" title="Tutorial:ACIS Tutorials (Exception Handling)">Exception&nbsp;Handling</a></td><td style="border-bottom:2px solid #A3B1BF" width="3">&nbsp;</td><td style="padding:0.5em; background-color:#e3e5e7; font-size:90%; line-height:0.95em; border:1px solid #A3B1BF; border-bottom:solid 2px #A3B1BF" width="20"> <a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28History_Streams%29" title="Tutorial:ACIS Tutorials (History Streams)">History</a></td><td style="border-bottom:2px solid #A3B1BF" width="3">&nbsp;</td><td style="padding:0.5em; background-color:#e3e5e7; font-size:90%; line-height:0.95em; border:1px solid #A3B1BF; border-bottom:solid 2px #A3B1BF" width="20"> <a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28Attributes%29" title="Tutorial:ACIS Tutorials (Attributes)">Attributes</a></td><td style="border-bottom:2px solid #A3B1BF" width="3">&nbsp;</td><td style="padding:0.5em; background-color:#e3e5e7; font-size:90%; line-height:0.95em; border:1px solid #A3B1BF; border-bottom:solid 2px #A3B1BF" width="20"> <a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28Importing_Non-ACIS_Models%29" title="Tutorial:ACIS Tutorials (Importing Non-ACIS Models)">Importing&nbsp;Models</a></td><td style="border-bottom:2px solid #A3B1BF" width="3">&nbsp;</td><td style="border-bottom:2px solid #A3B1BF" width="3000"> &nbsp;</td></tr></table><div style="border:2px solid #A3B1BF; padding:.5em 1em 1em 1em; border-top:none; background-color:#fff; color:#000"><a name="Memory_Management"></a><h2> <span class="mw-headline"> Memory Management  </span></h2><p>ACIS provides memory management capabilities that allow you to use free lists, overwrite <i>operator new( )</i> and <i>operator delete( )</i>, detect memory leaks, etc.  Customers may choose to use these capabilities or develop their own memory management capabilities.  The ACIS memory managment capabilities are described in several articles, starting with <a href="/r18/index.php?title=Memory_Management&amp;action=edit" class="new" title="Memory Management">Memory Management</a>.  In this tutorial we describe one means of consistently allocating and de-allocating memory in ACIS, routing all allocations and de-allocations through the ACIS memory manager.  We suggest this approach for all new users and any users who do not want to develop their own memory management systems.</p><div style="float:right;"><a href="http://doc.spatial.com/r18/index.php?title=Tutorial:ACIS_Tutorial_8:_Allocating_and_De-allocating_ACIS_Memory&amp;action=edit" class="external text" title="http://doc.spatial.com/r18/index.php?title=Tutorial:ACIS_Tutorial_8:_Allocating_and_De-allocating_ACIS_Memory&amp;action=edit" rel="nofollow"><span style="color: #000000">edit</span></a></div><table id="toc" class="toc" summary="Contents"><tr><td><div id="toctitle"><h2>Contents</h2></div><ul><li class="toclevel-1"><a href="#Memory_Management"><span class="tocnumber">1</span> <span class="toctext">Memory Management</span></a></li><li class="toclevel-1"><a href="#Local_Variables"><span class="tocnumber">2</span> <span class="toctext">Local Variables</span></a></li><li class="toclevel-1"><a href="#Persistent_Variables"><span class="tocnumber">3</span> <span class="toctext">Persistent Variables</span></a><ul><li class="toclevel-2"><a href="#Construction_and_Destruction_of_ENTITIES"><span class="tocnumber">3.1</span> <span class="toctext">Construction and Destruction of ENTITIES</span></a></li><li class="toclevel-2"><a href="#Construction_and_Destruction_of_non-ENTITY_classes"><span class="tocnumber">3.2</span> <span class="toctext">Construction and Destruction of non-ENTITY classes</span></a></li><li class="toclevel-2"><a href="#Construction_and_Destruction_of_Standard_Types"><span class="tocnumber">3.3</span> <span class="toctext">Construction and Destruction of Standard Types</span></a></li><li class="toclevel-2"><a href="#Construction_and_Destruction_of_Arrays"><span class="tocnumber">3.4</span> <span class="toctext">Construction and Destruction of Arrays</span></a></li></ul></li><li class="toclevel-1"><a href="#Static_Variables"><span class="tocnumber">4</span> <span class="toctext">Static Variables</span></a></li></ul></li></ul></td></tr></table><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script><p>This tutorial introduces you to one means of allocating and de-allocating memory in an ACIS based application, using the ACIS memory managment system for all allocations and de-allocations.  This is the preferred means for new users because it provides a consistent and efficient approach for all allocations and de-allocations.  Additional information on the ACIS memory management system is available at <a href="/r18/index.php?title=Memory_Management&amp;action=edit" class="new" title="Memory Management">Memory Management</a>.</p><a name="Local_Variables"></a><h2> <span class="mw-headline"> Local Variables </span></h2><p>Temporary (locally scoped) variables are created on the stack.  The creation of these non-persistent variables follows standard C++ conventions.  This applies to ACIS specific classes as well as standard types.  These allocations (and subsequent automatic de-allocations) are handled by the operating system and do not utilize the ACIS memory management system.  </p><p>The allocation of local variables is demonstrated below.  The SPAvector class is the ACIS implementation of a generalized vector class and is used quite extensively within ACIS.</p><pre class="source-cpp"><span class="br0">&#123;</span>  <span class="kw4">double</span> value1 = <span class="nu0">4.0</span>;  <span class="kw4">double</span> value2 = <span class="nu0">2.5</span>;&nbsp;  <span class="kw5">SPAvector</span> vec1<span class="br0">&#40;</span>value1, <span class="nu0">0.0</span>, <span class="nu0">0.0</span><span class="br0">&#41;</span>;  <span class="kw5">SPAvector</span> vec2<span class="br0">&#40;</span><span class="nu0">0.0</span>, value2, <span class="nu0">0.0</span><span class="br0">&#41;</span>;  <span class="kw5">SPAvector</span> vec3  = vec1 * vec2;  <span class="co1">// The cross product</span>  <span class="kw4">double</span> dot_prod = vec1&nbsp;% vec2;  <span class="co1">// The dot product</span><span class="br0">&#125;</span></pre><a name="Persistent_Variables"></a><h2> <span class="mw-headline"> Persistent Variables </span></h2><p>Persistent variables are created on the Free Store or heap.  Allocations and de-allocations of persistent variables are handled by the ACIS memory management system.  These variables may be created on an ACIS free list. (The use of free lists depends on whether or not free lists are created during ACIS initialization and whether or not a free list of the appropriate size for the given object exists.)   </p><p>In order to direct allocations and de-allocations of persistent variables to the ACIS memory management system two macros are used to allocate and de-allocate memory: <i>ACIS_NEW</i> and <i>ACIS_DELETE</i>.  These macros are used instead of the standard <i>operator new( )</i> and <i>operator delete( )</i>.  In Tutorial 2 we mentioned that every source file that uses ACIS should include the <i>acis.hxx</i> header file.  One of the benefits of including this header file is the definitions of all of the macros for memory allocation and de-allocation are included.</p><a name="Construction_and_Destruction_of_ENTITIES"></a><h3> <span class="mw-headline"> Construction and Destruction of ENTITIES </span></h3><p>Most ENTITIES will be constructed for you by calling API and direct interface functions; however, occasionally you may need to construct an instance of a class derived from ENTITY in your application.  In this case you will use the <i>ACIS_NEW</i> macro rather than the <i>operator new( )</i>.  An example of this is shown below.</p><pre class="source-cpp"><span class="kw5">SPAposition</span> my_loc<span class="br0">&#40;</span><span class="nu0">0.0</span>, <span class="nu0">0.0</span>, <span class="nu0">0.0</span><span class="br0">&#41;</span>;<span class="kw7">APOINT</span> * pt = <span class="kw6">ACIS_NEW</span> <span class="kw7">APOINT</span><span class="br0">&#40;</span>my_loc<span class="br0">&#41;</span>;<span class="kw7">VERTEX</span> * vert = <span class="kw6">ACIS_NEW</span> <span class="kw7">VERTEX</span><span class="br0">&#40;</span>pt<span class="br0">&#41;</span>;</pre><p>Because ENTITIES are maintained by the ACIS history mechanism to facilitate roll back and roll forward (i.e., undo and redo) you will typically not de-allocate the memory for an ENTITY; instead you will call either the lose( ) method of the ENTITY or a higher level function that calls the lose( ) method for you.  The reason for calling a higher level function is because you often want to lose a set of connected ENTITIES.  For example, you would not want to manually call lose( ) for each of the topological, geometrical, and application-specific ENTITIES in a complex BODY.  You would want to call a single function that traverses the data structure and calls lose( ) for you.  In addition, many types of ENTITIES are use counted, so you do not want to call lose( ) directly on them.  </p><p>There are two higher level functions that are typically called to lose ENTITIES.</p><table class="wikitable"><tr><th> API Function</th><th> Description</th></tr><tr><td> api_delent(ENTITY*,...)</td><td> Deletes a topological ENTITY and all subentities.</td></tr><tr><td> api_del_entity(ENTITY*,...)</td><td> Deletes a topological ENTITY and all connected entities.</td></tr></table><p>The difference between these two functions is very significant.  api_delent( ) loses the given topological entity, any associated geometrical entity, any associated application-specific entities on the given entity, and then traverses the topological structure downward, losing all lower level topological entities and their associated geometric and application-specific entities.  api_del_entity( ) loses the given topological entity, any associated geometrical entity, any associated application-specific entities on the given entity, and then traverses the data structure upward, downward, and horizontally, losing all connected entities.  If entities in one BODY point to entities in another BODY, all entities in both BODIES will be lost.  Neither function can affect pointers in your application that pointed to the lost entities.  It is the responsibility of the application programmer to be sure that no dangling pointers remain.  The use of api_delent( ) and api_del_entity( ) are demonstrated below.</p><pre class="source-cpp"><span class="kw5">SPAposition</span> my_loc<span class="br0">&#40;</span><span class="nu0">0.0</span>, <span class="nu0">0.0</span>, <span class="nu0">0.0</span><span class="br0">&#41;</span>;<span class="kw7">APOINT</span> * pt = <span class="kw6">ACIS_NEW</span> <span class="kw7">APOINT</span><span class="br0">&#40;</span>my_loc<span class="br0">&#41;</span>;<span class="kw7">VERTEX</span> * vert = <span class="kw6">ACIS_NEW</span> <span class="kw7">VERTEX</span><span class="br0">&#40;</span>pt<span class="br0">&#41;</span>;api_delent <span class="br0">&#40;</span>vert<span class="br0">&#41;</span>;vert = <span class="kw2">NULL</span>;pt = <span class="kw2">NULL</span>;</pre><pre class="source-cpp"><span class="kw7">BODY</span> * my_block = <span class="kw2">NULL</span>;api_make_cuboid <span class="br0">&#40;</span><span class="nu0">10.0</span>, <span class="nu0">10.0</span>, <span class="nu0">10.0</span>, my_block<span class="br0">&#41;</span>;api_delent <span class="br0">&#40;</span>my_block<span class="br0">&#41;</span>;my_block = <span class="kw2">NULL</span>;</pre><a name="Construction_and_Destruction_of_non-ENTITY_classes"></a><h3> <span class="mw-headline"> Construction and Destruction of non-ENTITY classes </span></h3><p>Instances of objects that are not derived from ENTITY should be constructed and destructed using the ACIS_NEW and ACIS_DELETE operators.  This ensures consistent use of the underlying allocation and de-allocation functions.  Use of ACIS_NEW and ACIS_DELETE to construct and destruct instances of classes not derived from ENTITY is demonstrated below.</p><pre class="source-cpp"><span class="kw5">SPAposition</span> * my_loc_ptr = <span class="kw6">ACIS_NEW</span> <span class="kw5">SPAposition</span><span class="br0">&#40;</span><span class="nu0">10.0</span>, <span class="nu0">10.0</span>, <span class="nu0">10.0</span><span class="br0">&#41;</span>;<span class="kw6">ACIS_DELETE</span> my_loc_ptr;</pre><a name="Construction_and_Destruction_of_Standard_Types"></a><h3> <span class="mw-headline"> Construction and Destruction of Standard Types </span></h3><p>The use of the ACIS_NEW macro when constructing a standard type object (for instance, a double or int) causes the memory allocation to be processed by the ACIS memory manager.  When destructing an instance of a standard type that was constructed with ACIS_NEW one should always use the STD_CAST macro with ACIS_DELETE.  This is demonstrated below.</p><pre class="source-cpp"><span class="kw4">double</span> * double_ptr = <span class="kw6">ACIS_NEW</span> <span class="kw4">double</span>;<span class="kw6">ACIS_DELETE</span> <span class="kw7">STD_CAST</span> double_ptr;</pre><a name="Construction_and_Destruction_of_Arrays"></a><h3> <span class="mw-headline"> Construction and Destruction of Arrays </span></h3><p>When destructing an array of objects one should always use [ ] with ACIS_DELETE just as with <i>operator delete( )</i>.  This is demonstrated below.</p><pre class="source-cpp"><span class="kw5">SPAposition</span> * pos_array = <span class="kw6">ACIS_NEW</span> <span class="kw5">SPAposition</span><span class="br0">&#91;</span>NUM_LOCS<span class="br0">&#93;</span>;<span class="kw6">ACIS_DELETE</span> <span class="br0">&#91;</span><span class="br0">&#93;</span> pos_array;&nbsp;<span class="kw4">int</span> * i_array = <span class="kw6">ACIS_NEW</span> <span class="kw4">int</span><span class="br0">&#91;</span>NUM_INTS<span class="br0">&#93;</span>;<span class="kw6">ACIS_DELETE</span> <span class="br0">&#91;</span> <span class="br0">&#93;</span> <span class="kw7">STD_CAST</span> i_array;</pre><a name="Static_Variables"></a><h2> <span class="mw-headline"> Static Variables </span></h2><p>Because the ACIS memory management system has not been initialized when static objects are constructed, you should not create instances of ACIS classes as static variables in your application.&nbsp;<br/>&nbsp;<br/>&nbsp;<br/>&nbsp;<br/></p><table cellspacing="0" cellpadding="0" align="center" style="width:100%; border:2px #a3b1bf solid; margin-top: 1em; font-size:85%"><tr><td id="totdhelp" colspan="2" style="width:100%; margin-top:0em; padding-top:1em; padding-left:1em; padding-right:1em; background:#f5faff;"><p>The <b>ACIS Tutorials</b> contain discussions and examples that will introduce you to ACIS concepts and terminology, and demonstrate how to successfully use ACIS.  Each tutorial builds upon prior tutorials; therefore, we suggest that new users proceed through these tutorials sequentially.  It is expected that new users will be familiar with programming in C++ and their development environment before beginning these tutorials.  In addition it is expected that new users will have at least a basic understanding of topology and geometry.  </p></td></tr></table><p><br /></p><div style="float:right; margin-top: 0.0em; margin-bottom:3px; background-color: #e3e5e7; padding: .2em .6em; font-size: 130%; border:1px solid #A3B1BF;"><b>Continue:</b> <b><a href="/r18/index.php/Tutorial:ACIS_Tutorials_%28Exception_Handling%29" title="Tutorial:ACIS Tutorials (Exception Handling)">What happens if something goes wrong?</a></b> <span style="font-size: larger; font-weight: bold;"></span><div style="clear:both"></div></div></div>

⌨️ 快捷键说明

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