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

File: [Pegasus] / pegasus_unsupported / utils / memo / sample.cpp (download)
Revision: 1.1, Wed Oct 26 17:19:58 2005 UTC (18 years, 7 months ago) by karl
Branch: MAIN
CVS Tags: HEAD
PEP#: 240
TITLE: Add new tool (memo)

DESCRIPTION: Add new tool.  Note readme explains tool.

/*
**==============================================================================
**
** Copyright (c) 2003, 2004, 2005 Michael E. Brasher
** 
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and associated documentation files (the "Software"),
** to deal in the Software without restriction, including without limitation
** the rights to use, copy, modify, merge, publish, distribute, sublicense,
** and/or sell copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following conditions:
** 
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
** 
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
**
**==============================================================================
*/

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <string.h>
#include "memo.h"

void test1()
{
    struct Node
    {
	Node() : p(0), q(0), r(0) { }
	Node* p;
	Node* q;
	Node* r;
	char buffer[1024 - 3 * sizeof(Node*)];
    };

    Node* node = new Node;
    node->p = new Node;
    node->q = new Node;
    node->r = new Node;
    node->p->p = new Node;

#if 0
    size_t size = memtool_deep_size(node, sizeof(Node));
    printf("%d\n", int(size));
#endif
}

int main(int argc, char** argv)
{
    char* p = new char[16];

    // test for good program load and run
    if (argc != 2)
    {
	fprintf(stderr, "Usage: %s command where commands are freewrite|doublefree|notfree\n", argv[0]);
	delete p;
	exit(1);
    }

    //if (strcmp(argv[1], "size") == 0)
	//test1();

    // test for free memory write
    if (strcmp(argv[1], "freewrite") == 0)
    {
        delete p;
        printf("freewrite\n");
        // Free memory write:
        *p = 'A';
    }

    // test for double free
    if (strcmp(argv[1], "doublefree") == 0)
    {
        delete p;
        // Double free:
        delete p;

        sleep(5);
    }

    // test for not freed memory
    if (strcmp(argv[1], "notfree") == 0)
    {
        return 0;
    }
    return 0;
}

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2