home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17078 < prev    next >
Encoding:
Internet Message Format  |  1992-11-24  |  2.3 KB

  1. Path: sparky!uunet!think.com!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory Info.
  5. Date: 24 Nov 1992 01:57:52 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 55
  8. Message-ID: <27629@dog.ee.lbl.gov>
  9. References: <1992Nov20.130913.20148@sifon.cc.mcgill.ca> <3900@dozo.and.nl>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In article <3900@dozo.and.nl> jos@and.nl (Jos Horsmeier) supplies code
  14. to remember allocation sizes.  The code is mostly right, but makes use
  15. of one ``gcc feature'' (or other system-specific feature):
  16.  
  17. >        ((char*)object)+= SLOTSIZE;    /* step over size slot */
  18.  
  19. This should be:
  20.  
  21.     object = (size_t *)((char *)object + SLOTSIZE);
  22.  
  23. and:
  24.  
  25. >        ((char*)object)-= SLOTSIZE;    /* object points to size */
  26.  
  27. this should similarly be:
  28.  
  29.  
  30.     object = (void *)((char *)object - SLOTSIZE);
  31.  
  32. The outer cast (to (void *)) can be omitted in this case.
  33.  
  34. >SLOTSIZE is a manifest constant that has to obey to the following rules:
  35. >
  36. >- SLOTSIZE >= sizeof(size_t)
  37. >
  38. >- SLOTSIZE is the smallest multiple of the most stringent alignment
  39. >  size needed on your machine, e.g. on a SPARC the alignment of a double
  40. >  is 8 bytes (the most stringent alignement), where the alignment of a
  41. >  size_t is 4 bytes, so SLOTSIZE would be 8 in this situation. 
  42.  
  43. Note that there is no portable way to discover this number.  If you
  44. know what kinds of objects you will allocate, you can discover the
  45. alignment needed for those using a union; but otherwise---and usually
  46. even when this is so---the only thing to do is include some
  47. machine-dependent information.  On 4.4BSD-based systems, you can
  48.  
  49.     #include <machine/param.h>
  50.  
  51. to get two macros called ALIGN and ALIGNBYTES:
  52.  
  53. /*
  54.  * Round p (pointer or byte index) up to a correctly-aligned value for
  55.  * the machine's strictest data type.  The result is u_int and must be
  56.  * cast to any desired pointer type.
  57.  */
  58. #define ALIGNBYTES    7
  59. #define ALIGN(p)    (((u_int)(p) + ALIGNBYTES) & ~ALIGNBYTES)
  60.  
  61. (Note that the result type of ALIGN() is in fact machine-dependent; as
  62. a user you are really only supposed to know that, while you can feed it
  63. either pointers or integers, you have to cast it if you want a pointer
  64. back.)
  65. -- 
  66. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  67. Berkeley, CA        Domain:    torek@ee.lbl.gov
  68.