Skip to content

Instantly share code, notes, and snippets.

Avatar

SF-Zhou

View GitHub Profile
@SF-Zhou
SF-Zhou / hazard_pointer.cc
Last active Jun 11, 2020
Lock Free Double Buffer
View hazard_pointer.cc
#include <atomic>
#include <cassert>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
template <class T>
struct HazardPointer {
@SF-Zhou
SF-Zhou / lru.cc
Created Dec 6, 2019
C++11 LRU Cache
View lru.cc
#include <list>
#include <unordered_map>
class LRUCache {
public:
LRUCache(int capacity) : capacity_(capacity) {}
int get(int key) {
auto it = used_.find(key);
if (it == used_.end()) {
@SF-Zhou
SF-Zhou / main.cpp
Created Nov 20, 2019
C++ Code Block Magic
View main.cpp
// Need C++ 11
#include <functional>
#include <iostream>
class A {
public:
template <class F>
auto operator-(const F &func) -> typename std::result_of<F()>::type {
return func();
}
@SF-Zhou
SF-Zhou / hook_write.cc
Created Oct 19, 2019
Linux Sys Func Hook
View hook_write.cc
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static bool is_hook = false;
int main() {
@SF-Zhou
SF-Zhou / epoll.c
Created Oct 17, 2019
Linux Epoll IO Multiplexing
View epoll.c
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/epoll.h>
#include <unistd.h>
@SF-Zhou
SF-Zhou / coroutine.cpp
Last active Sep 26, 2019
Coroutine Demo (libco)
View coroutine.cpp
#include <iostream>
#include <stdint.h>
#include <vector>
extern "C" void swap_context(void *, void *) asm("swap_context");
asm(R"(
swap_context:
mov 0x00(%rsp), %rdx
lea 0x08(%rsp), %rcx
mov %r12, 0x00(%rdi)
@SF-Zhou
SF-Zhou / rust-like.cpp
Created Sep 10, 2019
Rust-like Ownership and Reference
View rust-like.cpp
#include <atomic>
#include <cassert>
#include <iostream>
using namespace std;
template <class T>
class MutableRef;
template <class T>
class ImmutableRef;
@SF-Zhou
SF-Zhou / set_input_source.cpp
Created Mar 10, 2019
Switch to Specific Input Source for macOS
View set_input_source.cpp
/*
* Switch to Specific Input Source for macOS
* Author: SF-Zhou<sfzhou.scut@gmail.com>
* To English: g++ -framework Foundation -framework Carbon -std=c++11 -O2 change-input-source.cpp -o switch-to-english
* To Pinyin: g++ -framework Foundation -framework Carbon -std=c++11 -O2 change-input-source.cpp -D PINYIN -o switch-to-pinyin
*/
#include <string>
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
@SF-Zhou
SF-Zhou / lr_scheduler.schema.json
Created Mar 2, 2019
JSON Schema of LR Scheduler
View lr_scheduler.schema.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "LRSchedulerConfigs",
"description": "LR Scheduler Configs for PyTorch",
"type": "array",
"items": {
"$ref": "#/definitions/LRSchedulerConfig"
},
"definitions": {
"LRSchedulerConfig": {
@SF-Zhou
SF-Zhou / simple_shared_ptr.cpp
Created Jan 25, 2019
Simple C++ Shared Ptr
View simple_shared_ptr.cpp
#include <iostream>
template <class T>
class SharedPtr {
public:
SharedPtr(T *rep = nullptr) : rep_(rep), count_ptr_(new int32_t(1)) {}
SharedPtr(const SharedPtr &obj) : count_ptr_(obj.count_ptr_), rep_(obj.rep_) {
increse();
}
You can’t perform that action at this time.