“new byte [132]”对齐16个字节 [英] "new byte[132]" alignment on 16 bytes

查看:95
本文介绍了“new byte [132]”对齐16个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将C ++的一些缓冲类代码移植到C#。

这个C ++类使用

m_pBuffer = new BYTE [...]分配一块内存。 ];


但由于该类也用于指向使用原始MMX

和SSE功率的功能,因此起始指针必须从16开始字节存储器

边界。

在C ++中我分配的内存比需要的多,在第二阶段我搜索

的地址开始一个16字节的边界。然后我使用新的

内存地址指针。


有没有办法让C#始终对齐byte [] Buffer = new byte [132];

动态到16字节的内存地址?

[StructLayout(LayoutKind。???)] ???


我不是在谈论&缓冲区的位置,而是关于

&(新字节[132])的位置。

它将是很好,如果我可以决定将我的代码的一部分对齐16

字节,其余的正常8字节或32字节,具体取决于需要和

不强制所有用途缓冲区在16个字节上是alignend ..


-
http://www.skyscan.be

I am porting some of my buffer class code for C++ to C#.
This C++ class allocates a block of memory using
m_pBuffer=new BYTE[...];

But since the class is also used for pointers for funtions that uses raw MMX
and SSE power, the starting pointer MUST be starting at a 16 byte memory
boundary.
In C++ I allocate more memory than needed, and in a second phase I search
for the address that starts on a 16 byte boundary. And I then use that new
memory address pointer.

Is there a way to make C# to always align a "byte[] Buffer=new byte[132];"
dynamically to a 16 byte memory address?
[StructLayout(LayoutKind.???)] ???

I am not talking about the location of &Buffer but about the location of
&(new byte[132]).
It would be nice if I could decide to make parts of my code aligned on 16
bytes and the rest on normal 8 bytes or 32 byte depending on the need and
not force all uses of Buffer to be alignend on 16 bytes..

--
http://www.skyscan.be

推荐答案

你究竟是什么意思C#始终对齐一个...." ;. C#没有对齐

任何东西。字节数组在托管堆上分配,这个分配

由CLR完成,应用程序代码无法控制对齐,也没有
的位置。

但是,由于你的函数是非托管代码,所以你必须将字节数组复制到非托管内存,没有人阻止你实现一些自定义编组的形式。


Willy。

" Olaf Baeyens" <醇********** @ skyscan.be>在消息中写道

news:41 *********************** @ news.skynet.be ...
What exactly do you mean with "C# to always align a....". C# doesn''t align
anything. Byte arrays are allocated on the managed heap and this allocation
is done by the CLR, the application code cannot control the alignment nor
the location.
However, as your functions are unmanaged code and as such, you have to copy
the byte arrays to unmanaged memory anyway, no-one stops you from
implementing some form of custom marshalling.

Willy.
"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41***********************@news.skynet.be...
我将C ++的一些缓冲类代码移植到C#。
这个C ++类使用
m_pBuffer = new BYTE [...]分配一块内存;
<但是由于该类也用于使用原始MMX
和SSE功率的功能指针,因此起始指针必须从16字节存储器边界开始。
在C ++中,我分配的内存超过了所需的内存,在第二阶段,我搜索了从16字节边界开始的地址。然后我使用新的
内存地址指针。

有没有办法让C#始终对齐byte [] Buffer = new byte [132];" [StructLayout(LayoutKind。???)] ???

我不是在讨论& Buffer的位置而是关于位置
&(新字节[132])。
如果我决定让我的代码的一部分在16个字节上对齐而其余部分在正常的8个字节或32个字节上,这将是很好的字节取决于需要和
不强制所有缓冲区的使用在16个字节上。

-
http://www.skyscan.be
I am porting some of my buffer class code for C++ to C#.
This C++ class allocates a block of memory using
m_pBuffer=new BYTE[...];

But since the class is also used for pointers for funtions that uses raw
MMX
and SSE power, the starting pointer MUST be starting at a 16 byte memory
boundary.
In C++ I allocate more memory than needed, and in a second phase I search
for the address that starts on a 16 byte boundary. And I then use that new
memory address pointer.

Is there a way to make C# to always align a "byte[] Buffer=new byte[132];"
dynamically to a 16 byte memory address?
[StructLayout(LayoutKind.???)] ???

I am not talking about the location of &Buffer but about the location of
&(new byte[132]).
It would be nice if I could decide to make parts of my code aligned on 16
bytes and the rest on normal 8 bytes or 32 byte depending on the need and
not force all uses of Buffer to be alignend on 16 bytes..

--
http://www.skyscan.be





" Olaf Baeyens" <醇********** @ skyscan.be>在消息中写道

news:41 *********************** @ news.skynet.be ...

"Olaf Baeyens" <ol**********@skyscan.be> wrote in message
news:41***********************@news.skynet.be...
我将C ++的一些缓冲类代码移植到C#。
这个C ++类使用
m_pBuffer = new BYTE [...]分配一块内存;
<但是由于该类也用于使用原始MMX
和SSE功率的功能指针,因此起始指针必须从16字节存储器边界开始。
在C ++中,我分配的内存超过了所需的内存,在第二阶段,我搜索了从16字节边界开始的地址。然后我使用新的
内存地址指针。

有没有办法让C#始终对齐byte [] Buffer = new byte [132];" [StructLayout(LayoutKind。???)] ???

我不是在讨论& Buffer的位置而是关于位置
&(新字节[132])。
如果我决定让我的代码的一部分在16个字节上对齐而其余部分在正常的8个字节或32个字节上,这将是很好的字节取决于需要和
不强制所有使用缓冲区在16个字节上对齐..

(注意,每个都使用不安全的代码,因为你可能需要,除非

你使用像VirtualAlloc这样的API,总是在页面上返回一个缓冲区

边界)

好​​吧,我不认为我会非常热衷于您需要的任何方法

来确定字节数组的位置,因为它们通常随着管理中的所有其他项目移动

AP。你可以使用固定在它们上面并使用

a指向数组,循环查找第一个16byte边界,

但这会强制GC在缓冲区周围移动对象。 ..没有nessecerily

pleasent你必须保持缓冲区固定,只要你打算使用

它的指针。


选项二:您可以分配非托管内存(通过Win32或

Marshal :: AllocHGlobal()方法)并扫描该指针,找到一个

边界。这可能是一个问题,因为它需要您手动释放缓冲区并且可能再次导致GC内存布局问题,但至少

缓冲区无法启动坐在托管堆的中间,虽然如果你将缓冲区维持在
太长的时间,理论上堆可以在它周围增长。

第三个选项,如果你的缓冲区是短暂的(在单个方法中,在
调用及其子代)和小的,你可以使用stackalloc,它分配

a内存缓冲区直接在堆栈上。再一次你必须走

指针并找到一个16byte的边界,但在这种情况下它不应该导致任何

托管堆的麻烦或需要额外的步骤清理成本更大的堆栈帧
并且在方法结束之前没有释放。


stackalloc语法示例(所有部分都是必需的,stackalloc int [ 132]确实

并不孤立):


int * buffer = stackalloc int [132]; -
http://www.skyscan.be
I am porting some of my buffer class code for C++ to C#.
This C++ class allocates a block of memory using
m_pBuffer=new BYTE[...];

But since the class is also used for pointers for funtions that uses raw
MMX
and SSE power, the starting pointer MUST be starting at a 16 byte memory
boundary.
In C++ I allocate more memory than needed, and in a second phase I search
for the address that starts on a 16 byte boundary. And I then use that new
memory address pointer.

Is there a way to make C# to always align a "byte[] Buffer=new byte[132];"
dynamically to a 16 byte memory address?
[StructLayout(LayoutKind.???)] ???

I am not talking about the location of &Buffer but about the location of
&(new byte[132]).
It would be nice if I could decide to make parts of my code aligned on 16
bytes and the rest on normal 8 bytes or 32 byte depending on the need and
not force all uses of Buffer to be alignend on 16 bytes..
(note, each of these uses unsafe code as you will probably have to, unless
you use an API like VirtualAlloc that always returns a buffer on a page
boundary)
Well, I don''t think I would be terribly keen on any method you would require
to determine the location of a byte array, as they are generally moved along
with all other items in the managed heap. YOu can use fixed on them and take
a pointer to the array, looping through to find the first 16byte boundry,
but that forces the GC to move objects around the buffer...not nessecerily
pleasent and you have to keep the buffer fixed as long as you intend to use
its pointer.

Option two: You could allocate unmanaged memory(via Win32 or the
Marshal::AllocHGlobal() method) and scan through that pointer, locating a
boundry. This is potentially an issue as it requires you to manually release
the buffer and can, again, cause GC memory layout problems, but atleast the
buffer doesn''t start out sitting in the middle of the managed heap, although
the heap could theoretically grow around it if you maintain the buffer for
too long.

The third option, if your buffers are short lived(within a single method
call and its children) and small, you could use stackalloc, which allocates
a memory buffer directly on the stack. Again you would have to walk the
pointer and find a 16byte boundary, but in this case it shouldn''t cause any
trouble with the managed heap or require extra steps to clean up at the cost
of a larger stackframe and no release until the method ends.

stackalloc syntax example(all parts are required, stackalloc int[132] does
not stand alone):

int *buffer = stackalloc int[132]; --
http://www.skyscan.be



>究竟是什么意思用C#始终对齐......。 C#没有对齐
> What exactly do you mean with "C# to always align a....". C# doesn''t align
任何东西。字节数组在托管堆上分配,这个
分配由CLR完成,应用程序代码无法控制对齐或位置。
但是,因为你的函数是非托管代码和这样,你必须
将字节数组复制到非托管内存,没有人阻止你实现某种形式的自定义编组。
anything. Byte arrays are allocated on the managed heap and this allocation is done by the CLR, the application code cannot control the alignment nor
the location.
However, as your functions are unmanaged code and as such, you have to copy the byte arrays to unmanaged memory anyway, no-one stops you from
implementing some form of custom marshalling.




我想创建一个byte [] Buffer = new byte [xxx];

并将此Buffer传递给执行Assembler SSE的非托管C ++函数

指令缓冲区,而不必复制内存块。

SSE指令集假设起始物理内存块是从可以被16位置的地址位置开始的
,否则它生成

异常。


复制内存块是没有选择的,因为这些是巨大的内存块。使用SSE的

意图只是为了加快计算速度,但如果我需要复制内存块,那么SSE就不会是我的解决方案。然后应该使用另一个

技术。


在代码示例中我看到[StructLayout(LayoutKind。???)]使用的方式

用于对齐结构。但是这会将缓冲区指针对齐,而不是数据的实际内存地址。

因此,如果它适用于结构,那么可能还有一种方法可以对齐

字节数组。


另一种方法是我仍然使用非托管C ++来分配对齐的内存

块,然后传递给它到C#,但我真的更喜欢C#可以

这样做。


-
http://www.skyscan.be


这篇关于“new byte [132]”对齐16个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆