Skip to content

Instantly share code, notes, and snippets.

View GTANAdam's full-sized avatar
💭
very curious

Adam GTANAdam

💭
very curious
View GitHub Profile
@GTANAdam
GTANAdam / UnsafeBlockCopy.cs
Created May 11, 2021 18:15
Lower overhead (faster) BlockCopy alternative using /unsafe
// using System;
// using System.Diagnostics;
// using System.Runtime.CompilerServices;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe static void BlockCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
{
Debug.Assert(src != null);
Debug.Assert(dst != null);
Debug.Assert(srcOffset >= 0 && srcOffset < src.Length);
@GTANAdam
GTANAdam / sshfs
Last active April 23, 2021 20:29
Mounting directory over SSHFS (Alpine)
$ apk add sshfs
$ modprobe fuse
$ mkdir /mnt/server
$ sshfs root@192.168.1.12:/ /mnt/server
@GTANAdam
GTANAdam / restart_wireless.sh
Created April 23, 2021 20:25
Restart Padavan & N56U Wireless Radio
#!/bin/sh
radio2_restart && radio5_restart
@GTANAdam
GTANAdam / eventbus.h
Last active April 7, 2021 11:07
C++14 events similar to C#, low overhead event bus with optional thread safety
#pragma once
#include <vector>
#ifdef THREAD_SAFE_EVENTS
#include <shared_mutex>
#define MUTEX std::shared_mutex rwmutex;
#define RWLOCK rwmutex.lock();
#define RWUNLOCK rwmutex.unlock();
@GTANAdam
GTANAdam / Precompiled Headers Linux (Visual Studio 2019)
Last active February 9, 2023 13:25
Using Precompiled headers for Linux (WSL) development in Visual Studio
Precompiled headers basically improve build times by reducing the amount of code compiling each time a build
command is invoked.
This feature is available in Visual Studio for Windows C/C++ development
via project "Properties" -> "C/C++" -> "Precompiled Headers",
but this feature is missing for Linux C/C++ Development (Visual Studio) and
requires a workaround since GCC/Clang support pch.
The workaround:
- Creating a "precompile.sh" bash script and appending the content above.
- Set in your project's properties in "Properties" -> "Build Events" -> "WSL Pre-Build Event", the following:
@GTANAdam
GTANAdam / 1_export.h
Created April 5, 2021 11:58
Keep unused exported functions from being stripped by linker, useful for linking static libraries that have unused exported functions targeted for C# P/Invoke marshaling or misc purposes
#pragma once
// LINKER_INCLUDE_UNUSED: Forces linker to keep unused functions that were declared elsewhere
// LINKER_KEEP_UNUSED: Forces linker to keep unused functions that are delcared within the current file
#define EXPORT extern "C" _declspec(dllexport)
#if defined(LINKER_INCLUDE_UNUSED)
#if defined(_WIN64)
#define LINKER_KEEP(s) EXPORT __pragma(comment (linker, "/export:"#s))
@GTANAdam
GTANAdam / thread_ex.h
Last active April 5, 2021 12:02
GNU std::thread & Callable object, pass functions without having to use functionals and binds (drop-in replacement), seems to fix a bug in Visual Studio with WSL2 (musl)
#pragma once
#include <thread>
namespace std_ex {
template <typename Callable, typename... Args>
std::thread thread(Callable&& f, Args&& ... args) {
return std::thread(f, args...);
}
}
@GTANAdam
GTANAdam / MarshalPtrToStructureArray.cs
Last active April 5, 2021 12:00
Marshalling a pointer to an array of structures (C/C++/C# Interop)
public unsafe static void MarshalPtrToStructureArray<T>(IntPtr sPtr, ulong arrSize, ref T[] output) where T : unmanaged
{
fixed (T* dPtr = output)
{
ulong dataSize = arrSize * (uint)Marshal.SizeOf(typeof(T));
Unsafe.CopyBlock(dPtr, (void*)sPtr, (uint)dataSize);
// Alternatively, .NET Framework >= 4.6
// ulong dataSize = arrSize * (uint)Marshal.SizeOf<T>();
// Buffer.MemoryCopy((void*)sPtr, dPtr, dataSize, dataSize);