예제 #1
0
파일: alloc.c 프로젝트: groleo/mpatrol
static
allocnode *
getnode(allochead *h)
{
    allocnode *n;
    heapnode *p;

    /* If we have no more allocation node slots left then we must allocate
     * some more memory for them.  An extra MP_ALLOCFACTOR pages of memory
     * should suffice.
     */
    if ((n = (allocnode *) __mp_getslot(&h->table)) == NULL)
    {
        if ((p = __mp_heapalloc(&h->heap, h->heap.memory.page * MP_ALLOCFACTOR,
              h->table.entalign, 1)) == NULL)
            return NULL;
        __mp_initslots(&h->table, p->block, p->size);
        n = (allocnode *) __mp_getslot(&h->table);
        n->lnode.next = n->lnode.prev = NULL;
        __mp_treeinsert(&h->itree, &n->tnode, (unsigned long) p->block);
        n->block = p->block;
        n->size = p->size;
        n->info = NULL;
        h->isize += p->size;
        n = (allocnode *) __mp_getslot(&h->table);
    }
    return n;
}
예제 #2
0
파일: heap.c 프로젝트: groleo/mpatrol
MP_GLOBAL
heapnode *
__mp_heapalloc(heaphead *h, size_t l, size_t a, int i)
{
    heapnode *n;
    void *p;
    size_t s;

    /* If we have no more heap node slots left then we must allocate
     * some more memory for them.  An extra MP_ALLOCFACTOR pages of memory
     * should suffice.
     */
    if ((n = (heapnode *) __mp_getslot(&h->table)) == NULL)
    {
        s = h->memory.page * MP_ALLOCFACTOR;
        if ((p = __mp_memalloc(&h->memory, &s, h->table.entalign, 0)) == NULL)
            return NULL;
        __mp_initslots(&h->table, p, s);
        n = (heapnode *) __mp_getslot(&h->table);
        __mp_treeinsert(&h->itree, &n->node, (unsigned long) p);
        n->block = p;
        n->size = s;
        h->isize += s;
        if (h->tracing)
            __mp_traceheap(p, s, 1);
#if MP_INUSE_SUPPORT
        _Inuse_heapalloc(p, s);
#endif /* MP_INUSE_SUPPORT */
        n = (heapnode *) __mp_getslot(&h->table);
    }
    /* Allocate the requested block of memory and add it to the heap.
     */
    if ((p = __mp_memalloc(&h->memory, &l, a, !i)) == NULL)
    {
        __mp_freeslot(&h->table, n);
        return NULL;
    }
    __mp_treeinsert(&h->dtree, &n->node, (unsigned long) p);
    n->block = p;
    n->size = l;
    h->dsize += l;
    if (h->tracing)
        __mp_traceheap(p, l, i);
#if MP_INUSE_SUPPORT
    _Inuse_heapalloc(p, l);
#endif /* MP_INUSE_SUPPORT */
    return n;
}
예제 #3
0
int
main(int argc, char **argv)
{
    struct { char x; void *y; } z;
    char b[256];
    char *f, *s, *t;
#if MP_GUI_SUPPORT
    XGCValues g;
#endif /* MP_GUI_SUPPORT */
    long n;
    int c, e, h, v;

#if MP_GUI_SUPPORT
    appwidget = XtVaAppInitialize(&appcontext, "MPTrace", options,
                                  XtNumber(options), &argc, argv, NULL, NULL);
    XtVaGetApplicationResources(appwidget, NULL, resources, XtNumber(resources),
                                NULL);
#endif /* MP_GUI_SUPPORT */
    s = t = NULL;
    e = h = v = 0;
    progname = __mp_basename(argv[0]);
    while ((c = __mp_getopt(argc, argv, __mp_shortopts(b, options_table),
             options_table)) != EOF)
        switch (c)
        {
          case OF_GUI:
#if MP_GUI_SUPPORT
            usegui = 1;
#endif /* MP_GUI_SUPPORT */
            break;
          case OF_HATFFILE:
            t = __mp_optarg;
            break;
          case OF_HELP:
            h = 1;
            break;
          case OF_SIMFILE:
            s = __mp_optarg;
            break;
          case OF_SOURCE:
            displaysource = 1;
            break;
          case OF_VERBOSE:
            verbose = 1;
            break;
          case OF_VERSION:
            v = 1;
            break;
          default:
            e = 1;
            break;
        }
    argc -= __mp_optindex;
    argv += __mp_optindex;
    if (v == 1)
    {
        fprintf(stdout, "%s %s\n%s %s\n\n", progname, PROGVERSION,
                __mp_copyright, __mp_author);
        fputs("This is free software, and you are welcome to redistribute it "
              "under certain\n", stdout);
        fputs("conditions; see the GNU Lesser General Public License for "
              "details.\n\n", stdout);
        fputs("For the latest mpatrol release and documentation,\n", stdout);
        fprintf(stdout, "visit %s.\n\n", __mp_homepage);
    }
    if (argc > 1)
        e = 1;
    if ((e == 1) || (h == 1))
    {
        fprintf(stdout, "Usage: %s [options] [file]\n\n", progname);
        if (h == 0)
            fprintf(stdout, "Type `%s --help' for a complete list of "
                    "options.\n", progname);
        else
            __mp_showopts(options_table);
        if (e == 1)
            exit(EXIT_FAILURE);
        exit(EXIT_SUCCESS);
    }
    if (argc == 1)
        f = argv[0];
    else
        f = MP_TRACEFILE;
    __mp_newtree(&alloctree);
    if (strcmp(f, "-") == 0)
        tracefile = stdin;
    else if ((tracefile = fopen(f, "rb")) == NULL)
    {
        fprintf(stderr, "%s: Cannot open file `%s'\n", progname, f);
        exit(EXIT_FAILURE);
    }
    currentevent = 0;
    bufferpos = buffer;
    bufferlen = 0;
    n = (char *) &z.y - &z.x;
    __mp_newslots(&table, sizeof(void *), __mp_poweroftwo(n));
    __mp_initslots(&table, tableslots, sizeof(tableslots));
    maxslots = 1;
    if (s != NULL)
    {
        if (strcmp(s, "stdout") == 0)
            simfile = stdout;
        else if (strcmp(s, "stderr") == 0)
            simfile = stderr;
        else if ((simfile = fopen(s, "w")) == NULL)
        {
            fprintf(stderr, "%s: Cannot open file `%s'\n", progname, s);
            exit(EXIT_FAILURE);
        }
        fprintf(simfile, "/* produced by %s %s from %s */\n\n\n", progname,
                PROGVERSION, f);
        fputs("#include <stdio.h>\n", simfile);
        fputs("#include <stdlib.h>\n\n\n", simfile);
        fputs("typedef struct event\n{\n", simfile);
        fputs("    unsigned long index;\n", simfile);
        fputs("    unsigned long size;\n", simfile);
        fputs("    char resize;\n", simfile);
        fputs("}\nevent;\n\n\n", simfile);
        fputs("static event events[] =\n{\n", simfile);
    }
    if (t != NULL)
    {
        if (strcmp(t, "stdout") == 0)
            hatffile = stdout;
        else if (strcmp(t, "stderr") == 0)
            hatffile = stderr;
        else if ((hatffile = fopen(t, "w")) == NULL)
        {
            fprintf(stderr, "%s: Cannot open file `%s'\n", progname, t);
            exit(EXIT_FAILURE);
        }
        fprintf(hatffile, "## Tracename: %s\n", t);
        fputs("## Author: Unknown\n", hatffile);
        fputs("## Date: Unknown\n", hatffile);
        fputs("## DTDURL: hatf.dtd\n", hatffile);
        fprintf(hatffile, "## Description: Converted to HATF by %s %s.\n\n",
                progname, PROGVERSION);
    }
    readfile();
#if MP_GUI_SUPPORT
    if (usegui)
    {
        appdisplay = XtDisplay(appwidget);
        appscreen = XtScreen(appwidget);
        addrscale = (((addrspace * 1048576) - 1) / (width * height)) + 1;
        /* Set up the main application window and scrollable drawing area.
         * Also set up a pixmap to backup the drawing area.
         */
        mainwidget = XtVaCreateManagedWidget("main",
                                             xmScrolledWindowWidgetClass,
                                             appwidget, XmNwidth, vwidth,
                                             XmNheight, vheight,
                                             XmNscrollingPolicy, XmAUTOMATIC,
                                             XmNscrollBarDisplayPolicy,
                                             XmAS_NEEDED, NULL);
        drawwidget = XtVaCreateManagedWidget("draw", xmDrawingAreaWidgetClass,
                                             mainwidget, XmNwidth, width,
                                             XmNheight, height, NULL);
        pixmap = XCreatePixmap(appdisplay, RootWindowOfScreen(appscreen), width,
                               height, DefaultDepthOfScreen(appscreen));
        /* Set up the graphics contexts that are used for drawing in different
         * colours.
         */
        g.foreground = uncol;
        ungc = XCreateGC(appdisplay, RootWindowOfScreen(appscreen),
                         GCForeground, &g);
        g.foreground = incol;
        ingc = XCreateGC(appdisplay, RootWindowOfScreen(appscreen),
                         GCForeground, &g);
        g.foreground = frcol;
        frgc = XCreateGC(appdisplay, RootWindowOfScreen(appscreen),
                         GCForeground, &g);
        g.foreground = alcol;
        algc = XCreateGC(appdisplay, RootWindowOfScreen(appscreen),
                         GCForeground, &g);
        /* Add a callback procedure to handle the refreshing of the drawing
         * area and also a work procedure to read events from the tracing
         * output file.  Then initialise the drawing area and enter the main X
         * application loop.
         */
        XtAddCallback(drawwidget, XmNexposeCallback,
                      (XtCallbackProc) redrawmemory, NULL);
        XtAppAddWorkProc(appcontext, (XtWorkProc) readevent, NULL);
        XtRealizeWidget(appwidget);
        XFillRectangle(appdisplay, XtWindow(drawwidget), ungc, 0, 0, width - 1,
                       height - 1);
        XFillRectangle(appdisplay, pixmap, ungc, 0, 0, width - 1, height - 1);
        XtAppMainLoop(appcontext);
    }
#endif /* MP_GUI_SUPPORT */
    return EXIT_SUCCESS;
}