I wrote some code that has an epoll-eventloop, accepts new connections and pretends to be a http-server. The posted code is the absolute minimum ... I removed everything (including all error-checks) to make it as short and to the point as possible:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <sys/uio.h>
#include <unistd.h>
int main () {
int servFd = socket (AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, IPPROTO_IP);
int value = 1;
setsockopt …Run Code Online (Sandbox Code Playgroud) 我正在用C(gcc)编写一个应用程序,它做了很多字符串比较.始终是一个带有很长编译时常量字符串列表的未知/动态字符串.所以我想我对动态字符串进行散列并将结果散列与常量字符串的预计算散列进行比较.
这样做我在一个函数(对于动态运行时字符串)和宏中使用哈希算法,以便gcc在编译时评估哈希.
我懂了:
#define HASH_CALC(h, s) ((h) * 33 + *(s))
#define HASH_CALC1(s) (HASH_CALC(hash_calc_start, s))
#define HASH_CALC2(s) (HASH_CALC(HASH_CALC1(s), s + 1))
#define HASH_CALC3(s) (HASH_CALC(HASH_CALC2(s), s + 2))
#define HASH_CALC4(s) (HASH_CALC(HASH_CALC3(s), s + 3))
#define HASH_CALC5(s) (HASH_CALC(HASH_CALC4(s), s + 4))
//--> cut ... goes till HASH_CALC32
static const unsigned long hash_calc_start = 5381;
unsigned long hash_str (const char* c);
void func () {
//This string is not constant ... just in this case to show something
char dynStr = "foo";
unsigned …Run Code Online (Sandbox Code Playgroud)