std.stream.File help required (and classes)

akaz nemo at utopia.com
Wed Mar 28 04:38:03 PDT 2012


> > I am a bit lost between pointers
> > (s->x or (*s).x) and values (s.x). For structures there are
> pointers,
> > for classes there are no pointers?
>
> Yes.
>
> Ali

And migrating from std.stream.File (which was a class) to 
std.stdio.File (which is a structure) lost me completely.

Why, in fact, std.stream.File is a structure and not a class?

Citing: http://dlang.org/phobos/std_stdio.html

struct File;
Encapsulates a FILE*. Generally D does not attempt to provide 
thin wrappers over equivalent functions in the C standard 
library, but manipulating FILE* values directly is unsafe and 
error-prone in many ways. The File type ensures safe 
manipulation, automatic file closing, and a lot of convenience.

  The underlying FILE* handle is maintained in a reference-counted 
manner, such that as soon as the last File variable bound to a 
given FILE* goes out of scope, the underlying FILE* is 
automatically closed.

May I disable that reference counting?

I do not get how to use GC.addRange(). Can you, please, direct me 
towards an example?

You seem to be right, the f and f.data variables are allocated 
into my C code, so they are invisible to the garbage collector.

BUT!!! Is there any way to disable that garbage collector 
straight from the beginning? Maybe a compiler flag?

I should also add that I allocated my structure with:

(init)
s.filedesc = cast(File*)GC.calloc(1,File.sizeof);

(open)
(*(s.filedesc)).open(*(cast(string*)arg),"w+b");

but without success, since the MSF_State* 
s=ms_new!(MSF_State)(1); line allocating the memory for s is 
esentially a C library function (a wrapper around it):

extern(C){
     //...
     void* ortp_malloc(size_t sz);
     T* ortp_new(T)(int count){
         return cast(T*)ortp_malloc(T.sizeof*count);
     }
     alias ortp_new ms_new;
     //...
}

so that allocation is not visible to the garbage collector.

My code has so many pointers because I took it from C, directly. 
Many of those pointers are also imposed by the underlying (C) 
library. I had no intention to extensively re-write my code, just 
to port it.



More information about the Digitalmars-d-learn mailing list