DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
** spacecategory.c
**
** Cagtegorizes each allocation using a predefined set of rules
** and presents a tree of categories for browsing.
*/

/*
** Required include files.
*/
#include "spacetrace.h"
#include <ctype.h>
#include <string.h>

#include "nsQuickSort.h"

#if defined(HAVE_BOUTELL_GD)
/*
** See http://www.boutell.com/gd for the GD graphics library.
** Ports for many platorms exist.
** Your box may already have the lib (mine did, redhat 7.1 workstation).
*/
#include <gd.h>
#include <gdfontt.h>
#include <gdfonts.h>
#include <gdfontmb.h>
#endif /* HAVE_BOUTELL_GD */

/*
** AddRule
**
** Add a rule into the list of rules maintainted in global
*/
int
AddRule(STGlobals * g, STCategoryRule * rule)
{
    if (g->mNRules % ST_ALLOC_STEP == 0) {
        /* Need more space */
        STCategoryRule **newrules;

        newrules = (STCategoryRule **) realloc(g->mCategoryRules,
                                               (g->mNRules +
                                                ST_ALLOC_STEP) *
                                               sizeof(STCategoryRule *));
        if (!newrules) {
            REPORT_ERROR(__LINE__, AddRule_No_Memory);
            return -1;
        }
        g->mCategoryRules = newrules;
    }
    g->mCategoryRules[g->mNRules++] = rule;
    return 0;
}

/*
** AddChild
**
** Add the node as a child of the parent node
*/
int
AddChild(STCategoryNode * parent, STCategoryNode * child)
{
    if (parent->nchildren % ST_ALLOC_STEP == 0) {
        /* need more space */
        STCategoryNode **newnodes;

        newnodes = (STCategoryNode **) realloc(parent->children,
                                               (parent->nchildren +
                                                ST_ALLOC_STEP) *
                                               sizeof(STCategoryNode *));
        if (!newnodes) {
            REPORT_ERROR(__LINE__, AddChild_No_Memory);
            return -1;
        }
        parent->children = newnodes;
    }
    parent->children[parent->nchildren++] = child;
    return 0;
}

int
Reparent(STCategoryNode * parent, STCategoryNode * child)
{
    uint32_t i;

    if (child->parent == parent)
        return 0;

    /* Remove child from old parent */
    if (child->parent) {
        for (i = 0; i < child->parent->nchildren; i++) {
            if (child->parent->children[i] == child) {
                /* Remove child from list */
                if (i + 1 < child->parent->nchildren)
                    memmove(&child->parent->children[i],
                            &child->parent->children[i + 1],
                            (child->parent->nchildren - i -
                             1) * sizeof(STCategoryNode *));
                child->parent->nchildren--;
                break;
            }
        }
    }

    /* Add child into new parent */
    AddChild(parent, child);

    return 0;
}

/*
** findCategoryNode
**
** Given a category name, finds the Node corresponding to the category
*/
STCategoryNode *
findCategoryNode(const char *catName, STGlobals * g)
{
    uint32_t i;

    for (i = 0; i < g->mNCategoryMap; i++) {
        if (!strcmp(g->mCategoryMap[i]->categoryName, catName))
            return g->mCategoryMap[i]->node;
    }

    /* Check if we are looking for the root node */
    if (!strcmp(catName, ST_ROOT_CATEGORY_NAME))
        return &g->mCategoryRoot;

    return NULL;
}

/*
** AddCategoryNode
**
** Adds a mapping between a category and its Node into the categoryMap
*/
int
AddCategoryNode(STCategoryNode * node, STGlobals * g)
{
    if (g->mNCategoryMap % ST_ALLOC_STEP == 0) {
        /* Need more space */
        STCategoryMapEntry **newmap =
            (STCategoryMapEntry **) realloc(g->mCategoryMap,
                                            (g->mNCategoryMap +
                                             ST_ALLOC_STEP) *
                                            sizeof(STCategoryMapEntry *));
        if (!newmap) {
            REPORT_ERROR(__LINE__, AddCategoryNode_No_Memory);
            return -1;
        }
        g->mCategoryMap = newmap;

    }
    g->mCategoryMap[g->mNCategoryMap] =
        (STCategoryMapEntry *) calloc(1, sizeof(STCategoryMapEntry));
    if (!g->mCategoryMap[g->mNCategoryMap]) {
        REPORT_ERROR(__LINE__, AddCategoryNode_No_Memory);
        return -1;
    }
    g->mCategoryMap[g->mNCategoryMap]->categoryName = node->categoryName;
    g->mCategoryMap[g->mNCategoryMap]->node = node;
    g->mNCategoryMap++;
    return 0;
}

/*
** NewCategoryNode
**
** Creates a new category node for category name 'catname' and makes
** 'parent' its parent.
*/
STCategoryNode *
NewCategoryNode(const char *catName, STCategoryNode * parent, STGlobals * g)
{
    STCategoryNode *node;

    node = (STCategoryNode *) calloc(1, sizeof(STCategoryNode));
    if (!node)
        return NULL;

    node->runs =
        (STRun **) calloc(g->mCommandLineOptions.mContexts, sizeof(STRun *));
    if (NULL == node->runs) {
        free(node);
        return NULL;
    }

    node->categoryName = catName;

    /* Set parent of child */
    node->parent = parent;

    /* Set child in parent */
    AddChild(parent, node);

    /* Add node into mapping table */
    AddCategoryNode(node, g);

    return node;
}

/*
** ProcessCategoryLeafRule
**
** Add this into the tree as a leaf node. It doesn't know who its parent is. For now we make
** root as its parent
*/
int
ProcessCategoryLeafRule(STCategoryRule * leafRule, STCategoryNode * root,
                        STGlobals * g)
{
    STCategoryRule *rule;
    STCategoryNode *node;

    rule = (STCategoryRule *) calloc(1, sizeof(STCategoryRule));
    if (!rule)
        return -1;

    /* Take ownership of all elements of rule */
    *rule = *leafRule;

    /* Find/Make a STCategoryNode and add it into the tree */
    node = findCategoryNode(rule->categoryName, g);
    if (!node)
        node = NewCategoryNode(rule->categoryName, root, g);

    /* Make sure rule knows which node to access */
    rule->node = node;

    /* Add rule into rulelist */
    AddRule(g, rule);

    return 0;
}

/*
** ProcessCategoryParentRule
**
** Rule has all the children of category as patterns. Sets up the tree so that
** the parent child relationship is honored.
*/
int
ProcessCategoryParentRule(STCategoryRule * parentRule, STCategoryNode * root,
                          STGlobals * g)
{
    STCategoryNode *node;
    STCategoryNode *child;
    uint32_t i;

    /* Find the parent node in the tree. If not make one and add it into the tree */
    node = findCategoryNode(parentRule->categoryName, g);
    if (!node) {
        node = NewCategoryNode(parentRule->categoryName, root, g);
        if (!node)
            return -1;
    }

    /* For every child node, Find/Create it and make it the child of this node */
    for (i = 0; i < parentRule->npats; i++) {
        child = findCategoryNode(parentRule->pats[i], g);
        if (!child) {
            child = NewCategoryNode(parentRule->pats[i], node, g);
            if (!child)
                return -1;
        }
        else {
            /* Reparent child to node. This is because when we created the node
             ** we would have created it as the child of root. Now we need to
             ** remove it from root's child list and add it into this node
             */
            Reparent(node, child);
        }
    }

    return 0;
}

/*
** initCategories
**
** Initialize all categories. This reads in a file that says how to categorize
** each callsite, creates a tree of these categories and makes a list of these
** patterns in order for matching
*/
int
initCategories(STGlobals * g)
{
    FILE *fp;
    char buf[1024], *in;
    int n;
    PRBool inrule, leaf;
    STCategoryRule rule;

    fp = fopen(g->mCommandLineOptions.mCategoryFile, "r");
    if (!fp) {
        /* It isn't an error to not have a categories file */
        REPORT_INFO("No categories file.");
        return -1;
    }

    inrule = PR_FALSE;
    leaf = PR_FALSE;

    memset(&rule, 0, sizeof(rule));

    while (fgets(buf, sizeof(buf), fp) != NULL) {
        /* Lose the \n */
        n = strlen(buf);
        if (buf[n - 1] == '\n')
            buf[--n] = '\0';
        in = buf;

        /* skip comments */
        if (*in == '#')
            continue;

        /* skip empty lines. If we are in a rule, end the rule. */
        while (*in && isspace(*in))
            in++;
        if (*in == '\0') {
            if (inrule) {
                /* End the rule : leaf or non-leaf */
                if (leaf)
                    ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);
                else
                    /* non-leaf */
                    ProcessCategoryParentRule(&rule, &g->mCategoryRoot, g);
                inrule = PR_FALSE;
                memset(&rule, 0, sizeof(rule));
            }
            continue;
        }

        /* if we are in a rule acculumate */
        if (inrule) {
            rule.pats[rule.npats] = strdup(in);
            rule.patlen[rule.npats++] = strlen(in);
        }
        else if (*in == '<') {
            /* Start a category */
            inrule = PR_TRUE;
            leaf = PR_TRUE;

            /* Get the category name */
            in++;
            n = strlen(in);
            if (in[n - 1] == '>')
                in[n - 1] = '\0';
            rule.categoryName = strdup(in);
        }
        else {
            /* this is a non-leaf category. Should be of the form CategoryName
             ** followed by list of child category names one per line
             */
            inrule = PR_TRUE;
            leaf = PR_FALSE;
            rule.categoryName = strdup(in);
        }
    }

    /* If we were in a rule when processing the last line, end the rule */
    if (inrule) {
        /* End the rule : leaf or non-leaf */
        if (leaf)
            ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);
        else
            /* non-leaf */
            ProcessCategoryParentRule(&rule, &g->mCategoryRoot, g);
    }

    /* Add the final "uncategorized" category. We make new memory locations
     ** for all these to conform to the general principle of all strings are allocated
     ** so it makes release logic very simple.
     */
    memset(&rule, 0, sizeof(rule));
    rule.categoryName = strdup("uncategorized");
    rule.pats[0] = strdup("");
    rule.patlen[0] = 0;
    rule.npats = 1;
    ProcessCategoryLeafRule(&rule, &g->mCategoryRoot, g);

    return 0;
}

/*
** callsiteMatchesRule
**
** Returns the corresponding node if callsite matches the rule. Rule is a sequence
** of patterns that must match contiguously the callsite.
*/
int
callsiteMatchesRule(tmcallsite * aCallsite, STCategoryRule * aRule)
{
    uint32_t patnum = 0;
    const char *methodName = NULL;

    while (patnum < aRule->npats && aCallsite && aCallsite->method) {
        methodName = tmmethodnode_name(aCallsite->method);
        if (!methodName)
            return 0;
        if (!*aRule->pats[patnum]
            || !strncmp(methodName, aRule->pats[patnum],
                        aRule->patlen[patnum])) {
            /* We have matched so far. Proceed up the stack and to the next pattern */
            patnum++;
            aCallsite = aCallsite->parent;
        }
        else {
            /* Deal with mismatch */
            if (patnum > 0) {
                /* contiguous mismatch. Stop */
                return 0;
            }
            /* We still haven't matched the first pattern. Proceed up the stack without
             ** moving to the next pattern.
             */
            aCallsite = aCallsite->parent;
        }
    }

    if (patnum == aRule->npats) {
        /* all patterns matched. We have a winner. */
#if defined(DEBUG_dp) && 0
        fprintf(stderr, "[%s] match\n", aRule->categoryName);
#endif
        return 1;
    }

    return 0;
}

#ifdef DEBUG_dp
PRIntervalTime _gMatchTime = 0;
uint32_t _gMatchCount = 0;
uint32_t _gMatchRules = 0;
#endif

/*
** matchAllocation
**
** Runs through all rules and returns the node corresponding to
** a match of the allocation.
*/
STCategoryNode *
matchAllocation(STGlobals * g, STAllocation * aAllocation)
{
#ifdef DEBUG_dp
    PRIntervalTime start = PR_IntervalNow();
#endif
    uint32_t rulenum;
    STCategoryNode *node = NULL;
    STCategoryRule *rule;

    for (rulenum = 0; rulenum < g->mNRules; rulenum++) {
#ifdef DEBUG_dp
        _gMatchRules++;
#endif
        rule = g->mCategoryRules[rulenum];
        if (callsiteMatchesRule(aAllocation->mEvents[0].mCallsite, rule)) {
            node = rule->node;
            break;
        }
    }
#ifdef DEBUG_dp
    _gMatchCount++;
    _gMatchTime += PR_IntervalNow() - start;
#endif
    return node;
}

/*
** categorizeAllocation
**
** Given an allocation, it adds it into the category tree at the right spot
** by comparing the allocation to the rules and adding into the right node.
** Also, does propogation of cost upwards in the tree.
** The root of the tree is in the globls as the tree is dependent on the
** category file (options) rather than the run.
*/
int
categorizeAllocation(STOptions * inOptions, STContext * inContext,
                     STAllocation * aAllocation, STGlobals * g)
{
    /* Run through the rules in order to see if this allcation matches
     ** any of them.
     */
    STCategoryNode *node;

    node = matchAllocation(g, aAllocation);
    if (!node) {
        /* ugh! it should atleast go into the "uncategorized" node. wierd!
         */
        REPORT_ERROR(__LINE__, categorizeAllocation);
        return -1;
    }

    /* Create run for node if not already */
    if (!node->runs[inContext->mIndex]) {
        /*
         ** Create run with positive timestamp as we can harvest it later
         ** for callsite details summarization
         */
        node->runs[inContext->mIndex] =
            createRun(inContext, PR_IntervalNow());
        if (!node->runs[inContext->mIndex]) {
            REPORT_ERROR(__LINE__, categorizeAllocation_No_Memory);
            return -1;
        }
    }

    /* Add allocation into node. We expand the table of allocations in steps */
    if (node->runs[inContext->mIndex]->mAllocationCount % ST_ALLOC_STEP == 0) {
        /* Need more space */
        STAllocation **allocs;

        allocs =
            (STAllocation **) realloc(node->runs[inContext->mIndex]->
                                      mAllocations,
                                      (node->runs[inContext->mIndex]->
                                       mAllocationCount +
                                       ST_ALLOC_STEP) *
                                      sizeof(STAllocation *));
        if (!allocs) {
            REPORT_ERROR(__LINE__, categorizeAllocation_No_Memory);
            return -1;
        }
        node->runs[inContext->mIndex]->mAllocations = allocs;
    }
    node->runs[inContext->mIndex]->mAllocations[node->
                                                runs[inContext->mIndex]->
                                                mAllocationCount++] =
        aAllocation;

    /*
     ** Make sure run's stats are calculated. We don't go update the parents of allocation
     ** at this time. That will happen when we focus on this category. This updating of
     ** stats will provide us fast categoryreports.
     */
    recalculateAllocationCost(inOptions, inContext,
                              node->runs[inContext->mIndex], aAllocation,
                              PR_FALSE);

    /* Propagate upwards the statistics */
    /* XXX */
#if defined(DEBUG_dp) && 0
    fprintf(stderr, "DEBUG: [%s] match\n", node->categoryName);
#endif
    return 0;
}

typedef PRBool STCategoryNodeProcessor(STRequest * inRequest,
                                       STOptions * inOptions,
                                       STContext * inContext,
                                       void *clientData,
                                       STCategoryNode * node);

PRBool
freeNodeRunProcessor(STRequest * inRequest, STOptions * inOptions,
                     STContext * inContext, void *clientData,
                     STCategoryNode * node)
{
    if (node->runs && node->runs[inContext->mIndex]) {
        freeRun(node->runs[inContext->mIndex]);
        node->runs[inContext->mIndex] = NULL;
    }
    return PR_TRUE;
}

PRBool
freeNodeRunsProcessor(STRequest * inRequest, STOptions * inOptions,
                      STContext * inContext, void *clientData,
                      STCategoryNode * node)
{
    if (node->runs) {
        uint32_t loop = 0;

        for (loop = 0; loop < globals.mCommandLineOptions.mContexts; loop++) {
            if (node->runs[loop]) {
                freeRun(node->runs[loop]);
                node->runs[loop] = NULL;
            }
        }

        free(node->runs);
        node->runs = NULL;
    }

    return PR_TRUE;
}

#if defined(DEBUG_dp)
PRBool
printNodeProcessor(STRequest * inRequest, STOptions * inOptions,
                   STContext * inContext, void *clientData,
                   STCategoryNode * node)
{
    STCategoryNode *root = (STCategoryNode *) clientData;

    fprintf(stderr, "%-25s [ %9s size", node->categoryName,
            FormatNumber(node->run ? node->run->mStats[inContext->mIndex].
                         mSize : 0));
    fprintf(stderr, ", %5.1f%%",
            node->run ? ((double) node->run->mStats[inContext->mIndex].mSize /
                         root->run->mStats[inContext->mIndex].mSize *
                         100) : 0);
    fprintf(stderr, ", %7s allocations ]\n",
            FormatNumber(node->run ? node->run->mStats[inContext->mIndex].
                         mCompositeCount : 0));
    return PR_TRUE;
}

#endif

typedef struct __struct_optcon
{
    STOptions *mOptions;
    STContext *mContext;
}
optcon;

/*
** compareNode
**
** qsort callback.
** Compare the nodes as specified by the options.
*/
int
compareNode(const void *aNode1, const void *aNode2, void *aContext)
{
    int retval = 0;
    STCategoryNode *node1, *node2;
    uint32_t a, b;
    optcon *oc = (optcon *) aContext;

    if (!aNode1 || !aNode2 || !oc->mOptions || !oc->mContext)
        return 0;

    node1 = *((STCategoryNode **) aNode1);
    node2 = *((STCategoryNode **) aNode2);

    if (node1 && node2) {
        if (oc->mOptions->mOrderBy == ST_COUNT) {
            a = (node1->runs[oc->mContext->mIndex]) ? node1->runs[oc->
                                                                  mContext->
                                                                  mIndex]->
                mStats[oc->mContext->mIndex].mCompositeCount : 0;
            b = (node2->runs[oc->mContext->mIndex]) ? node2->runs[oc->
                                                                  mContext->
                                                                  mIndex]->
                mStats[oc->mContext->mIndex].mCompositeCount : 0;
        }
        else {
            /* Default is by size */
            a = (node1->runs[oc->mContext->mIndex]) ? node1->runs[oc->
                                                                  mContext->
                                                                  mIndex]->
                mStats[oc->mContext->mIndex].mSize : 0;
            b = (node2->runs[oc->mContext->mIndex]) ? node2->runs[oc->
                                                                  mContext->
                                                                  mIndex]->
                mStats[oc->mContext->mIndex].mSize : 0;
        }
        if (a < b)
            retval = __LINE__;
        else
            retval = -__LINE__;
    }
    return retval;
}

PRBool
sortNodeProcessor(STRequest * inRequest, STOptions * inOptions,
                  STContext * inContext, void *clientData,
                  STCategoryNode * node)
{
    if (node->nchildren) {
        optcon context;

        context.mOptions = inOptions;
        context.mContext = inContext;

        NS_QuickSort(node->children, node->nchildren,
                     sizeof(STCategoryNode *), compareNode, &context);
    }

    return PR_TRUE;
}


/*
** walkTree
**
** General purpose tree walker. Issues callback for each node.
** If 'maxdepth' > 0, then stops after processing that depth. Root is
** depth 0, the nodes below it are depth 1 etc...
*/
#define MODINC(n, mod) ((n+1) % mod)

void
walkTree(STCategoryNode * root, STCategoryNodeProcessor func,
         STRequest * inRequest, STOptions * inOptions, STContext * inContext,
         void *clientData, int maxdepth)
{
    STCategoryNode *nodes[1024], *node;
    uint32_t begin, end, i;
    int ret = 0;
    int curdepth = 0, ncurdepth = 0;

    nodes[0] = root;
    begin = 0;
    end = 1;
    ncurdepth = 1;
    while (begin != end) {
        node = nodes[begin];
        ret = (*func) (inRequest, inOptions, inContext, clientData, node);
        if (!ret) {
            /* Abort */
            break;
        }
        begin = MODINC(begin, 1024);
        for (i = 0; i < node->nchildren; i++) {
            nodes[end] = node->children[i];
            end = MODINC(end, 1024);
        }
        /* Depth tracking. Do it only if walkTree is contrained by a maxdepth */
        if (maxdepth > 0 && --ncurdepth == 0) {
            /*
             ** No more children in current depth. The rest of the nodes
             ** we have in our list should be nodes in the depth below us.
             */
            ncurdepth = (begin < end) ? (end - begin) : (1024 - begin + end);
            if (++curdepth > maxdepth) {
                /*
                 ** Gone too deep. Stop.
                 */
                break;
            }
        }
    }
    return;
}

int
freeRule(STCategoryRule * rule)
{
    uint32_t i;
    char *p = (char *) rule->categoryName;

    PR_FREEIF(p);

    for (i = 0; i < rule->npats; i++)
        free(rule->pats[i]);

    free(rule);
    return 0;
}

void
freeNodeRuns(STCategoryNode * root)
{
    walkTree(root, freeNodeRunsProcessor, NULL, NULL, NULL, NULL, 0);
}

void
freeNodeMap(STGlobals * g)
{
    uint32_t i;

    /* all nodes are in the map table. Just delete all of those. */
    for (i = 0; i < g->mNCategoryMap; i++) {
        free(g->mCategoryMap[i]->node);
        free(g->mCategoryMap[i]);
    }
    free(g->mCategoryMap);
}

int
freeCategories(STGlobals * g)
{
    uint32_t i;

    /*
     ** walk the tree and free runs held in nodes
     */
    freeNodeRuns(&g->mCategoryRoot);

    /*
     ** delete nodemap. This is the where nodes get deleted.
     */
    freeNodeMap(g);

    /*
     ** delete rule stuff
     */
    for (i = 0; i < g->mNRules; i++) {
        freeRule(g->mCategoryRules[i]);
    }
    free(g->mCategoryRules);

    return 0;
}


/*
** categorizeRun
**
** categorize all the allocations of the run using the rules into
** a tree rooted at globls.mCategoryRoot
*/
int
categorizeRun(STOptions * inOptions, STContext * inContext,
              const STRun * aRun, STGlobals * g)
{
    uint32_t i;

#if defined(DEBUG_dp)
    PRIntervalTime start = PR_IntervalNow();

    fprintf(stderr, "DEBUG: categorizing run...\n");
#endif

    /*
     ** First, cleanup our tree
     */
    walkTree(&g->mCategoryRoot, freeNodeRunProcessor, NULL, inOptions,
             inContext, NULL, 0);

    if (g->mNCategoryMap > 0) {
        for (i = 0; i < aRun->mAllocationCount; i++) {
            categorizeAllocation(inOptions, inContext, aRun->mAllocations[i],
                                 g);
        }
    }

    /*
     ** the run is always going to be the one corresponding to the root node
     */
    g->mCategoryRoot.runs[inContext->mIndex] = (STRun *) aRun;
    g->mCategoryRoot.categoryName = ST_ROOT_CATEGORY_NAME;

#if defined(DEBUG_dp)
    fprintf(stderr,
            "DEBUG: categorizing ends: %dms [%d rules, %d allocations]\n",
            PR_IntervalToMilliseconds(PR_IntervalNow() - start), g->mNRules,
            aRun->mAllocationCount);
    fprintf(stderr, "DEBUG: match : %dms [%d calls, %d rule-compares]\n",
            PR_IntervalToMilliseconds(_gMatchTime), _gMatchCount,
            _gMatchRules);
#endif

    /*
     ** sort the tree based on our sort criterion
     */
    walkTree(&g->mCategoryRoot, sortNodeProcessor, NULL, inOptions, inContext,
             NULL, 0);

#if defined(DEBUG_dp)
    walkTree(&g->mCategoryRoot, printNodeProcessor, NULL, inOptions,
             inContext, &g->mCategoryRoot, 0);
#endif

    return 0;
}


/*
** displayCategoryReport
**
** Generate the category report - a list of all categories and details about each
** depth parameter controls how deep we traverse the category tree.
*/
PRBool
displayCategoryNodeProcessor(STRequest * inRequest, STOptions * inOptions,
                             STContext * inContext, void *clientData,
                             STCategoryNode * node)
{
    STCategoryNode *root = (STCategoryNode *) clientData;
    uint32_t byteSize = 0, heapCost = 0, count = 0;
    double percent = 0;
    STOptions customOps;

    if (node->runs[inContext->mIndex]) {
        /*
         ** Byte size
         */
        byteSize =
            node->runs[inContext->mIndex]->mStats[inContext->mIndex].mSize;

        /*
         ** Composite count
         */
        count =
            node->runs[inContext->mIndex]->mStats[inContext->mIndex].
            mCompositeCount;

        /*
         ** Heap operation cost
         **/
        heapCost =
            node->runs[inContext->mIndex]->mStats[inContext->mIndex].
            mHeapRuntimeCost;

        /*
         ** % of total size
         */
        if (root->runs[inContext->mIndex]) {
            percent =
                ((double) byteSize) /
                root->runs[inContext->mIndex]->mStats[inContext->mIndex].
                mSize * 100;
        }
    }

    PR_fprintf(inRequest->mFD, " <tr>\n" "  <td>");

    /* a link to topcallsites report with focus on category */
    memcpy(&customOps, inOptions, sizeof(customOps));
    PR_snprintf(customOps.mCategoryName, sizeof(customOps.mCategoryName),
                "%s", node->categoryName);

    htmlAnchor(inRequest, "top_callsites.html", node->categoryName, NULL,
               "category-callsites", &customOps);
    PR_fprintf(inRequest->mFD,
               "</td>\n" "  <td align=right>%u</td>\n"
               "  <td align=right>%4.1f%%</td>\n"
               "  <td align=right>%u</td>\n" "  <td align=right>"
               ST_MICROVAL_FORMAT "</td>\n" " </tr>\n", byteSize, percent,
               count, ST_MICROVAL_PRINTABLE(heapCost));

    return PR_TRUE;
}


int
displayCategoryReport(STRequest * inRequest, STCategoryNode * root, int depth)
{
    PR_fprintf(inRequest->mFD,
               "<table class=\"category-list data\">\n"
               " <tr class=\"row-header\">\n"
               "  <th>Category</th>\n"
               "  <th>Composite Byte Size</th>\n"
               "  <th>%% of Total Size</th>\n"
               "  <th>Heap Object Count</th>\n"
               "  <th>Composite Heap Operations Seconds</th>\n" " </tr>\n");

    walkTree(root, displayCategoryNodeProcessor, inRequest,
             &inRequest->mOptions, inRequest->mContext, root, depth);

    PR_fprintf(inRequest->mFD, "</table>\n");

    return 0;
}