00001 /***************************************************************************** 00002 00003 The following code is derived, directly or indirectly, from the SystemC 00004 source code Copyright (c) 1996-2006 by all Contributors. 00005 All Rights reserved. 00006 00007 The contents of this file are subject to the restrictions and limitations 00008 set forth in the SystemC Open Source License Version 2.4 (the "License"); 00009 You may not use this file except in compliance with such restrictions and 00010 limitations. You may obtain instructions on how to receive a copy of the 00011 License at http://www.systemc.org/. Software distributed by Contributors 00012 under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 00013 ANY KIND, either express or implied. See the License for the specific 00014 language governing rights and limitations under the License. 00015 00016 *****************************************************************************/ 00017 00018 /***************************************************************************** 00019 00020 sc_object_manager.cpp -- Manager of objects (naming, &c.) 00021 00022 Original Author: Stan Y. Liao, Synopsys, Inc. 00023 00024 *****************************************************************************/ 00025 00026 /***************************************************************************** 00027 00028 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 00029 changes you are making here. 00030 00031 Name, Affiliation, Date: 00032 Description of Modification: 00033 00034 *****************************************************************************/ 00035 00036 00037 // $Log: sc_object_manager.cpp,v $ 00038 // Revision 1.1.1.1 2006/12/15 20:31:37 acg 00039 // SystemC 2.2 00040 // 00041 // Revision 1.3 2006/01/13 18:44:30 acg 00042 // Added $Log to record CVS changes into the source. 00043 // 00044 00045 #include <stdio.h> 00046 #include <cstdlib> 00047 #include <cassert> 00048 #include <ctype.h> 00049 00050 #include "sysc/utils/sc_iostream.h" 00051 #include "sysc/kernel/sc_object.h" 00052 #include "sysc/utils/sc_hash.h" 00053 #include "sysc/utils/sc_list.h" 00054 #include "sysc/utils/sc_mempool.h" 00055 #include "sysc/kernel/sc_object_manager.h" 00056 #include "sysc/kernel/sc_kernel_ids.h" 00057 #include "sysc/kernel/sc_module_name.h" 00058 00059 namespace sc_core { 00060 00061 int 00062 strcmp_void(const void* a, const void* b) 00063 { 00064 return strcmp(static_cast<const char*>(a),static_cast<const char*>(b)); 00065 } 00066 00067 00068 // ---------------------------------------------------------------------------- 00069 // CLASS : sc_object_manager 00070 // 00071 // Manager of objects. 00072 // ---------------------------------------------------------------------------- 00073 00074 sc_object_manager::sc_object_manager() 00075 { 00076 m_ordered_object_vector = new object_vector_type; 00077 00078 m_object_table = new object_table_type; 00079 m_object_table->set_hash_fn(default_str_hash_fn); 00080 m_object_table->set_cmpr_fn(strcmp_void); 00081 00082 m_object_hierarchy = new object_hierarchy_type; 00083 m_ordered_object_vector_dirty = true; 00084 m_next_object_index = 0; 00085 m_module_name_stack = 0; 00086 } 00087 00088 sc_object_manager::~sc_object_manager() 00089 { 00090 delete m_object_hierarchy; 00091 00092 /* Go through each object in the table, and 00093 mark the m_simc field of the object NULL */ 00094 object_table_type::iterator it(m_object_table); 00095 for ( ; !it.empty(); it++) { 00096 sc_object* obj = it.contents(); 00097 obj->m_simc = 0; 00098 } 00099 delete m_object_table; 00100 delete m_ordered_object_vector; 00101 } 00102 00103 sc_object* 00104 sc_object_manager::find_object(const char* name) 00105 { 00106 return (*m_object_table)[name]; 00107 } 00108 00109 extern "C" { 00110 static int 00111 object_name_compare(const void* o1, const void* o2) 00112 { 00113 const sc_object* obj1 = *(const sc_object**) o1; 00114 const sc_object* obj2 = *(const sc_object**) o2; 00115 return strcmp(obj1->name(), obj2->name()); 00116 } 00117 } 00118 00119 sc_object* 00120 sc_object_manager::first_object() 00121 { 00122 if (m_ordered_object_vector_dirty) { 00123 m_ordered_object_vector->erase_all(); 00124 object_table_type::iterator it(m_object_table); 00125 while (! it.empty()) { 00126 sc_object* obj = it.contents(); 00127 m_ordered_object_vector->push_back(obj); 00128 it++; 00129 } 00130 m_ordered_object_vector->sort(object_name_compare); 00131 m_ordered_object_vector_dirty = false; 00132 } 00133 m_next_object_index = 0; 00134 return next_object(); 00135 } 00136 00137 sc_object* 00138 sc_object_manager::next_object() 00139 { 00140 assert( ! m_ordered_object_vector_dirty ); 00141 if ( m_next_object_index >= m_ordered_object_vector->size() ) 00142 return 0; 00143 else 00144 return (*m_ordered_object_vector)[m_next_object_index++]; 00145 } 00146 00147 void 00148 sc_object_manager::hierarchy_push(sc_object* mdl) 00149 { 00150 m_object_hierarchy->push_front(mdl); 00151 } 00152 00153 sc_object* 00154 sc_object_manager::hierarchy_pop() 00155 { 00156 return m_object_hierarchy->pop_front(); 00157 } 00158 00159 sc_object* 00160 sc_object_manager::hierarchy_curr() 00161 { 00162 return m_object_hierarchy->empty() ? 0 : m_object_hierarchy->front(); 00163 } 00164 00165 int 00166 sc_object_manager::hierarchy_size() 00167 { 00168 return m_object_hierarchy->size(); 00169 } 00170 00171 void 00172 sc_object_manager::push_module_name(sc_module_name* mod_name) 00173 { 00174 mod_name->m_next = m_module_name_stack; 00175 m_module_name_stack = mod_name; 00176 } 00177 00178 sc_module_name* 00179 sc_object_manager::pop_module_name() 00180 { 00181 sc_module_name* mod_name = m_module_name_stack; 00182 m_module_name_stack = m_module_name_stack->m_next; 00183 mod_name->m_next = 0; 00184 return mod_name; 00185 } 00186 00187 sc_module_name* 00188 sc_object_manager::top_of_module_name_stack() 00189 { 00190 if( m_module_name_stack == 0 ) { 00191 SC_REPORT_ERROR( SC_ID_MODULE_NAME_STACK_EMPTY_, 0 ); 00192 } 00193 return m_module_name_stack; 00194 } 00195 00196 void 00197 sc_object_manager::insert_object(const char* name, sc_object* obj) 00198 { 00199 m_object_table->insert(name, obj); 00200 m_ordered_object_vector_dirty = true; 00201 } 00202 00203 void 00204 sc_object_manager::remove_object(const char* name) 00205 { 00206 m_object_table->remove(name); 00207 m_ordered_object_vector_dirty = true; 00208 } 00209 00210 } // namespace sc_core