00001 #include "soarkernel.h"
00002 #include "debugutil.h"
00003
00004 extern void detailed_print_wme(wme * w);
00005 extern void detailed_print_preference(preference * p);
00006
00007 void examine_memory_pool(memory_pool * p)
00008 {
00009
00010 print("Examine Memory Pool:\n");
00011 print("Free List Head: %p\n", p->free_list);
00012
00013 #ifdef MEMORY_POOL_STATS
00014 print("Used Count: %ld\n", p->used_count);
00015 #endif
00016 #ifdef TRACE_MEMORY_USAGE
00017 print("Free List Length: %ld\n", p->free_list_length);
00018 print("Pool Size: %ld\n", p->pool_size);
00019 #endif
00020
00021 print("Item Size: %ld\n", p->item_size);
00022 print("Items / Block: %ld\n", p->items_per_block);
00023 print("Num Blocks: %ld\n", p->num_blocks);
00024 print("First Block: %p\n", p->first_block);
00025 print("Name: %s\n", p->name);
00026
00027 }
00028
00029 void print_item_info_header(void)
00030 {
00031 print("Block Number, Block Address, Next Block, Item Address\n");
00032 }
00033
00034 void *get_item_in_pool_block(memory_pool * p, long n, long b, bool p_free)
00035 {
00036
00037 char *block_head;
00038 long i;
00039 void *item;
00040 bool in_free_list;
00041
00042 block_head = p->first_block;
00043 for (i = 0; i < b; i++) {
00044
00045
00046
00047
00048
00049
00050 block_head = *(char **) block_head;
00051 }
00052
00053 block_head += sizeof(char *);
00054
00055 if (n > p->items_per_block) {
00056 print("ERROR: The block contains less than %ld items.\n", n);
00057 return NULL;
00058 }
00059
00060 for (i = 0; i < n; i++) {
00061 block_head += p->item_size;
00062 }
00063 item = block_head;
00064
00065 in_free_list = (char) check_for_addr_in_free_list(p, block_head);
00066
00067 if (!in_free_list || p_free) {
00068 print("%ld, %p, %p, ", b, (char *) block_head, *(char **) block_head);
00069 print("%p\n", block_head);
00070 }
00071 if (in_free_list && p_free) {
00072 print("This item is in the free list. \n");
00073 }
00074 if (in_free_list)
00075 return NULL;
00076 return block_head;
00077
00078 }
00079
00080 void print_all_productions_in_block(long b, bool full, bool p_free)
00081 {
00082 long i;
00083 production *pro;
00084
00085 print_item_info_header();
00086 for (i = 0; i < current_agent(production_pool).items_per_block; i++) {
00087 pro = (production *) get_item_in_pool_block(¤t_agent(production_pool), i, b, p_free);
00088
00089 if (pro) {
00090 if (full) {
00091 print_production(pro, FALSE);
00092 } else {
00093 print_with_symbols("%y\n", pro->name);
00094 }
00095 }
00096 }
00097
00098 }
00099
00100 void print_all_identifiers_in_block(long b, bool p_free)
00101 {
00102 long i;
00103 Symbol *s;
00104
00105 print_item_info_header();
00106 for (i = 0; i < current_agent(identifier_pool).items_per_block; i++) {
00107 s = (Symbol *) get_item_in_pool_block(¤t_agent(identifier_pool), i, b, p_free);
00108
00109 if (s) {
00110 print(symbol_to_string(s, TRUE, NIL, 0));
00111 print(" reference count: %d\n", s->common.reference_count);
00112
00113 }
00114
00115 }
00116
00117 }
00118
00119 void print_all_wmes_in_block(long b, bool p_free)
00120 {
00121 long i;
00122 wme *the_wme;
00123
00124 print_item_info_header();
00125 for (i = 0; i < current_agent(wme_pool).items_per_block; i++) {
00126 the_wme = (wme *) get_item_in_pool_block(¤t_agent(wme_pool), i, b, p_free);
00127
00128 if (the_wme)
00129 detailed_print_wme(the_wme);
00130 }
00131
00132 }
00133
00134 void print_all_conditions_in_block(long b, bool p_free)
00135 {
00136 long i;
00137 condition *the_condition;
00138
00139 print_item_info_header();
00140 for (i = 0; i < current_agent(condition_pool).items_per_block; i++) {
00141 the_condition = (condition *) get_item_in_pool_block(¤t_agent(condition_pool), i, b, p_free);
00142
00143 if (the_condition)
00144 print_condition(the_condition);
00145 }
00146
00147 }
00148
00149 void print_all_preferences_in_block(long b, bool p_free)
00150 {
00151 long i;
00152 preference *the_pref;
00153
00154 print_item_info_header();
00155
00156 for (i = 0; i < current_agent(preference_pool).items_per_block; i++) {
00157 the_pref = (preference *) get_item_in_pool_block(¤t_agent(preference_pool), i, b, p_free);
00158
00159 if (the_pref)
00160 detailed_print_preference(the_pref);
00161 }
00162
00163 }
00164
00165 void print_all_instantiations_in_block(long b, wme_trace_type wtt, bool p_free)
00166 {
00167 long i;
00168 instantiation *the_inst;
00169
00170 print_item_info_header();
00171 for (i = 0; i < current_agent(instantiation_pool).items_per_block; i++) {
00172 the_inst = (instantiation *) get_item_in_pool_block(¤t_agent(instantiation_pool), i, b, p_free);
00173
00174 if (the_inst)
00175 print_instantiation_with_wmes(the_inst, wtt);
00176 }
00177 }
00178
00179 int check_for_addr_in_free_list(memory_pool * p, void *addr)
00180 {
00181
00182 void *free_block_addr;
00183
00184 free_block_addr = p->free_list;
00185
00186 while (free_block_addr != NULL) {
00187
00188
00189
00190
00191
00192 if (free_block_addr == addr)
00193 return 1;
00194
00195 free_block_addr = *(void **) free_block_addr;
00196
00197 }
00198
00199 return 0;
00200 }
00201
00202 #ifdef USE_DEBUG_UTILS
00203
00204 void allocate_with_pool_fn(memory_pool * p, void **dest_item_pointer)
00205 {
00206
00207 if (!(p)->free_list)
00208 add_block_to_memory_pool(p);
00209 *(dest_item_pointer) = (p)->free_list;
00210 (p)->free_list = *(void **) (*(dest_item_pointer));
00211 fill_with_garbage(*(dest_item_pointer), (p)->item_size);
00212 increment_used_count(p);
00213 decrement_free_list_length(p);
00214
00215 }
00216
00217 void free_with_pool_fn(memory_pool * p, void *item)
00218 {
00219
00220 fill_with_garbage((item), (p)->item_size);
00221 *(void **) (item) = (p)->free_list;
00222 (p)->free_list = (void *) (item);
00223 decrement_used_count(p);
00224 increment_free_list_length(p);
00225
00226 }
00227 #endif