Skip to content

Instantly share code, notes, and snippets.

@JackyYin
JackyYin / test_cpucache.c
Created October 24, 2021 13:13
Test L1 cache in raspi 4.
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#define L1_CACHE_SZ (32 * 1024)
#define L2_CACHE_SZ (1024 * 1024)
#define TESTOBJ sx
@JackyYin
JackyYin / test_cpucache.cpp
Last active October 24, 2021 07:43
Test for L1 cache misses in raspi 4.
#include <unistd.h>
#include <iostream>
#include <chrono>
#include <vector>
typedef struct x {
int a;
float b;
} sx;
@JackyYin
JackyYin / base64.c
Last active October 22, 2021 08:02
my base64 implementation
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
# define LIKELY(x) __builtin_expect(!!(x), 1)
# define UNLIKELY(x) __builtin_expect(!!(x), 0)
static char encode_table[64] = {
@JackyYin
JackyYin / futex.c
Last active September 24, 2021 08:00
A simple program using futex to mimic mutex, but much slower than mutex
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdatomic.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/futex.h>
@JackyYin
JackyYin / coroutine_ucontext.c
Created September 12, 2021 14:34
a simple coroutine implementation using ucontext
#include <ucontext.h>
#include <stdio.h>
#include <stdlib.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define CO_STACK_SIZE 2048
typedef struct {
@JackyYin
JackyYin / remove_bad_utf8.c
Last active September 3, 2021 09:04
in-place replace bad UTF-8 chars for CLD2
void remove_bad_utf8 (uint8_t *src, size_t srclen)
{
uint8_t *dst = src;
uint8_t *srcend = src + srclen;
while (src < srcend) {
// 0xxxxxxx
if ((src[0] & 0x80) == 0x00) {
if (src[0] < 0x20 || src[0] == 0x7F) {
/* printf("not printable one-byte char...%02X\n", src[0]); */
@JackyYin
JackyYin / coroutine.c
Last active March 30, 2021 09:10
a simple coroutine implementation
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#define COROUTINE_START int r = setjmp(current->context);\
switch (r) {\
case 0:
#define COROUTINE_PREEMPT if (current->iter % 500 == 0) {\
longjmp(m, 1);\
@JackyYin
JackyYin / rsa.js
Created January 16, 2021 12:58
test size limit of RSA message body
const bits = 2048
const { publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: bits,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
}
})
const test = (len) => {
@JackyYin
JackyYin / myspacevim.vim
Last active July 3, 2021 03:39
some custom config for space vim
function! myspacevim#before() abort
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" BASICS
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax on
set updatetime=1000
"for performance issue
set re=1
@JackyYin
JackyYin / init.toml
Last active July 3, 2021 03:37
my spacevim config
#=============================================================================
# dark_powered.toml --- dark powered configuration example for SpaceVim
# Copyright (c) 2016-2020 Wang Shidong & Contributors
# Author: Wang Shidong < wsdjeg at 163.com >
# URL: https://spacevim.org
# License: GPLv3
#=============================================================================
# All SpaceVim option below [option] section
[options]