home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE50.TAR / mbase / dox / struct.dox < prev    next >
Encoding:
Text File  |  1992-10-02  |  2.7 KB  |  63 lines

  1. C-Structure Interface                                             MetalBase 5.0
  2. -------------------------------------------------------------------------------
  3.  
  4. MetalBase 4.0 and up read and write database information directly from C
  5. structures, using the following assumptions:
  6.  
  7.             sizeof(long)   = sizeof(ulong)  = 4 bytes
  8.             sizeof(short)  = sizeof(ushort) = 2 bytes
  9.             sizeof(float)  = 4 bytes
  10.             sizeof(double) = 8 bytes
  11.  
  12. If any of the above are not valid for your system, MetalBase 5.0 will not work
  13. properly; however, this is the defacto standard, and should produce little
  14. difficulty in porting.  So far, I have yet to receive even one complaint that
  15. this assumption has limited a user's use of this package (one Mac user had
  16. to change the defaults for his compiler from a 12-byte double to an 8-byte,
  17. but it worked fine after that).
  18.  
  19. The main problem between machines is therefore not type sizes, but byte-
  20. aligning in C structures.  At first, one would expect that the following:
  21.    struct
  22.     { char   A[3];
  23.       long   B;
  24.       char   C[5];
  25.       char   D[5];
  26.       double E;
  27.       char   F[2];
  28.     }
  29. would appear like this in memory:
  30.       AAABBBBCCCCCDDDDDEEEEEEEEFF
  31. Unfortunately, this is not the case for most systems.  On this machine
  32. (Sun4 SunOs/cc), structures are aligned comme ca:
  33.       AAAxBBBBCCCCCDDDDDxxxxxxEEEEEEEEFFxxxxxx
  34. While Xenix 2.3.2 would align them as:
  35.       AAAxBBBBCCCCCDDDDDxxEEEEEEEEFF
  36. Why?  Because, for the Sun, each field is aligned on 4- or 8- byte boundaries,
  37. depending on size; shorts, longs and floats to 4-byte boundaries, and doubles
  38. to 8-byte ones.  Essentially, the algorithm is:
  39.  
  40.       start_pos = &base_of_structure;
  41.       for (each field)
  42.        { if (type != char)
  43.             if (sizeof (type) < 8)  align_to_4_byte_boundary (start_pos);
  44.             else                    align_to_8_byte_boundary (start_pos);
  45.          add_field
  46.        }
  47.       align_to_8_byte_boundary
  48.  
  49. The last alignment is to obtain the structure size.
  50.  
  51. All this crap was discovered through trial and error (yick!)  If anyone
  52. encounters a structure which does not seem to meet the standards shown
  53. by struct.c, please contact me (virtual!richid@owlnet.rice.edu) and describe
  54. the structure and your machine type.  If you decide to make any necessary
  55. modifications yourself, you'll find the above algorithm (in essentially that
  56. form) in the routine _fill_info() [mbase.c]--but let me know if you get it
  57. working, so I can add machine-dependency to the code with #defines.
  58.  
  59. Currently, there are 4 different version supported, as shown by "make struct".
  60. In general, these conform to Sun, Coherent/Zortec, Xenix, and Mac/Microsoft C
  61. respectively (definitely not an exhaustive list of compatibility there).
  62.  
  63.