1 // $Header$
2 //
3 // Copyright (C) 2000 - 2003, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13 
14 #include "sys.h"
15 #include "alloctag_debug.h"
16 #include <cstdlib>
17 #include <iostream>
18 
19 libcwd::debug_ct list_allocations_on_test_do;
20 
21 MAIN_FUNCTION
22 { PREFIX_CODE
23   using namespace libcwd;
24 
25 #if !CWDEBUG_ALLOC || !CWDEBUG_LOCATION
26   DoutFatal(dc::fatal, "Expected Failure.");
27 #else // CWDEBUG_ALLOC && CWDEBUG_LOCATION
28 
29   Debug( check_configuration() );
30 
31 #if CWDEBUG_LOCATION
32   // Make sure we initialized the bfd stuff before we turn on WARNING.
33   Debug( (void)pc_mangled_function_name((void*)exit) );
34 #endif
35 
36   // Select channels
37   ForAllDebugChannels( if (!debugChannel.is_on()) debugChannel.on(); );
38   Debug( dc::debug.off() );
39 #if CWDEBUG_LOCATION
40   Debug( dc::bfd.off() );
41 #endif
42 #ifndef THREADTEST
43   // Write debug output to cout
44   Debug( libcw_do.set_ostream(&std::cout) );
45 #endif
46   // Turn debug object on
47   Debug( libcw_do.on() );
48 
49   // List all debug channels
50   Debug( list_channels_on(libcw_do) );
51 
52   alloc_filter_ct filter(0);
53 #if CWDEBUG_LOCATION
54   Debug(dc::malloc.off());
55   std::vector<std::string> masks;
56   masks.push_back("lib*");
57   filter.hide_objectfiles_matching(masks);
58   Debug(dc::malloc.on());
59   filter.hide_unknown_locations();
60 #endif
61 
62   char* cp = new char[50];
63   AllocTag(cp, "Test of \"new char[50]\"");
64 
65 #if CWDEBUG_ALLOC && !defined(THREADTEST)
66   // Don't show allocations that are allocated before main() and while creating the filter.
67   libcwd::make_all_allocations_invisible_except(cp);
68 #endif
69 
70   int* i = new int;
71   AllocTag(i, "Test of \"new int\"");
72 
73   void* vp = malloc(33);
74   AllocTag(vp, "Test of \"(void*)malloc(33)\"");
75 
76   int* vpi = (int*)malloc(55);
77   AllocTag(vpi, "Test of \"(int*)malloc(55)\"");
78 
79   void* cp2 = calloc(22, 10);
80   AllocTag(cp2, "Test of \"(void*)calloc(22, 10)\"");
81 
82   int* cp2i = (int*)calloc(55, 10);
83   AllocTag(cp2i, "Test of \"(int*)calloc(55, 10)\"");
84 
85   void* mp = malloc(11);
86   AllocTag(mp, "Test of \"(void*)malloc(1100)\"");
87 
88   void* rp = realloc(mp, 1000);
89   AllocTag(rp, "Test of \"(void*)realloc(mp, 1000)\"");
90 
91   int* mpi = (int*)malloc(66);
92   AllocTag(mpi, "Test of \"(int*)malloc(66)\"");
93 
94   int* rpi = (int*)realloc(mpi, 1000);
95   AllocTag(rpi, "Test of \"(int*)realloc(mpi, 1000)\"");
96 
97 #if CWDEBUG_ALLOC
98   do
99   {
100     Debug( libcw_do.off() );
101 #ifdef THREADTEST
102     static bool done = false;
103     if (!done)
104     {
105       done = true;
106       Debug( list_allocations_on_test_do.set_ostream(&std::cout, &cout_mutex) );
107     }
108 #else
109     Debug( list_allocations_on_test_do.set_ostream(&std::cout) );
110 #endif
111     Debug( list_allocations_on_test_do.on() );
112     list_allocations_on(list_allocations_on_test_do, filter);
113     Debug( libcw_do.on() );
114   }
115   while(0);
116 #endif
117 
118   delete [] cp;
119   delete i;
120   free(vp);
121   free(vpi);
122   free(cp2);
123   free(cp2i);
124   free(rp);
125   free(rpi);
126 
127 #endif // CWDEBUG_ALLOC && CWDEBUG_LOCATION
128 
129   Debug( libcw_do.off() );
130 
131   EXIT(0);
132 }
133