home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / eiffel / 1015 < prev    next >
Encoding:
Internet Message Format  |  1992-07-29  |  4.2 KB

  1. Path: sparky!uunet!eiffel!eiffel.com
  2. From: ram@eiffel.com (Raphael Manfredi)
  3. Newsgroups: comp.lang.eiffel
  4. Subject: Re: Eiffel on UNIX
  5. Summary: ISE Eiffel 3 uses sbrk()
  6. Keywords: malloc, sbrk, memory management
  7. Message-ID: <108@eiffel.eiffel.com>
  8. Date: 29 Jul 92 17:26:33 GMT
  9. References: <5496@m1.cs.man.ac.uk>
  10. Sender: ram@eiffel.com
  11. Organization: Interactive Software Engineering, Santa Barbara CA
  12. Lines: 81
  13.  
  14. Quoting ian@cs.man.ac.uk (Ian Cottam) from comp.lang.eiffel:
  15. >Will ISE Eiffel 3 still use the UNIX sbrk call for some memory
  16. >management?  Given that many library functions use malloc
  17. >(even fprintf on the SUNs I belive), should not sbrk be reserved
  18. >for the implementors of (alternative) mallocs?
  19.  
  20. The sbrk() system call is simply used to ask the kernel more core. The
  21. process receives a new chunk of memory which it can freely use for whatever
  22. purposes. The argument to the system call is a single integer, which represents
  23. the amount of memory wanted. For instance, sbrk(65536) would ask for 64K of
  24. memory, starting at the old break position (which is the return value of the
  25. system call). Note that saying sbrk(-65536) actually gives the memory back
  26. to the kernel, starting at the current break position and releasing the top
  27. 64K used (before doing so, it might be a good idea to make sure the process
  28. does not reference anything within that area, or you'll get a nice and clean
  29. memory fault -- core dumped at the next dereference :-).
  30.  
  31. So sbrk() is a low level system call, which of course is the basis for any
  32. memory management package, as it is is the only way to get core under UNIX
  33. (modulo brk(), but that's another story). No wonder malloc() (a library
  34. function) is built on top of that system call. Usually, malloc() asks for
  35. big chunks of memory via sbrk() and then butchers that memory into small
  36. fragments at the user's request, keeping track of what is allocated.
  37.  
  38. Very few malloc() will allow you to shrink your process's size by actually
  39. giving some memory back to the kernel, and that seemed a desirable feature for
  40. Eiffel, which can allocate vast amounts of memory at instant t, and then
  41. release it in whole at (t + 1) -- via the garbage collector, naturally.
  42.  
  43. Furthermore, as you correctly stated it, some library functions do use malloc()
  44. internally to be able to perform correctly. Not only fprintf(), but virtually
  45. any standard I/O routine (on SUNs), or some X widgets, or whatever library
  46. function needs non-static memory. However, Eiffel has no way to control the
  47. way malloc() works, and its allocation scheme may not allow the run-time to
  48. shrink the data space when it could be possible, since it has no way of
  49. knowing who references what. Besides, there could be a huge amount of memory
  50. allocated by "the C side" under malloc() control, and then suddenly freed
  51. without giving Eiffel a chance to use that free memory.
  52.  
  53. This is why it seemed desirable to write our own memory management scheme on
  54. top of sbrk() (so YES, we do use sbrk() in ISE Eiffel 3), and also provide
  55. a C-compatible malloc() interface in our own run-time, which will override
  56. the system provided malloc during the linking phase and force every external
  57. function using malloc() to use OUR malloc() from the Eiffel run-time (which
  58. has malloc(3X) comparable performances, both in size and time). That should
  59. make no difference at all with respect to the standard malloc() use from C
  60. and makes things easier for the run-time.
  61.  
  62. To give but a single example, the following is allowed in ISE Eiffel 3 and
  63. will be correctly detected by the garbage collector (otherwise, boom!):
  64.  
  65. In Eiffel:
  66.  
  67.     some_object: GENERAL;
  68.  
  69.     allocated: GENERAL is
  70.             -- Allocated C area
  71.         external
  72.             "C"
  73.         end;
  74.     
  75.     f is do some_object := allocated; end
  76.  
  77. In C:
  78.  
  79.     char *allocated()
  80.     {
  81.         return malloc(100);    /* For example */
  82.     }
  83.  
  84. Of course, this is only an example. The correct way of doing the above would
  85. be to use the POINTER type instead of GENERAL, to avoid any accidental
  86. feature call on something which is certainly not an Eiffel object. But that
  87. is beside the point -- we are talking memory management here.
  88.  
  89. Having your own malloc buys you a lot.
  90. -- 
  91. Raphael Manfredi <ram@eiffel.com>
  92. Interactive Software Engineering Inc.
  93. 270 Storke Road, Suite #7                      / Tel +1 (805) 685-1006 \
  94. Goleta, California 93117, USA                  \ Fax +1 (805) 685-6869 /
  95.