|
MS uses unsafe code all over the place in their own frameworks. Not sure how its an issue but ok.
Even if you could address managed array to a native buffer that would make it unsafe usage.
|
|
|
|
|
unsafe isn't a problem. This has nothing to do with a desire not to run unsafe code
Again, what it has to do with is mapping a complex managed data structure to a file using Vmem.
It's not doable.
At best, you resolve to something much like standard file i/o which defeats the whole reason i'd use vmem. Adding, span would help here, but it doesn't go near enough to to be a solution since these data structures aren't simple arrays.
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.
|
|
|
|
|
Then just wrap the C API that does what you want. Anything you can do in C you can do in C# by using it like C. So it is doable just maybe not how you like.
|
|
|
|
|
can't do that either because it ties it to a platform.
the goal is cross platform. At best i'd have to do two binaries per platform. and it would only support a limited number of them. So it doesn't fulfill the requirements.
If I didn't care about being cross platform, I'd just use mixed mode C++.
It's okay that this isn't doable. I accept it. It would just be nice if it was.
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 don't need multiple native or managed binaries for this. You can just wrap each platform and detect what platform you're on in the .NET runtime via RuntimeInformation class.
So you would probably only need one for Win32 and one for POSIX (aka Linux, BSD and macOS).
Because a .NET lib doesn't exist for what you need thats portable, you will have to make one that is.
|
|
|
|
|
Once again, I cannot map objects to a specific process address space in .NET. No amount of marshalling or unsafe code will change that.
It might be possible to create an unmanaged cross compiled library and link to the different calling conventions etc, by conditional P/Invoke, at the cost of extra complexity and adds marshalling to a class that shouldn't have it for perf reasons, and still requiring a cross compiled unmanaged binary and still therefore being limited to certain platforms, not all .NET platforms. So again, it doesn't fulfill the basic requirement because of that latter bit.
I accept that it can't be done in .NET
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 said using unsafe code wasn't an issue?
So again anything you can do in C can be done in C# if you use unsafe code if thats not an issue to you. It sounds like you don't fully understand what you can do in unsafe code in C#.
Cross compiling a native binary would be the same as just using unsafe code in C# and wraping the C methods you need.
|
|
|
|
|
zezba9000 wrote: So again anything you can do in C can be done in C# if you use unsafe code
This is categorically incorrect.
There is no way to map a .NET object to a file backed portion of the process address space and there likely never will be in .NET. Copying the object to that space or otherwise serializing it is not the same thing at all.
I'm going attempt to summarize: You cannot create an object at a specific memory location in managed code.
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'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.
|
|
|
|