Thread overview
Memory Allocation
Mar 29, 2017
Enigma
Mar 29, 2017
Faux Amis
Mar 29, 2017
Faux Amis
Mar 29, 2017
Enigma
Mar 29, 2017
H. S. Teoh
Mar 30, 2017
Enigma
Mar 30, 2017
Gary Willoughby
Mar 30, 2017
Enigma
March 29, 2017
I have a memory buffer allocated using different methods. It is simply a pointer and a size.

I would like to be able to manage this buffer by treating it as a memory pool or heap. I think I can use allocators to do this but not sure how.

Effectively I want something like new or malloc but it pulls from the memory buffer rather than the program heap.

// Allocated once at program start
void* FancyBuffer = FancyAlloc(1000);


Then when I want to use the buffer I'll do stuff like



auto myptr = FancyMalloc(10);

....

FancyFree(myptr);


or whatever.

The main thing is, I don't want to have to write my own allocator to manage this buffer as it seems that D's allocators would do a better job.

I imagine that most of the time the buffer will not have more than one piece of code using it(no overlapping uses) but since I won't be 100% sure, I need allow for the cases where there might be overlapping usage. (else I wouldn't ever have to worry about "allocating or releasing" from it.

Thanks.



March 29, 2017
On 2017-03-29 21:19, Enigma wrote:
> I have a memory buffer allocated using different methods. It is simply a
> pointer and a size.
>
Can you maybe just tread it like an array and slice it for allocation?

March 29, 2017
On 2017-03-29 23:30, Faux Amis wrote:
> On 2017-03-29 21:19, Enigma wrote:
>> I have a memory buffer allocated using different methods. It is simply a
>> pointer and a size.
>>
> Can you maybe just tread it like an array and slice it for allocation?
>
*treat*
March 29, 2017
On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
> I have a memory buffer allocated using different methods. It is simply a pointer and a size.
>
> I would like to be able to manage this buffer by treating it as a memory pool or heap. I think I can use allocators to do this but not sure how.
>
> Effectively I want something like new or malloc but it pulls from the memory buffer rather than the program heap.
>
> // Allocated once at program start
> void* FancyBuffer = FancyAlloc(1000);
>
>
> Then when I want to use the buffer I'll do stuff like
>
>
>
> auto myptr = FancyMalloc(10);
>
> ....
>
> FancyFree(myptr);
>
>
> or whatever.
>
> The main thing is, I don't want to have to write my own allocator to manage this buffer as it seems that D's allocators would do a better job.
>
> I imagine that most of the time the buffer will not have more than one piece of code using it(no overlapping uses) but since I won't be 100% sure, I need allow for the cases where there might be overlapping usage. (else I wouldn't ever have to worry about "allocating or releasing" from it.
>
> Thanks.

It looks like you are looking for this: http://dlang.org/phobos-prerelease/std_experimental_allocator_building_blocks_region.html.
March 29, 2017
On Wednesday, 29 March 2017 at 21:36:14 UTC, Petar Kirov [ZombineDev] wrote:
> On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
>> [...]
>
> It looks like you are looking for this: http://dlang.org/phobos-prerelease/std_experimental_allocator_building_blocks_region.html.

But these seem to require passing a mallocator. I simply want to pass an already allocated region/block/buffer and have the allocators use it. I will allocate and free on my own.
March 29, 2017
On Wed, Mar 29, 2017 at 11:01:12PM +0000, Enigma via Digitalmars-d-learn wrote:
> On Wednesday, 29 March 2017 at 21:36:14 UTC, Petar Kirov [ZombineDev] wrote:
> > On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
> > > [...]
> > 
> > It looks like you are looking for this: http://dlang.org/phobos-prerelease/std_experimental_allocator_building_blocks_region.html.
> 
> But these seem to require passing a mallocator. I simply want to pass an already allocated region/block/buffer and have the allocators use it. I will allocate and free on my own.

Huh?  Where does it say that a mallocator is required?

As far as I can tell, you could simply do this:

	void[] myBuffer = ...;
	auto allocator = Region!()(myBuffer);
	auto p = allocator.allocate(...);

The default parent allocator is NullAllocator, which does nothing, so that leaves the management of myBuffer entirely up to you.


T

-- 
Life would be easier if I had the source code. -- YHL
March 30, 2017
On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
> I have a memory buffer allocated using different methods. It is simply a pointer and a size.
>
> I would like to be able to manage this buffer by treating it as a memory pool or heap. I think I can use allocators to do this but not sure how.

You can re-task the GC to use an arena if that's suitable:

https://bitbucket.org/infognition/dstuff/src/1dca752af1021ed5998bb041004465674695113f/gcarena.d?fileviewer=file-view-default
March 30, 2017
On Wednesday, 29 March 2017 at 23:26:04 UTC, H. S. Teoh wrote:
> On Wed, Mar 29, 2017 at 11:01:12PM +0000, Enigma via Digitalmars-d-learn wrote:
>> On Wednesday, 29 March 2017 at 21:36:14 UTC, Petar Kirov [ZombineDev] wrote:
>> > [...]
>> 
>> But these seem to require passing a mallocator. I simply want to pass an already allocated region/block/buffer and have the allocators use it. I will allocate and free on my own.
>
> Huh?  Where does it say that a mallocator is required?
>
> As far as I can tell, you could simply do this:
>
> 	void[] myBuffer = ...;
> 	auto allocator = Region!()(myBuffer);
> 	auto p = allocator.allocate(...);
>
> The default parent allocator is NullAllocator, which does nothing, so that leaves the management of myBuffer entirely up to you.
>
>
> T

Thanks. All the examples show using a size rather than an array so I assumed that was the case.

March 30, 2017
On Thursday, 30 March 2017 at 11:22:05 UTC, Gary Willoughby wrote:
> On Wednesday, 29 March 2017 at 19:19:48 UTC, Enigma wrote:
>> I have a memory buffer allocated using different methods. It is simply a pointer and a size.
>>
>> I would like to be able to manage this buffer by treating it as a memory pool or heap. I think I can use allocators to do this but not sure how.
>
> You can re-task the GC to use an arena if that's suitable:
>
> https://bitbucket.org/infognition/dstuff/src/1dca752af1021ed5998bb041004465674695113f/gcarena.d?fileviewer=file-view-default

cool. thanks.