Orbiter 2022
Combinatorial Objects
mem_object_registry.cpp
Go to the documentation of this file.
1/*
2 * mem_object_registry.cpp
3 *
4 * Created on: Apr 23, 2019
5 * Author: betten
6 */
7
8
9
10
11
12
13#include "foundations.h"
14
15using namespace std;
16
17
18
19
20namespace orbiter {
21namespace layer1_foundations {
22namespace orbiter_kernel_system {
23
24
25static int registry_key_pair_compare_by_size(void *K1v, void *K2v);
26static int registry_key_pair_compare_by_type(void *K1v, void *K2v);
27static int registry_key_pair_compare_by_location(void *K1v, void *K2v);
28
29
30
31
33{
34 int verbose_level = 0;
35
39
40 entries = NULL;
43 cur_time = 0;
44
47
48 init(verbose_level);
49}
50
52{
53 if (entries) {
54 delete [] entries;
55 entries = NULL;
56 }
57}
58
59void mem_object_registry::init(int verbose_level)
60{
61 int f_v = (verbose_level >= 1);
62
63 if (f_v) {
64 cout << "mem_object_registry::init" << endl;
65 }
66
69
72 cur_time = 0;
73
76
77 if (f_v) {
78 cout << "mem_object_registry::init trying to allocate "
79 << nb_entries_allocated << " entries" << endl;
80 }
81
83
84 if (f_v) {
85 cout << "mem_object_registry::init allocation successful" << endl;
86 }
87
88
89 if (f_v) {
90 cout << "mem_object_registry::init done" << endl;
91 }
92}
93
95 int verbose_level)
96{
97 int f_v = (verbose_level >= 1);
98
99 if (f_v) {
100 cout << "mem_object_registry::accumulate_and_ignore_duplicates" << endl;
101 }
104}
105
106void mem_object_registry::allocate(int N, int verbose_level)
107{
108 int f_v = (verbose_level >= 1);
109
110 if (f_v) {
111 cout << "mem_object_registry::allocate" << endl;
112 }
113
115 nb_entries_used = 0;
116
117 if (f_v) {
118 cout << "mem_object_registry::allocate "
119 "trying to allocate "
120 << nb_entries_allocated << " entries" << endl;
121 }
122
124
125 if (f_v) {
126 cout << "mem_object_registry::allocate allocation successful" << endl;
127 }
128
129
130 if (f_v) {
131 cout << "mem_object_registry::allocate done" << endl;
132 }
133}
134
135
136
138 int automatic_dump_interval, const char *fname_mask,
139 int verbose_level)
140{
141 int f_v = (verbose_level >= 1);
142
143 if (f_v) {
144 cout << "mem_object_registry::set_automatic_dump" << endl;
145 }
148 strcpy(automatic_dump_fname_mask, fname_mask);
149}
150
152{
153 if (!f_automatic_dump) {
154 return;
155 }
156 if ((cur_time % automatic_dump_interval) != 0) {
157 return;
158 }
159 char fname[1000];
160 int a;
161
163
164 cout << "automatic memory dump " << a << endl;
165 sprintf(fname, automatic_dump_fname_mask, a);
166
167 dump_to_csv_file(fname);
168}
169
171{
172 if (!f_automatic_dump) {
173 return;
174 }
175 char fname[1000];
176 int a;
177
179
180 sprintf(fname, automatic_dump_fname_mask, a);
181
182 dump_to_csv_file(fname);
183}
184
186 const char *fname)
187{
188 dump_to_csv_file(fname);
189}
190
192{
193 int i, s, sz;
194
195 cout << "memory registry:" << endl;
196
197 sz = 0;
198 for (i = 0; i < nb_entries_used; i++) {
199 s = entries[i].size_of();
200 sz += s;
201 }
202
203 cout << "nb_entries_used=" << nb_entries_used << endl;
204 cout << "nb_allocate_total=" << nb_allocate_total << endl;
205 cout << "nb_delete_total=" << nb_delete_total << endl;
206 cout << "cur_time=" << cur_time << endl;
207 cout << "total allocation size in char=" << sz << endl;
208 cout << "table of all currently active memory allocations in increasing "
209 "order of the value of the pointer" << endl;
210 for (i = 0; i < nb_entries_used; i++) {
211 entries[i].print(i);
212 }
213}
214
216{
217 int i, s, sz;
218
219
220 {
221 ofstream fp(fname);
222
223
224 //cout << "memory registry:" << endl;
225
226 fp << "Line,Pointer,Timestamp,Type,N,Sizeof,"
227 "ExtraTypeInfo,File,LineInFile" << endl;
228 sz = 0;
229 for (i = 0; i < nb_entries_used; i++) {
230 s = entries[i].size_of();
231 sz += s;
232 }
233
234 for (i = 0; i < nb_entries_used; i++) {
235 entries[i].print_csv(fp, i);
236 }
237 fp << "END" << endl;
238 fp << "nb_entries_used=" << nb_entries_used << endl;
239 fp << "nb_allocate_total=" << nb_allocate_total << endl;
240 fp << "nb_delete_total=" << nb_delete_total << endl;
241 fp << "cur_time=" << cur_time << endl;
242 fp << "total allocation size in char=" << sz << endl;
243 }
244}
245
246
248 const char *file, int line)
249{
250 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
251
252 int *p;
253 p = new int[n];
254
255 if (f_v) {
256 cout << "mem_object_registry::allocate_int cur_time=" << cur_time << " int[n], "
257 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
258 }
259 if (Orbiter->f_memory_debug) {
260 add_to_registry(p /* pointer */,
261 POINTER_TYPE_int, n, sizeof(int),
262 "", file, line,
264 }
265 return p;
266}
267
268void mem_object_registry::free_int(int *p, const char *file, int line)
269{
270 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
271
272 if (f_v) {
273 cout << "mem_object_registry::free_int int[n], "
274 " file=" << file << " line=" << line << endl;
275 }
276 if (p == NULL) {
277 cout << "mem_object_registry::free_int "
278 "NULL pointer, ignoring" << endl;
279 cout << "p=" << p << " file=" << file
280 << " line=" << line << endl;
281 return;
282 }
283 if (Orbiter->f_memory_debug) {
285 }
286 delete [] p;
287}
288
290 const char *file, int line)
291{
292 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
293
294 long int *p;
295 p = new long int[n];
296 if (f_v) {
297 cout << "mem_object_registry::allocate_lint cur_time=" << cur_time << " int[n], "
298 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
299 }
300 if (Orbiter->f_memory_debug) {
301 add_to_registry(p /* pointer */,
302 POINTER_TYPE_lint, n, sizeof(int),
303 "", file, line,
305 }
306 return p;
307}
308
309void mem_object_registry::free_lint(long int *p, const char *file, int line)
310{
311 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
312
313 if (f_v) {
314 cout << "mem_object_registry::free_lint int[n], "
315 " file=" << file << " line=" << line << endl;
316 }
317 if (p == NULL) {
318 cout << "mem_object_registry::free_lint "
319 "NULL pointer, ignoring" << endl;
320 cout << "p=" << p << " file=" << file
321 << " line=" << line << endl;
322 return;
323 }
324 if (Orbiter->f_memory_debug) {
326 }
327 delete [] p;
328}
329
331 const char *file, int line)
332{
333 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
334
335 int **p;
336 p = new pint[n];
337 if (f_v) {
338 cout << "mem_object_registry::allocate_pint cur_time=" << cur_time << " pint[n], "
339 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
340 }
341 if (Orbiter->f_memory_debug) {
342 add_to_registry(p /* pointer */,
343 POINTER_TYPE_pint, n, sizeof(int *),
344 "", file, line,
346 }
347 return p;
348}
349
351 const char *file, int line)
352{
353 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
354
355 if (f_v) {
356 cout << "mem_object_registry::free_pint pint[n], "
357 " file=" << file << " line=" << line << endl;
358 }
359 if (p == NULL) {
360 cout << "mem_object_registry::free_pint "
361 "NULL pointer, ignoring" << endl;
362 cout << "p=" << p << " file=" << file
363 << " line=" << line << endl;
364 return;
365 }
366 if (Orbiter->f_memory_debug) {
368 }
369 delete [] p;
370}
371
373 const char *file, int line)
374{
375 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
376
377 long int **p;
378 p = new plint[n];
379 if (f_v) {
380 cout << "mem_object_registry::allocate_plint cur_time=" << cur_time << " plint[n], "
381 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
382 }
383 if (Orbiter->f_memory_debug) {
384 add_to_registry(p /* pointer */,
385 POINTER_TYPE_plint, n, sizeof(long int *),
386 "", file, line,
388 }
389 return p;
390}
391
393 const char *file, int line)
394{
395 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
396
397 if (f_v) {
398 cout << "mem_object_registry::free_plint pint[n], "
399 " file=" << file << " line=" << line << endl;
400 }
401 if (p == NULL) {
402 cout << "mem_object_registry::free_plint "
403 "NULL pointer, ignoring" << endl;
404 cout << "p=" << p << " file=" << file
405 << " line=" << line << endl;
406 return;
407 }
408 if (Orbiter->f_memory_debug) {
410 }
411 delete [] p;
412}
413
415 const char *file, int line)
416{
417 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
418
419 int ***p;
420 p = new ppint[n];
421 if (f_v) {
422 cout << "mem_object_registry::allocate_ppint cur_time=" << cur_time << " ppint[n], "
423 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
424 }
425 if (Orbiter->f_memory_debug) {
426 add_to_registry(p /* pointer */,
427 POINTER_TYPE_ppint, n, sizeof(int **),
428 "", file, line,
430 }
431 return p;
432}
433
434void mem_object_registry::free_ppint(int ***p, const char *file, int line)
435{
436 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
437
438 if (f_v) {
439 cout << "mem_object_registry::free_ppint ppint[n], "
440 " file=" << file << " line=" << line << endl;
441 }
442 if (p == NULL) {
443 cout << "mem_object_registry::free_ppint "
444 "NULL pointer, ignoring" << endl;
445 cout << "p=" << p << " file=" << file
446 << " line=" << line << endl;
447 return;
448 }
449 if (Orbiter->f_memory_debug) {
451 }
452 delete [] p;
453}
454
456 const char *file, int line)
457{
458 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
459
460 long int ***p;
461 p = new pplint[n];
462 if (f_v) {
463 cout << "mem_object_registry::allocate_ppint cur_time=" << cur_time << " pplint[n], "
464 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
465 }
466 if (Orbiter->f_memory_debug) {
467 add_to_registry(p /* pointer */,
468 POINTER_TYPE_pplint, n, sizeof(int **),
469 "", file, line,
471 }
472 return p;
473}
474
475void mem_object_registry::free_pplint(long int ***p, const char *file, int line)
476{
477 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
478
479 if (f_v) {
480 cout << "mem_object_registry::free_pplint ppint[n], "
481 " file=" << file << " line=" << line << endl;
482 }
483 if (p == NULL) {
484 cout << "mem_object_registry::free_pplint "
485 "NULL pointer, ignoring" << endl;
486 cout << "p=" << p << " file=" << file
487 << " line=" << line << endl;
488 return;
489 }
490 if (Orbiter->f_memory_debug) {
492 }
493 delete [] p;
494}
495
497 const char *file, int line)
498{
499 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
500
501 char *p;
502 p = new char[n];
503 if (f_v) {
504 cout << "mem_object_registry::allocate_char cur_time=" << cur_time << " char[n], "
505 "n=" << n << " file=" << file << " line=" << line << " p=" << (int *) p << endl;
506 }
507 if (Orbiter->f_memory_debug) {
508 add_to_registry(p /* pointer */,
509 POINTER_TYPE_char, n, sizeof(char),
510 "", file, line,
512 }
513 return p;
514}
515
516void mem_object_registry::free_char(char *p, const char *file, int line)
517{
518 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
519
520 if (f_v) {
521 cout << "mem_object_registry::free_char char[n], "
522 " file=" << file << " line=" << line << endl;
523 }
524 if (p == NULL) {
525 cout << "mem_object_registry::free_char "
526 "NULL pointer, ignoring" << endl;
527 cout << "p=" << p << " file=" << file
528 << " line=" << line << endl;
529 return;
530 }
531 if (Orbiter->f_memory_debug) {
533 }
534 delete [] p;
535}
536
538 const char *file, int line)
539{
540 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
541
542 uchar *p;
543 p = new uchar[n];
544 if (f_v) {
545 cout << "mem_object_registry::allocate_uchar cur_time=" << cur_time << " uchar[n], "
546 "n=" << n << " file=" << file << " line=" << line << " p=" << (int *) p << endl;
547 }
548 if (Orbiter->f_memory_debug) {
549 add_to_registry(p /* pointer */,
550 POINTER_TYPE_uchar, n, sizeof(uchar),
551 "", file, line,
553 }
554 return p;
555}
556
558 const char *file, int line)
559{
560 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
561
562 if (f_v) {
563 cout << "mem_object_registry::free_uchar uchar[n], "
564 " file=" << file << " line=" << line << endl;
565 }
566 if (p == NULL) {
567 cout << "mem_object_registry::free_uchar "
568 "NULL pointer, ignoring" << endl;
569 cout << "p=" << p << " file=" << file
570 << " line=" << line << endl;
571 return;
572 }
573 if (Orbiter->f_memory_debug) {
575 }
576 delete [] p;
577}
578
580 const char *file, int line)
581{
582 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
583
584 char **p;
585 p = new pchar[n];
586 if (f_v) {
587 cout << "mem_object_registry::allocate_pchar cur_time=" << cur_time << " pchar[n], "
588 "n=" << n << " file=" << file << " line=" << line << " p=" << (int *) p << endl;
589 }
590 if (Orbiter->f_memory_debug) {
591 add_to_registry(p /* pointer */,
592 POINTER_TYPE_pchar, n, sizeof(char *),
593 "", file, line,
595 }
596 return p;
597}
598
600 const char *file, int line)
601{
602 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
603
604 if (f_v) {
605 cout << "mem_object_registry::free_pchar pchar[n], "
606 " file=" << file << " line=" << line << endl;
607 }
608 if (p == NULL) {
609 cout << "mem_object_registry::free_pchar "
610 "NULL pointer, ignoring" << endl;
611 cout << "p=" << p << " file=" << file
612 << " line=" << line << endl;
613 return;
614 }
615 if (Orbiter->f_memory_debug) {
617 }
618 delete [] p;
619}
620
622 const char *file, int line)
623{
624 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
625
626 uchar **p;
627 p = new puchar[n];
628 if (f_v) {
629 cout << "mem_object_registry::allocate_puchar cur_time=" << cur_time << " puchar[n], "
630 "n=" << n << " file=" << file << " line=" << line << " p=" << (int *) p << endl;
631 }
632 if (Orbiter->f_memory_debug) {
633 add_to_registry(p /* pointer */,
634 POINTER_TYPE_puchar, n, sizeof(char *),
635 "", file, line,
637 }
638 return p;
639}
640
642 const char *file, int line)
643{
644 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
645
646 if (f_v) {
647 cout << "mem_object_registry::free_puchar puchar[n], "
648 " file=" << file << " line=" << line << endl;
649 }
650 if (p == NULL) {
651 cout << "mem_object_registry::free_puchar "
652 "NULL pointer, ignoring" << endl;
653 cout << "p=" << p << " file=" << file
654 << " line=" << line << endl;
655 return;
656 }
657 if (Orbiter->f_memory_debug) {
659 }
660 delete [] p;
661}
662
664 const char *file, int line)
665{
666 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
667
668 void **p;
669 p = new pvoid[n];
670 if (f_v) {
671 cout << "mem_object_registry::allocate_pvoid cur_time=" << cur_time << " pvoid[n], "
672 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
673 }
674 if (Orbiter->f_memory_debug) {
675 add_to_registry(p /* pointer */,
676 POINTER_TYPE_PVOID, n, sizeof(void *),
677 "", file, line,
679 }
680 return p;
681}
682
684 const char *file, int line)
685{
686 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
687
688 if (f_v) {
689 cout << "mem_object_registry::free_pvoid pvoid[n], "
690 " file=" << file << " line=" << line << endl;
691 }
692 if (p == NULL) {
693 cout << "mem_object_registry::free_pvoid "
694 "NULL pointer, ignoring" << endl;
695 cout << "p=" << p << " file=" << file
696 << " line=" << line << endl;
697 return;
698 }
699 if (Orbiter->f_memory_debug) {
701 }
702 delete [] p;
703}
704
706 long int n, std::size_t size_of,
707 const char *extra_type_info, const char *file, int line)
708{
709 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
710
711 if (f_v) {
712 cout << "mem_object_registry::allocate_OBJECTS cur_time=" << cur_time << " char[n * size_of], "
713 "n=" << n << " file=" << file << " line=" << line << " p=" << p << endl;
714 }
715 if (Orbiter->f_memory_debug) {
716 add_to_registry(p /* pointer */,
717 POINTER_TYPE_OBJECTS, n, (int) size_of,
718 extra_type_info, file, line,
720 }
721 return p;
722}
723
725 const char *file, int line)
726{
727 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
728
729 if (f_v) {
730 cout << "mem_object_registry::free_OBJECTS char[n * size_of], "
731 " file=" << file << " line=" << line << endl;
732 }
733 if (p == NULL) {
734 cout << "mem_object_registry::free_OBJECTS "
735 "NULL pointer, ignoring" << endl;
736 cout << "p=" << p << " file=" << file
737 << " line=" << line << endl;
738 return;
739 }
740 if (Orbiter->f_memory_debug) {
742 }
743 //delete [] p;
744}
745
746void *mem_object_registry::allocate_OBJECT(void *p, std::size_t size_of,
747 const char *extra_type_info, const char *file, int line)
748{
749 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
750
751 if (f_v) {
752 cout << "mem_object_registry::allocate_OBJECT cur_time=" << cur_time << " char[size_of], "
753 " file=" << file << " line=" << line << " p=" << p << endl;
754 }
755 if (Orbiter->f_memory_debug) {
756 add_to_registry(p /* pointer */,
757 POINTER_TYPE_OBJECT, 1, (int) size_of,
758 extra_type_info, file, line,
760 }
761 return p;
762}
763
765 const char *file, int line)
766{
767 int f_v = (Orbiter->memory_debug_verbose_level >= 1);
768
769 if (f_v) {
770 cout << "mem_object_registry::free_OBJECT char[size_of], "
771 " file=" << file << " line=" << line << endl;
772 }
773 if (p == NULL) {
774 cout << "mem_object_registry::free_OBJECTS "
775 "NULL pointer, ignoring" << endl;
776 cout << "p=" << p << " file=" << file
777 << " line=" << line << endl;
778 return;
779 }
780 if (Orbiter->f_memory_debug) {
782 }
783 //delete [] p;
784}
785
786
787
788
789
790int mem_object_registry::search(void *p, int &idx)
791{
792 int l, r, m;
793 int f_found = FALSE;
794
795 if (nb_entries_used == 0) {
796 idx = 0;
797 return FALSE;
798 }
799 l = 0;
800 r = nb_entries_used;
801 // invariant:
802 // v[i] <= a for i < l;
803 // v[i] > a for i >= r;
804 // r - l is the length of the area to search in.
805 while (l < r) {
806 m = (l + r) >> 1;
807 // if the length of the search area is even
808 // we examine the element above the middle
809 //res = registry_pointer[m] - p;
810 //cout << "search l=" << l << " m=" << m << " r="
811 // << r << "a=" << a << " v[m]=" << v[m] << " res=" << res << endl;
812 if (p >= entries[m].pointer) {
813 l = m + 1;
814 if (p == entries[m].pointer)
815 f_found = TRUE;
816 }
817 else
818 r = m;
819 }
820 // now: l == r;
821 // and f_found is set accordingly */
822 if (f_found) {
823 l--;
824 }
825 idx = l;
826 return f_found;
827}
828
830{
831 int i;
832
835 cout << "mem_object_registry::insert_at reallocating table to "
836 << nb_entries_allocated << " elements" << endl;
837 mem_object_registry_entry *old_entries;
838
839 old_entries = entries;
841 for (i = 0; i < nb_entries_used; i++) {
842 entries[i] = old_entries[i];
843 }
844 delete [] old_entries;
845 }
846 for (i = nb_entries_used; i > idx; i--) {
847 entries[i] = entries[i - 1];
848 }
849 entries[idx].null();
851}
852
854 int object_type, long int object_n, int object_size_of,
855 const char *extra_type_info,
856 const char *source_file, int source_line,
857 int verbose_level)
858{
859 int f_v = (verbose_level >= 1);
860 int idx;
861
862 if (f_v) {
863 cout << "mem_object_registry::add_to_registry" << endl;
864 }
866 if (search(pointer, idx)) {
868
869 }
870 else {
871 cout << "mem_object_registry::add_to_registry pointer p is "
872 "already in the registry, something is wrong" << endl;
873 cout << "extra_type_info = " << extra_type_info << endl;
874 cout << "source_file = " << source_file << endl;
875 cout << "source_line = " << source_line << endl;
876 cout << "object_type = " << object_type << endl;
877 cout << "object_n = " << object_n << endl;
878 cout << "object_size_of = " << object_size_of << endl;
879 cout << "the previous object is:" << endl;
880 entries[idx].print(idx);
881 cout << "ignoring the problem" << endl;
882 //exit(1);
883 }
884 }
885 insert_at(idx);
887 entries[idx].pointer = pointer;
888 entries[idx].object_type = object_type;
889 entries[idx].object_n = object_n;
890 entries[idx].object_size_of = object_size_of;
891 entries[idx].extra_type_info = extra_type_info;
892 entries[idx].source_file = source_file;
893 entries[idx].source_line = source_line;
894
895
896
898 cur_time++;
899
900 if (f_v) {
901 cout << "mem_object_registry::add_to_registry done, there are "
902 << nb_entries_used << " entries in the registry" << endl;
903 }
904}
905
906void mem_object_registry::delete_from_registry(void *pointer, int verbose_level)
907{
908 int f_v = (verbose_level >= 1);
909 int idx, i;
910
911 if (f_v) {
912 cout << "mem_object_registry::delete_from_registry" << endl;
913 }
915
916 if (f_accumulate) {
917 // do not delete entries so we can see all allocations
918 }
919 else {
920 if (!search(pointer, idx)) {
921 cout << "mem_object_registry::delete_from_registry pointer is "
922 "not in registry, something is wrong; "
923 "ignoring, pointer = " << pointer << endl;
924 //exit(1);
925 }
926 for (i = idx + 1; i < nb_entries_used; i++) {
927 entries[i - 1] = entries[i];
928 }
931 }
933 //cur_time++;
934 if (f_v) {
935 cout << "mem_object_registry::delete_from_registry done, there are "
936 << nb_entries_used << " entries in the registry" << endl;
937 }
938}
940{
941 int f_v = (verbose_level >= 1);
943
944 if (f_v) {
945 cout << "mem_object_registry::sort_by_size" << endl;
946 }
947
948 if (f_v) {
949 cout << "mem_object_registry::sort_by_size before Heapsort" << endl;
950 }
953 registry_key_pair_compare_by_size);
954 if (f_v) {
955 cout << "mem_object_registry::sort_by_size after Heapsort" << endl;
956 }
957
958 if (f_v) {
959 cout << "mem_object_registry::sort_by_size done" << endl;
960 }
961
962}
963
965 int verbose_level)
966{
967 int f_v = (verbose_level >= 1);
968 int i;
970
971 if (f_v) {
972 cout << "mem_object_registry::sort_by_location_and_get_frequency" << endl;
973 }
974
975 sort_by_location(verbose_level - 1);
976
977 int nb_types;
978 int *type_first;
979 int *type_len;
980 int c, f, l;
981
982 type_first = new int[nb_entries_used]; // use system memory
983 type_len = new int[nb_entries_used];
984
985
986 nb_types = 0;
987 type_first[0] = 0;
988 type_len[0] = 1;
989 for (i = 1; i < nb_entries_used; i++) {
990 c = registry_key_pair_compare_by_location(
991 entries + i, entries + (i - 1));
992 if (c == 0) {
993 type_len[nb_types]++;
994 }
995 else {
996 type_first[nb_types + 1] =
997 type_first[nb_types] + type_len[nb_types];
998 nb_types++;
999 type_len[nb_types] = 1;
1000 }
1001 }
1002 nb_types++;
1003 cout << "we have " << nb_types
1004 << " different allocation locations:" << endl;
1005
1006 int t, j, sz, s;
1007 int *perm;
1008 int *perm_inv;
1009 int *frequency;
1010
1011 perm = new int[nb_types]; // use system memory
1012 perm_inv = new int[nb_types];
1013 frequency = new int[nb_types];
1014
1015 Int_vec_copy(type_len, frequency, nb_types);
1016
1017 Sorting.int_vec_sorting_permutation(frequency, nb_types,
1018 perm, perm_inv, FALSE /* f_increasingly */);
1019
1020 for (t = nb_types - 1; t >= 0; t--) {
1021 i = perm_inv[t];
1022
1023 f = type_first[i];
1024 l = type_len[i];
1025
1026 sz = 0;
1027 for (j = 0; j < l; j++) {
1028 s = entries[f + j].size_of();
1029 sz += s;
1030 }
1031
1032 //idx = entries[f].user_data;
1033 cout << l << " times file "
1034 << entries[f].source_file << " line "
1035 << entries[f].source_line
1036 << " object type ";
1037 entries[f].print_type(cout);
1038 if (entries[f].object_type == POINTER_TYPE_OBJECT ||
1039 entries[f].object_type == POINTER_TYPE_OBJECTS) {
1040 cout << " = " << entries[f].extra_type_info;
1041 }
1042 cout << " for a total of " << sz << " char" << endl;
1043 }
1044
1045 delete [] type_first;
1046 delete [] type_len;
1047 delete [] perm;
1048 delete [] perm_inv;
1049 delete [] frequency;
1050
1051 if (f_v) {
1052 cout << "mem_object_registry::sort_by_location_and_get_frequency" << endl;
1053 }
1054}
1055
1057{
1058 int f_v = (verbose_level >= 1);
1060
1061 if (f_v) {
1062 cout << "mem_object_registry::sort_by_type" << endl;
1063 }
1064
1065 if (f_v) {
1066 cout << "mem_object_registry::sort_by_type "
1067 "before Heapsort" << endl;
1068 }
1069
1072 registry_key_pair_compare_by_type);
1073
1074 if (f_v) {
1075 cout << "mem_object_registry::sort_by_type "
1076 "after Heapsort" << endl;
1077 }
1078
1079 if (f_v) {
1080 cout << "mem_object_registry::sort_by_type "
1081 "done" << endl;
1082 }
1083
1084}
1085
1087{
1088 int f_v = (verbose_level >= 1);
1090
1091 if (f_v) {
1092 cout << "mem_object_registry::sort_by_location" << endl;
1093 }
1094
1095 if (f_v) {
1096 cout << "mem_object_registry::sort_by_location "
1097 "before Heapsort" << endl;
1098 }
1099
1102 registry_key_pair_compare_by_location);
1103
1104 if (f_v) {
1105 cout << "mem_object_registry::sort_by_location "
1106 "after Heapsort" << endl;
1107 }
1108
1109 if (f_v) {
1110 cout << "mem_object_registry::sort_by_location "
1111 "done" << endl;
1112 }
1113
1114}
1115
1116//##############################################################################
1117// global functions:
1118//##############################################################################
1119
1120
1121static int registry_key_pair_compare_by_size(void *K1v, void *K2v)
1122{
1123 int s1, s2, c;
1125
1126 K1 = (mem_object_registry_entry *) K1v;
1127 K2 = (mem_object_registry_entry *) K2v;
1128 s1 = K1->size_of();
1129 s2 = K2->size_of();
1130 c = s2 - s1;
1131 return c;
1132}
1133
1134static int registry_key_pair_compare_by_type(void *K1v, void *K2v)
1135{
1136 int t1, t2, l1, l2, c;
1137 mem_object_registry_entry *K1, *K2;
1138
1139 K1 = (mem_object_registry_entry *) K1v;
1140 K2 = (mem_object_registry_entry *) K2v;
1141 t1 = K1->object_type;
1142 t2 = K2->object_type;
1143 c = t2 - t1;
1144 if (c) {
1145 return c;
1146 }
1147 //new the two entries have the same type
1148 if (t1 == POINTER_TYPE_OBJECTS || t1 == POINTER_TYPE_OBJECT) {
1149 c = strcmp(K1->extra_type_info, K2->extra_type_info);
1150 if (c) {
1151 return c;
1152 }
1153 }
1154 c = strcmp(K1->source_file, K2->source_file);
1155 if (c) {
1156 return c;
1157 }
1158 l1 = K1->source_line;
1159 l2 = K2->source_line;
1160 c = l2 - l1;
1161 return c;
1162}
1163
1164static int registry_key_pair_compare_by_location(void *K1v, void *K2v)
1165{
1166 int l1, l2, c;
1167 mem_object_registry_entry *K1, *K2;
1168
1169 K1 = (mem_object_registry_entry *) K1v;
1170 K2 = (mem_object_registry_entry *) K2v;
1171 c = strcmp(K1->source_file, K2->source_file);
1172 if (c) {
1173 return c;
1174 }
1175 l1 = K1->source_line;
1176 l2 = K2->source_line;
1177 c = l2 - l1;
1178 return c;
1179}
1180
1181
1182}}}
1183
a collection of functions related to sorted vectors
void int_vec_sorting_permutation(int *v, int len, int *perm, int *perm_inv, int f_increasingly)
Definition: sorting.cpp:830
void Heapsort(void *v, int len, int entry_size_in_chars, int(*compare_func)(void *v1, void *v2))
Definition: sorting.cpp:1790
a class related to mem_object_registry
const char * source_file
void print(int line)
int object_size_of
void * pointer
int size_of()
int time_stamp
void null()
int object_type
long int object_n
void print_csv(std::ostream &ost, int line)
int source_line
const char * extra_type_info
void print_type(std::ostream &ost)
void add_to_registry(void *pointer, int object_type, long int object_n, int object_size_of, const char *extra_type_info, const char *source_file, int source_line, int verbose_level)
long int ** allocate_plint(long int n, const char *file, int line)
long int * allocate_lint(long int n, const char *file, int line)
long int *** allocate_pplint(long int n, const char *file, int line)
void * allocate_OBJECTS(void *p, long int n, std::size_t size_of, const char *extra_type_info, const char *file, int line)
void set_automatic_dump(int automatic_dump_interval, const char *fname_mask, int verbose_level)
void * allocate_OBJECT(void *p, std::size_t size_of, const char *extra_type_info, const char *file, int line)
long int * plint
Definition: foundations.h:198
int ** ppint
Definition: foundations.h:199
unsigned char uchar
Definition: foundations.h:204
char * pchar
Definition: foundations.h:203
int * pint
Definition: foundations.h:197
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
uchar * puchar
Definition: foundations.h:205
long int ** pplint
Definition: foundations.h:200
#define Int_vec_copy(A, B, C)
Definition: foundations.h:693
void * pvoid
Definition: foundations.h:206
orbiter_kernel_system::orbiter_session * Orbiter
global Orbiter session
the orbiter library for the classification of combinatorial objects
#define POINTER_TYPE_puchar
#define POINTER_TYPE_uchar
#define POINTER_TYPE_pplint
#define POINTER_TYPE_pint
#define POINTER_TYPE_lint
#define POINTER_TYPE_ppint
#define REGISTRY_SIZE
#define POINTER_TYPE_int
#define POINTER_TYPE_OBJECTS
#define POINTER_TYPE_pchar
#define POINTER_TYPE_PVOID
#define POINTER_TYPE_OBJECT
#define POINTER_TYPE_char
#define POINTER_TYPE_plint