home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / eiffel / 1076 < prev    next >
Encoding:
Text File  |  1992-08-20  |  5.1 KB  |  124 lines

  1. Path: sparky!uunet!eiffel!eiffel.com
  2. From: ram@eiffel.com (Raphael Manfredi)
  3. Newsgroups: comp.lang.eiffel
  4. Subject: Re: CECIL and garbage collection
  5. Summary: New Eiffel-C interface scheme in Eiffel 3
  6. Keywords: memory management, Cecil, garbage collection
  7. Message-ID: <116@eiffel.eiffel.com>
  8. Date: 20 Aug 92 16:24:52 GMT
  9. References: <seanb.714234719@cs.man.ac.uk>
  10. Sender: ram@eiffel.com
  11. Organization: Interactive Software Engineering, Santa Barbara CA
  12. Lines: 110
  13.  
  14. Quoting seanb@cs.man.ac.uk (Sean Bechhofer) from comp.lang.eiffel:
  15. >        "What keeps the garbage collector from collecting Eiffel
  16. >        objects that are only referenced in C code? Nothing. If
  17. >        garbage collection is enabled, you must keep a reference
  18. >        on the Eiffel side to prevent an object from being collected."
  19. > [deletion]
  20. >
  21. >        o Is this "feature" likely to change in Eiffel 3?
  22.  
  23. Do you want a short answer or a long answer? Both? Very well, the short answer
  24. is YES. This "bug" has changed in Eiffel 3.
  25.  
  26. Although in your case the problem manifested itself with the use of Cecil,
  27. it is more general than that, as the quote from the Environment Manual which
  28. you reproduced above would tell us.
  29.  
  30. In Eiffel 3, whenever the Eiffel side calls an external C routine or whenever
  31. Cecil is requested an object creation, all you get is an EIF_OBJ pointer.
  32. If you wish to get access to the object, you must use the eif_access() macro.
  33. That will give you the physical address of the object, but this pointer is not
  34. to be kept around as the memory manager (aka GC) has entire liberty to move
  35. your objects to optimize the memory usage and reduce fragmentation for instance.
  36. So any access via eif_access() on an EIF_OBJ pointer is safe.
  37.  
  38. The compiler automatically generates code to protect your references when you
  39. are calling an external C routine. This protection is, by default, released
  40. when Eiffel regains control (but see below). Here is an example:
  41.  
  42.  --- Eiffel side:
  43.  
  44.     f (a: GENERAL): GENERAL is
  45.             -- Call to external `f'
  46.         external
  47.             "C"
  48.         end;
  49.     
  50.  --- C side:
  51.  
  52. #include <cecil.h>
  53.  
  54. char *f(x)
  55. EIF_OBJ x;
  56. {
  57.     char *pointer = eif_access(x);
  58.     EIF_TYPE_ID general_id;
  59.     EIF_OBJ new_object;
  60.  
  61.     /* It's safe to keep 'pointer' as long as no Eiffel calls are made, and
  62.      * no memory allocation is done.
  63.      */
  64.     
  65.     ...
  66.  
  67.     general_id = eif_type_id("GENERAL");
  68.     new_object = eif_create(general_id);
  69.  
  70.     some_function(eif_access(new_object), ...);
  71.  
  72.     /* Eiffel function was declared to return an object, not an EIF_OBJ! */
  73.  
  74.     return eif_access(new_object);
  75. }
  76.  
  77.  --- End of example
  78.  
  79. Now sometimes it is necessary to have a finer control over the memory. For
  80. instance, if Eiffel wishes to call a C library function bcopy(), this function
  81. expects real pointers, not EIF_OBJ pointers! So it is possible to direct the
  82. compiler not to protect references given to the external function by preceding
  83. the variable name with a '$'. The raw reference will then be given to the C.
  84. Should this function call an Eiffel routine or call malloc(), chaos is to be
  85. expected if you ever use the reference you originally got from Eiffel again.
  86.  
  87. If the C side wants to keep an EIF_OBJ pointer even after the return to Eiffel,
  88. it must issue the run-time call eif_adopt, which will return you a new EIF_OBJ
  89. pointer, owned only by the C side (the original EIF_OBJ will be released when
  90. the control flow returns to the Eiffel routine which issued the protection).
  91. As a side effect, you have the guarantee that this object will be always alive,
  92. even if all the references from Eiffel have been cut. Each time you want to
  93. access the object, you must still use eif_access().
  94.  
  95. When the C side does not have any more use for an "adopted" EIF_OBJ pointer,
  96. it must call the run-time routine eif_wean(). Any further use of EIF_OBJ is
  97. forbidden once eif_wean() has been called.
  98.  
  99. If you need to access a given object many times, it is possible to *freeze* its
  100. location in memory by calling eif_freeze(). This run-time call will return you
  101. a real (char *) reference which can be use freely by all the routines. The
  102. object will never move again. In fact, the memory management has not further
  103. control over this object. Even when it is dead, the GC will not collect it.
  104. Therefore, it is up to the programmer to call eif_unfreeze() to let the GC see
  105. the object again and allow it to move and/or be collected.
  106.  
  107. For more information, you should read "Eiffel: The Language", paragraph 24.7
  108. entitled "The Cecil Library".
  109.  
  110. To summarize, Eiffel 3 overcomes those memory peculiarities at the cost of a
  111. stronger discipline imposed to the programmer. The scheme is flexible enough
  112. to allow any kind of inter-connection between Eiffel and C. And this is not
  113. even ISE Eiffel-dependent since it is part of the Eiffel specifications which
  114. every Eiffel compiler must meet. However, the ISE Eiffel run-time documentation
  115. will probably mention some over specific low-level run-time calls which bring
  116. much more flexibility, at the price of a lack of portability.
  117. -- 
  118. Raphael Manfredi <ram@eiffel.com>
  119. Interactive Software Engineering Inc.
  120. 270 Storke Road, Suite #7                      / Tel +1 (805) 685-1006 \
  121. Goleta, California 93117, USA                  \ Fax +1 (805) 685-6869 /
  122.