Re: Memory Allocation Tracking

From: d. hall (dhall@APK.NET)
Date: 01/09/98


: thus on Wed, 7 Jan 1998 21:29:06 -0900, Gary virtually wrote:

> I've reached the point where it's time to start adding
> statistics to the mud to track how resources are being
> used.

> Something like so.

> #define CREATE(result, type, number)  {\
>    if (!((result) = (type *) calloc ((number), sizeof(type)))) {\
>             log_info("malloc failure");\
>                 abort();\
>    }\
>    statistics.mem_alloc+=sizeof(type)*number; }

> #define FREE(item) { statistics.mem_alloc-=sizeof(item); free(item); }

Make function wrappers for malloc and free.

inline static void memchk (void *mem)
{
   if (mem == NULL) {
      fputs ("SYSERR: out of memory\n", stderr);
      exit (-1);
   }
}

void *my_malloc (size_t size)
{
   void *mem;

   mem = malloc (size);
   memchk (mem);

   statistics.mem_alloc += size;

   return mem;
}

void *my_calloc (site_t nelem, size_t elsize)
{
   void *mem;
   size_t size = nelem * elsize;

   mem = malloc (size);
   memchk (mem);
   memset (mem, 0, (size);

   statistics.mem_alloc += size;

   return mem;
}

void *my_realloc (void *mem, size_t size)
{
   statistics.mem_alloc -= sizeof (*mem);
   mem = realloc (mem, size);
   memchk (mem);
   statistics.mem_alloc += sizeof (*mem);

   return mem;
}

void my_free (void *mem)
{
#ifdef FREE_IS_BROKEN
   if (mem != NULL)
#endif
      free (mem);

   statistics.mem_alloc -= sizeof (*mem);
}

> As you will no doubt see, the free call is not returning the
> correct amount of memory that will be freed. I deduce this from
> two clues: 1) PS and the memory alloc stat get farther apart in
> value as time passes  2) Creating 200 objs in a room and then purging
> them does not decrease the amount of memory allocated. In fact, it goes
> up..

sizeof (mem) based upon architecture will be constants.  On linux it's
always 32 bits, size = 4;

> In particular I can live with REALLOC not being supported.

> How would you do it?

See above.

d.


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST