Skip to content

Instantly share code, notes, and snippets.

View zhangyuchi's full-sized avatar

Terrell Franzman zhangyuchi

View GitHub Profile
@zhangyuchi
zhangyuchi / echo.cc
Created December 1, 2014 03:14
echo libev
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <ev++.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
#include <list>
@zhangyuchi
zhangyuchi / tips.c
Last active August 29, 2015 14:10
Tips of Linux C programming
//list define
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
//链表使用者定义:
struct user_t {
data domain;
struct list_head node;
@zhangyuchi
zhangyuchi / imagemagick.c
Last active August 29, 2015 14:10
image magick memory leak
unsigned char *ping_pixels;
ping_pixels=(unsigned char *) NULL;
if (setjmp(png_jmpbuf(ping)))
{
/*
PNG image is corrupt.
*/
png_destroy_read_struct(&ping,&ping_info,&end_info);
@zhangyuchi
zhangyuchi / evserver.hpp
Last active August 29, 2015 14:11
friend template class, shared_from_this, and libev
#ifndef EVSERVER_HPP
#define EVSERVER_HPP
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <ev++.h>
#include <netinet/in.h>
#include <sys/socket.h>
@zhangyuchi
zhangyuchi / sock_addr.c
Created December 11, 2014 01:51
compare sockaddr_in
/*++
/* NAME
/* sock_addr 3
/* SUMMARY
/* sockaddr utilities
/* SYNOPSIS
/* #include <sock_addr.h>
/*
/* int sock_addr_cmp_addr(sa, sb)
/* const struct sockaddr *sa;
@zhangyuchi
zhangyuchi / variadic.cc
Created December 11, 2014 10:20
var length template args example: implement a zip function
#include <vector>
#include <tuple>
#include <algorithm>
#include <type_traits>
template<class T>
using Invoke = typename T::type;
template<class T>
using Unqualified = Invoke<std::remove_cv<Invoke<std::remove_reference<T>>>>;
@zhangyuchi
zhangyuchi / perfect_task.cc
Created December 19, 2014 04:33
perfect task function
template<typename ... MTArg>
struct Task {
typedef void (*RawFunction)(MTArg...);
//Here we want to forward some set of arguments into a tuple, but the arguments need not match MTArg -- they just have to be convertible:
template<typename ... Args>
explicit Task(RawFunction rawFunction, Args&&... args)
rawFunction(rawFunction),
args(std::make_tuple(std::forward<Args>(args)...)) {}
//which the above checks. Note I made it explicit, as if Args... is empty, we don't want this to be a converting constructor.
@zhangyuchi
zhangyuchi / thread_libev.cc
Created December 24, 2014 07:31
multithread libev exsample, cross thread notify
//This program is demo for using pthreads with libev.
//Try using Timeout values as large as 1.0 and as small as 0.000001
//and notice the difference in the output
//(c) 2009 debuguo
//(c) 2013 enthusiasticgeek for stack overflow
//Free to distribute and improve the code. Leave credits intact
#include <ev.h>
#include <stdio.h> // for puts
@zhangyuchi
zhangyuchi / multiinherit_shared.cc
Created January 7, 2015 11:39
multiple inherit enable_shared_from_this
/* Trick to allow multiple inheritance of objects
* inheriting shared_from_this.
* cf. http://stackoverflow.com/a/12793989/587407
*/
/* First a common base class
* of course, one should always virtually inherit from it.
*/
class MultipleInheritableEnableSharedFromThis: public std::enable_shared_from_this<MultipleInheritableEnableSharedFromThis>
{
@zhangyuchi
zhangyuchi / sort1mb.cpp
Last active August 29, 2015 14:13 — forked from preshing/sort1mb.cpp
Sort one million 8-digit numbers in 1MB RAM, with statistics
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef unsigned int u32;
typedef unsigned long long u64;
//-------------------------------------------------------------------------
// WorkArea
//-------------------------------------------------------------------------