Skip to content

Instantly share code, notes, and snippets.

@fcamel
fcamel / one_symbol_with_two_addresses.log
Created March 5, 2017 07:42
multiple address for the same symbol
$ tail -n +1 a.h b.c c.c m.c
==> a.h <==
#include <stdio.h>
static void foo() {
printf("from a.h\n");
}
==> b.c <==
#include <stdio.h>
// std::bind() cannot bind std::weak_ptr.
// This file provides BindWeakPtr() which can bind a method with a std::weak_ptr.
// If the object inside weak_ptr is deleted, the function will become a NOP.
#include <iostream>
#include <functional>
#include <memory>
template <typename T>
class WeakFunction
{
// Source: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1525.htm
const int num_mailboxes = 32;
_Atomic volatile int mailbox[num_mailboxes];
void MailboxInspection(int my_id, void (*do_work)(int)) {
for (int i = 0; i < num_mailboxes; i++) {
if (atomic_load_explicit(&mailbox[i], memory_order_relaxed) == my_id) {
atomic_thread_fence(memory_order_acquire); // prevent speculative reads in do_work [7.17.4.1]
do_work(i);
}
$ g++ -std=c++11 count_bits.cpp -o count_bits -O3 && ./count_bits
count_directly : sum=7998166000, duration=3315520721 ns
count_by_table : sum=7998166000, duration=607687929 ns
count_by_table8 : sum=7998166000, duration=694708929 ns
count_by_bit_operation: sum=7998166000, duration=241576894 ns
count_by_popcnt : sum=7998166000, duration=69186289 ns
$ g++ count_bits.cpp -o count_bits -std=c++11 -O3 && ./count_bits
count_by_table : sum=7999273000, duration=576939173ns
count_by_bit_operation: sum=7999273000, duration=236211785ns
count_by_popcnt : sum=7999273000, duration=67943502ns
$ g++ --version
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ count_bits.cpp -o count_bits -std=c++11 -O0
$ ./count_bits
count_by_table : sum=7999207000, duration=1989441628ns
count_by_bit_operation: sum=7999207000, duration=1062128807ns
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ g++ count_bits.cpp -o count_bits -std=c++11 -O0
$ ./count_bits
count_by_table : sum=7999011000, duration=1735250964ns
count_by_bit_operation: sum=7999011000, duration=1024207704ns
@fcamel
fcamel / delete_this.cpp
Last active October 1, 2016 17:54
Object managed by unique_ptr and run in only one MessageLoop
// Public method.
void Object::DeleteThis()
{
// From now on, all methods will act like NOP.
m_deleted = true;
// Post a new task to run DoDeleteThis().
// We cannot delete |this| right now because |this| may be in m_messageLoop.
m_messageLoop->PostTask(...);
}
#!/bin/bash -x
# Exit when error.
set -e
sudo apt-get install aptitude
sudo aptitude update
sudo aptitude install build-essential apt-file checkinstall manpages-dev htop rsync screen cdargs openssh-server vim git gitg subversion exuberant-ctags cscope python-dev python-setuptools python-pip id-utils cgdb clang cmake
sudo apt-file update
@fcamel
fcamel / proxy.txt
Created September 11, 2015 07:15
Backup of SPDY Proxy Examples
Source: http://www.chromium.org/spdy/spdy-proxy-examples
Client HTTPS Proxy Origin Server
------ ----------- -------------
*** If an available connection to the proxy exists, jump to step 9 ***
*** Client opens TCP connection to Proxy ***
|---------------------->| |
| 1) SYN | |
| | |