Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tue, 14 Jul 2015 08:33:56 +, Nordlöw wrote: > On Wednesday, 8 July 2015 at 05:14:48 UTC, ketmar wrote: >> so while there is nothing wrong in creating slices from pointers, >> programmer should be aware of some potential pitfalls. > > I believe there's a DIP (involving the scope keyword) related to this > problem, right? yes, but i never get it. signature.asc Description: PGP signature
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Wednesday, 8 July 2015 at 05:14:48 UTC, ketmar wrote: so while there is nothing wrong in creating slices from pointers, programmer should be aware of some potential pitfalls. I believe there's a DIP (involving the scope keyword) related to this problem, right?
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tuesday, 7 July 2015 at 12:26:33 UTC, Per Nordlöw wrote: I'm currently developing a high-level wrapper for FFMPEG at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d My question now becomes how to most easily wrap the iteration over streams at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L150 https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L152 in a nice D-style range interface. Do I have to allocate a new D array and copy the `AVStream*` elements into that or is there a convenience wrapper for constructing a lazy range from a C style Array-pointer plus array-length? Note that the elements of the C array are pointers to structs, in this case instances of `AVStream`. This, of course, complicates the matter with regard to GC-safety. Comments on that please :) I think a wrapper is quite easy to do, isn't it? You just need to implement front popFront and empty and you're done. So you avoid slice-related problems...
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tue, 07 Jul 2015 12:36:09 +, Rikki Cattermole wrote: > I'm not aware of much docs regarding these sort of tricks. But I believe > it was Adam who originally discovered this little trick. > Atleast I'm pretty sure he is how I learnt about it. On D.learn as well. i bet this trick was planned from the moment of introduction of slices in D. yet there is something that makes it dangerous: user is free to change ".length" of the "slice wrapper". and as GC doesn't manage the memory slite is pointing to, it will copy slice contents. the program definitely will not crash, but if copied data had some pointers to that copied data (sometimes C program does this)... besides, one cannot use ".ptr" to `free ()` memory anymore. so while there is nothing wrong in creating slices from pointers, programmer should be aware of some potential pitfalls. signature.asc Description: PGP signature
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tuesday, 7 July 2015 at 12:33:23 UTC, Per Nordlöw wrote: On Tuesday, 7 July 2015 at 12:29:04 UTC, Rikki Cattermole wrote: size_t count; AVStream* thePtr; AVStream[] array = thePtr[0 .. count]; That should work. Thanks. Will that reuse the existing allocate memory at `thePtr` for internal storage of the D array? If so how is the GC aware of this memory? Is there any tutorials, reference documentation, etc on these matters? Slicing never* allocates a new array. The GC is only aware of memory it allocates itself and memory it is explicitly told about (see core.memory) Slicing pointers etc. should all be covered in http://dlang.org/arrays.html, http://dlang.org/d-array-article.html, http://ddili.org/ders/d.en/arrays.html and http://ddili.org/ders/d.en/slices.html *unless you're using operator overloading, in which case it can do anything of course
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tuesday, 7 July 2015 at 12:36:11 UTC, Rikki Cattermole wrote: If you need to confirm that it is doing that, just @nogc it. If it allocates it'll fail. I could tag it as @nogc. Thx.
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tuesday, 7 July 2015 at 12:33:23 UTC, Per Nordlöw wrote: On Tuesday, 7 July 2015 at 12:29:04 UTC, Rikki Cattermole wrote: size_t count; AVStream* thePtr; AVStream[] array = thePtr[0 .. count]; That should work. Thanks. Will that reuse the existing allocate memory at `thePtr` for internal storage of the D array? If so how is the GC aware of this memory? Is there any tutorials, reference documentation, etc on these matters? To my knowledge it should reuse the memory and not inform the GC about it. It basically makes D treat that pointer like a slice. If you need to confirm that it is doing that, just @nogc it. If it allocates it'll fail. I'm not aware of much docs regarding these sort of tricks. But I believe it was Adam who originally discovered this little trick. Atleast I'm pretty sure he is how I learnt about it. On D.learn as well.
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On Tuesday, 7 July 2015 at 12:29:04 UTC, Rikki Cattermole wrote: size_t count; AVStream* thePtr; AVStream[] array = thePtr[0 .. count]; That should work. Thanks. Will that reuse the existing allocate memory at `thePtr` for internal storage of the D array? If so how is the GC aware of this memory? Is there any tutorials, reference documentation, etc on these matters?
Re: Wrapping a C-style Array (Pointer + Length) in a Range Interface
On 8/07/2015 12:26 a.m., "Per =?UTF-8?B?Tm9yZGzDtnci?= " wrote: I'm currently developing a high-level wrapper for FFMPEG at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d My question now becomes how to most easily wrap the iteration over streams at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L150 https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L152 in a nice D-style range interface. Do I have to allocate a new D array and copy the `AVStream*` elements into that or is there a convenience wrapper for constructing a lazy range from a C style Array-pointer plus array-length? Note that the elements of the C array are pointers to structs, in this case instances of `AVStream`. This, of course, complicates the matter with regard to GC-safety. Comments on that please :) size_t count; AVStream* thePtr; AVStream[] array = thePtr[0 .. count]; That should work.
Wrapping a C-style Array (Pointer + Length) in a Range Interface
I'm currently developing a high-level wrapper for FFMPEG at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d My question now becomes how to most easily wrap the iteration over streams at https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L150 https://github.com/nordlow/justd/blob/master/tests/t_ffmpeg.d#L152 in a nice D-style range interface. Do I have to allocate a new D array and copy the `AVStream*` elements into that or is there a convenience wrapper for constructing a lazy range from a C style Array-pointer plus array-length? Note that the elements of the C array are pointers to structs, in this case instances of `AVStream`. This, of course, complicates the matter with regard to GC-safety. Comments on that please :)