home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / transput / 1218 < prev    next >
Encoding:
Internet Message Format  |  1992-11-19  |  4.3 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!sgiblab!munnari.oz.au!metro!mama!andy
  2. From: andy@research.canon.oz.au (Andy Newman)
  3. Newsgroups: comp.sys.transputer
  4. Subject: Dynamic Memory Allocation (was Re: Why Occam3 probably won't be recursive)
  5. Keywords: Occam3 recursive, no
  6. Message-ID: <BxyB6I.LLF@research.canon.oz.au>
  7. Date: 19 Nov 92 06:54:17 GMT
  8. References: <1992Nov11.033835.21118@netcom.com> <2225@eagle.ukc.ac.uk> <dbc.722086651@bill>
  9. Sender: news@research.canon.oz.au
  10. Organization: Canon Information Systems Research Australia
  11. Lines: 82
  12.  
  13. dbc@ecs.soton.ac.uk (Bryan Carpenter) writes:
  14. >
  15. >Personally, I wouldn't object to building my own stack, if I just
  16. >had `malloc' to do it with.  Building my own `malloc' I object to
  17. >more strongly.
  18. >
  19. >Bryan
  20. >
  21.  
  22. Building a memory allocation module isn't all that difficult (once
  23. you've done it once or twice :-). Anyway in most "serious"
  24. applications you layer an application specific allocator on the
  25. "standard" allocator.
  26.  
  27. I worked on a (now defunct) TV animation product based on T800's. It
  28. comprised a LARGE amount of occam (the last I heard it was up around
  29. the 500,000 line mark) We used a dynamic memory allocation quite
  30. extensively. We used a very obvious and simple method but it worked
  31. quite well and gave us the facilities we needed.
  32.  
  33. I'll describe what we did but first I must state that we
  34. didn't adopt the occam "way", i.e. we used dynamically allocated
  35. things; we turned off the compiler's checking (it didn't work for us
  36. anyway), etc... We were heathens! We used occam because it was the
  37. only thing available at the time for transputers that could generate
  38. reasonable code. There were the first C compilers available but we'd
  39. gone too far by the time they were useful.
  40.  
  41. The system comprised a number of long lived processes, i.e. the
  42. modules in the system. Each of these processes accepted a number of
  43. parameters, one of which was a unsized vector of INT - the memory pool.
  44.  
  45. One of the standard "kernel" processes was a memory allocator. It
  46. "owned" the memory pool vector and serviced requests for portions of
  47. the pool - it serialised other processes attempts to modify the
  48. memory allocator data structure.
  49.  
  50. The allocator process understood a few requests; allocate some memory,
  51. add some memory to the freelist, report the largest block available,
  52. etc... You know, standard general purpose allocator things. As I
  53. recall we had a couple of procedures....
  54.  
  55.     PROC Alloc.Mem( [2]CHAN OF ANY mm.chans, VAL INT nbytes, INT ptr )
  56.     PROC Free.Mem( [2]CHAN OF ANY mm.chans, INT ptr )
  57.  
  58. Notes:
  59.  
  60.     Yes we used CHAN OF ANY - it allowed our configuration langauage and
  61. communications system work! The two element vector holds the "to"
  62. channel and the "from" channel used to perform the RPC with the memory
  63. allocator process (this use exposed a limitation in the compiler, it
  64. assumed all channels in a vector were used in the same "direction")
  65. Passing the channels also allowed these procs to be used with
  66. different allocators (we had a few - one for the local processor's
  67. memory, one for our processor board's shared memory and one for the
  68. on-chip memory).
  69.  
  70.     The ptr parameter to Free.Mem is not a VAL INT. It should be the
  71. exact same actual parameter passed as ptr in the call to Alloc.Mem.
  72.  
  73. "Addresses" were the (INT) index into the memory pool vector. You'd
  74. send a message to the allocator process (or more specifically call a
  75. proc that hid the protocol) requesting some memory and it would return
  76. the index of the first word you could use. To free something you sent
  77. the index back to the allocator and it would put a block onto the
  78. freelist. This is quite open to abuse of course but in our environment
  79. we had control and there are a number of simple techniques that assist
  80. in detecting memory corruption and mis-use. We didn't have all that
  81. much trouble.
  82.  
  83. This scheme goes totally against occam's usage checking! We were
  84. passing a single vector to nearly every process in the system and
  85. those processes are accessing practically any part of the vector. Not
  86. a checkable thing really.
  87.  
  88. Okay this method is not safe (in fact its downright dangerous!) but it
  89. gave us flexible dynamic allocation facilities. I'm not advocating its
  90. use (its not wise to "bend" occam, if you want to do these things use
  91. a language that supports them properly) but it did work.
  92.  
  93. -- 
  94. Andy Newman (andy@research.canon.oz.au)
  95.