home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Memory Info.
- Message-ID: <3900@dozo.and.nl>
- Date: 21 Nov 92 12:54:31 GMT
- References: <1992Nov20.130913.20148@sifon.cc.mcgill.ca>
- Organization: AND Software BV Rotterdam
- Lines: 92
-
- In article <1992Nov20.130913.20148@sifon.cc.mcgill.ca> vugranam@pike.ee.mcgill.ca (Vugranam Chakravarthy Sreedhar) writes:
- |
- |Given a pointer to a object that has been previously allocated using
- |one of the allocation routine, how can I get the
- |size of the object.
-
- If you want to do that in a portable way, I have to disappoint you: there's
- no way to determine the size of an allocated object. but read on ...
-
- |char *c;
- |long x ;
- |
- |c = (char *) malloc (100) ;
- |x = mem_size(c) ; /* x is block size of c, which in this case is 100 */
- |
- |I want to use this to keep track of memory statistics. Now what I do is
- |to pass the size to my memory routines. Like
- |
- |int *mymalloc(size) ; /* this is easy */
- |int myfree(char *, long size) /* I don't like this !!*/
-
- Two solutions come to mind:
-
- 1. Use an array of the followingt objects:
-
- typedef struct {
- void *object;
- size_t size;
- } object_t;
-
- and write your own my_malloc and my_free routines. my_malloc has to
- find an empty slot in that array and store the pointer and the size
- of the allocated object into that slot. my_free has to find the
- specified slot and mark it as free again. The bad thing about this
- solution is the fixed size array. You cannot allocated more objects
- than there are slots in the array, without freeing at least one of
- the other objects before. reallocating the array of slots seems a
- bit clumsy to me, but it can be done.
-
- 2. Allocate a bit more memory than was requested and stick the size
- into the additionally allocated bytes. my_malloc and my_free would
- look something like this:
-
- void *my_malloc(size_t size) {
-
- size_t *object;
-
- size+= SLOTSIZE; /* make room for size */
-
- if (object= malloc(size)) {
- *object= size; /* register the size */
- ((char*)object)+= SLOTSIZE; /* step over size slot */
- }
- return (void*) object;
- }
-
- void my_free(void *object) {
-
- if (object) {
- ((char*)object)-= SLOTSIZE; /* object points to size */
- free(object);
- }
- }
-
- SLOTSIZE is a manifest constant that has to obey to the following rules:
-
- - SLOTSIZE >= sizeof(size_t)
-
- - SLOTSIZE is the smallest multiple of the most stringent alignment
- size needed on your machine, e.g. on a SPARC the alignment of a double
- is 8 bytes (the most stringent alignement), where the alignment of a
- size_t is 4 bytes, so SLOTSIZE would be 8 in this situation.
-
- Your wanted function mem_size would look something like this:
-
- size_t mem_size(void *object) {
-
- if (object)
- return *((size_t*)((char*)object)-SLOTSIZE);
- else
- return 0; /* or whatever */
- }
-
- All this casting and byte fiddling works just because malloc always
- returns a pointer to a chunk of memory that is always suitably aligned
- for all types of objects.
-
- I hope this helps a bit.
-
- kind regards,
-
- Jos aka jos@and.nl
-