Skip to content

Instantly share code, notes, and snippets.

@neesenk
neesenk / test.c
Created November 27, 2010 15:43
fast ffs and ffsl
static char ffslut32[] = {
32, 1, 23, 2, 29, 24, 14, 3, 30, 27, 25, 18, 20, 15, 10, 4,
31, 22, 28, 13, 26, 17, 19, 9, 21, 12, 16, 8, 11, 7, 6, 5
};
static char ffslut64[] = {
64, 1, 48, 2, 57, 49, 28, 3, 61, 58, 50, 42, 38, 29, 17, 4,
62, 55, 59, 36, 53, 51, 43, 22, 45, 39, 33, 30, 24, 18, 12, 5,
63, 47, 56, 27, 60, 41, 37, 16, 54, 35, 52, 21, 44, 32, 23, 11,
@neesenk
neesenk / test.c
Created November 27, 2010 15:57
16进制打印内存的内容
/* 0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................ */
void hexdump(const char *buff, int blen)
{
static char hexmap[] = "0123456789abcdef";
int offset, len, count, hexoff;
unsigned char str[58], c;
str[57] = 0, str[40] = ' ';
for (offset = 0, len = 16; offset < blen; offset += 16) {
if (blen - offset < 16) {
len = blen - offset;
@neesenk
neesenk / darray.h
Created November 28, 2010 13:21
简单的动态buff实现
#ifndef __DYNAMINC_ARRAY_H__
#define __DYNAMINC_ARRAY_H__
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "darray.h"
int change_path(D_ARRAY *path, const char *change, size_t ch_len)
{
#define IS_DIR_END(path) ((path)->array[(path)->len - 1] == '/')
#define PUT_DRI(path, dir, dir_len) do { \
if ((path)->len == 0 || !IS_DIR_END(path)) \
dynamic_array_put(path, "/", 1); \
dynamic_array_put(path, dir, dir_len); \
} while (0);
/* 最多可能需要添加额外两个'/' */
/* from http://www.isthe.com/chongo/tech/comp/fnv/index.html */
uint32_t fnv(const void *buf, size_t len, uint32_t seed)
{
const uint8_t *bp, *be;
uint32_t h = seed ^ len;
for (bp = (const uint8_t *)buf, be = bp + len; bp < be; bp++) {
h ^= (uint32_t)(*bp);
/* h *= 0x01000193 */
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
char *strtrim(char *str)
{
size_t len;
char *pb;
for (pb = str; isspace(*pb); pb++); /* isspace(0) == 0 */
for (len = strlen(pb); len > 0 && isspace(pb[len - 1]); len--);
if (pb > str && len > 0)
memmove(str, pb, len);
str[len] = 0;
return str;
@neesenk
neesenk / test.c
Created December 4, 2010 13:12
生成集合中的一个随机子集
/* 输出[0..n-1]的一个随机子集m */
void randselect(int *a, int m, int n)
{
while (m > 0) {
if (random() % n-- < m)
a[--m] = n;
}
}
@neesenk
neesenk / getprogname_by_pid
Created December 4, 2010 13:29
通过pid获取程序名
const char *getprogname_by_pid(int pid)
{
static char path[1024];
static char sympath[32];
int len = 0;
snprintf(sympath, sizeof(sympath), "/proc/%d/exe", pid);
len = readlink(sympath, path, sizeof(path) - 1);
if (len < 0)
return NULL;
@neesenk
neesenk / macro.h
Created December 4, 2010 15:06
比较有用的宏
/* 编译时断言, cond为编译时常量 */
#define BUILD_ASSERT(cond) do { (void) sizeof(char [1 - 2*!(cond)]); } while(0)
#define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2*!(cond)]) - 1)
/* 数据类型的对齐方式 == __alignof__(t) */
#define ALIGNOF(t) ((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0)
#define container_of(ptr, type, member) \
((type *)((char *)(ptr) - (size_t)(&((type *)0)->member)))
@neesenk
neesenk / inthash.c
Created December 8, 2010 10:09
对整数hash的一些函数的测试
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
uint32_t hashint10(uint32_t h)
{
h ^= (h >> 20) ^ (h >> 12);
return h ^ (h >> 7) ^ (h >> 4);
}