Greedy memory handling

2013-09-11 Thread monarch_dodra
I have a function that will *massively* benefit from having a persistent internal buffer it can re-use (and grow) from call to call, instead of re-allocating on every call. What I don't want is either of: 1. To set a fixed limitation of size, if the user ends up making repeated calls to someth

Re: Greedy memory handling

2013-09-11 Thread Gary Willoughby
On Wednesday, 11 September 2013 at 08:06:37 UTC, monarch_dodra wrote: I have a function that will *massively* benefit from having a persistent internal buffer it can re-use (and grow) from call to call, instead of re-allocating on every call. What I don't want is either of: 1. To set a fixed l

Re: Greedy memory handling

2013-09-11 Thread monarch_dodra
On Wednesday, 11 September 2013 at 10:28:37 UTC, Gary Willoughby wrote: You can then let the GC handle it or free it yourself with GC.free(*buffer). But if the buffer is stored in a static variable, the GC will never collect it. I *could* also free it myself, but why/when would I do that? D

Re: Greedy memory handling

2013-09-11 Thread Joseph Rushton Wakeling
On 11/09/13 12:34, monarch_dodra wrote: But if the buffer is stored in a static variable, the GC will never collect it. I *could* also free it myself, but why/when would I do that? Did you just just let your buffer grow, and never let it get collected? Is there a way to do something like "I'm u

Re: Greedy memory handling

2013-09-11 Thread Joseph Rushton Wakeling
On 11/09/13 13:14, Joseph Rushton Wakeling wrote: On 11/09/13 12:34, monarch_dodra wrote: But if the buffer is stored in a static variable, the GC will never collect it. I *could* also free it myself, but why/when would I do that? Did you just just let your buffer grow, and never let it get col

Re: Greedy memory handling

2013-09-11 Thread Namespace
On Wednesday, 11 September 2013 at 08:06:37 UTC, monarch_dodra wrote: I have a function that will *massively* benefit from having a persistent internal buffer it can re-use (and grow) from call to call, instead of re-allocating on every call. What I don't want is either of: 1. To set a fixed l

Re: Greedy memory handling

2013-09-11 Thread Dmitry Olshansky
11-Sep-2013 14:34, monarch_dodra пишет: On Wednesday, 11 September 2013 at 10:28:37 UTC, Gary Willoughby wrote: You can then let the GC handle it or free it yourself with GC.free(*buffer). But if the buffer is stored in a static variable, the GC will never collect it. I *could* also free it my

Re: Greedy memory handling

2013-09-11 Thread monarch_dodra
On Wednesday, 11 September 2013 at 11:19:27 UTC, Joseph Rushton Wakeling wrote: On 11/09/13 13:14, Joseph Rushton Wakeling wrote: On 11/09/13 12:34, monarch_dodra wrote: But if the buffer is stored in a static variable, the GC will never collect it. I *could* also free it myself, but why/when

Re: Greedy memory handling

2013-09-11 Thread Joseph Rushton Wakeling
On 11/09/13 15:13, monarch_dodra wrote: That's somewhat better, as it would allow the GC to collect my buffer, if it wants to, but I wouldn't actually know about it afterwards which leaves me screwed. Just to clarify, is this buffer meant only for internal use in your function or is it meant

Re: Greedy memory handling

2013-09-11 Thread Dmitry Olshansky
11-Sep-2013 17:33, Joseph Rushton Wakeling пишет: On 11/09/13 15:13, monarch_dodra wrote: That's somewhat better, as it would allow the GC to collect my buffer, if it wants to, but I wouldn't actually know about it afterwards which leaves me screwed. Just to clarify, is this buffer meant only

Re: Greedy memory handling

2013-09-11 Thread Joseph Rushton Wakeling
On 11/09/13 15:45, Dmitry Olshansky wrote: Problem is - said GC-freed memory could be then reused in some way. I can't imagine how you'd test that the block that is allocated is *still your old* block. Ahh, nasty. I'd assumed that the buffer would have been reset to null in the event that th

Re: Greedy memory handling

2013-09-11 Thread monarch_dodra
On Wednesday, 11 September 2013 at 13:33:23 UTC, Joseph Rushton Wakeling wrote: On 11/09/13 15:13, monarch_dodra wrote: That's somewhat better, as it would allow the GC to collect my buffer, if it wants to, but I wouldn't actually know about it afterwards which leaves me screwed. Just to clar

Re: Greedy memory handling

2013-09-11 Thread Jacob Carlborg
On 2013-09-11 10:06, monarch_dodra wrote: I have a function that will *massively* benefit from having a persistent internal buffer it can re-use (and grow) from call to call, instead of re-allocating on every call. What I don't want is either of: 1. To set a fixed limitation of size, if the user

Re: Greedy memory handling

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 08:27:59AM +0200, Jacob Carlborg wrote: > On 2013-09-11 10:06, monarch_dodra wrote: > >I have a function that will *massively* benefit from having a > >persistent internal buffer it can re-use (and grow) from call to > >call, instead of re-allocating on every call. > > > >Wh

Re: Greedy memory handling

2013-09-12 Thread Dmitry Olshansky
12-Sep-2013 17:51, H. S. Teoh пишет: On Thu, Sep 12, 2013 at 08:27:59AM +0200, Jacob Carlborg wrote: On 2013-09-11 10:06, monarch_dodra wrote: I have a function that will *massively* benefit from having a persistent internal buffer it can re-use (and grow) from call to call, instead of re-alloc

Re: Greedy memory handling

2013-09-12 Thread Dmitry Olshansky
12-Sep-2013 20:51, H. S. Teoh пишет: On Thu, Sep 12, 2013 at 07:50:25PM +0400, Dmitry Olshansky wrote: 12-Sep-2013 17:51, H. S. Teoh пишет: [...] struct WeakPointer(T) { enum size_t mask = 0xdeadbeef; union Impl { T* ptr;

Re: Greedy memory handling

2013-09-12 Thread monarch_dodra
On Thursday, 12 September 2013 at 19:13:40 UTC, Dmitry Olshansky wrote: Double indirection? Allocate a class that has finalizer, hold that via weak-ref. The wrapper in turn contains a pointer to the buffer. The interesting point then is that one may allocate said buffer via C's realloc. Then

Re: Greedy memory handling

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 07:50:25PM +0400, Dmitry Olshansky wrote: > 12-Sep-2013 17:51, H. S. Teoh пишет: [...] > > struct WeakPointer(T) { > > enum size_t mask = 0xdeadbeef; > > union Impl { > > T* ptr; > > size_t uintVal; > >

Re: Greedy memory handling

2013-09-12 Thread Jacob Carlborg
On 2013-09-12 15:51, H. S. Teoh wrote: The problem is, he wants to reuse the buffer next time if the GC hasn't collected it yet. I was thinking he could reuse the stack/static buffer. Basically using two buffers, one static and one dynamic. -- /Jacob Carlborg

Re: Greedy memory handling

2013-09-12 Thread H. S. Teoh
On Thu, Sep 12, 2013 at 11:13:30PM +0400, Dmitry Olshansky wrote: > 12-Sep-2013 20:51, H. S. Teoh пишет: > >On Thu, Sep 12, 2013 at 07:50:25PM +0400, Dmitry Olshansky wrote: [...] > >>Better option is to have finalizer hooked up to set some flag. Then > >>_after_ restoring the pointer we consult th

Re: Greedy memory handling

2013-09-12 Thread Dmitry Olshansky
13-Sep-2013 00:11, H. S. Teoh пишет: On Thu, Sep 12, 2013 at 11:13:30PM +0400, Dmitry Olshansky wrote: 12-Sep-2013 20:51, H. S. Teoh пишет: On Thu, Sep 12, 2013 at 07:50:25PM +0400, Dmitry Olshansky wrote: [...] Better option is to have finalizer hooked up to set some flag. Then _after_ resto