Re: Memory Allocation Tracking

From: Erwin S. Andreasen (erwin@PIP.DKNET.DK)
Date: 01/08/98


On Wed, 7 Jan 1998, Gary Barnett wrote:


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

Syntax seems a bit clumsy. Check http://www.toof.net/~spectrum, and
download SAM to see what sort of memory tracking is offered there. Some
output from the c++ port of SAM:

>> mem alloc
Structure          |Size |In use        |Prealloc      |Freelist      |
                   |     |count    bytes|count    bytes|count    bytes|
accounts           |   60|   13      780|    0        0|    1       60|
account characters |   32|   65     2080|    0        0|   16      512|
affects            |   20| 4449    88980|     N/A      |    0        0|
aliases            |   12|  275     3300|    0        0|  408     4896|
auction objects    |   52|  118     6136|    0        0|    0        0|
arena bets         |   16|    0        0|    0        0|    0        0|
bounties           |   40|   44     1760|    0        0|    0        0|
areas              |  160|  102    16320|    0        0|    0        0|
bans               |   28|   25      700|    0        0|    0        0|
blacklists         |    8|  551     4408|    0        0|    0        0|
CL heads           |   40|   44     1760|    0        0|    0        0|
CL nodes           |   12|  575     6900|    0        0|    0        0|
[...]



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

in a situation like:

char *something;

CREATE(something, char, 100);

then FREE(something);

sizeof(something) will be sizeof(char*). Which is 4. sizeof is a
compile-time thing. There's no portable way to ask malloc how big this
block memory is.

> 1) What is the correct method of determining the amount of memory
> a particular data structure is using? I don't want to write a routine
> to handle every darn struct in the mud and all the possiblities. I'd
> rather spend my time creating a new feature or finishing a skill.

> How would you do it?

See SAM :)

You could also, store your own memory info:


void *alloc_mem(int size)
{
        int *p = calloc(size+sizeof(int),1);
        *p = size;
        allocated_memory += size;
        return p+1;
}

void free_mem(void *ptr)
{
        allocated_memory -= *(int*)ptr;
        free(ptr);
}

To track stuff better though, you should probably employ totally your own
memory management. See SAM :)

>> mem caller
Allocations:
show_string:2239:     1 allocs of       1 bytes
                      0 blocks =        0 bytes in use [1]
save_text_entry:819:  1 allocs of    1027 bytes
                      1 allocs of    1119 bytes
                      1 allocs of    1127 bytes
                      0 blocks =        0 bytes in use [data.dsize]

[...]



 =============================================================================
Erwin Andreasen   Herlev, Denmark <erwin@pip.dknet.dk>  UNIX System Programmer
<URL:http://www.abandoned.org/drylock/>     <*>         (not speaking for) DDE
 =============================================================================


     +------------------------------------------------------------+
     | 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