When I do a byte [] dStyleArray = buffer [0 .. * bufferSize], D allocates a new piece of memory and copies everything into a D-style array or points to an already allocated C-style array?
It just points to it. The slice operator on the right side is the same (conceptually) as array.pointer = &first_element; array._length = length; array.pointer = &first_element; array._length = length; - very fast and easy operation. (I called it _length instead of length btw, because setting the length property can actually call the function following.)
What happens when I do dStyleArray.length = dStyleArray.length + 16?
This will allocate new memory. When the length extends, if the runtime cannot prove that it is safe (or you tell her that she is, and she knows that she came from the GC), the array is copied to a new location. It basically calls realloc() pointer - although not literally, it is incompatible with C realloc.
Since it came from C, the runtime simply knows that it does not own the memory, that it is somehow managed elsewhere and will always allocate a new one when trying to expand. If you want to expand some other ways, you need to do it yourself.
When I make a buffer [0 .. dStyleArray.length] = dStyleArray [0 .. dStyleArray.length] ;, I copy the memory, right?
right, this copies because you cut the left side.
Is it possible to simply bind a D-style array to a C-style array and access the previously allocated memory through the D-style array interface?
Simple rectangular cut:
auto d_array = c_array[0 .. c_array_length];
handles it for all but length extensions. It holds the pointer, so writing to elements instantly affects the original thing. (By the way, since this is C's shared memory, make sure you don’t free it, and D is still using it! You should be fine while you use it only inside this function, but not saving the slice anywhere.)
If you need to lengthen the length, you need to do it yourself. The way I like to do this is to cut the entire potential array, full capacity, and then cut it again to get a window of limited capacity.
So maybe:
auto whole_array = buffer[0 .. bufferMaxSize];
The reason I did the whole_array thing instead of reslicing buffer is that D can catch the constraints for me. He does not do this on a bare pointer, but does on a threaded pointer, since he knows the maximum size as length.
If you need to expand the buffer, do it with the correct C function, like realloc or something else, and then cut the whole_addresses and part_youre_using again.