digitalmars.D - Memory allocation purity
- Brian Schott (11/11) May 14 2014 What is the plan for the "pure"-ity of memory management?
- w0rp (7/18) May 14 2014 I think even C malloc should be considered pure. True, it affects
- Idan Arye (3/9) May 14 2014 `free` is not pure, because if you have a reference to that
- Meta (4/13) May 14 2014 It is weakly pure, i.e., it can only affect the world through the
- Jonathan M Davis via Digitalmars-d (10/19) May 14 2014 But does that really matter from the perspective of pure? That's really ...
- Jonathan M Davis via Digitalmars-d (8/19) May 14 2014 I think malloc should definitely be considered pure for the same reasons...
- Walter Bright (3/5) May 14 2014 Because GC failures are not recoverable, so the pure allocation cannot f...
- Meta (4/11) May 14 2014 Malloc is a tricky case. The results it returns are theoretically
- Brian Schott (2/9) May 14 2014 Can we say that Mallocator failures are not recoverable?
- Walter Bright (3/4) May 14 2014 malloc itself does not have that property. But you could design a wrappe...
- Brian Schott (14/18) May 14 2014 I'm concerned specifically with this wrapper:
- Andrei Alexandrescu (4/22) May 14 2014 I think so. A more cautious solution would be to define PureMallocator
- Steven Schveighoffer (10/26) May 15 2014 Be careful. The above is all correct, but as has been discussed, the
- Andrei Alexandrescu (7/19) May 14 2014 FWIW std.allocator allows constructing a bunch of small-business
- Kapps (7/9) May 14 2014 Is this intentionally the case? I always thought you could handle
- Jonathan M Davis via Digitalmars-d (13/23) May 14 2014 It's intended that all Errors be considered unrecoverable. You can catch...
- Jonathan M Davis via Digitalmars-d (5/12) May 14 2014 Then we should create a wrapper for malloc which throws a MemoryError wh...
- Meta (11/22) May 14 2014 Allocating memory through new and malloc should always be pure, I
- Walter Bright (5/8) May 14 2014 malloc cannot be pure if, with the same arguments, it returns null somet...
- Meta (3/12) May 14 2014 If we pretend that there's infinite memory, then malloc will
- Walter Bright (2/16) May 14 2014 And what happens when malloc returns null?
- Manu via Digitalmars-d (8/15) May 15 2014 Even if it doesn't fail, malloc called with identical arguments still
- Steven Schveighoffer (5/25) May 15 2014 That only applies to strong-pure functions. malloc would be weak-pure,
- Manu via Digitalmars-d (3/31) May 15 2014 Why should returning a mutable pointer imply weak purity?
- Steven Schveighoffer (7/44) May 15 2014 Because if it were strong-pure, the compiler could optimize two sequenti...
- Manu via Digitalmars-d (16/69) May 15 2014 I don't follow. Forget that it's a pointer... it's just a number. A
- Steven Schveighoffer (22/43) May 15 2014 But that doesn't make sense. What you are returning is not the pointer
- David Nadlinger (10/11) May 15 2014 The argument here is that a valid mutable pointer returned from a
- Steven Schveighoffer (8/14) May 15 2014 Basically, you are saying that malloc must return the same block wheneve...
- Andrei Alexandrescu (5/19) May 15 2014 Null is special - it's a singularity. It can't be subsequently used for
- Adam Sakareassen via Digitalmars-d (43/53) May 14 2014 For my 2 cents,
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (14/21) May 14 2014 ...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (9/13) May 14 2014 This of course presumes a transactional view, that the
- bearophile (8/11) May 14 2014 A little example of D purity (this compiles):
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (3/7) May 14 2014 Yes, and then you may as well allow a random generator with
- Steven Schveighoffer (14/21) May 15 2014 This has nothing to do with allocators being pure. They must be pure as ...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (8/11) May 15 2014 That's the wrong attitude to take when it comes to the compiler
- Steven Schveighoffer (25/35) May 15 2014 ed =
- David Nadlinger (13/22) May 15 2014 I have to agree with Ola here. If you write a piece of pure,
- Steven Schveighoffer (29/51) May 15 2014 =
- David Nadlinger (15/22) May 15 2014 Which rules exactly? My point is mainly that this area of the
- Steven Schveighoffer (65/81) May 16 2014 =
- bearophile (5/8) May 15 2014 Yes, if you allow only a referentially pure view of pointers,
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (9/11) May 15 2014 You need:
- Dicebot (11/31) May 15 2014 Which is what makes D pure lint-like helper and not effective
- Steven Schveighoffer (13/42) May 15 2014 as =
- Andrei Alexandrescu (3/6) May 15 2014 I think code that doesn't return pointers should be memoizable. Playing
- Jonathan M Davis via Digitalmars-d (11/18) May 17 2014 On Thu, 15 May 2014 08:43:11 -0700
- H. S. Teoh via Digitalmars-d (12/33) May 18 2014 [...]
- Steven Schveighoffer (7/38) May 19 2014 Memoizing reference returns that are immutable should be fine.
- Jonathan M Davis via Digitalmars-d (15/38) May 19 2014 On Mon, 19 May 2014 09:42:31 -0400
- Steven Schveighoffer (10/54) May 19 2014 It shouldn't matter. Something that returns immutable references, can
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/10) May 19 2014 I think this is at odds with generic programming. What you are
- Steven Schveighoffer (15/23) May 19 2014 =
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (12/15) May 19 2014 This isn't at all obvious to me. Also I think the "coin flip
- Steven Schveighoffer (25/37) May 19 2014 .
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (9/21) May 19 2014 Nah, not on modern archiectures without GC compaction.
- Steven Schveighoffer (6/10) May 19 2014 Let me cut to the chase here. I haven't seen it. Let's not go any furthe...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (20/30) May 19 2014 I've given several examples, but I oppose the general attitude.
- Steven Schveighoffer (22/44) May 19 2014 =
- Steven Schveighoffer (8/12) May 19 2014 BTW, you probably shouldn't expect any response from me, I'm about to go...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (5/9) May 19 2014 I am not going to be there, but I guess this topic could easily
- Jonathan M Davis via Digitalmars-d (20/24) May 19 2014 On Mon, 19 May 2014 14:33:55 -0400
- Dicebot (17/32) May 19 2014 immutable(Object*) alloc() pure
- Steven Schveighoffer (16/47) May 19 2014 e =
- Dicebot (15/61) May 19 2014 Oh, and are probably eager to show me links to specs which
- Steven Schveighoffer (33/86) May 19 2014 :
- Dicebot (23/94) May 19 2014 No it is not. It is semantically valid code which does exactly
- Steven Schveighoffer (14/47) May 20 2014 Again, the bug is to break if a function that allocates an immutable
- Jonathan M Davis via Digitalmars-d (22/64) May 19 2014 On Mon, 19 May 2014 15:20:46 -0400
- Steven Schveighoffer (20/97) May 20 2014 :
- Timon Gehr (4/19) May 19 2014 Furthermore, it may not at all be obvious that this is happening: After
- Jonathan M Davis via Digitalmars-d (22/78) May 19 2014 On Mon, 19 May 2014 13:11:43 -0400
- Steven Schveighoffer (11/63) May 20 2014 Compiles today (2.065):
- Jonathan M Davis via Digitalmars-d (31/62) May 18 2014 Hmmmm. I think that it was pointed out somewhere else in this thread tha...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (5/20) May 18 2014 Memoization is valid throughout the program. Opportunities occur
- Jonathan M Davis via Digitalmars-d (26/46) May 18 2014 I seriously question that assertion. How often do you really call the sa...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (7/15) May 18 2014 It can, through the parameters, like an array of pointers. And
- Jonathan M Davis via Digitalmars-d (33/48) May 18 2014 Except that if most of your code is marked pure, then there aren't very ...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/17) May 19 2014 It does not appear as a clean design that functions should have
- Jonathan M Davis via Digitalmars-d (19/36) May 19 2014 I don't follow you. The fact that D's pure helps the compiler determine ...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (15/22) May 19 2014 No, I just don't think it makes much sense the way "pure" is
- Timon Gehr (15/39) May 15 2014 Not really, allocation is just an implementation detail. The
- Steven Schveighoffer (14/55) May 15 2014 as
- Timon Gehr (12/48) May 15 2014 (Well,
- David Nadlinger (14/21) May 15 2014 No, this particular example appears to be invalid code. Under the
- bearophile (8/9) May 14 2014 If you start using pure in D you see it's like const: it allows
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (4/9) May 14 2014 Local mutability does not affect purity, it is the pre and post
- Jonathan M Davis via Digitalmars-d (13/14) May 14 2014 No, it doesn't. _All_ that it means when a function is pure is that it c...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (8/17) May 15 2014 Which makes it pointless and misleading.
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/6) May 15 2014 And mutating through parameters does not affect functional purity
- Jonathan M Davis via Digitalmars-d (68/85) May 15 2014 Originally, pure required that the function parameters be pure in additi...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (42/81) May 15 2014 If it didn't return the memory allocated with new and if the call
- Jonathan M Davis via Digitalmars-d (62/113) May 15 2014 That would only matter if the compiler were trying to optimize based on ...
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (24/51) May 15 2014 I think this C linking excuse is used too much :-). Clearly one
- luka8088 (8/28) May 15 2014 Um. Yes it does. http://dlang.org/function.html#pure-functions
- Jonathan M Davis via Digitalmars-d (39/69) May 15 2014 The reread the paragraph at the top of the section of the documentation
- luka8088 (23/105) May 15 2014 I am aware of weak/strong purity. I am only talking about strong purity ...
- Don (31/71) May 15 2014 Please note: D's 'pure' annotation does *not* mean that the
- luka8088 (6/88) May 15 2014 Yeah, I read all about weak/string purity and I do understand the
- Don (10/127) May 15 2014 Yes. 'strong pure' means pure in the way that the functional
- Jonathan M Davis via Digitalmars-d (8/17) May 15 2014 Yeah, I agree. The problem is that it always seems necessary to use the ...
- luka8088 (3/25) May 15 2014 Yeah, +1.
- luka8088 (2/106) May 15 2014 Ok. Now it is much clearer, thanks.
- Andrei Alexandrescu (8/13) May 15 2014 Yes, as long as you don't rely on distinguishing objects by address.
- Timon Gehr (11/27) May 15 2014 Why?
- Andrei Alexandrescu (7/35) May 15 2014 It's rather obvious. You've got to have the ability to create new values...
- Timon Gehr (7/18) May 15 2014 This kind of operational reasoning is not essential. Of course, in
- Andrei Alexandrescu (5/26) May 15 2014 That's the point. There's no specification of allocation - the evaluator...
- Peter Alexander (13/48) May 16 2014 I believe Timon's point is that allocation is an implementation
- luka8088 (11/31) May 15 2014 Hm, this does not seem right. @safe prevents you from taking the address
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (10/15) May 15 2014 Uhm. That is a pretty strong assumption. "memoizable" is very
- Don (11/27) May 15 2014 It's useful, but it's not a deep property, and importantly, it
- bearophile (5/7) May 15 2014 I suggested a little more powerful @outer:
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (13/21) May 15 2014 What you need is some way to formally specify what a lock covers
- w0rp (12/12) May 15 2014 Ola, you do not understand 'pure.'
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/8) May 15 2014 Of course you can. Functional languages execute in an "imperative
- Timon Gehr (8/17) May 15 2014 Strictly speaking you don't "need" monads, they are sometimes just an
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (9/19) May 16 2014 Yes, from haskell.org:
- Timon Gehr (3/7) May 15 2014 Why? A memoizable function is still memoizable if it is changed
- Walter Bright (2/4) May 15 2014 I doubt a compiler could prove it was pure.
- Timon Gehr (5/10) May 15 2014 Yes, that was actually my point. Memoizable is actually a non-trivial
- Walter Bright (4/15) May 15 2014 If the compiler cannot mechanically verify purity, the notion of purity ...
- H. S. Teoh via Digitalmars-d (22/40) May 15 2014 What if the language allowed the user to supply a proof of purity, which
- bearophile (18/23) May 15 2014 Yes, this is common enough. As an example the Whiley
- Walter Bright (3/5) May 15 2014 I think those sorts of things are PhD research topics. It's a bit beyond...
- bearophile (8/10) May 15 2014 Perhaps yes.
- Walter Bright (2/9) May 15 2014 Yes, the VRP has been a nice win for D. No other language does it.
- Araq (2/4) May 16 2014 I don't know why you keep saying things like that, you don't know
- Walter Bright (4/8) May 16 2014 I having trouble finding it in the spec:
- bearophile (58/59) May 16 2014 There are ways to improve it in very useful ways, that increase
- Timon Gehr (7/12) May 16 2014 Well, feasibility has long ago been demonstrated and I hope those ideas
- Timon Gehr (14/53) May 16 2014 Yes, either that or one could even just implement it in the existing
- Andrei Alexandrescu (2/34) May 16 2014 Typo: int_leibiz_equality :o). -- Andrei
- Timon Gehr (2/16) May 17 2014 If that is everything, then I am in good shape! :o)
- Timon Gehr (4/8) May 17 2014 It could be argued though, that this axiom was not too aptly named in
- Walter Bright (2/4) May 15 2014 I hadn't thought of that. Pretty cool!
- Andrei Alexandrescu (2/7) May 15 2014 Yah, that's unexpected in a nice way. -- Andrei
- Walter Bright (2/10) May 15 2014 Nice to have a positive surprise!
- Jonathan M Davis via Digitalmars-d (17/21) May 17 2014 Definitely, but we also need to be careful with it. If @nogc just restri...
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (14/29) May 15 2014 There's an important difference between malloc and new: malloc
- bearophile (5/7) May 15 2014 What optimizations do you think GDC compiler is doing (or will
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (7/12) May 15 2014 I don't know whether it does or will do any. It is a theoretical
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (11/17) May 15 2014 I most code, but not all, so how does the compiler know if you
What is the plan for the "pure"-ity of memory management? Right now the "new" operator is considered to be pure even though it is not, but related functinos like malloc, GC.addRange, GC.removeRange, and others are not. This prevents large portions of std.allocator from being pure. This then prevents any containers library built on std.allocator from being pure, which does the same for any funcions and data structures written using those containers. If malloc can never be considered pure, even when hidden behind an allocator, why can it be considered pure when hidden behind the GC?
May 14 2014
On Wednesday, 14 May 2014 at 22:42:47 UTC, Brian Schott wrote:What is the plan for the "pure"-ity of memory management? Right now the "new" operator is considered to be pure even though it is not, but related functinos like malloc, GC.addRange, GC.removeRange, and others are not. This prevents large portions of std.allocator from being pure. This then prevents any containers library built on std.allocator from being pure, which does the same for any funcions and data structures written using those containers. If malloc can never be considered pure, even when hidden behind an allocator, why can it be considered pure when hidden behind the GC?I think even C malloc should be considered pure. True, it affects global state by allocating memory, but it never changes existing values, it just allows for new values. free is pure because it isn't side-effecting, it deallocates what you give it. That's just my perspective on it though, others might have other views on it.
May 14 2014
On Wednesday, 14 May 2014 at 22:50:10 UTC, w0rp wrote:I think even C malloc should be considered pure. True, it affects global state by allocating memory, but it never changes existing values, it just allows for new values. free is pure because it isn't side-effecting, it deallocates what you give it. That's just my perspective on it though, others might have other views on it.`free` is not pure, because if you have a reference to that memory that reference is no longer valid.
May 14 2014
On Thursday, 15 May 2014 at 01:33:36 UTC, Idan Arye wrote:On Wednesday, 14 May 2014 at 22:50:10 UTC, w0rp wrote:It is weakly pure, i.e., it can only affect the world through the parameters given to it (and the state of the computer's memory... but we should ignore that).I think even C malloc should be considered pure. True, it affects global state by allocating memory, but it never changes existing values, it just allows for new values. free is pure because it isn't side-effecting, it deallocates what you give it. That's just my perspective on it though, others might have other views on it.`free` is not pure, because if you have a reference to that memory that reference is no longer valid.
May 14 2014
On Thu, 15 May 2014 01:33:34 +0000 Idan Arye via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Wednesday, 14 May 2014 at 22:50:10 UTC, w0rp wrote:But does that really matter from the perspective of pure? That's really more of an safety issue. There might be some way that that violates purtiy, but I can't think of one at the moment. free can't be strongly pure, because it's arguments couldn't be immutable (or even const) without violating the type system, but I _think_ that it's fine for free to be weakly pure. It's quite possible that I'm missing something though. - Jonathan M DavisI think even C malloc should be considered pure. True, it affects global state by allocating memory, but it never changes existing values, it just allows for new values. free is pure because it isn't side-effecting, it deallocates what you give it. That's just my perspective on it though, others might have other views on it.`free` is not pure, because if you have a reference to that memory that reference is no longer valid.
May 14 2014
On Wed, 14 May 2014 22:42:46 +0000 Brian Schott via Digitalmars-d <digitalmars-d puremagic.com> wrote:What is the plan for the "pure"-ity of memory management? Right now the "new" operator is considered to be pure even though it is not, but related functinos like malloc, GC.addRange, GC.removeRange, and others are not. This prevents large portions of std.allocator from being pure. This then prevents any containers library built on std.allocator from being pure, which does the same for any funcions and data structures written using those containers. If malloc can never be considered pure, even when hidden behind an allocator, why can it be considered pure when hidden behind the GC?I think malloc should definitely be considered pure for the same reasons that new is considered pure. I don't know about the other memory management functions though. I'd really have to think through their side effects to have an opinion on them. If we can make them pure though, that would certainly help with ensuring that allocators can be pure. - Jonathan M Davis
May 14 2014
On 5/14/2014 3:42 PM, Brian Schott wrote:If malloc can never be considered pure, even when hidden behind an allocator,It cannot be pure as long as it can fail.why can it be considered pure when hidden behind the GC?Because GC failures are not recoverable, so the pure allocation cannot fail.
May 14 2014
On Thursday, 15 May 2014 at 00:00:37 UTC, Walter Bright wrote:On 5/14/2014 3:42 PM, Brian Schott wrote:Malloc is a tricky case. The results it returns are theoretically always unique, and it is referentially transparent if we pretend that we have infinite memory.If malloc can never be considered pure, even when hidden behind an allocator,It cannot be pure as long as it can fail.why can it be considered pure when hidden behind the GC?Because GC failures are not recoverable, so the pure allocation cannot fail.
May 14 2014
On Thursday, 15 May 2014 at 00:00:37 UTC, Walter Bright wrote:On 5/14/2014 3:42 PM, Brian Schott wrote:Can we say that Mallocator failures are not recoverable?If malloc can never be considered pure, even when hidden behind an allocator,It cannot be pure as long as it can fail.why can it be considered pure when hidden behind the GC?Because GC failures are not recoverable, so the pure allocation cannot fail.
May 14 2014
On 5/14/2014 5:44 PM, Brian Schott wrote:Can we say that Mallocator failures are not recoverable?malloc itself does not have that property. But you could design a wrapper for it that did.
May 14 2014
On Thursday, 15 May 2014 at 00:48:52 UTC, Walter Bright wrote:On 5/14/2014 5:44 PM, Brian Schott wrote:I'm concerned specifically with this wrapper: https://github.com/andralex/phobos/blob/allocator/std/allocator.d#L773 We need to make these functions pure if they are going to be usable. Removing the stdlib import and adding private extern (C) { void* malloc(size_t) pure nothrow trusted; void free(void*) pure nothrow trusted; void* realloc(void*, size_t) pure nothrow trusted; } as well as a throwing OutOfMemoryError if they fail should be sufficient, correct?Can we say that Mallocator failures are not recoverable?malloc itself does not have that property. But you could design a wrapper for it that did.
May 14 2014
On 5/14/14, 6:11 PM, Brian Schott wrote:On Thursday, 15 May 2014 at 00:48:52 UTC, Walter Bright wrote:I think so. A more cautious solution would be to define PureMallocator in addition to Mallocator. AndreiOn 5/14/2014 5:44 PM, Brian Schott wrote:I'm concerned specifically with this wrapper: https://github.com/andralex/phobos/blob/allocator/std/allocator.d#L773 We need to make these functions pure if they are going to be usable. Removing the stdlib import and adding private extern (C) { void* malloc(size_t) pure nothrow trusted; void free(void*) pure nothrow trusted; void* realloc(void*, size_t) pure nothrow trusted; } as well as a throwing OutOfMemoryError if they fail should be sufficient, correct?Can we say that Mallocator failures are not recoverable?malloc itself does not have that property. But you could design a wrapper for it that did.
May 14 2014
On Wed, 14 May 2014 21:11:30 -0400, Brian Schott <briancschott gmail.com> wrote:On Thursday, 15 May 2014 at 00:48:52 UTC, Walter Bright wrote:Be careful. The above is all correct, but as has been discussed, the minute you start making returns immutable or parameters immutable, they become strong-pure, which has undesirable properties for allocators. If you wrap the above with calls to typed data, then strong-pure might be inferred. The compiler can specially designate things like idup to make sure they always are considered weak-pure. But library code has to be more cautious. -SteveOn 5/14/2014 5:44 PM, Brian Schott wrote:I'm concerned specifically with this wrapper: https://github.com/andralex/phobos/blob/allocator/std/allocator.d#L773 We need to make these functions pure if they are going to be usable. Removing the stdlib import and adding private extern (C) { void* malloc(size_t) pure nothrow trusted; void free(void*) pure nothrow trusted; void* realloc(void*, size_t) pure nothrow trusted; }Can we say that Mallocator failures are not recoverable?malloc itself does not have that property. But you could design a wrapper for it that did.
May 15 2014
On 5/14/14, 5:44 PM, Brian Schott wrote:On Thursday, 15 May 2014 at 00:00:37 UTC, Walter Bright wrote:FWIW std.allocator allows constructing a bunch of small-business allocators for which failure to allocate is entirely legit. The methods of such allocator may be weakly pure. An allocator that ultimately falls back to GCAllocator and "never fails" should be pure - better yet, inferred as such. AndreiOn 5/14/2014 3:42 PM, Brian Schott wrote:Can we say that Mallocator failures are not recoverable?If malloc can never be considered pure, even when hidden behind an allocator,It cannot be pure as long as it can fail.why can it be considered pure when hidden behind the GC?Because GC failures are not recoverable, so the pure allocation cannot fail.
May 14 2014
On Thursday, 15 May 2014 at 00:00:37 UTC, Walter Bright wrote:Because GC failures are not recoverable, so the pure allocation cannot fail.Is this intentionally the case? I always thought you could handle it with onOutOfMemoryError if you knew that your program could handle running out of memory and free some memory (i.e., cached data) to recover. I've never actually had a use for it myself, so I'm just basing this off of newsgroup discussions I remember (possibly incorrectly) reading.
May 14 2014
On Thu, 15 May 2014 01:25:52 +0000 Kapps via Digitalmars-d <digitalmars-d puremagic.com> wrote:On Thursday, 15 May 2014 at 00:00:37 UTC, Walter Bright wrote:It's intended that all Errors be considered unrecoverable. You can catch them when necessary to try and do cleanup, but that's fairly risky, and you should probably only do it when you're very sure of what's going on (which generally means that the Error was thrown very close to where you're catching it). There's no guarantee that automatic cleanup occurs when Errors are thrown (unlike with Exceptions), so when an Error is thrown, the program tends to be in a weird state on top of the weird state that caused the Error to be thrown in the first place. In theory, you could catch an OutOfMemoryError very close to where it was thrown and deal with it, but that's really not what was intended. - Jonathan M DavisBecause GC failures are not recoverable, so the pure allocation cannot fail.Is this intentionally the case? I always thought you could handle it with onOutOfMemoryError if you knew that your program could handle running out of memory and free some memory (i.e., cached data) to recover. I've never actually had a use for it myself, so I'm just basing this off of newsgroup discussions I remember (possibly incorrectly) reading.
May 14 2014
On Wed, 14 May 2014 17:00:39 -0700 Walter Bright via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 5/14/2014 3:42 PM, Brian Schott wrote:Then we should create a wrapper for malloc which throws a MemoryError when malloc fails. Then malloc failures would be the same as GC failures. - Jonathan M DavisIf malloc can never be considered pure, even when hidden behind an allocator,It cannot be pure as long as it can fail.why can it be considered pure when hidden behind the GC?Because GC failures are not recoverable, so the pure allocation cannot fail.
May 14 2014
On Wednesday, 14 May 2014 at 22:42:47 UTC, Brian Schott wrote:What is the plan for the "pure"-ity of memory management? Right now the "new" operator is considered to be pure even though it is not, but related functinos like malloc, GC.addRange, GC.removeRange, and others are not. This prevents large portions of std.allocator from being pure. This then prevents any containers library built on std.allocator from being pure, which does the same for any funcions and data structures written using those containers. If malloc can never be considered pure, even when hidden behind an allocator, why can it be considered pure when hidden behind the GC?Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case, or throws an error in new's case. As for GC.addRange, GC.removeRange, etc... it's hard to say. These functions definitely alter the state of the GC, but it's all wrapped up in the GC's black box, guaranteed to never escape (I think? I don't know exactly how they're implemented). In the general case, as long as side-effects can be guaranteed to never affect anything outside of a function/class/struct, I think it can probably be pure (D's notion of purity, anyway).
May 14 2014
On 5/14/2014 5:03 PM, Meta wrote:Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case,malloc cannot be pure if, with the same arguments, it returns null sometimes and not other times.or throws an error in new's case.A non-recoverable error. ^^^^^^^^^^^^^^^
May 14 2014
On Thursday, 15 May 2014 at 00:50:06 UTC, Walter Bright wrote:On 5/14/2014 5:03 PM, Meta wrote:If we pretend that there's infinite memory, then malloc will never return null.Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case,malloc cannot be pure if, with the same arguments, it returns null sometimes and not other times.or throws an error in new's case.A non-recoverable error. ^^^^^^^^^^^^^^^
May 14 2014
On 5/14/2014 5:56 PM, Meta wrote:On Thursday, 15 May 2014 at 00:50:06 UTC, Walter Bright wrote:And what happens when malloc returns null?On 5/14/2014 5:03 PM, Meta wrote:If we pretend that there's infinite memory, then malloc will never return null.Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case,malloc cannot be pure if, with the same arguments, it returns null sometimes and not other times.or throws an error in new's case.A non-recoverable error. ^^^^^^^^^^^^^^^
May 14 2014
On 15 May 2014 10:50, Walter Bright via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 5/14/2014 5:03 PM, Meta wrote:Even if it doesn't fail, malloc called with identical arguments still returns a different result every time. You can't factor malloc outside a loop and cache the result because it's being called repeatedly with the same argument like you're supposed to be able to do with any other pure function. You shouldn't be able to do that with gcalloc either... how can gcalloc be pure?Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case,malloc cannot be pure if, with the same arguments, it returns null sometimes and not other times.
May 15 2014
On Thu, 15 May 2014 07:52:20 -0400, Manu via Digitalmars-d <digitalmars-d puremagic.com> wrote:On 15 May 2014 10:50, Walter Bright via Digitalmars-d <digitalmars-d puremagic.com> wrote:That only applies to strong-pure functions. malloc would be weak-pure, since it returns a mutable pointer. -SteveOn 5/14/2014 5:03 PM, Meta wrote:Even if it doesn't fail, malloc called with identical arguments still returns a different result every time. You can't factor malloc outside a loop and cache the result because it's being called repeatedly with the same argument like you're supposed to be able to do with any other pure function. You shouldn't be able to do that with gcalloc either... how can gcalloc be pure?Allocating memory through new and malloc should always be pure, I think, because failure either returns null in malloc's case,malloc cannot be pure if, with the same arguments, it returns null sometimes and not other times.
May 15 2014









"Meta" <jared771 gmail.com> 