Skip to content

Instantly share code, notes, and snippets.

@dmikurube
dmikurube / memory-test.cc
Created December 3, 2011 13:30
Memory allocation test code
// g++ -L<path-to-libtcmalloc.so> -ltcmalloc memory-test.cc
// env LD_LIBRARY_PATH=<path-to-libtcmalloc.so>b HEAPPROFILE=<prefix-to-output-files HEAP_PROFILE_INUSE_INTERVAL=1048576 ./a.out
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
class Sub {
public:
@dmikurube
dmikurube / type-preserving-new.cc
Created December 8, 2011 09:15
Type-preserving operator new
// Test code of http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=6080813.
// Actually, it doesn't require to make type_identifier_to_string.
// The mapping can be obtained from a linker.
#include <map>
#include <string>
#include <typeinfo>
std::map<void *, char *> pointer_to_type_identifier;
std::map<void *, const std::type_info*> pointer_to_type_info;
@dmikurube
dmikurube / core_dumper.cc
Created January 20, 2012 06:25
Core image archaeologist
// g++ -I/.../google-coredumper/include -L/.../google-coredumper/lib -lcoredumper core_dumper.cc
// env LD_LIBRARY_PATH=/.../google-coredumper-working/lib ./a.out
#include <google/coredumper.h>
int main() {
WriteCoreDump("hyanore.core");
return 0;
}
#include <iostream>
using namespace std;
class parent_t {
public:
void print_it() {
local_print();
}
#include <stdlib.h>
#include <stdio.h>
void* backed_malloc(size_t size) __attribute__ ((weak, alias ("malloc")));
void* hook_malloc(size_t size) {
printf("%ld\n", size);
return backed_malloc(size + 1);
}
@dmikurube
dmikurube / range.cc
Created March 28, 2012 04:35
Exclusive closed ranges and classification trees.
#include <iterator>
#include <iostream>
#include <map>
#include <unordered_map>
#include <utility>
#include <vector>
typedef unsigned long long uint64;
class ExclusiveClosedRange {
#include <iostream>
class Parent {
public:
int x;
virtual void print() { std::cout << "Parent" << std::endl; }
};
class Child: public Parent {
public:
#include <iostream>
#include <typeinfo>
class Base {
public:
Base() : x_(0) {}
virtual void print() { std::cout << "Base" << std::endl; }
private:
int x_;
};
/*
* stdout compiled without -fallocated-type-in-operator-new:
* > 0
* > 2
* > 9
* > 5
*
* stdout compiled without -fallocated-type-in-operator-new:
* > Allocated at 0xb80010 as an instance of 5Klass
* > 0
#include <cstddef>
void* operator new(size_t size, int dummy) {
return ::operator new(size);
}
class Class {
public:
Class() : x_(0) {}
void* operator new(size_t size) {