home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: comp.lang.c
- Subject: Re: Memory Info.
- Date: 24 Nov 1992 01:57:52 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley
- Lines: 55
- Message-ID: <27629@dog.ee.lbl.gov>
- References: <1992Nov20.130913.20148@sifon.cc.mcgill.ca> <3900@dozo.and.nl>
- Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
- NNTP-Posting-Host: 128.3.112.15
-
- In article <3900@dozo.and.nl> jos@and.nl (Jos Horsmeier) supplies code
- to remember allocation sizes. The code is mostly right, but makes use
- of one ``gcc feature'' (or other system-specific feature):
-
- > ((char*)object)+= SLOTSIZE; /* step over size slot */
-
- This should be:
-
- object = (size_t *)((char *)object + SLOTSIZE);
-
- and:
-
- > ((char*)object)-= SLOTSIZE; /* object points to size */
-
- this should similarly be:
-
-
- object = (void *)((char *)object - SLOTSIZE);
-
- The outer cast (to (void *)) can be omitted in this case.
-
- >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.
-
- Note that there is no portable way to discover this number. If you
- know what kinds of objects you will allocate, you can discover the
- alignment needed for those using a union; but otherwise---and usually
- even when this is so---the only thing to do is include some
- machine-dependent information. On 4.4BSD-based systems, you can
-
- #include <machine/param.h>
-
- to get two macros called ALIGN and ALIGNBYTES:
-
- /*
- * Round p (pointer or byte index) up to a correctly-aligned value for
- * the machine's strictest data type. The result is u_int and must be
- * cast to any desired pointer type.
- */
- #define ALIGNBYTES 7
- #define ALIGN(p) (((u_int)(p) + ALIGNBYTES) & ~ALIGNBYTES)
-
- (Note that the result type of ALIGN() is in fact machine-dependent; as
- a user you are really only supposed to know that, while you can feed it
- either pointers or integers, you have to cast it if you want a pointer
- back.)
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-