| Author |
Message |
< Erlang bugs mailing list ~ Missing definitions in OTP Design Principles manual |
| Guest |
Posted: Sun Oct 15, 2006 12:29 am |
|
|
|
Guest
|
I didn't find the definitions for alloc/1 and free/2 in OTP Design
Principles manual version 5.5.1 (at
http://www.erlang.org/doc/doc-5.5.1/doc/design_principles/part_frame.html)
sections 1 and 2.
For example, in section 1.2 there was this example:
-module(ch1).
-export([start/0]).
-export([alloc/0, free/1]).
-export([init/0]).
start() ->
spawn(ch1, init, []).
alloc() ->
ch1 ! {self(), alloc},
receive
{ch1, Res} ->
Res
end.
free(Ch) ->
ch1 ! {free, Ch},
ok.
init() ->
register(ch1, self()),
Chs = channels(),
loop(Chs).
loop(Chs) ->
receive
{From, alloc} ->
{Ch, Chs2} = alloc(Chs),
From ! {ch1, Ch},
loop(Chs2);
{free, Ch} ->
Chs2 = free(Ch, Chs),
loop(Chs2)
end.
It's not clear from this what alloc(Chs) and free(Ch, Chs) are
supposed to do.
_______________________________________________
erlang-bugs mailing list
erlang-bugs@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-bugs
Post recived from mailinglist |
|
|
| Back to top |
|
| uffe |
Posted: Sun Oct 15, 2006 8:16 am |
|
|
|
User
Joined: 02 Mar 2005
Posts: 365
Location: Sweden
|
Den 2006-10-15 02:22:58 skrev Kari Pahula <kaol@iki.fi>:
> I didn't find the definitions for alloc/1 and free/2 in OTP Design
> Principles manual version 5.5.1 (at
> http://www.erlang.org/doc/doc-5.5.1/doc/design_principles/part_frame.html)
> sections 1 and 2.
>
> For example, in section 1.2 there was this example:
>
> -module(ch1).
> -export([start/0]).
[...]
>
> It's not clear from this what alloc(Chs) and free(Ch, Chs) are
> supposed to do.
Well, the implementation of those functions isn't really
relevant to the example, and a realistic implementation would
probably also do something "interesting" when allocating and
freeing a channel. Nonetheless, one way to write these
functions, for completeness, would be:
channels() ->
{_Allocated = [], _Free = lists:seq(1,100)}.
alloc({Allocated, [H|T] = _Free}) ->
{H, {[H|Allocated], T]}}.
free(Ch, {Alloc, Free} = Channels) ->
case lists:member(Ch, Alloc) of
true ->
{lists:delete(Ch, Alloc), [Ch|Free]};
false ->
Channels
end.
The alloc/1 function doesn't care to check for the
case that the server runs out of free resources, but
neither does the calling function. The most common
situation would be to make the server return an error
code rather than crashing, of course, but all this
would most likely distract the reader from the actual
point of the example.
BR,
/Ulf W
--
Ulf Wiger
_______________________________________________
erlang-bugs mailing list
erlang-bugs@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-bugs
Post recived from mailinglist |
|
|
| Back to top |
|
| Guest |
Posted: Tue Oct 17, 2006 7:45 am |
|
|
|
Guest
|
Thank you for your comments.
As Ulf pointed out, the implementation of those functions are not
really relevant. In fact, they have intentionally been left out to
avoid cluttering the code unnecessarily. This is not made clear in
the text, however. I will add a clarification and maybe an example
implementation to be used when trying out the example.
Regards,
Gunilla, Erlang/OTP team
Kari Pahula wrote:
> I didn't find the definitions for alloc/1 and free/2 in OTP Design
> Principles manual version 5.5.1 (at
> http://www.erlang.org/doc/doc-5.5.1/doc/design_principles/part_frame.html)
> sections 1 and 2.
>
> For example, in section 1.2 there was this example:
>
> -module(ch1).
> -export([start/0]).
> -export([alloc/0, free/1]).
> -export([init/0]).
>
> start() ->
> spawn(ch1, init, []).
>
> alloc() ->
> ch1 ! {self(), alloc},
> receive
> {ch1, Res} ->
> Res
> end.
>
> free(Ch) ->
> ch1 ! {free, Ch},
> ok.
>
> init() ->
> register(ch1, self()),
> Chs = channels(),
> loop(Chs).
>
> loop(Chs) ->
> receive
> {From, alloc} ->
> {Ch, Chs2} = alloc(Chs),
> From ! {ch1, Ch},
> loop(Chs2);
> {free, Ch} ->
> Chs2 = free(Ch, Chs),
> loop(Chs2)
> end.
>
> It's not clear from this what alloc(Chs) and free(Ch, Chs) are
> supposed to do.
> _______________________________________________
> erlang-bugs mailing list
> erlang-bugs@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-bugs
>
>
_______________________________________________
erlang-bugs mailing list
erlang-bugs@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-bugs
Post recived from mailinglist |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|