Skip to content

Instantly share code, notes, and snippets.

View nasacj's full-sized avatar
🍇
Autumn Leaves

NASa nasacj

🍇
Autumn Leaves
View GitHub Profile
@nasacj
nasacj / ReadBufferedImageFromByte.java
Last active April 9, 2017 07:52
Read BufferedImage From Byte
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ReadBufferedImageFromByte {
@nasacj
nasacj / JavaImageGradient.md
Last active July 4, 2017 03:57
Image Gradient in Java

Maven Dependency:

    <dependency>
      <groupId>com.jhlabs</groupId>
      <artifactId>filters</artifactId>
      <version>2.0.235</version>
    </dependency>
@nasacj
nasacj / noncopyable.md
Last active July 11, 2018 14:54
Usage of C++ noncopyable and analysis

Usage

You can make you C++ class derive from boost::noncopyable, which makes cannot be copied. The usage is very simple as following

#include <boost/core/noncopyable.hpp>

class your_class :
  private boost::noncopyable
{
  // ...
};
@nasacj
nasacj / inet_pton4.c
Created August 5, 2018 15:52
inet_pton4
/* int
* inet_pton4(src, dst)
* like inet_aton() but without all the hexadecimal and shorthand.
* return:
* 1 if `src' is a valid dotted quad, else 0.
* notice:
* does not touch `dst' unless it's returning 1.
* author:
* Paul Vixie, 1996.
*/
/* From openssh4.3p2 compat/inet_aton.c */
/*
* Copyright (c) 1983, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
#include <stdint.h>
#include <stdio.h>
#include <cassert>
static __inline int isdigit(char c)
{
return c >= '0' && c <= '9';
}
int aton(const char* ip, int* inet_addr) {
#include <stdio.h>
#include <unistd.h>
#define ONE_MB (1024 * 1024)
int main()
{
printf("The number of processors configured is :%ld\n",
sysconf(_SC_NPROCESSORS_CONF));
printf("The number of processors currently online (available) is :%ld\n",
@nasacj
nasacj / benchmark_tsc.c
Last active December 22, 2023 12:01
How to Benchmark Code Execution Times on Intel® IA-32 and IA-64 Instruction Set Architectures
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/hardirq.h>
#include <linux/preempt.h>
#include <linux/sched.h>
#include <linux/slab.h>
#define SIZE_OF_STAT 100000
#define BOUND_OF_LOOP 1000
#include <cstdint>
#include <iostream>
//#include <linux/preempt.h>
__attribute__((always_inline)) uint64_t perf_counter(void)
{
uint32_t lo, hi;
// take time stamp counter, rdtscp does serialize by itself, and is much cheaper than using CPUID
__asm__ __volatile__ (
"rdtscp" : "=a"(lo), "=d"(hi)

In my time at Headlands Technologies, I’ve gotten the opportunity to build some utilities that have improved the ergonomics of maintaining high-performance C++ codebases. This article will give a generic overview of one of those utilities, OutOfLine.

Let’s start with a motivating example. Suppose you have a system that opens a very large number of paths. Maybe they are files, maybe they are named UNIX sockets, maybe pipes. But for whatever reason, you open a lot of file descriptors at startup, then you do a lot of processing on those descriptors, and finally when you’re done you close the descriptors and unlink the paths.

An (abbreviated) initial design might look like this:

class UnlinkingFD {
  std::string path;
 public:
 int fd;