-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnio4c.c
112 lines (91 loc) · 2.01 KB
/
nio4c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* nio4c.c
*
* copyright (c) 2019, 2020 Xiongfei Shi
*
* author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
* license: Apache-2.0
*
* https://github.com/shixiongfei/nio4c
*/
#include "nio4c_internal.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <stdio.h>
static WSADATA wsa_data;
#else /* _WIN32 */
#include <signal.h>
#endif /* _WIN32 */
static void *alloc_emul(void *ptr, size_t size) {
if (size)
return realloc(ptr, size);
free(ptr);
return NULL;
}
static void *(*nio_alloc)(void *, size_t) = alloc_emul;
void nio_setalloc(void *(*allocator)(void *, size_t)) {
nio_alloc = allocator ? allocator : alloc_emul;
}
void *nio_realloc(void *ptr, size_t size) { return nio_alloc(ptr, size); }
void *nio_calloc(size_t count, size_t size) {
void *p = nio_malloc(count * size);
if (p)
memset(p, 0, count * size);
return p;
}
unsigned long nio_nextpower(unsigned long size) {
if (0 == size)
return 2;
/* fast check if power of two */
if (0 == (size & (size - 1)))
return size;
size -= 1;
size |= size >> 1;
size |= size >> 2;
size |= size >> 4;
size |= size >> 8;
size |= size >> 16;
#if ULONG_MAX == ULLONG_MAX
size |= size >> 32;
#endif
size += 1;
return size;
}
int nio_initialize(nio_pollcreator creator) {
#ifdef _WIN32
_setmaxstdio(2048);
WSAStartup(MAKEWORD(2, 2), &wsa_data);
#else
signal(SIGPIPE, SIG_IGN);
#endif
return nio_pollinit(creator);
}
void nio_finalize(void) {
#ifdef _WIN32
WSACleanup();
#endif
}
unsigned short nio_checksum(const void *buffer, int len) {
unsigned int acc;
unsigned short src;
unsigned char *octetptr;
acc = 0;
octetptr = (unsigned char *)buffer;
while (len > 1) {
src = (*octetptr) << 8;
octetptr += 1;
src |= (*octetptr);
octetptr += 1;
acc += src;
len -= 2;
}
if (len > 0) {
src = (*octetptr) << 8;
acc += src;
}
while (0 != (acc & 0xFFFF0000UL))
acc = (acc >> 16) + (acc & 0x0000FFFFUL);
return htons((unsigned short)acc);
}