Re: How to allocate/free memory under @nogc
On Thursday, 21 May 2020 at 15:09:57 UTC, Steven Schveighoffer wrote: On 5/20/20 10:50 PM, data pulverizer wrote: how do you allocate/free memory without using the garbage collector? Use C malloc and free. Does allocating and freeing memory using `GC.malloc` and `GC.free` avoid D's garbage collector? No, an allocation can trigger a collection. D does not have a scheduled GC, it basically runs the GC when it can't allocate any more memory from it's pools before it tries to get more from the OS. I *think* that @nogc is supposed to mean "Be able to run this without a GC implemented", not that no collections will run. -Steve Hi all! I will try to ask again(previous two posts still have no answers) : are there any site/page/docs somewhere to track actual info about @nogc support in language itself and in phobos library? Or, at least plans to extend such support?
How to move from Unique(someClass) to Unique(someInterface)?
import std.stdio; import automem; import std.experimental.allocator.mallocator : Mallocator; interface IGetInt { @nogc int GetInt(); } class Foo : IGetInt { @nogc int GetInt() { return 42; } } @nogc void main() { auto foo = Unique!(Foo, Mallocator).construct; printf("Value is %d!\n", foo.GetInt()); // Unique!(IGetInt, Mallocator) ifoo = Unique!(Foo, Mallocator).construct; // printf("Value is %d!\n", ifoo.GetInt()); } How to cast from a class to an interface in such situation? Is it possible? And one more question: Where can i find actual status(or plans) of @nogc support for Phobos and language constructs?
Right ways to work without gc without betterC.
I saw docs for std.experimental.allocator and also had found automem library(https://code.dlang.org/packages/automem) for c++ style memory management via smartpointers. From GC documentation std.experimental.allocator can not be used for: NewExpression Array appending Array concatenation Array literals (except when used to initialize static data) Associative array literals Any insertion, removal, or lookups in an associative array Extracting keys or values from an associative array Taking the address of (i.e. making a delegate to) a nested function that accesses variables in an outer scope A function literal that accesses variables in an outer scope An AssertExpression that fails its condition Is that actual information? Does someone work to implement switch between gc and non-gc approach? I learned for now automem's smartpointers which based on std.experimental.allocator can be used to store structs/classes. Does automem(or std.experimental.allocator) has support for inherited classes, polymorphism like std::unique_ptr (or base class) in c++? Does Phobos support std.experimental.allocator? Are there another gotchas with "c++ like" memory management way in D? P.S. Sorry for my bad english.
Re: Multithreading in D
Are you looking for parallel? http://dlang.org/library/std/parallelism/parallel.html I have seen this, but I'm not sure how to use it. Maybe: float[][] maps = new float[#threads][resolution * resolution]; foreach(i, ref elem; parallel(maps)){ elem = generateTerrain(...); } Does this look right?
Multithreading in D
Hello D-World, I've written a small terraingenerator in D based on the Hill-Algorithm. To generate a terrain I only need to call the method generateTerrain(...) which returns a float-Array containing the height of each pixel (2D Array mapped with a 1D array with length resolution^2). What I'd like to do: Generate #treads seperate maps in a own thread and combine and normalize them afterwards. I have little to no experience working with multiple threads. I tried it once in java for University but did not really get working. What I imagine as solution (I know it won't work this way, but to give you a better idea): for(int i = 0; i #threads; i++){ runInThread(generateTerrain(...)); } Greetings and thanks in advance Konstantin