123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359 |
- /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
- /*================================================================
- *
- * Project : MTK
- * Author : Terry 'Mongoose' Hendrix II
- * Website : http://www.westga.edu/~stu7440/
- * Email : stu7440@westga.edu
- * Object : memeory_test
- * License : No use w/o permission (C) 2002 Mongoose
- * Comments: Memory testing tool kit
- *
- *
- * This file was generated using Mongoose's C++
- * template generator script. <stu7440@westga.edu>
- *
- *-- History -------------------------------------------------
- *
- * 2002.03.27:
- * Mongoose - Created
- =================================================================*/
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
-
-
- #define DWORD unsigned long
- #define USE_ITERATIVE_TREE_INSERT
-
-
- typedef enum { RB_BLACK = 0, RB_RED = 1 } rbtree_color_t;
-
- typedef struct rbtree_s
- {
- void *data;
- DWORD key;
-
- rbtree_color_t color;
-
- struct rbtree_s *left;
- struct rbtree_s *right;
- struct rbtree_s *parent;
-
- } rbtree_t;
-
-
-
- bool tree_check(rbtree_t *tree, char *file, int line)
- {
- bool ret = true;
-
-
- if (tree)
- {
- if (tree == tree->parent ||
- tree == tree->right ||
- tree == tree->left)
- {
- printf("\nERROR: Self pointing %s:%i\n", file, line);
- ret = false;
- }
-
- if (tree->parent &&
- (tree->parent == tree->left || tree->parent == tree->right))
- {
- printf("\nERROR: Parent is also child %s:%i\n", file, line);
- ret = false;
- }
-
- if (tree->left && tree->left == tree->right)
- {
- printf("\nERROR: Duplicate children %s:%i\n", file, line);
- ret = false;
- }
- }
-
- if (!ret)
- {
- printf("ERROR: Left %p, Parent %p, Right %p, This %p\a\n",
- tree->left, tree->parent, tree->right, tree);
- }
-
- return ret;
- }
-
- #define CHECK_TREENODE(tree) if (!tree_check(tree, __FILE__, __LINE__)) {exit(0);}
-
-
- bool check_red_black_tree(rbtree_t *current, bool valid, bool strict)
- {
- rbtree_t *right, *left;
- int color_red;
-
-
- if (!current)
- {
- return valid;
- }
-
- if (strict)
- {
- CHECK_TREENODE(current);
- }
-
- // Mongoose 2002.02.19, Check for a red root
- if (!current->parent && current->color == RB_RED)
- {
- printf("Invalid root color\n");
- return false;
- }
-
- color_red = (current->color == RB_RED);
- left = current->left;
- right = current->right;
-
- // Mongoose 2002.02.19, Check for adj red nodes
- if (left)
- {
- if (color_red && left->color == RB_RED)
- {
- printf("Invalid adjacent red colors in a left branch, %p\n",
- left);
- return false;
- }
-
- if (!check_red_black_tree(left, valid, strict))
- {
- return false;
- }
- }
-
- if (right)
- {
- if (color_red && right->color == RB_RED)
- {
- printf("Invalid adjacent red colors in a right branch, %p\n",
- right);
- return false;
- }
-
- if (!check_red_black_tree(right, valid, strict))
- {
- return false;
- }
- }
-
- return true;
- }
-
-
- void tree_valid_report(rbtree_t *root)
- {
- printf("Is valid red-black tree? %s\n",
- check_red_black_tree(root, true, true) ? "true" : "false \a");
- }
-
-
- rbtree_t *tree_find(rbtree_t *tree, DWORD key)
- {
- if (!tree)
- return 0;
-
- if (key == tree->key)
- {
- return tree;
- }
- else if (tree->left && key < tree->key)
- {
- return tree_find(tree->left, key);
- }
- else if (tree->right)
- {
- return tree_find(tree->right, key);
- }
-
- return 0;
- }
-
-
- rbtree_t *tree_new(void *data, DWORD key)
- {
- rbtree_t *tree;
-
-
- tree = (rbtree_t *)malloc(sizeof(rbtree_t));
- tree->parent = 0;
- tree->left = 0;
- tree->right = 0;
- tree->data = data;
- tree->color = RB_RED;
- tree->key = key;
-
- return tree;
- }
-
-
- void tree_rotate_left(rbtree_t **root, rbtree_t *tree)
- {
- rbtree_t *right, *right_leftchild, *uncle;
-
-
- if (!tree || !(*root))
- {
- return;
- }
-
- // Get tree's right node
- right = tree->right;
-
- // Get right node's left child
- if (right)
- {
- right_leftchild = right->left;
- }
- else
- {
- right_leftchild = 0;
- }
-
- // Set tree's right node to right's left child
- tree->right = right_leftchild;
-
- // Child now has a new parent
- if (right_leftchild)
- {
- right_leftchild->parent = tree;
- }
-
- // Right also has a new parent
- if (right)
- {
- right->parent = tree->parent;
- }
-
- if (tree->parent) // Not root
- {
- uncle = (tree->parent)->left;
-
- // Mix up at hosptial, switch parent's children!
- if (tree == uncle)
- {
- (tree->parent)->left = right;
- }
- else
- {
- (tree->parent)->right = right;
- }
- }
- else // TreeNode 'tree' was root, so now right is root
- {
- *root = right;
- }
-
- if (right)
- right->left = tree;
-
- tree->parent = right;
- }
-
-
- void tree_rotate_right(rbtree_t **root, rbtree_t *tree)
- {
- rbtree_t *left, *left_rightchild, *parent, *uncle;
-
-
- if (!tree || !(*root))
- {
- return;
- }
-
- left = tree->left;
-
- left_rightchild = 0;
-
- if (left)
- {
- left_rightchild = left->right;
- }
-
- tree->left = left_rightchild;
-
- if (left_rightchild)
- {
- left_rightchild->parent = tree;
- }
-
- if (left)
- {
- left->parent = tree->parent;
- }
-
- parent = tree->parent;
-
- if (parent) //if node is not the root
- {
- uncle = parent->right;
-
- if (tree == uncle)
- {
- parent->right = left;
- }
- else
- {
- parent->left = left;
- }
- }
- else
- {
- *root = left;
- }
-
- left->right = tree;
- tree->parent = left;
- }
-
-
- rbtree_t *tree_insert(rbtree_t **root, void *data, DWORD key)
- {
- rbtree_t *tree, *prev, *uncle;
-
-
- // Allocate and store new node in tree ////////
-
- if (!(*root))
- {
- *root = tree_new(data, key);
- (*root)->color = RB_BLACK;
- return (*root);
- }
-
- tree = *root;
- prev = 0x0;
-
- do
- {
- prev = tree;
-
- if (key < prev->key)
- {
- tree = prev->left;
- }
- else if (key > prev->key)
- {
- tree = prev->right;
- }
- else // Duplicate
- {
- return 0x0; // Don't allow duplicates
- }
-
- } while (tree);
-
-
- if (key < prev->key)
- {
- prev->left = tree_new(data, key);
- tree = prev->left;
- }
- else
- {
- prev->right = tree_new(data, key);
- tree = prev->right;
- }
-
- tree->parent = prev;
- prev = tree;
-
- // Now do red-black fix for insertion ///////////////////
-
- while ((tree != (*root)) && tree->parent->color == RB_RED)
- {
- // Parent is a left child
- if (tree->parent == tree->parent->parent->left)
- {
- // Tree's right uncle
- uncle = tree->parent->parent->right;
-
- // Parent and Uncle are RED
- if ((uncle) && (uncle->color == RB_RED))
- {
- // Change Parent and Uncle to BLACK, make grandparent RED
- tree->parent->color = RB_BLACK;
- uncle->color = RB_BLACK;
- tree->parent->parent->color = RB_RED;
-
- // Continue from grandparent
- tree = tree->parent->parent;
- }
- else // Parent is RED and Uncle is BLACK
- {
- // Tree is a right child
- if (tree == tree->parent->right)
- {
- tree_rotate_left(root, tree->parent);
- tree = tree->left;
- }
-
- tree->parent->color = RB_BLACK;
- tree->parent->parent->color = RB_RED;
- tree_rotate_right(root, tree->parent->parent);
- }
- }
- else // Parent is a right child
- {
- // Tree's left uncle
- uncle = tree->parent->parent->left;
-
- // Parent and Uncle are RED
- if ((uncle) && (uncle->color == RB_RED))
- {
- // Change Parent and Uncle to BLACK, make grandparent RED
- tree->parent->color = RB_BLACK;
- uncle->color = RB_BLACK;
- tree->parent->parent->color = RB_RED;
-
- // Continue from grandparent
- tree = tree->parent->parent;
- }
- else // Parent is RED and Uncle is BLACK
- {
- // Tree is a left child
- if (tree == tree->parent->left)
- {
- tree_rotate_right(root, tree->parent);
- tree = tree->right;
- }
-
- tree->parent->color = RB_BLACK;
- tree->parent->parent->color = RB_RED;
- tree_rotate_left(root, tree->parent->parent);
- }
- }
- }
-
- (*root)->color = RB_BLACK;
-
- return prev;
- }
-
-
- int tree_print(rbtree_t *tree, void (*print_func)(void *))
- {
- int i = 0;
-
- if (!tree)
- return 0;
-
- if (tree->left)
- {
- i += tree_print(tree->left, print_func);
- printf(",\n");
- }
-
- if (tree->data && print_func)
- {
- (*print_func)(tree->data);
-
- #ifdef DEBUG_MEMEORY_RBTREE
- printf(" :%s%s)",
- (!tree->parent) ? " (root, " : " ("),
- (tree->color == RB_BLACK) ? "black" : "red");
- #endif
- }
-
- ++i;
-
- if (tree->right)
- {
- printf(",\n");
- i += tree_print(tree->right, print_func);
- }
-
- if (!tree->parent)
- {
- printf("\n%i nodes, %u bytes each : %i bytes\n",
- i, sizeof(rbtree_t), i * sizeof(rbtree_t));
- }
-
- return i;
- }
-
-
- rbtree_t *tree_get_successor(rbtree_t *root, rbtree_t *tree)
- {
- rbtree_t *successor;
-
-
- successor = tree->right;
-
- if (successor)
- {
- while (successor->left)
- {
- successor = successor->left;
- }
-
- return successor;
- }
- else
- {
- successor = tree->parent;
-
- while (tree == successor->right)
- {
- tree = successor;
- successor = successor->parent;
- }
-
- if (successor == root)
- {
- return 0;
- }
-
- return successor;
- }
- }
-
-
- rbtree_t *tree_get_predecessor(rbtree_t *root,rbtree_t *tree)
- {
- rbtree_t *predecessor;
-
-
- predecessor = tree->left;
-
- if (predecessor)
- {
- while (predecessor->right)
- {
- predecessor = predecessor->right;
- }
-
- return predecessor;
- }
- else
- {
- predecessor = tree->parent;
-
- while (tree == predecessor->left)
- {
- if (predecessor == root)
- {
- return 0;
- }
-
- tree = predecessor;
- predecessor = predecessor->parent;
- }
-
- return predecessor;
- }
- }
-
-
- void tree_restore_after_remove(rbtree_t **root, rbtree_t *tree)
- {
- rbtree_t *parent, *sibling, *sleft, *sright;
-
-
- if (!tree || !(*root))
- {
- return;
- }
-
- parent = tree->parent;
-
- while ((tree != (*root)) && (tree->color == RB_BLACK)) // was parent->color
- {
- if (tree == parent->left)
- {
- sibling = parent->right;
-
- if (sibling && sibling->color == RB_RED)
- {
- sibling->color = RB_BLACK;
- parent->color = RB_RED;
- tree_rotate_left(root, tree->parent);
- parent = tree->parent;
- sibling = parent->right;
- }
-
- if (sibling)
- {
- sleft = sibling->left;
- sright = sibling->right;
- }
- else
- {
- sleft = sright = NULL;
- }
-
- if ((!sright || sright->color == RB_BLACK) &&
- (!sleft || sleft->color == RB_BLACK))
- {
- if (sibling)
- sibling->color = RB_RED;
-
- tree = parent;
- }
- else
- {
- if (!sright || sright->color == RB_BLACK)
- {
- sibling->color = RB_RED;
- sleft->color = RB_BLACK;
- tree_rotate_right(root, sibling);
- sibling = parent->right;
- sright = sibling->right;
- sleft = sibling->left;
- }
-
- sibling->color = parent->color;
- parent->color = RB_BLACK;
- sright->color = RB_BLACK;
- tree_rotate_left(root, tree->parent);
- parent = tree->parent;
- tree = (*root);
- }
- }
- else
- {
- sibling = parent->left;
-
- if (sibling && sibling->color == RB_RED)
- {
- sibling->color = RB_BLACK;
- parent->color = RB_RED;
- tree_rotate_right(root, tree->parent);
- parent = tree->parent;
- sibling = parent->left;
- }
-
- if (sibling)
- {
- sleft = sibling->left;
- sright = sibling->right;
- }
- else
- {
- sleft = sright = 0;
- }
-
- if ((!sright || sright->color == RB_BLACK) &&
- (!sleft || sleft->color == RB_BLACK))
- {
- if (sibling)
- sibling->color = RB_RED;
-
- tree = parent;
- }
- else
- {
- if (!sleft || sleft->color == RB_BLACK)
- {
- sibling->color = RB_RED;
- sright->color = RB_BLACK;
- tree_rotate_left(root, sibling);
- sibling = parent->left;
- }
-
- sibling->color = parent->color;
- parent->color = RB_BLACK;
- if (sleft) sleft->color = RB_BLACK;
- tree_rotate_right(root, tree->parent);
- parent = tree->parent;
- tree = (*root);
- }
- }
-
- parent = tree->parent;
- }
-
- tree->color = RB_BLACK;
- }
-
-
- void tree_remove(rbtree_t **root, rbtree_t *tree)
- {
- rbtree_t *left, *right, *parent, *prev, *cur;
-
-
- // Mongoose 2002.02.16, Nothing to remove
- if (!tree || !(*root))
- {
- return;
- }
-
- left = tree->left;
- right = tree->right;
- parent = tree->parent;
-
- if (!left || !right)
- {
- prev = tree;
- }
- else
- {
- prev = tree_get_successor((*root), tree);
- }
-
- if (prev->left)
- {
- cur = prev->left;
- }
- else
- {
- cur = prev->right;
- }
-
- if (cur)
- {
- cur->parent = prev->parent;
- }
-
- if (!prev->parent)
- {
- *root = cur;
- }
- else
- {
- parent = prev->parent;
-
- if (prev == parent->left)
- {
- parent->left = cur;
- }
- else
- {
- parent->right = cur;
- }
- }
-
- if (prev != tree)
- {
- tree->data = prev->data;
- tree->key = prev->key;
- }
-
- if (prev->color == RB_BLACK)
- {
- tree_restore_after_remove(root, cur);
- }
-
- free(prev);
- }
-
-
- #ifdef DEBUG_MEMEORY
-
- #define ZERO_ALLOC_SLOTS 3
-
- typedef struct meminfo_filename_s
- {
- char *filename;
- char filename_len;
- DWORD size;
- unsigned int alloc_zero;
- unsigned short int alloc_zero_at_line[ZERO_ALLOC_SLOTS];
-
- struct meminfo_filename_s *next;
-
- } meminfo_filename_t;
-
-
- typedef struct meminfo_s
- {
- DWORD address;
- DWORD size;
- unsigned short int line;
- meminfo_filename_t *filename;
-
- } meminfo_t;
-
-
- void __print_dword(DWORD d)
- {
- printf("0x%08lx", d);
- }
-
-
- void __print_meminfo(meminfo_t *meminfo)
- {
- if (meminfo)
- {
- printf("<addr 0x%08lx, sz %lu, \"%s\":%u>",
- meminfo->address,
- meminfo->size,
- (!meminfo->filename) ? "?" : meminfo->filename->filename,
- meminfo->line);
- }
- }
-
- ///////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
-
- rbtree_t *MEMEORY_INFO = 0;
- meminfo_filename_t *MEMEORY_FILENAME = 0;
- long MEMEORY_USED = 0L;
- long MEMEORYA_USED = 0L;
- long MEMEORYC_USED = 0L;
- long MAX_MEMEORY_USED = 0L;
- long MAX_MEMEORYA_USED = 0L;
- long MAX_MEMEORYC_USED = 0L;
-
-
- typedef enum
- {
- MEMORY_USED_BY_PROGRAM = 1,
- MEMORY_USED_BY_OVERHEAD = 2,
- MEMORY_USED_TOTAL = 3,
- MAX_MEMORY_USED_BY_PROGRAM = 4,
- MAX_MEMORY_USED_BY_OVERHEAD = 5
-
- } memory_query_t;
-
- long memory_used(memory_query_t query)
- {
- switch (query)
- {
- case MEMORY_USED_BY_PROGRAM:
- return MEMEORY_USED;
- break;
- case MAX_MEMORY_USED_BY_PROGRAM:
- return MAX_MEMEORY_USED;
- break;
- case MAX_MEMORY_USED_BY_OVERHEAD:
- return MAX_MEMEORYA_USED + MAX_MEMEORYC_USED;
- break;
- case MEMORY_USED_BY_OVERHEAD:
- return MEMEORYA_USED + MEMEORYC_USED;
- break;
- case MEMORY_USED_TOTAL:
- return MEMEORY_USED + MEMEORYA_USED + MEMEORYC_USED;
- break;
- default:
- ;
- }
-
- return 0;
- }
-
-
- void display_memory_usage()
- {
- unsigned int i;
- meminfo_filename_t *cur = MEMEORY_FILENAME;
-
-
- printf("\n============================================================\n");
-
- printf("Memory usage per file:\n");
-
- while (cur)
- {
- printf(" %s : ( %lu bytes, %3.2f%% )\n",
- cur->filename, cur->size,
- 100.0 * ((float)cur->size / (float)MEMEORY_USED));
-
- cur = cur->next;
- }
-
- printf("------------------------------------------------------------\n");
-
- cur = MEMEORY_FILENAME;
-
- printf("Memory special errors per file:\n");
-
- while (cur)
- {
- if (cur->alloc_zero)
- {
- printf(" %s : %u zero allocation errors\n",
- cur->filename, cur->alloc_zero);
-
- printf(" lines:");
-
- for (i = 0; i < ZERO_ALLOC_SLOTS; ++i)
- {
- if (cur->alloc_zero_at_line[i])
- {
- printf(" %u",
- cur->alloc_zero_at_line[i]);
- break;
- }
- }
-
- printf("\n");
- }
-
- cur = cur->next;
- }
-
- printf("------------------------------------------------------------\n");
-
- printf("Memory usage summary:\n");
-
- printf(" Tracked program memory : %lu bytes \t(%.2f MB)\n",
- MEMEORY_USED, (double)MEMEORY_USED / 1024000.0);
- printf(" Untracked overhead memory : %lu bytes \t(%.2f MB)\n",
- MEMEORYA_USED, (double)MEMEORYA_USED / 1024000.0);
- printf(" Untracked m-string memory : %lu bytes\n",
- MEMEORYC_USED);
-
- printf("\n Total accounted memory : %lu bytes \t(%.2f MB)\n",
- MEMEORY_USED + MEMEORYA_USED + MEMEORYC_USED,
- (double)(MEMEORY_USED + MEMEORYA_USED + MEMEORYC_USED) / 1024000.0);
-
- printf("------------------------------------------------------------\n");
-
- printf("Memory max usage summary:\n");
-
- printf(" Tracked program memory : %lu bytes \t(%.2f MB)\n",
- MAX_MEMEORY_USED, (double)MAX_MEMEORY_USED / 1024000.0);
- printf(" Untracked overhead memory : %lu bytes \t(%.2f MB)\n",
- MAX_MEMEORYA_USED, (double)MAX_MEMEORYA_USED / 1024000.0);
- printf(" Untracked m-string memory : %lu bytes\n",
- MAX_MEMEORYC_USED);
-
- printf("\n Total accounted memory : %lu bytes \t(%.2f MB)\n",
- MAX_MEMEORY_USED + MAX_MEMEORYA_USED + MAX_MEMEORYC_USED,
- (double)(MAX_MEMEORY_USED + MAX_MEMEORYA_USED + MAX_MEMEORYC_USED) / 1024000.0);
-
- printf("============================================================\n");
- }
-
-
- void dump_memory_report()
- {
- int i;
-
-
- printf("\n============================================================\n");
- printf(" Memeory status report\n");
- printf("============================================================\n");
- printf("Memory tracking table:\n");
- i = tree_print(MEMEORY_INFO, (void (*)(void *))&__print_meminfo);
- printf("%i records, %u bytes each : %i bytes\n",
- i, sizeof(meminfo_t), i * sizeof(meminfo_t));
- display_memory_usage();
- printf("============================================================\n\n");
- }
-
- void delete_check(const char *file, int line, int print);
-
-
- ///////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////
-
- void add_track(DWORD addr, DWORD size, const char *filename, DWORD line_num)
- {
- meminfo_filename_t *memfile, *memfile_prev;
- meminfo_t *meminfo;
- unsigned int len, i;
-
-
- MEMEORY_USED += size;
-
- if (MEMEORY_USED > MAX_MEMEORY_USED)
- {
- MAX_MEMEORY_USED = MEMEORY_USED;
- }
-
- MEMEORYA_USED += sizeof(meminfo_t);
-
- if (MEMEORYA_USED > MAX_MEMEORYA_USED)
- {
- MAX_MEMEORYA_USED = MEMEORYA_USED;
- }
-
- meminfo = (meminfo_t *)malloc(sizeof(meminfo_t));
-
- meminfo->address = addr;
- meminfo->line = line_num;
- meminfo->size = size;
- meminfo->filename = 0;
-
- if (size == 0)
- {
- printf("\nERROR: %s:%lu Allocated 0 bytes\n", filename, line_num);
- }
-
- if (filename || filename[0])
- {
- len = strlen(filename);
-
- memfile = memfile_prev = 0;
-
- if (MEMEORY_FILENAME)
- {
- memfile = MEMEORY_FILENAME;
-
- while (memfile)
- {
- if (strncmp(filename, memfile->filename, len) == 0)
- break;
-
- memfile_prev = memfile;
- memfile = memfile->next;
- }
- }
-
- if (!memfile)
- {
- memfile = (meminfo_filename_t *)malloc(sizeof(meminfo_filename_t));
- memfile->filename_len = len;
- memfile->filename = (char *)malloc(len+1);
- strncpy(memfile->filename, filename, len);
- memfile->filename[len+1] = 0;
- memfile->next = 0;
- memfile->size = 0;
- memfile->alloc_zero = 0;
- memfile->alloc_zero_at_line[0] = 0;
- memfile->alloc_zero_at_line[1] = 0;
- memfile->alloc_zero_at_line[2] = 0;
-
- MEMEORYC_USED += sizeof(meminfo_filename_t) + memfile->filename_len;
-
- if (MEMEORYC_USED > MAX_MEMEORYC_USED)
- MAX_MEMEORYC_USED = MEMEORYC_USED;
-
- if (memfile_prev)
- {
- memfile_prev->next = memfile;
- }
-
- if (!MEMEORY_FILENAME)
- {
- MEMEORY_FILENAME = memfile;
- }
- }
-
- meminfo->filename = memfile;
-
- if (memfile)
- {
- if (meminfo->size == 0)
- {
- memfile->alloc_zero += 1;
-
- for (i = 0; i < ZERO_ALLOC_SLOTS; ++i)
- {
- if (memfile->alloc_zero_at_line[i] == line_num)
- break;
-
- if (!memfile->alloc_zero_at_line[i])
- {
- memfile->alloc_zero_at_line[i] = line_num;
- break;
- }
- }
- }
-
- memfile->size += meminfo->size;
- }
- }
-
- MEMEORYA_USED += sizeof(rbtree_t);
- tree_insert(&MEMEORY_INFO, meminfo, meminfo->address);
-
- #ifdef DEBUG_MEMEORY_VERBOSE
- printf("add_track> addr 0x%08lx, size %lu, filename %s, line %lu\n",
- addr, size, filename, line_num);
- #endif
- }
-
-
- /////////////////////////////////////////////////////////////////
-
-
- void remove_track(DWORD addr)
- {
- meminfo_t *meminfo = 0;
- rbtree_t *tree = 0;
-
-
- tree = tree_find(MEMEORY_INFO, addr);
-
- if (tree)
- {
- meminfo = (meminfo_t *)tree->data;
-
- if (meminfo)
- {
- tree_remove(&MEMEORY_INFO, tree);
-
- if (meminfo->filename)
- {
- meminfo->filename->size -= meminfo->size;
- }
-
- if (meminfo->size == 0)
- {
- printf("\nERROR: %s:%u Deallocated 0 bytes\n",
- (meminfo->filename) ? meminfo->filename->filename : "?",
- meminfo->line);
- }
-
- MEMEORY_USED -= meminfo->size;
- MEMEORYA_USED -= sizeof(meminfo_t);
- MEMEORYA_USED -= sizeof(rbtree_t);
-
- free(meminfo);
- }
- }
- else
- {
- #ifndef DEBUG_MEMEORY_ERROR_OFF
- printf("\nERROR: remove_track> Unknown addr 0x%08lx ", addr);
- delete_check(0, 0, 1);
- printf("?\n");
- #endif
- }
-
- if (!MEMEORY_INFO)
- {
- meminfo_filename_t *cur;
- meminfo_filename_t *del;
-
-
- cur = MEMEORY_FILENAME;
- MEMEORY_FILENAME = 0;
-
- while (cur)
- {
- del = cur;
- cur = cur->next;
-
- MEMEORYC_USED -= sizeof(meminfo_filename_t);
-
- if (del->filename)
- {
- MEMEORYC_USED -= del->filename_len;
- free(del->filename);
- }
-
- free(del);
- }
- }
-
- #ifdef DEBUG_MEMEORY_VERBOSE
- printf("remove_track> addr 0x%lx\n", addr);
- #endif
- }
-
-
- //////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////
-
- void *operator new(size_t size, const char *file, int line)
- {
- void *ptr = (void *)malloc(size);
-
- add_track((DWORD)ptr, size, file, line);
- return ptr;
- };
-
-
- void *operator new [](size_t size, const char *file, int line)
- {
- void *ptr = (void *)malloc(size);
-
- add_track((DWORD)ptr, size, file, line);
- return ptr;
- }
-
-
- // Mongoose 2002.03.29, Prob should reduce this with meminfo_filename use
- void delete_check(const char *file, int line, int print)
- {
- static char s[64];
- static int l;
- unsigned int len;
-
-
- if (!print)
- {
- l = line;
-
- if (!file || !file[0])
- {
- s[0] = 0;
- return;
- }
-
- len = strlen(file);
-
- if (len > 63)
- len = 63;
-
- strncpy(s, file, len);
- s[len] = 0;
- }
- else
- {
- printf("%s:%i", s, l);
- }
- }
-
-
- void operator delete(void *p)
- {
- remove_track((DWORD)p);
- free(p);
- };
-
-
- void operator delete [](void *p)
- {
- remove_track((DWORD)p);
- free(p);
- };
- #endif
-
-
-
- ////////////////////////////////////////////////////////////
- // Unit Test code
- ////////////////////////////////////////////////////////////
-
- #ifdef __TEST__
- #include "memeory_test.h"
-
- #define PRINT_TEST_BREAK_LINE printf("\n############################################################\n\n");
-
- int memeory_test_unit_test(int argc, char *argv[])
- {
- int *i, *j, *k, *l, *m, *n, *o;
-
-
- PRINT_TEST_BREAK_LINE
- printf("Memeory test\n");
- PRINT_TEST_BREAK_LINE
-
- printf(">\ti = new int; %s:%i\n", __FILE__, __LINE__);
- i = new int;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete i; (%p) %s:%i\n", i, __FILE__, __LINE__);
- delete i;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- PRINT_TEST_BREAK_LINE
-
- printf(">\ti = new int[3]; (%p) %s:%i\n", i, __FILE__, __LINE__);
- i = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] i; (%p) %s:%i\n", i, __FILE__, __LINE__);
- delete [] i;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- PRINT_TEST_BREAK_LINE
-
- printf(">\ti = new int[3]; (%p) %s:%i\n", i, __FILE__, __LINE__);
- i = new int[3];
- printf(">\tj = new int; (%p) %s:%i\n", j, __FILE__, __LINE__);
- j = new int;
- printf(">\tk = new int[3]; (%p) %s:%i\n", k, __FILE__, __LINE__);
- k = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
- printf(">\tdelete [] i; (%p) %s:%i\n", i, __FILE__, __LINE__);
- delete [] i;
- printf(">\tdelete j; (%p) %s:%i\n", j, __FILE__, __LINE__);
- delete j;
- printf(">\tdelete [] k; (%p) %s:%i\n", k, __FILE__, __LINE__);
- delete [] k;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- PRINT_TEST_BREAK_LINE
-
- printf(">\ti = new int[3]; (%p) %s:%i\n", i, __FILE__, __LINE__);
- i = new int[3];
- printf(">\tj = new int; (%p) %s:%i\n", j, __FILE__, __LINE__);
- j = new int;
- printf(">\tk = new int[3]; (%p) %s:%i\n", k, __FILE__, __LINE__);
- k = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
- printf(">\tdelete j; (%p) %s:%i\n", j, __FILE__, __LINE__);
- delete j;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
- printf(">\tj = new int[3]; (%p) %s:%i\n", j, __FILE__, __LINE__);
- j = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
- printf(">\tl = new int[3]; (%p) %s:%i\n", l, __FILE__, __LINE__);
- l = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
- printf(">\tm = new int[3]; (%p) %s:%i\n", m, __FILE__, __LINE__);
- m = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tn = new int[3]; (%p) %s:%i\n", n, __FILE__, __LINE__);
- n = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\to = new int[3]; (%p) %s:%i\n", o, __FILE__, __LINE__);
- o = new int[3];
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] k; (%p) %s:%i\n", k, __FILE__, __LINE__);
- delete [] k;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] j; (%p) %s:%i\n", j, __FILE__, __LINE__);
- delete [] j;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] l; (%p) %s:%i\n", l, __FILE__, __LINE__);
- delete [] l;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] m; (%p) %s:%i\n", m, __FILE__, __LINE__);
- delete [] m;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] n; (%p) %s:%i\n", n, __FILE__, __LINE__);
- delete [] n;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] o; (%p) %s:%i\n", o, __FILE__, __LINE__);
- delete [] o;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- printf(">\tdelete [] i; (%p) %s:%i\n", i, __FILE__, __LINE__);
- delete [] i;
- dump_memory_report();
- tree_valid_report(MEMEORY_INFO);
-
- PRINT_TEST_BREAK_LINE
-
- return 0;
- }
-
-
- int main(int argc, char *argv[])
- {
- printf("[memeory_test test]\n");
-
- return memeory_test_unit_test(argc, argv);
- }
- #endif
|