unsafe

Arithmetic operation resulted in an overflow in unsafe C#

纵然是瞬间 提交于 2019-12-04 10:26:29
Background We've been using some code copied verbatim from Joe Duffy's "Concurrent Programming on Windows" (page 149) in production for over a year. The code (below) is used in our Asp.Net web application to probe if there's enough stack space. Our site allows users to script out their own web pages and control logic in a simple proprietry scripting language - it's possible for a user to script something nasty and cause a stackoverflow exception, so we use Duffy's code example to stop execution of the errant script before the uncatchable StackOverflow exception takes down the whole IIS AppPool

Why are JNI calls to native methods slower than similar methods in sun.misc.Unsafe?

萝らか妹 提交于 2019-12-04 08:35:45
问题 I'm developing a JNI implementation similar to sun.misc.Unsafe but with extended memory management. Why are the call times of native methods from sun.misc.Unsafe and from my developed library extremely different? Some numbers: sun.misc.Unsafe.getInt(address) takes ~1ns when my similar method takes ~10ns Both implementations are quite the same, following the source code of OpenJDK, just returning the variable by pointer. Both are registered in the same manner. How can I speed up JNI calls?

Java Unsafe.copyMemory java.lang.IllegalArgumentException

∥☆過路亽.° 提交于 2019-12-04 03:54:17
问题 I have a problem with copyMemory from Unsafe. I spent 2 days in resolving it but with no result. The code presented below always ends up with "IllegalArgumentException". Can You help me and show where is problem ? public void testMemoryCopy() { class A { public long val = 10; } A a0 = new A(); A a1 = new A(); try { long offset = unsafe.objectFieldOffset(A.class.getField("val")); unsafe.copyMemory(a0, offset, a1, offset, 8); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch

Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules?

巧了我就是萌 提交于 2019-12-04 03:18:31
I don't have a particularly solid understanding of Rust's aliasing rules (and from what I've heard they're not solidly defined), but I'm having trouble understanding what makes this code example in the std::slice documentation okay. I'll repeat it here: let x = &mut [1, 2, 4]; let x_ptr = x.as_mut_ptr(); unsafe { for i in 0..x.len() { *x_ptr.offset(i as isize) += 2; } } assert_eq!(x, &[3, 4, 6]); The problem I see here is that x , being an &mut reference, can be assumed to be unique by the compiler. The contents of x get modified through x_ptr , and then read back via x , and I see no reason

How to call unsafe code from ASP.NET xproj

依然范特西╮ 提交于 2019-12-04 03:14:18
I created a class library today with the new Class Library (Package) templates in Visual Studio 2015. Apparently it uses a fancy new project format, the ASP.NET xproj, for building the package. While that's fine by me, how do I call unsafe code from the library? I looked into Project > Properties > Build , where the option to toggle unsafe code should be, but all I got was this: So yeah, no such luck. I even tried pasting "<AllowUnsafeBlocks>true</AllowUnsafeBlocks>" manually into the .xproj file, but the compiler still complains. Is it possible to enable this without creating another non

Unsafe code won't compile on Visual Studio 2015

半腔热情 提交于 2019-12-04 03:05:46
问题 I'm trying to compile a program on the new DNX4.6 core, but it won't compile due to: error CS0227: Unsafe code may only appear if compiling with /unsafe This is my code: [CompilerGenerated] public unsafe class GrayscaleQuantizer : PaletteQuantizer { I've looked online, and I can't get any source with the same problem as I have. I can't tick the 'Allow Unsafe Code' at the Build tab of the Project Properties, because there is no option to do so... Does anyone know a solution? 回答1: You need to

Windows Phone 8(WP8) C# unsafe code?

▼魔方 西西 提交于 2019-12-03 16:45:11
EDIT: You can use unsafe code... you just have to manually edit the proj file. Why or why does C# on WP8 not support unsafe code when I can use native C++ code on the phone(I did not expect this)? I mean rly come on, I am so disappointed with what Microsoft is trying to force C# into. Is there any MS plans to support this in the future on WP8? I pass shader constants from C# to C++ and use unsafe code to optimize this process but on WP8 i'm going to be forced into doing this in some slow fashion on a WAY slower device compared to a PCs and it makes this very frustrating. As an example to pass

Can you limit the CPU usage on a .NET Process Object?

拥有回忆 提交于 2019-12-03 12:30:30
问题 An application I'm contributing to fires up a component written in C. The C process does some pretty heavy crunching and if your not careful can really hammer your CPU. Is there a way to set a limit to external processes spawned by .NET? I've seen this artivcle on setting a hard memory limit at the OS level, is there a similar thing for CPU? 回答1: Not in Windows. You can lower the process priority though, which will reduce the likelihood that the problematic process will be scheduled on the

moving objects off heap

梦想的初衷 提交于 2019-12-03 12:30:17
问题 Did anyone try to move java objects off heap? I tried using serializing, deserializing and storing byte arrays by using Unsafe libraries. But complex objects with multiple objects in it making this a tedious process. Any better solutions? 回答1: The unsafe field is your instance of Unsafe. If you don't know how to get it, simply use: Unsafe unsafe = getUnsafe(); Unsafe getUnsafe() { try { Field f = Unsafe.class.getDeclaredField("theUnsafe"); f.setAccessible(true); return (Unsafe) f.get(null); }

C# Bitmap image masking using unsafe code

允我心安 提交于 2019-12-03 06:30:24
问题 I'm using the following code to make image masks in C#: for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { bmp.SetPixel(x,y,Color.White); } } for(int x = left; x < width; x++) { for(int y = top; y < height; y++) { bmp.SetPixel(x,y,Color.Transparent); } } But it's WAY too slow... What is the unsafe equivalent to this? Will it be allot faster? In the end I do a bmp.Save() in PNG format. UPDATE: After reading through [Link removed, dangerous site] as suggested by MusiGenesis, I