(file) Return to sample.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus_unsupported / utils / memo

 1 karl  1.1 /*
 2           **==============================================================================
 3           **
 4           ** Copyright (c) 2003, 2004, 2005 Michael E. Brasher
 5           ** 
 6           ** Permission is hereby granted, free of charge, to any person obtaining a
 7           ** copy of this software and associated documentation files (the "Software"),
 8           ** to deal in the Software without restriction, including without limitation
 9           ** the rights to use, copy, modify, merge, publish, distribute, sublicense,
10           ** and/or sell copies of the Software, and to permit persons to whom the
11           ** Software is furnished to do so, subject to the following conditions:
12           ** 
13           ** The above copyright notice and this permission notice shall be included in
14           ** all copies or substantial portions of the Software.
15           ** 
16           ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17           ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18           ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19           ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20           ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21           ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 karl  1.1 ** SOFTWARE.
23           **
24           **==============================================================================
25           */
26           
27           #include <cstdio>
28           #include <cstdlib>
29           #include <unistd.h>
30           #include <string.h>
31           #include "memo.h"
32           
33           void test1()
34           {
35               struct Node
36               {
37           	Node() : p(0), q(0), r(0) { }
38           	Node* p;
39           	Node* q;
40           	Node* r;
41           	char buffer[1024 - 3 * sizeof(Node*)];
42               };
43 karl  1.1 
44               Node* node = new Node;
45               node->p = new Node;
46               node->q = new Node;
47               node->r = new Node;
48               node->p->p = new Node;
49           
50           #if 0
51               size_t size = memtool_deep_size(node, sizeof(Node));
52               printf("%d\n", int(size));
53           #endif
54           }
55           
56           int main(int argc, char** argv)
57           {
58               char* p = new char[16];
59           
60               // test for good program load and run
61               if (argc != 2)
62               {
63           	fprintf(stderr, "Usage: %s command where commands are freewrite|doublefree|notfree\n", argv[0]);
64 karl  1.1 	delete p;
65           	exit(1);
66               }
67           
68               //if (strcmp(argv[1], "size") == 0)
69           	//test1();
70           
71               // test for free memory write
72               if (strcmp(argv[1], "freewrite") == 0)
73               {
74                   delete p;
75                   printf("freewrite\n");
76                   // Free memory write:
77                   *p = 'A';
78               }
79           
80               // test for double free
81               if (strcmp(argv[1], "doublefree") == 0)
82               {
83                   delete p;
84                   // Double free:
85 karl  1.1         delete p;
86           
87                   sleep(5);
88               }
89           
90               // test for not freed memory
91               if (strcmp(argv[1], "notfree") == 0)
92               {
93                   return 0;
94               }
95               return 0;
96           }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2