home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / rpl / part02 / debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-10  |  2.0 KB  |  121 lines

  1. /****************************************************************
  2.  
  3. Module:
  4.     Debug
  5.  
  6. Description:
  7.     Contains variables of all types to enable casts in the debugger.
  8.     Contains functions used for debugging.
  9.  
  10.     WARNING! This module is very compiler-dependent. If you are not
  11.         using Turbo C 2.0 on an IBM PC-compatible, you will have
  12.         to change it or simply throw it out.
  13.  
  14. Modification history:
  15.  
  16.     0.0    hjp    89-07-08
  17.  
  18.         initial version
  19.  
  20.     0.1    hjp    89-07-25
  21.  
  22.         #include <stdio.h> added to supress warnings
  23.  
  24.     0.2    hjp    89-08-28
  25.  
  26.         stringobjs added
  27.  
  28.     0.3    hjp    89-08-28
  29.  
  30.         opobjs added
  31.  
  32.     0.4    hjp    89-09-04
  33.  
  34.         memmap added.
  35.         clearmem added.
  36.  
  37.     0.5    clearmem improved. 
  38.         uses variable blocksize now.
  39.  
  40. ****************************************************************/
  41.  
  42. #include <alloc.h>
  43. #include <mem.h>
  44. #include <stdio.h>
  45.  
  46. #include "rpl.h"
  47. #include "errors.h"
  48.  
  49. nameobj    no;
  50. nameobj    * nop;
  51. stringobj    so;
  52. stringobj    * sop;
  53. opobj    oo;
  54. opobj    * oop;
  55. realobj    ro;
  56. realobj    * rop;
  57.  
  58. void * debugmalloc (unsigned n)
  59. {
  60.     void * p = malloc (n);
  61.  
  62.     printf ("allocated %u bytes at %p\n", n, p);
  63.     return p;
  64. }
  65.  
  66. void    debugfree (void * p)
  67. {
  68.     printf ("freeing %lu bytes at %p\n", ((long *) p)[-2] - 1, p);
  69.  
  70.     free (p);
  71. }
  72.  
  73. void * debugrealloc (void * p, unsigned n)
  74. {
  75.     void * p1;
  76.  
  77.     printf ("reallocating %lu bytes at %p ", ((long *) p)[-2] - 1, p);
  78.     p1 = realloc (p, n);
  79.     printf ("to %u bytes at %p\n", n, p1);
  80.     return p1;
  81. }
  82.  
  83. void memmap (void)
  84. {
  85.     uint    seg;
  86.     uint    * p;
  87.     char    * s;
  88.  
  89.     for (seg = _SS + (_stklen >> 4); seg < 0xA000; seg ++) {
  90.  
  91.         p = (uint *) (((long) seg << 16) + 0x0008);
  92.         if (s = id2str (* p)) {
  93.             printf ("%x: %s (link = %u, size = %u)\n", seg, s, p [1], p [2]);
  94.         }
  95.     }
  96. }
  97.  
  98.  
  99. /*
  100.     clearmem
  101.  
  102.     Allocate as much memory as possible, clear it, and release it again.
  103. */
  104.  
  105. void clearmem (uint chunk)
  106. {
  107.     void * p;
  108.  
  109.     if (chunk >= 16) {
  110.         if (p = malloc (chunk - 8)) {
  111.             printf ("clearmem: %d bytes at %p\n", chunk - 8, p);
  112.             memset (p, 0, chunk - 8);
  113.             clearmem (chunk);
  114.             free (p);
  115.         } else {
  116.             chunk >>= 1;
  117.             clearmem (chunk);
  118.         }
  119.     }
  120. }
  121.