home *** CD-ROM | disk | FTP | other *** search
/ Toolkit for DOOM / DOOMTOOL.ISO / editors / dme301.zip / SOURCE.ZIP / MEMORY.C < prev    next >
C/C++ Source or Header  |  1994-07-16  |  9KB  |  412 lines

  1. /*
  2.     This is a DMapEdit source code module.  Though it is copyrighted, you
  3.     may modify it and use it for your own personal use, meaning that new
  4.     modified code and anything derived from it (such as exe files) doesn't
  5.     get distributed to anyone, unless you get my permission first.  Code
  6.     from this file, or code based on ideas from this file may be used with
  7.     other programs, provided that you give credit for it in the source code,
  8.     documentation, and 'about' windows or screens, if one exists, for the
  9.     programs using it.  Giving credit means something like this:
  10.  
  11.     Code from DMapEdit was used in this program
  12.  
  13.                               or
  14.  
  15.     Some code for this program was based on ideas presented in DMapEdit
  16.  
  17.     Whatever.  Just be sure to mention "DMapEdit" in such a way that it's
  18.     self-evident how it was useful to the new program, and be sure to have
  19.     "DMapEdit is a trademark of Jason Hoffoss" in the docs.  That's all..
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <alloc.h>
  24. #include <stdarg.h>
  25. #include "dme.h"
  26. #include "dme2.h"
  27.  
  28. extern int mem_trace;
  29. extern char path[];
  30.  
  31. uint freesize, largest, core;
  32. ulong farfreesize, farlargest, farcore;
  33.  
  34. void *get_mem(uint size, char *name)
  35. {
  36.     void *ptr;
  37.  
  38.     if (heapcheck() == -1)
  39.         heaperr("get_mem/", name);
  40.     if (!size)
  41.         return 0;
  42.  
  43. alloc:
  44.     ptr = malloc(size);
  45.     if (!ptr)
  46.         fatal_mem_error(name, size);
  47.     if (mem_trace)
  48.         mem_log("alloc %s(%u)", name, size);
  49.     return ptr;
  50. }
  51.  
  52. void *resize_mem(void *old, uint size, char *name)
  53. {
  54.     void *ptr;
  55.  
  56.     if (heapcheck() == -1)
  57.         heaperr("resize_mem/", name);
  58.     check_if_used(old, name);
  59.  
  60. alloc:
  61.     ptr = farrealloc(old, size);
  62.     if (!ptr)
  63.         fatal_mem_error(name, size);
  64.     if (mem_trace)
  65.         mem_log("realloc %s(%u)", name, size);
  66.     return ptr;
  67. }
  68.  
  69. void huge *get_farmem(ulong size, char *name)
  70. {
  71.     void huge *ptr;
  72.  
  73.     if (farheapcheck() == -1)
  74.         farheaperr("get_farmem/", name);
  75.     if (!size)
  76.         return 0L;
  77.  
  78. alloc:
  79.     while (!(ptr = farmalloc(size)))
  80.     {
  81.         if (free_patch())
  82.             fatal_farmem_error(name, size);
  83.     }
  84.     if (mem_trace)
  85.         mem_log("faralloc %s(%lu)", name, size);
  86.     return ptr;
  87. }
  88.  
  89. void huge *resize_farmem(void huge *old, ulong size, char *name)
  90. {
  91.     void huge *ptr;
  92.  
  93.     if (farheapcheck() == -1)
  94.         farheaperr("resize_farmem/", name);
  95.     check_if_used_far(old, name);
  96.  
  97. alloc:
  98.     while (!(ptr = farrealloc(old, size)))
  99.     {
  100.         if (free_patch())
  101.             fatal_farmem_error(name, size);
  102.     }
  103.     if (mem_trace)
  104.         mem_log("farrealloc %s(%lu)", name, size);
  105.     return ptr;
  106. }
  107.  
  108. void free_mem(void *ptr, char *name)
  109. {
  110.     if (!ptr)
  111.     {
  112.         mem_log("%s=@null", name);
  113.         fatal_error("Call to free_mem with %s pointing to null", name);
  114.     }
  115.  
  116.     if (heapcheck() == -1)
  117.         heaperr("free_mem/", name);
  118.     check_if_used(ptr, name);
  119.  
  120.     free(ptr);
  121.     if (mem_trace)
  122.         mem_log("free %s", name);
  123.     return;
  124. }
  125.  
  126. void free_farmem(void huge *ptr, char *name)
  127. {
  128.     if (!ptr)
  129.     {
  130.         mem_log("%s=@null", name);
  131.         fatal_error("Call to free_farmem with %s pointing to null", name);
  132.     }
  133.  
  134.     if (farheapcheck() == -1)
  135.         farheaperr("free_farmem/", name);
  136.     check_if_used_far(ptr, name);
  137.  
  138.     farfree(ptr);
  139.     if (mem_trace)
  140.         mem_log("farfree %s", name);
  141.     return;
  142. }
  143.  
  144. void check_if_used(void *ptr, char *name)
  145. {
  146.     struct heapinfo info;
  147.  
  148.     info.ptr = 0;
  149.     while (heapwalk(&info) == 2)
  150.     {
  151.         if (info.ptr == ptr)
  152.         {
  153.             if (info.in_use)
  154.                 return;
  155.             break;
  156.         }
  157.     }
  158.  
  159.     mem_log("%s is free", name);
  160.     fatal_error("%s's memory block is free!", name);
  161. }
  162.  
  163. void check_if_used_far(void huge *ptr, char *name)
  164. {
  165.     struct farheapinfo info;
  166.  
  167.     info.ptr = 0L;
  168.     while (farheapwalk(&info) == 2)
  169.     {
  170.         if (info.ptr == ptr)
  171.         {
  172.             if (info.in_use)
  173.                 return;
  174.             break;
  175.         }
  176.     }
  177.  
  178.     mem_log("%s is free", name);
  179.     fatal_error("%s's memory block is free!", name);
  180. }
  181.  
  182. void sizeof_mem_block(void *ptr, char *name)
  183. {
  184.     struct heapinfo info;
  185.  
  186.     if (heapcheck() == -1)
  187.         heaperr("sizeof_mem_block/", name);
  188.  
  189.     info.ptr = 0;
  190.     while (heapwalk(&info) == 2)
  191.     {
  192.         if (info.ptr == ptr)
  193.         {
  194.             if (!info.in_use)
  195.                 fatal_error("%s's memory block is free!", name);
  196.  
  197.             mem_log("%s=%u@%p\n", name, info.size, ptr);
  198.             return;
  199.         }
  200.     }
  201.     fatal_error("%s's pointer was not found!", name);
  202. }
  203.  
  204. void sizeof_farmem_block(void far *ptr, char *name)
  205. {
  206.     struct farheapinfo info;
  207.  
  208.     if (farheapcheck() == -1)
  209.         farheaperr("sizeof_farmem_block/", name);
  210.  
  211.     info.ptr = 0L;
  212.     while (farheapwalk(&info) == 2)
  213.     {
  214.         if (info.ptr == ptr)
  215.         {
  216.             if (!info.in_use)
  217.                 fatal_error("%s's far memory block is free!", name);
  218.  
  219.             mem_log("%s=%luL@%Fp\n", name, info.size, ptr);
  220.             return;
  221.         }
  222.     }
  223.     return;
  224. }
  225.  
  226. void check_mem(void)
  227. {
  228.     struct heapinfo info;
  229.  
  230.     if (heapcheck() == -1)
  231.         heaperr("check_mem", "");
  232.  
  233.     freesize = largest = core = coreleft();
  234.     info.ptr = 0;
  235.     while (heapwalk(&info) == 2)
  236.     {
  237.         if (!info.in_use)
  238.         {
  239.             freesize += info.size;
  240.             if (info.size > largest)
  241.                 largest = info.size;
  242.         }
  243.     }
  244.  
  245.     mem_log("mem=%u %u %u\n", freesize, largest, core);
  246.     return;
  247. }
  248.  
  249. void check_farmem(void)
  250. {
  251.     struct farheapinfo info;
  252.  
  253.     if (farheapcheck() == -1)
  254.         farheaperr("check_farmem", "");
  255.  
  256.     farfreesize = farlargest = farcore = farcoreleft();
  257.     info.ptr = 0L;
  258.     while (farheapwalk(&info) == 2)
  259.     {
  260.         if (!info.in_use)
  261.         {
  262.             farfreesize += info.size;
  263.             if (info.size > farlargest)
  264.                 farlargest = info.size;
  265.         }
  266.     }
  267.  
  268.     mem_log("farmem=%lu %lu %lu\n", farfreesize, farlargest, farcore);
  269.     return;
  270. }
  271.  
  272. void mem_log(char *msg, ...)
  273. {
  274.     va_list args;
  275.     FILE *file;
  276.  
  277.     if (file = fopen("memory.log", "a"))
  278.     {
  279.         va_start(args, msg);
  280.         vfprintf(file, msg, args);
  281.         va_end(args);
  282.         fprintf(file, "\n");
  283.         fclose(file);
  284.     }
  285.     return;
  286. }
  287.  
  288. void statistics(void)
  289. {
  290.     char msg[4096];
  291.  
  292.     check_mem();
  293.     check_farmem();
  294.     sprintf(msg, "Statistics:\t\n"
  295.         "  Things: %5u (%lu)\n"
  296.         "Linedefs: %5u (%lu)\n"
  297.         "Sidedefs: %5u (%lu)\n"
  298.         "Vertexes: %5u (%lu)\n"
  299.         "          %5u\n"
  300.         "    Segs: %5u (%lu)\n"
  301.         "Ssectors: %5u (%lu)\n"
  302.         "   Nodes: %5u (%lu)\n"
  303.         " Sectors: %5u (%lu)\n"
  304.         "  Reject: %5u\n"
  305.         "Blockmap: %5u\n"
  306.         "          %u x %u\n\n"
  307.         "Near mem: %u\n"
  308.         "^   (%u/%u)\n"
  309.         " Far mem: %lu\n"
  310.         "^   (%lu/%lu)\n",
  311.         t_size, (ulong) t_max * sizeof(struct t_struct),
  312.         l_size, (ulong) l_max * sizeof(struct l_struct),
  313.         s_size, (ulong) s_max * sizeof(struct s_struct),
  314.         v_size, (ulong) v_max * sizeof(struct v_struct), max_vertex,
  315.         seg_size, (ulong) seg_max * sizeof(struct seg_struct),
  316.         ss_size, (ulong) ss_max * sizeof(struct ss_struct),
  317.         n_size, (ulong) n_max * sizeof(struct n_struct),
  318.         sec_size, (ulong) sec_max * sizeof(struct sec_struct),
  319.         r_size, b_size, blockmap->xsize, blockmap->ysize,
  320.         freesize, largest, core,
  321.         farfreesize, farlargest, farcore);
  322.  
  323.     window_text(msg, 1);
  324.     while (!mouse_check())
  325.         if (keypress)
  326.             break;
  327.  
  328.     return;
  329. }
  330.  
  331. void heaperr(char *func, char *name)
  332. {
  333.     mem_log("heap is corrupt (%s%s)", func, name);
  334.     fatal_error("Heap is corrupt!!!");
  335. }
  336.  
  337. void farheaperr(char *func, char *name)
  338. {
  339.     mem_log("farheap is corrupt (%s%s)", func, name);
  340.     fatal_error("FarHeap is corrupt!!!");
  341. }
  342.  
  343. void fatal_mem_error(char *name, uint size)
  344. {
  345.     struct heapinfo info;
  346.     uint freesize, largest;
  347.  
  348.     if (heapcheck() == -1)
  349.         heaperr("fatal_mem_error/", name);
  350.  
  351.     freesize = largest = core = coreleft();
  352.     info.ptr = 0;
  353.     while (heapwalk(&info) == 2)
  354.     {
  355.         if (!info.in_use)
  356.         {
  357.             freesize += info.size;
  358.             if (info.size > largest)
  359.                 largest = info.size;
  360.         }
  361.     }
  362.  
  363.     mem_log("out of mem for %s(%u)\n"
  364.         "mem=%u %u %u", name, size, freesize, largest, core);
  365.  
  366.     info.ptr = 0;
  367.     mem_log("   Size   Status\n   ----   ------");
  368.     while(heapwalk(&info) == _HEAPOK)
  369.     {
  370.         sprintf(path, "%7u    %s", info.size, info.in_use ? "used" : "free");
  371.         mem_log(path);
  372.     }
  373.  
  374.     fatal_error("Out of near memory!\n"
  375.         "Could not allocate %u bytes for: %s\n"
  376.         "%u free bytes of memory left\n"
  377.         "Largest free block is %u bytes in size\n"
  378.         "Coreleft = %u bytes",
  379.         size, name, freesize, largest, core);
  380. }
  381.  
  382. void fatal_farmem_error(char *name, ulong size)
  383. {
  384.     struct farheapinfo info;
  385.     ulong freesize, largest;
  386.  
  387.     if (farheapcheck() == -1)
  388.         farheaperr("fatal_farmem_error/", name);
  389.  
  390.     freesize = largest = farcore = farcoreleft();
  391.     info.ptr = 0L;
  392.     while (farheapwalk(&info) == 2)
  393.     {
  394.         if (!info.in_use)
  395.         {
  396.             freesize += info.size;
  397.             if (info.size > largest)
  398.                 largest = info.size;
  399.         }
  400.     }
  401.  
  402.     mem_log("out of farmem for %s(%lu)\n"
  403.         "mem=%lu %lu %lu", name, size, freesize, largest, farcore);
  404.  
  405.     fatal_error("Out of far memory!\n"
  406.         "Could not allocate %lu bytes for: %s\n"
  407.         "%lu free bytes of memory left\n"
  408.         "Largest free block is %lu bytes in size\n"
  409.         "Farcoreleft = %lu bytes",
  410.         size, name, freesize, largest, farcore);
  411. }
  412.