www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting void* to D array

reply "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
Hi.
I want to call a C library function that returns a data buffer as 
a void*.  How do I convert the resulting void* into something I 
can process in D?

//I have the following function from the GDAL C library.
extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* 
);


So I have (GByte is defined in the GDAL library):

void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size 
);

I fill the buffer (and ignore any errors :o)

GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer );


Now, how can I access the data in buffer?
Apr 14 2015
next sibling parent Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Wed, 15 Apr 2015 04:24:20 +0000
Craig Dillabaugh via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data buffer as 
 a void*.  How do I convert the resulting void* into something I 
 can process in D?
 
 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* 
 );
 
 
 So I have (GByte is defined in the GDAL library):
 
 void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size 
 );
 
 I fill the buffer (and ignore any errors :o)
 
 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer );
 
 
 Now, how can I access the data in buffer?
auto buf = (cast(GByte *)buffer)[0 .. xblock * yblock];
Apr 14 2015
prev sibling parent reply Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn writes:
On Wed, 15 Apr 2015 04:24:20 +0000
Craig Dillabaugh via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data buffer as 
 a void*.  How do I convert the resulting void* into something I 
 can process in D?
 
 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* 
 );
 
 
 So I have (GByte is defined in the GDAL library):
 
 void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size 
 );
 
 I fill the buffer (and ignore any errors :o)
 
 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer );
 
 
 Now, how can I access the data in buffer?
Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Apr 14 2015
next sibling parent reply "Daniel Kozak" <kozzi11 gmail.com> writes:
On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:
 On Wed, 15 Apr 2015 04:24:20 +0000
 Craig Dillabaugh via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data buffer 
 as a void*.  How do I convert the resulting void* into 
 something I can process in D?
 
 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, 
 void* );
 
 
 So I have (GByte is defined in the GDAL library):
 
 void* buffer = malloc( GByte.sizeof * x_block_size * 
 y_block_size );
 
 I fill the buffer (and ignore any errors :o)
 
 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, 
 buffer );
 
 
 Now, how can I access the data in buffer?
Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
But in this case memory will be scan by GC. Which probably is not something what you want.
Apr 14 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/15/15 12:47 AM, Daniel Kozak wrote:
 On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:
 On Wed, 15 Apr 2015 04:24:20 +0000
 Craig Dillabaugh via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data buffer as a
 void*.  How do I convert the resulting void* into something I can
 process in D?

 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* );


 So I have (GByte is defined in the GDAL library):

 void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size );

 I fill the buffer (and ignore any errors :o)

 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer );


 Now, how can I access the data in buffer?
Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
But in this case memory will be scan by GC. Which probably is not something what you want.
Depends on what GByte is. If it doesn't contain pointers (I'm assuming its probably ubyte?), then it won't be scanned. If you still want to use malloc, but in a safe way, you can do: immutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr); -Steve
Apr 15 2015
next sibling parent reply "CraigDillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer 
wrote:
 On 4/15/15 12:47 AM, Daniel Kozak wrote:
 On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák 
 wrote:
 On Wed, 15 Apr 2015 04:24:20 +0000
 Craig Dillabaugh via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data 
 buffer as a
 void*.  How do I convert the resulting void* into something 
 I can
 process in D?

 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, 
 void* );


 So I have (GByte is defined in the GDAL library):

 void* buffer = malloc( GByte.sizeof * x_block_size * 
 y_block_size );

 I fill the buffer (and ignore any errors :o)

 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, 
 buffer );


 Now, how can I access the data in buffer?
Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
But in this case memory will be scan by GC. Which probably is not something what you want.
Depends on what GByte is. If it doesn't contain pointers (I'm assuming its probably ubyte?), then it won't be scanned. If you still want to use malloc, but in a safe way, you can do: immutable blocksize = GByte.sizeof * x_block_size * y_block_size; auto buffer = malloc(blocksize)[0..blocksize]; Also, you don't need to cast pointers to void *. Should be able to do: GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr); -Steve
Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?
Apr 15 2015
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/15/15 8:35 AM, CraigDillabaugh wrote:
 On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer wrote:
 immutable blocksize = GByte.sizeof * x_block_size * y_block_size;
 auto buffer = malloc(blocksize)[0..blocksize];

 Also, you don't need to cast pointers to void *. Should be able to do:

 GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, buffer.ptr);
Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?
malloc returns void *, but I am applying the slice operator which turns a pointer into a slice. I'll split it up: auto bufptr = malloc(blocksize); // typeof(bufptr) = void * auto buffer = bufptr[0..blocksize]; // typeof(buffer) = void[] -Steve
Apr 15 2015
parent reply "CraigDillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 15 April 2015 at 14:02:38 UTC, Steven Schveighoffer 
wrote:
 On 4/15/15 8:35 AM, CraigDillabaugh wrote:
 On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven 
 Schveighoffer wrote:
 immutable blocksize = GByte.sizeof * x_block_size * 
 y_block_size;
 auto buffer = malloc(blocksize)[0..blocksize];

 Also, you don't need to cast pointers to void *. Should be 
 able to do:

 GDALReadBlock(AGDALRasterBandHInstance, xblock, yblock, 
 buffer.ptr);
Thanks for the pointers (no pun intended!) Just out of curiosity, what is the type of 'buffer'? 'malloc' returns a void* but using the .ptr suggests 'buffer' is an array. Is the return of malloc automatically converted to an array?
malloc returns void *, but I am applying the slice operator which turns a pointer into a slice. I'll split it up: auto bufptr = malloc(blocksize); // typeof(bufptr) = void * auto buffer = bufptr[0..blocksize]; // typeof(buffer) = void[] -Steve
Nice. Thanks. I didn't realize you can slice a bare pointer like that. Does druntime have any way of making sure that is safe, or are you on your own?
Apr 15 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 4/15/15 11:02 AM, CraigDillabaugh wrote:
 Nice.  Thanks.  I didn't realize you can slice a bare pointer like that.

 Does druntime have any way of making sure that is safe, or are you on
 your own?
No, druntime cannot know what the pointer actually points at. This would not work in safe code. It's on you to make sure it's valid. -Steve
Apr 15 2015
prev sibling parent "CraigDillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 15 April 2015 at 11:18:03 UTC, Steven Schveighoffer 
wrote:
clip
 Depends on what GByte is. If it doesn't contain pointers (I'm 
 assuming its probably ubyte?), then it won't be scanned.
clip
 -Steve
Yes, GByte is an alias for ubyte.
Apr 15 2015
prev sibling parent "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote:
 On Wed, 15 Apr 2015 04:24:20 +0000
 Craig Dillabaugh via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> wrote:

 Hi.
 I want to call a C library function that returns a data buffer 
 as a void*.  How do I convert the resulting void* into 
 something I can process in D?
 
 //I have the following function from the GDAL C library.
 extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, 
 void* );
 
 
 So I have (GByte is defined in the GDAL library):
 
 void* buffer = malloc( GByte.sizeof * x_block_size * 
 y_block_size );
 
 I fill the buffer (and ignore any errors :o)
 
 GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, 
 buffer );
 
 
 Now, how can I access the data in buffer?
Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr );
Thank you very much. Works like a charm. Make that last line: GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock,
 cast(void*)buffer.ptr );
Apr 14 2015