This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __DYNAMINC_ARRAY_H__ | |
#define __DYNAMINC_ARRAY_H__ | |
#include <string.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#ifdef __cplusplus | |
extern "C" { | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
/* 最多可能需要添加额外两个'/' */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 输出[0..n-1]的一个随机子集m */ | |
void randselect(int *a, int m, int n) | |
{ | |
while (m > 0) { | |
if (random() % n-- < m) | |
a[--m] = n; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 编译时断言, 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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
OlderNewer