home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / g / help / 1627 < prev    next >
Encoding:
Text File  |  1993-01-04  |  5.2 KB  |  114 lines

  1. Newsgroups: gnu.g++.help
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!wupost!cs.uiuc.edu!sparc0b!wang
  3. From: wang@cs.uiuc.edu (Eric Wang)
  4. Subject: Can I tell g++ NOT to assume that doubles are 8-byte aligned?
  5. Message-ID: <C0CKKy.D9A@cs.uiuc.edu>
  6. Sender: news@cs.uiuc.edu
  7. Reply-To: wang@cs.uiuc.edu
  8. Organization: University of Illinois at Urbana-Champaign
  9. Distribution: usa
  10. Date: Mon, 4 Jan 1993 20:50:58 GMT
  11. Lines: 101
  12.  
  13. Sparcstation 2
  14. SunOS 4.1.1
  15. gcc & libg++ 2.2.2
  16. Problem summary: an app's own version of malloc() is 4-byte aligned, but
  17.     when the allocated struct starts with a double, g++'s generated code
  18.     assumes that it's 8-byte aligned, and uses double-word instructions
  19.     to access this pointer, which causes a bus error.
  20.  
  21. I'm trying to use g++ to customize an app by writing extensions that
  22. will be linked into the app's libraries.  Among other things, this app's
  23. libraries defines its own memory-allocation functions, including
  24. malloc(), free(), etc, and these apparently get linked into my app
  25. before either the system version or libg++'s version.  The app's vendor
  26. distributes only the libraries, so there's no way for me to hack their
  27. source and recompile it.
  28.  
  29. I must perforce do this work on a Sparcstation 2 (running SunOS 4.1.1),
  30. which is a Sun-4.  The Sun-4 programmers' references says that C (and
  31. hence C++) doubles must be 8-byte aligned, which is a change from Sun-3
  32. and earlier architectures, which require only 4-byte alignment.  They
  33. also say that the system's version of malloc() returns pointers that are
  34. aligned to point to any kind of data, i.e. they're aligned to 8 bytes.
  35. However, my tests don't bear this out; Sun's malloc() still is 4-byte
  36. aligned, and if you malloc() a (double *) that is 4-byte aligned, the
  37. correct (but less efficient) single-word code is generated to operate on
  38. this pointer.  Apparently, forcing doubles to be 8-byte aligned is a
  39. compiler switch, not the default compiler setting.
  40.  
  41. The problem is that this app's own versions of the mem-alloc routines
  42. seems not to take Sun-4 architecture into account, so it remains only
  43. 4-byte aligned, regardless of the compiler switches (since it's already
  44. been compiled without the switch).  g++, on the other hand, seems to
  45. assume that doubles, and (double *)s, are 8-byte aligned; when I compile
  46. it without any funky compiler switches, it generates code that does
  47. double-word operations.  Hence, using operator new to allocate an array
  48. of (double *)s, or an array of structs or objects whose first field is a
  49. double, gives me a pointer which is only 4-byte aligned, and attempting
  50. to access in any way the double at this pointer results in a Bus Error,
  51. since the pointer is not properly aligned to support double-word
  52. accesses.  (BTW, g++'s code generation seems to contradict the Info
  53. entry gcc/Invoking GCC/Submodel Options/SPARC Options/`-mforce-align',
  54. which says "Force doubles to be 8-byte aligned, and use dword accesses",
  55. and implies "I am NOT the default action, since you have to specify me
  56. explicitly to get this effect".  Nonetheless, it does seem to do this by
  57. default.)
  58.  
  59. Finally, due to limited disk space and my limited user privileges, I am
  60. not in a position to recompile gcc.  I need a fix that doesn't requiring
  61. building a new gcc.
  62.  
  63. I've identified some potential solutions:
  64.  
  65. 1.  Specify -mno-force-align to g++.
  66.  
  67.     Unfortunately, this doesn't seem to be a valid switch; g++ complains
  68.     about it.  Is there any other way AT COMPILE-TIME to make g++ on a
  69.     Sun-4 NOT assume that (double *)s are 8-byte aligned?
  70.  
  71. 2.  Force g++ to link in the libg++ version of malloc, free, and
  72.     realloc.
  73.  
  74.     I'm not sure if my copy of libg++ even has these functions.
  75.     Whatever version of free() I get does NOT zero out the free'd block
  76.     of memory, contrary to the libg++/New Info entry.  How can I find
  77.     out for sure?  (Again, I don't have the source code for libg++, I
  78.     didn't build it, and I probably couldn't get the resources to build
  79.     it.)
  80.  
  81. 2.  Do all accesses to these data structures in C instead of C++, and
  82.     compile them with Sun cc instead of gcc/g++.
  83.  
  84.     But this essentially forces me to abandon g++ altogether, as these
  85.     data structures are prevalent throughout my code.  I really would
  86.     prefer to stay with C++.
  87.  
  88. 3.  Get the app's vendor to fix its mem-alloc routines.
  89.  
  90.     I am pursuing this separately, but not holding my breath.
  91.  
  92. 4.  Write my own wrappers around the app's malloc() and free() that
  93.     insure 8-byte alignment.
  94.  
  95.     Icky, but probably the sol'n with the best ease-to-time trade-off.
  96.     Note that I can't just overload new() and delete() for this one
  97.     struct, since I need to new() an array of these structs, and
  98.     new()ing an array always uses the global ::new(), not the
  99.     class-specific new().  (Stroustrup r.5.3.3)
  100.  
  101. The bottom line is that this "bug" isn't necessarily an error in either
  102. the app or g++, since both work fine when they stand alone; rather, it's
  103. a case of mismatched expectactions between the two.  If there's an easy
  104. way to have g++ "bridge the gap" and allow single-word access to C/C++
  105. doubles, I'd like to know about it.  (The real fix would be to have the
  106. app bridge the gap from its end, but I expect this will take too long to
  107. be practical.)
  108.  
  109. Any help or hints would be appreciated.
  110.  
  111. Eric Wang
  112. wang@cs.uiuc.edu
  113.  
  114.