473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

copy from pointer to pointer

I need to make a quick raw copy of a large block of data from one pointer to
another.
Marshal.Copy let me copy from pointer to array and another call can take it
from aray to pointer. That is obviously not optimal.

I can however not find any way of copying BLOCKS of data from pointer to
pointer... apart from a loop and a load of copy instructions.

What is the method for copying a large block of data from here to there
given two pointers?
....hm.. I guess I could make an unmanaged c++ function and do a memcpy, but
that seems a bit odd
Jul 8 '07 #1
10 12434
>I need to make a quick raw copy of a large block of data from one pointer to
>another.
Marshal.Copy let me copy from pointer to array and another call can take it
from aray to pointer. That is obviously not optimal.

I can however not find any way of copying BLOCKS of data from pointer to
pointer... apart from a loop and a load of copy instructions.

What is the method for copying a large block of data from here to there
given two pointers?
...hm.. I guess I could make an unmanaged c++ function and do a memcpy, but
that seems a bit odd
You can call RtlMoveMemory in kernel32.dll with PInvoke.

[DllImport("kernel32.dll")]
static extern void RtlMoveMemory(IntPtr dest, IntPtr src, uint len);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 8 '07 #2
You can call RtlMoveMemory in kernel32.dll with PInvoke.
>
[DllImport("kernel32.dll")]
static extern void RtlMoveMemory(IntPtr dest, IntPtr src, uint len);
Ok.. Not wrapping c++ but invoking win32. It is a little less messy, but
still... Is there really no native way of doing this in c#? When one can
write unsafe code and move data arround through pointers, it seems natural
to have some featurein the language/framework which allows for a block copy
from pointer to pointer?

Oh well... you gave a solution. Thanks. I will try it. After all, it is
clean _enough_ and should work :-)
Jul 10 '07 #3
"Jason Who" <a@b.dewrote in message
news:46**********************@dreader2.cybercity.d k...
Ok.. Not wrapping c++ but invoking win32. It is a little less messy, but
still... Is there really no native way of doing this in c#? When one can
write unsafe code and move data arround through pointers, it seems natural
to have some featurein the language/framework which allows for a block
copy from pointer to pointer?
I thought the same thing but couldn't find anything. I'd wondered if I was
missing something but with 3 people confirming the same thing here I guess
I'm not.

Michael
Jul 10 '07 #4
Jason Who wrote:
I need to make a quick raw copy of a large block of data from one pointer to
another.
Marshal.Copy let me copy from pointer to array and another call can take it
from aray to pointer. That is obviously not optimal.

I can however not find any way of copying BLOCKS of data from pointer to
pointer... apart from a loop and a load of copy instructions.

What is the method for copying a large block of data from here to there
given two pointers?
...hm.. I guess I could make an unmanaged c++ function and do a memcpy, but
that seems a bit odd
How about a simple loop?

while (count-- 0) (p2++)* = (p1++)*;

--
Göran Andersson
_____
http://www.guffa.com
Jul 10 '07 #5
"Jason Who" <a@b.dewrote in message
news:46**********************@dreader2.cybercity.d k...
>You can call RtlMoveMemory in kernel32.dll with PInvoke.

[DllImport("kernel32.dll")]
static extern void RtlMoveMemory(IntPtr dest, IntPtr src, uint len);

Ok.. Not wrapping c++ but invoking win32. It is a little less messy, but
still... Is there really no native way of doing this in c#? When one can
write unsafe code and move data arround through pointers, it seems natural
to have some featurein the language/framework which allows for a block
copy from pointer to pointer?

Oh well... you gave a solution. Thanks. I will try it. After all, it is
clean _enough_ and should work :-)
If you want a "pure" managed (but slower) solution, you can always use
Marshal.ReadInt64 and Marshal.WriteInt64 like this:

int bufferSize = ......;
IntPtr dest = Marshal.AllocHGlobal(bufferSize);
IntPtr src = Marshal.AllocHGlobal(bufferSize);
int size1 = bufferSize % 8;
int size8 = bufferSize - size1;
int ofs;
// copy multiples of 8 bytes first
for(ofs = 0; ofs < size8; ofs += 8)
Marshal.WriteInt64(dest, ofs, Marshal.ReadInt64(src, ofs));
// copy remaining bytes
for(int n = 0; n < size1; n++,ofs++)
Marshal.WriteByte(dest, ofs, Marshal.ReadByte(src, ofs));

Willy.
Jul 10 '07 #6
If you want a "pure" managed (but slower) solution, you can always use
Marshal.ReadInt64 and Marshal.WriteInt64 like this:
That is larger blocks then one byte, but stille it is a large number of
small reads and writes and not a single large copy.
Yes, I realize that the cpu can not in one operation copy a thousand
bytes.It does however have operations such as movsw which is optimized for
looping over a large number of copies and which is faster than the generic
read and write operations done a number of times.

In a way i do not "want" managed and slow. I was simply suprised that there
are no operations for the task built into .net.
Jul 10 '07 #7
"Jason Who" <a@b.dewrote in message
news:46***********************@dreader1.cybercity. dk...
>If you want a "pure" managed (but slower) solution, you can always use
Marshal.ReadInt64 and Marshal.WriteInt64 like this:

That is larger blocks then one byte, but stille it is a large number of
small reads and writes and not a single large copy.
Yes, I realize that the cpu can not in one operation copy a thousand
bytes.It does however have operations such as movsw which is optimized for
looping over a large number of copies and which is faster than the generic
read and write operations done a number of times.
Yep, "rep movsd" is what is used by memcopy (RtlMoveMemory), now, what takes
time is storing and loading the data to/from main memory, whether you do it
with one single REP instruction or in a loop is a nonissue.
To give you an idea, the "RtlMoveMemory" version is less than twice as fast
as the "managed version" (400MB/s vs. 790MB/s on my box). If this is too
slow, well, then you will have to call "RtlMoveMemory".
In a way i do not "want" managed and slow. I was simply suprised that
there are no operations for the task built into .net.
Managed code primary deals with "managed" memory, unmanaged memory is only
dealt with in interop scenarios, that is moving data across the
unmanaged/managed memory boundary, moving from unmanaged to unmanaged memory
was not high on the priority list when FCL team designed the Marshal class.
After all, PInvoke is part of .NET., so you can always call into unmanaged
code if you need to.

Willy.

Jul 10 '07 #8
To give you an idea, the "RtlMoveMemory" version is less than twice as
fast as the "managed version" (400MB/s vs. 790MB/s on my box). If this is
too slow, well, then you will have to call "RtlMoveMemory".
The point is that managed code is pointer to array or the reverse, while
what I need is pointer to pointer.
That means that I have to resort to a loop copying small blocks one at a
time and THAT is slow.
Managed code primary deals with "managed" memory, unmanaged memory is only
dealt with in interop scenarios, that is moving data across the
unmanaged/managed memory boundary, moving from unmanaged to unmanaged
memory was not high on the priority list when FCL team designed the
Marshal class. After all, PInvoke is part of .NET., so you can always call
into unmanaged code if you need to.
Yes, I can see the reason for it. Unmanaged to managed and managed to
unmanaged while unmanaged to unmanaged in a way is none of .net's business
Jul 11 '07 #9
The pointers doesn't have to be byte pointers. :)

No, but there are no built in datatypes which are more than 64 bit, i think,
so it doesnt make a lot of difference.
What will happen if defining a struct of a large number of bytes?
Jul 11 '07 #10
"Jason Who" <a@b.dewrote in message
news:46***********************@dreader1.cybercity. dk...
>To give you an idea, the "RtlMoveMemory" version is less than twice as
fast as the "managed version" (400MB/s vs. 790MB/s on my box). If this is
too slow, well, then you will have to call "RtlMoveMemory".

The point is that managed code is pointer to array or the reverse, while
what I need is pointer to pointer.
That means that I have to resort to a loop copying small blocks one at a
time and THAT is slow.
As I said before using "RtlMoveMemory" (or "rep movsd") is the fastest, but
using Marshal.ReadXXXX and WriteXXXX is only two times slower (worse case)
no matter the size of the blocks (assuming they are 64 bytes). If you
call this TOO SLOW, then you know what you'll have to do, don't you?

Willy.


Jul 11 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
25
by: MC | last post by:
Hi I have the following piece of code. int *p; p = malloc(10); int *j; j = p; free(p); Is this legal ? will j still point to the same
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
4
by: prasad8619 | last post by:
please help me out in this regard as this is urgent to me because i stuck in the middle of a program....... here I have a program like this...... char name="rajesh"; char...
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.