Path: sran265!katsu From: katsu@sran14.sra.co.jp (WATANABE Katsuhiro) Message-ID: Date: 5 Apr 93 13:49:01 Organization: Software Research Associates, Inc.,Japan In-reply-to: utashiro@sran230.sra.co.jp's message of 26 Mar 93 17:02:20 GMT Newsgroups: sra.os.unix Subject: Re: can others allocate freed area ? Distribution: sra References: 記事 で utashiro@sran230.sra.co.jp (Kazumasa Utashiro) さんいはく > In article m-cho@sra.co.jp (Masao Cho) writes: > > >> |free しても sbrk で広げた空間は縮まらなくて、 その仮想空間はプロ > >> |セスで全く使わず、将来 malloc で再割り当て可能な場所として覚えて > >> |おいてくれます (malloc がそうプログラムされている)。 > > そんなこと誰も決めてないと思う。malloc, free の実装の仕方に > よります。どの OS についてなのかということに触れなければ無意 > 味でしょう。  なんでも大昔、free(3) した領域も次の malloc(3) の呼びだしまでは 有効だった時代があるという噂をきいたことがあります。ライブラリーを 書く人の立場でこのような古い Unix 系のライブラリーとの (backward) compatibility を考えた場合、やはり free(3) では OS に空間をお返し しないことになるほうが普通ではないでしょうか。  実際、各社この backward compatibility をひきずっているようで、 NEWS-OS 4.2R のマニュアルでは、 > The argument to free is a pointer to a block previously > allocated by malloc; this space is made available for > further allocation, but its contents are left undisturbed. とのことで、"left undisturbed" にする、すなわち free 後も空間のみならず その内容も保持しておかねばならないことを示唆しています。また、 > In order to be compatible with older versions, realloc also > works if ptr points to a block freed since the last call of > malloc, realloc or calloc; sequences of free, malloc and > realloc were previously used to attempt storage compaction. > This procedure is no longer recommended. という記述もあります。(この禁じられた storage compaction の技は 知りませんでしたが、具体的に何を malloc して、どう realloc するのだろう?)  SunOS Release 4.1.1-JLE1.1.1 のマニュアルでも同様に > For > backwards compatibility, realloc() accepts a pointer to a > block freed since the most recent call to malloc(), > calloc(), realloc(), valloc(), or memalign(). Note: using > realloc() with a block freed before the most recent call to > malloc(), calloc(), realloc(), valloc(), or memalign() is an > error. と述べており、free された領域が暫時有効であることを主張しています。  もちろん使う側の立場としては、今や上のようなことを仮定したコードを 書いてはいけないでしょう。  また、ほとんどのコードを新たに書き直してる GNU project の人々に とっては、上のような古い仕様を無視して free(3) 時に空間を OS に お返しする選択をするというのも全く自然なことだと思います。  free の時には返さずにがまんして malloc の時に返すようにすれば、 上のような backward compatibility を保持しつつ空間を OS に返すことが 可能だとは思います。 -- 渡邊克宏