Skip to content

Instantly share code, notes, and snippets.

View Lisoph's full-sized avatar

Daniel Hauser Lisoph

  • Fronius International GmbH
  • Austria
  • 07:49 (UTC +02:00)
View GitHub Profile
@Lisoph
Lisoph / container_of.c
Last active September 6, 2021 13:54
CONTAINER_OF macro to downcast types more easily in C
//===--------------------~ Source ~--------------------===//
#include <stdint.h>
#ifndef offsetof
#define offsetof(type, member) ((uintptr_t)&((type*)0)->member)
#endif
#define CONTAINER_OF_CONST(member_ptr, container_ty, member_name) \
((container_ty const*) ((uintptr_t) member_ptr - offsetof(container_ty, member_name)))
@Lisoph
Lisoph / first_free_key.rs
Created December 16, 2019 11:25
BTreeMap first free key
use std::collections::BTreeMap;
fn first_free_key<V>(map: &BTreeMap<usize, V>) -> usize {
map.keys()
.zip(map.keys().skip(1))
.find(|(a, b)| (*b - *a) > 1)
.map(|(a, _)| *a + 1)
.unwrap_or_else(|| map.keys().last().map(|x| x + 1).unwrap_or(0))
}
@Lisoph
Lisoph / zig_matrix.zig
Created September 26, 2018 06:26
Zig generic Matrix struct
pub fn main() void {
const foo = Matrix(f32, 4, 4).identity();
}
fn Matrix(comptime Scalar: type, comptime Width: usize, comptime Height: usize) type {
return struct {
const Self = @This();
pub elems: [Width * Height]Scalar,
@Lisoph
Lisoph / generic_mut_event.rs
Created September 14, 2018 10:13
Generic event container inspired by delegate / event in C#
use std::cell::UnsafeCell;
use std::marker::PhantomData;
type EventReceiver<'a, A, R> = Box<dyn (FnMut(A) -> R + 'a)>;
struct Event<'a, A, R> {
receivers: Vec<UnsafeCell<EventReceiver<'a, A, R>>>,
_m: PhantomData<&'a ()>,
}
@Lisoph
Lisoph / cpp17_tokens.cpp
Created August 31, 2018 11:49
C++17 Token struct
#include <cstdint>
#include <cstddef>
#include <string>
#include <iostream>
struct TokIdentifier {
std::string name;
TokIdentifier() = delete;
TokIdentifier(TokIdentifier const &) = default;
@Lisoph
Lisoph / zig1.zig
Created August 6, 2018 21:55
Some fun with the Zig programming language
const std = @import("std");
const Controller = struct {
up: u1,
down: u1,
left: u1,
right: u1,
};
pub fn main() void {
@Lisoph
Lisoph / alloc_locality.rs
Created April 6, 2018 10:21
Rust allocator locality tester. Analyses whether the current allocator allocates successive allocations sequentially in memory.
const PAYLOAD_SIZE: usize = 128;
const TESTS: usize = 20;
fn main() {
let mut items: [Box<[u8; PAYLOAD_SIZE]>; TESTS] = unsafe { std::mem::uninitialized() };
for item in items.iter_mut() {
unsafe {
std::ptr::write(item as *mut _, Box::new([0u8; PAYLOAD_SIZE]));
}
@Lisoph
Lisoph / stacked_coroutine.rs
Created April 5, 2018 12:20
Coroutine implementation similar to Unity's, in nightly Rust.
#![feature(generators, generator_trait)]
use std::ops::{Generator, GeneratorState};
use std::thread;
use std::time::{Duration, Instant};
struct CoStep;
impl CoGenerator for CoStep {
fn resume(&mut self) -> CoGenState {
@Lisoph
Lisoph / obj_bytes_obj.rs
Created April 5, 2018 08:12
A struct which can store an object to raw memory and restore from it.
use std::marker::PhantomData;
#[derive(Clone)]
struct App {
data: Vec<String>,
data2: Tester,
}
#[derive(Clone)]
struct Tester;
@Lisoph
Lisoph / quake2_fade_dithering.c
Last active April 5, 2018 08:15
Dithering based fading in Quake 2
// https://github.com/id-Software/Quake-2/blob/372afde46e7defc9dd2d719a1732b8ace1fa096e/ref_soft/r_draw.c#L428
void Draw_FadeScreen (void)
{
int x,y;
byte *pbuf;
int t;
for (y=0 ; y<vid.height ; y++)
{
pbuf = (byte *)(vid.buffer + vid.rowbytes*y);