home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / JMALLOC.C < prev    next >
C/C++ Source or Header  |  1992-07-03  |  8KB  |  208 lines

  1. /*-------------------------[ jmalloc.c ]------------------------*/
  2. /*                 drop-in for malloc with diags                */
  3. /*--------------------------------------------------------------*/
  4. /* Released to the public domain, 1992: Jeff Dunlop             */
  5. /*--------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------*/
  8. /*-----------------------[ header files ]-----------------------*/
  9. /*--------------------------------------------------------------*/
  10.  
  11. #include <alloc.h>
  12. #include <errno.h>
  13. #include <mem.h>
  14. #include <stdio.h>
  15. #include "dbug.h"
  16. #include "jmalloc.h"
  17.  
  18. /*--------------------------------------------------------------*/
  19. /*---------------------[ global variables ]---------------------*/
  20. /*--------------------------------------------------------------*/
  21.  
  22. MLINK *MalTop;                        /* top of allocation chain */
  23.  
  24. /*--------------------------[ JMalloc ]-------------------------*/
  25. /*            Memory allocator with diagnostics                 */
  26. /*--------------------------------------------------------------*/
  27. /* input:                                                       */
  28. /*      Size = number of bytes to allocate                      */
  29. /* local:                                                       */
  30. /*      CurMal = pointer current mem struct                     */
  31. /*      PrevMal = pointer to previous mem struct                */
  32. /*      AllocAddr = pointer to allocated ram                    */
  33. /* return:                                                      */
  34. /*      address of allocated ram, or NULL on error              */
  35. /*--------------------------------------------------------------*/
  36.  
  37. void *JMalloc(unsigned Size)
  38. {
  39.     /* 1. Allocate the memory */
  40.     /* 2. Add to allocation chain */
  41.     /* 3. Fill with non-null */
  42.  
  43.     MLINK *CurMal = MalTop,
  44.           *PrevMal;
  45.  
  46.     void *AllocAddr;
  47.  
  48.     DBUG_ENTER("JMalloc");
  49.  
  50.     /* Allocate the memory + 1 (for check byte) */
  51.  
  52.     if ( (AllocAddr = malloc(Size + 1)) == NULL )
  53.         DBUG_PRINT("alloc", ("Allocation failed"));
  54.  
  55.     /* Add to the allocation chain */
  56.  
  57.     PrevMal = CurMal;
  58.     while ( CurMal != NULL )
  59.     {
  60.         PrevMal = CurMal;
  61.         CurMal = CurMal->NextLink;
  62.     }
  63.     if ( (CurMal = malloc(sizeof *CurMal)) == NULL )
  64.         DBUG_PRINT("alloc", ("Allocation failed"));
  65.  
  66.     /* Deal with bootstrap */
  67.     if ( PrevMal == NULL )
  68.         MalTop = CurMal;
  69.     else
  70.         PrevMal->NextLink = CurMal;
  71.  
  72.     /* initialize */
  73.     CurMal->NextLink = NULL;
  74.     CurMal->MAddr = AllocAddr;
  75.     CurMal->MSize = Size;
  76.  
  77.     /* fill with dirty char */
  78.     memset(AllocAddr, 0x01, Size + 1);
  79.     DBUG_RETURN(AllocAddr);
  80. }
  81.  
  82. /*--------------------------[ JCalloc ]-------------------------*/
  83. /*            Memory allocator with diagnostics                 */
  84. /*--------------------------------------------------------------*/
  85. /* method:                                                      */
  86. /*      - Allocate the memory via JMalloc                       */
  87. /*      - Fill with null                                        */
  88. /*      - Add check-byte                                        */
  89. /* input:                                                       */
  90. /*      Size = number of bytes to allocate                      */
  91. /* local:                                                       */
  92. /*      CurCal = pointer current mem struct                     */
  93. /*      PrevCal = pointer to previous mem struct                */
  94. /*      AllocAddr = pointer to allocated ram                    */
  95. /* return:                                                      */
  96. /*      address of allocated ram, or NULL on error              */
  97. /*--------------------------------------------------------------*/
  98.  
  99. void *JCalloc(unsigned Size, unsigned SizEach)
  100. {
  101.     char *AllocAddr;
  102.  
  103.     DBUG_ENTER("JCalloc");
  104.     /* Allocate the memory + 1 (for check byte) */
  105.  
  106.     /* Do not piss over NULL return - JMalloc handled that */
  107.     if ( (AllocAddr = JMalloc(Size * SizEach)) != NULL )
  108.     {
  109.         /* Prep allocated ram */
  110.         memset(AllocAddr, 0, Size * SizEach);
  111.         AllocAddr[Size * SizEach] = 0x01;
  112.     }
  113.  
  114.     DBUG_RETURN(AllocAddr);
  115. }
  116.  
  117. /*------------------------------[ j_free ]----------------------*/
  118. /*           Memory deallocator with diagnostics                */
  119. /*--------------------------------------------------------------*/
  120. /* input:                                                       */
  121. /*      AllocAddr = pointer to ram to deallocate                */
  122. /* local:                                                       */
  123. /*      CurMal = pointer to current mem struct                  */
  124. /*      PrevMal = pointer to prev mem struct                    */
  125. /* note:                                                        */
  126. /*      This function is designed to be implemented with the    */
  127. /*      macro JFree                                             */
  128. /*--------------------------------------------------------------*/
  129.  
  130. void j_free(void *AllocAddr, char *file, int line)
  131. {
  132.     /* 1. Find the block in the alloc list */
  133.     /* 2. Check for check-byte overwrite   */
  134.     /* 3. Remove allocation record         */
  135.     /* 3. Free the ram                     */
  136.  
  137.     MLINK *CurMal = MalTop,
  138.           *PrevMal;
  139.  
  140.     DBUG_ENTER("j_free");
  141.  
  142.     /* Find the block in the alloc list */
  143.     while ( CurMal->MAddr != AllocAddr && CurMal != NULL )
  144.     {
  145.         PrevMal = CurMal;
  146.         CurMal = CurMal->NextLink;
  147.     }
  148.     if ( CurMal == NULL )
  149.     {
  150.         DBUG_PRINT("alloc", ("Free attempt failed %s %d", file, line));
  151.     }
  152.     else
  153.     {
  154.         /* Check for check-byte overwrite */
  155.         if ( CurMal->MAddr[CurMal->MSize] != 0x01 )
  156.             DBUG_PRINT("alloc", ("Memory overrun detected"));
  157.  
  158.         /* dirty the deallocated ram - if it's still being used, it */
  159.         /* should become fairly obvious                             */
  160.         memset(AllocAddr, 0x02, CurMal->MSize + 1);
  161.  
  162.         /* Remove allocation record */
  163.         PrevMal->NextLink = CurMal->NextLink;
  164.         free(CurMal);
  165.     }
  166.  
  167.     /* Free the ram regardless of validity */
  168.     free(AllocAddr);
  169.     DBUG_VOID_RETURN;
  170. }
  171.  
  172. /*-------------------------[ JMemcheck ]------------------------*/
  173. /*                Verify memory chain integrity                 */
  174. /*--------------------------------------------------------------*/
  175. /* local:                                                       */
  176. /*      i = link number                                         */
  177. /*      CurMal = link pointer                                   */
  178. /*      status = current error condition                        */
  179. /* return:                                                      */
  180. /*      -1 = error,                                             */
  181. /*      0 = no error detected                                   */
  182. /* warning:                                                     */
  183. /*      I haven't had the pleasure of testing this              */
  184. /*--------------------------------------------------------------*/
  185.  
  186. int JMemcheck(void)
  187. {
  188.     int i = 0,
  189.         status = 0;
  190.  
  191.     MLINK *CurMal = MalTop;
  192.  
  193.     DBUG_ENTER("JMemcheck");
  194.  
  195.     /* Walk the alloc list */
  196.     while ( CurMal != NULL )
  197.     {
  198.         i++;
  199.         if ( CurMal->MAddr[CurMal->MSize] != 0x01 )
  200.         {
  201.             DBUG_PRINT("alloc", ("Memory overrun detected in link %d", i));
  202.             status = -1;
  203.         }
  204.         CurMal = CurMal->NextLink;
  205.     }
  206.     DBUG_RETURN(status);
  207. }
  208.