home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / Mem_Size.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-22  |  2.2 KB  |  79 lines

  1. /* 
  2.  * Mem_Size.c --
  3.  *
  4.  *    Source code for the "Mem_Size" library procedure.  See memInt.h
  5.  *    for overall information about how the allocator works.
  6.  *
  7.  * Copyright 1985, 1988 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/Mem_Size.c,v 1.3 88/07/25 11:10:55 ouster Exp $ SPRITE (Berkeley)";
  19. #endif not lint
  20.  
  21. #include <stdio.h>
  22. #include "memInt.h"
  23.  
  24.  
  25. /*
  26.  * ----------------------------------------------------------------------------
  27.  *
  28.  * Mem_Size --
  29.  *
  30.  *      Return the size of a previously-allocated storage block.
  31.  *
  32.  * Results:
  33.  *      The return value is the size of *blockPtr, in bytes.  This is
  34.  *    the total usable size of the block.  It may be slightly greater
  35.  *    than the size actually requested from malloc, since the size
  36.  *    might have been rounded up to a convenient boundary.
  37.  *
  38.  * Side effects:
  39.  *    None.
  40.  *
  41.  * ----------------------------------------------------------------------------
  42.  */
  43.  
  44. ENTRY int
  45. Mem_Size(blockPtr)
  46.     Address blockPtr;    /* Pointer to storage block.  Must have been the
  47.              * return value from malloc at some previous time. */
  48. {
  49.     int admin;
  50.  
  51.     LOCK_MONITOR;
  52.  
  53.     if (!memInitialized) {
  54.         panic("Mem_Size: allocator not initialized!\n");
  55.     UNLOCK_MONITOR;
  56.     return(0);            /* should never get here */
  57.     }
  58.  
  59.     /* 
  60.      *  Make sure that this block bears some resemblance to a
  61.      *  well-formed storage block.
  62.      */
  63.     
  64.     blockPtr -= sizeof(AdminInfo);
  65.     admin = GET_ADMIN(blockPtr);
  66.     if (!IS_IN_USE(admin)) {
  67.     if (IS_DUMMY(admin)) {
  68.         panic("Mem_Size: storage block is corrupted\n");
  69.     } else {
  70.         panic("Mem_Size: storage block is free\n");
  71.     }
  72.     UNLOCK_MONITOR;
  73.     return(0);            /* (should never get here) */
  74.     }
  75.  
  76.     UNLOCK_MONITOR;
  77.     return(SIZE(admin) - sizeof(AdminInfo));
  78. }
  79.