|
I'm sorry but yes you do if you want that region to be backed by a memory mapped file.
otherwise you're just copying/serializing objects into that space at best.
to back an object with a memory mapped region of the process address space you'd have to be able to pin an object to a specific address - not really possible in a garbage collected system without some sort of significant work around.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
No you don't have to copy memory if you use a native pointer to a memory mapped file.
In C# a pointer like "int*" is the same as a pointer in C/C++. There is no difference. That pointer in C# will directly be pointing to your native memory buffer you allocated from a native Win32 or POSIX API.
Pointers in C# can point to literally anything just as they can in C/C++. That includes managed and unmanaged memory locations.
For example this psuedo C# code would work:
[DllImport("nativeLib")]
static unsafe extern int* CreateNativeMemoryMappedFile();
unsafe void WriteMemoryMappedFile()
{
int* nativeMemory = CreateNativeMemoryMappedFile();
nativeMemory[0] = 123;
if (nativeMemory[1] == 321) return;
}
|
|
|
|
|
you cannot create objects at that pointer.
ergo, no vmem backed objects.
you can do pointer ops, but then you're copying object data from the object (on GC heap) to the unmanaged heap.
If I do a memcpy, or pointer based assignment in a for loop, either way i'm copying the data.
and doing so, even if i did, is really no different than calling (filestream).Write(...) which would be more direct.
there is no way around it because of how a GC works.
if you think I'm wrong show me how to *create an object* at a specific address in .NET - because that's the only way to back an object with vmem, short of creating a custom CLI host in C(++) or some other unmanaged language and AFAICT would have to back an entire appdomain with vmem for it to work.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
modified 19-Sep-19 14:47pm.
|
|
|
|
|
"Quote: if you think I'm wrong show me how to *create an object* at a specific address in .NET
Invoke the C method from C# you would use to allocation memory at that specific vmem address. Keep a reference to that address in C# as a pointer as you would in C. No difference from doing it in C. Again if you can address it in C you can address it in C# using pointers.
Can you show me what method you're invoking in C that allocates memory at a specific address? Then I can give an example and it would remove the confusion. I can only guess at what methods you're trying to use.
|
|
|
|
|
what C method?
the method to create the object?
that's not a .NET object.
I can already do all this mapping in C. There's no point in any of that.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
you do understand that paged memory allows you to do pointer ops directly to a file, i assume?
and that normally what you'd do is just create your data structures at that memory mapped address and then use them like normal classes and structure, which are automatically written to disk as they are accessed.
struct foo {
int bar;
};
foo* pfoo = VirtualAlloc(..., sizeof(foo));
pfoo->bar=1; printf("%d",pfoo->bar);
there is simply no way to do that in .NET
if you're saying there is show me how. Your method call technique does not enable this.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
lol yes there is. You didn't read anything I said.
As I said before. C# pointers are the same as C/C++. Look at the example below.
I literally just compiled and ran this on my computer with zero errors.
using System;
using System.Runtime.InteropServices;
using SIZE_T = System.IntPtr;
using DWORD = System.UInt32;
namespace TestNet
{
[StructLayout(LayoutKind.Sequential)]
struct Foo
{
public int bar;
}
static class Program
{
[DllImport("Kernel32.dll")]
private static unsafe extern void* VirtualAlloc(void* lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
private const DWORD MEM_COMMIT = 0x00001000;
private const DWORD PAGE_READWRITE = 0x04;
static unsafe void Main(string[] args)
{
Foo* pfoo = (Foo*)VirtualAlloc((void*)0, (SIZE_T)sizeof(Foo), MEM_COMMIT, PAGE_READWRITE);
pfoo->bar = 1;
Console.WriteLine(pfoo->bar.ToString());
}
}
}
Make a C# console app and paste that code in. WaLa!!
|
|
|
|
|
i'm going to try that. Assuming it works, I apologize for all the churn. You may have just taught my stubborn self something.
the example really helped, so thank you.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
np
|
|
|
|
|
Thanks for sticking with it and being nice and teaching us all something new.
#SupportHeForShe
Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson
You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun
|
|
|
|
|
Is Scrabble just the IKEA booklist?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
[ I guess there's not going to be a Thought of the Day today ]
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
What's the Swedish for "that went clean over my head?"
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
build your own book using scrabble words.
ikea: some assembly required.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
sigh, everybody knocks IKEA furniture
... then it falls over
Message Signature
(Click to edit ->)
|
|
|
|
|
when we remodeled we gutted the kitchen and installed ikea cabinets. I have no complaints. In fact, the instructions were good, and they are fairly durable.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
honey the codewitch wrote: the instructions were good, and they are fairly durable.
Well I have always wanted durable instructions...
I, for one, like Roman Numerals.
|
|
|
|
|
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Of course!
Thank you, I'm clearly an Allen key short today!
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
No. You also need the NOUNSTIK glue, and the PAGESTIK bindery (extra cost).
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Can you, as a favour to us at CodeProject, and to all those who just want to come to the lounge to chat and enjoy a bit of company, give it a rest?
All you're doing is taking our time away from helping those who are trying to share their code, their articles, their time to answer questions on the forums and question boards.
If you're bored then please take it elsewhere. A little respect for what we're trying to do here, please.
cheers
Chris Maunder
|
|
|
|
|
am i posting here too much? I'm not bored, just wordy, but I will can it.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
There's being loquacious and there's being deliberately offensive. The difference is pretty easy to spot.
cheers
Chris Maunder
|
|
|
|
|
The message you closed, i honestly wasn't trying to be offensive, but i apologize. I'll be more careful.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I didn't close anything. It may have been reported and closed by others.
cheers
Chris Maunder
|
|
|
|