Skip to content

Instantly share code, notes, and snippets.

View mrenouf's full-sized avatar

Mark Renouf mrenouf

View GitHub Profile
@mrenouf
mrenouf / PosixFileModes.kt
Created April 22, 2024 14:18
Parse and format posix raw file modes (including file type and special (suid/setgid/sticky) bits)
private const val FILE_TYPES = "?pc?d?b?-?l?s???"
fun String.toPosixModeInt(): Int {
require(length == 10) { "Should have 10 characters" }
// type
val type = when(get(0)) {
'p' -> 1
'c' -> 2
'd' -> 4
'b' -> 6
package adb
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.lang.ref.Reference
import java.lang.ref.ReferenceQueue
@mrenouf
mrenouf / ByteBufferSlicePool.kt
Last active April 10, 2024 20:57
Pooled ByteBuffers (Fast TreeSet+Double-linked-list version)
package adb.io
import adb.WeakReferenceMap
import java.lang.ref.Cleaner
import java.nio.ByteBuffer
import java.util.*
class ByteBufferSlicePool2(
capacity: Int,
) : ByteBufferAllocator {
@mrenouf
mrenouf / ByteBufferSlicePool.kt
Last active April 10, 2024 20:58
Original Slow version, scales terribly (N^2 with the number of allocations)
package adb.io
import adb.io.ByteBufferSlicePool.Region.Alloc
import adb.io.ByteBufferSlicePool.Region.Free
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.lang.ref.ReferenceQueue
import java.lang.ref.WeakReference
package adb.io
import adb.io.ByteBufferSlicePool.Region.Alloc
import adb.io.ByteBufferSlicePool.Region.Free
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import java.lang.ref.ReferenceQueue
import java.lang.ref.WeakReference
import java.net.SocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousByteChannel
import java.nio.channels.AsynchronousSocketChannel
import java.nio.channels.CompletionHandler
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlinx.coroutines.suspendCancellableCoroutine
suspend fun AsynchronousByteChannel.readInto(buffer: ByteBuffer): Int = suspendCancellableCoroutine {
read(buffer, null, object : CompletionHandler<Int, Any?> {
override fun completed(result: Int, attachment: Any?) { it.resume(result) }
override fun failed(exc: Throwable, attachment: Any?) = it.resumeWithException(exc)
})
}
suspend fun AsynchronousByteChannel.writeFrom(buffer: ByteBuffer): Int = suspendCancellableCoroutine {
write(buffer, null, object : CompletionHandler<Int, Any?> {
override fun completed(result: Int, attachment: Any?) = it.resume(result)
sudo apt install build-essential autoconf bison flex texinfo \
help2man gawk libtool libncurses5-dev python3-dev \
python3-distutils git
git clone http://github.com/crosstool-ng/crosstool-ng
cd crosstool-ng
./bootstrap
./configure --enable-local
DEFCONFIG=samples/armv8-rpi3-linux-gnueabihf/crosstool.config ./ct-ng defconfig
#!/usr/bin/env bash
#
# Enable port forwarding when using Private Internet Access
#
# Usage:
# ./port_forwarding.sh
error( )
{
echo "$@" 1>&2
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<android.support.constraint.Guideline
android:id="@+id/guideline_h50"