scoped allocations

Namespace rswhite4 at googlemail.com
Thu Nov 28 10:22:58 PST 2013


On Thursday, 28 November 2013 at 17:12:01 UTC, Denis Shelomovskij 
wrote:
> 27.11.2013 3:33, Namespace пишет:
>> First of all: I apologize for my bad english.
>>
>> In the last few weeks I searched for a way to allocate nicely 
>> temporary
>> buffer of unknown lengths.
>
> You can use `unstd.memory.allocation.tempAlloc` [1]. Also there 
> is `unstd.c.string.tempCString` [2] for common case of 
> temporary C strings.
>
> [1] 
> http://denis-sh.bitbucket.org/unstandard/unstd.memory.allocation.html#tempAlloc
> [2] 
> http://denis-sh.bitbucket.org/unstandard/unstd.c.string.html#tempCString

Currently I use something like:
----
import std.stdio;
import core.memory : GC;

struct scoped(A : T[], T) {
	T[] arr;

	this(T[] arr) {
		this.arr = arr;
		writefln("Allocate %d %s's (ptr = %x)", arr.length, T.stringof, 
arr.ptr);
	}

	alias arr this;

	@disable
	this(this);

	~this() {
		writefln("Deallocate %d %s's (ptr = %x)", this.arr.length, 
T.stringof, this.arr.ptr);
		version(none) {
			GC.free(this.arr.ptr);
		} else {
			delete this.arr;
		}

		GC.minimize();
		this.arr = null;
	}
}

void main() {
	scoped!(int[]) bytes = new int[100_000];
}
----

It's the nearest to scope int[] bytes = new int[100_000]; I could 
find.


More information about the Digitalmars-d mailing list