Merge pull request #41 from tezc/sock-init

Sock init, readme, introduce version
This commit is contained in:
Tezc 2021-02-14 18:08:34 +03:00 committed by GitHub
commit 9db998bad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 110 additions and 18 deletions

View File

@ -5,6 +5,11 @@ Common C libraries and data structures. (C99)
Each folder is stand-alone and contains a single .h .c pair.
There is no build, copy .h .c files you want.
Libraries are portable, see [test](#test) section for details.
As a general rule, all libraries report errors back to users (e.g out of memory).
If a library uses heap memory, you can plug your allocator.
### List
| Library | Description |
@ -58,7 +63,7 @@ cmake .. -DSANITIZER=address && make && make check
mkdir build; cd build;
cmake .. -DSANITIZER=undefined && make && make check
#coverage, requires GCC.
#coverage, requires GCC and lcov
mkdir build; cd build;
cmake .. -DCMAKE_BUILD_TYPE=Coverage; make; make coverage

View File

@ -32,6 +32,8 @@
#include <stdint.h>
#include <stdlib.h>
#define SC_ARRAY_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -31,6 +31,8 @@
#include <stdlib.h>
#include <string.h>
#define SC_BUF_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -27,6 +27,8 @@
#include <stdbool.h>
#define SC_COND_VERSION "1.0.0"
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else

View File

@ -28,6 +28,14 @@ enable_testing()
add_executable(${PROJECT_NAME}_test crc32_test.c sc_crc32.c)
if("${CMAKE_SIZEOF_VOID_P}" EQUAL "8")
check_c_compiler_flag(-msse4.2 HAVE_CRC32_HARDWARE)
if (${HAVE_CRC32_HARDWARE})
target_compile_options(${PROJECT_NAME}_test PRIVATE -msse4.2)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE -DHAVE_CRC32C)
endif ()
endif()
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang" OR
"${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")

View File

@ -42,6 +42,7 @@
#define CRC32_POLY 0x82f63b78
#ifdef HAVE_CRC32C
#include <memory.h>
#include <x86intrin.h>
/* Multiply a matrix times a vector over the Galois field of two elements,

View File

@ -27,6 +27,8 @@
#include <stdint.h>
#define SC_CRC32_VERSION "1.0.0"
/**
* Call once globally.
*/

View File

@ -29,6 +29,8 @@
#include <stddef.h>
#include <stdint.h>
#define SC_HEAP_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -29,6 +29,8 @@
#include <stddef.h>
#include <stdio.h>
#define SC_INI_VERSION "1.0.0"
// Set max line length. If a line is longer, it will be truncated silently.
#define SC_INI_MAX_LINE_LEN 1024

View File

@ -28,6 +28,7 @@
#include <stdbool.h>
#include <stddef.h>
#define SC_LIST_VERSION "1.0.0"
struct sc_list
{

View File

@ -31,6 +31,8 @@
#include <stdlib.h>
#include <string.h>
#define SC_LOG_VERSION "1.0.0"
enum sc_log_level
{
SC_LOG_DEBUG,

View File

@ -31,6 +31,8 @@
#include <stdint.h>
#include <stdlib.h>
#define SC_MAP_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -29,6 +29,8 @@
#include <stdbool.h>
#include <stdlib.h>
#define SC_MMAP_VERSION "1.0.0"
#if defined(_WIN32)
#include <windows.h>

View File

@ -25,6 +25,8 @@
#ifndef SC_MUTEX_H
#define SC_MUTEX_H
#define SC_MUTEX_VERSION "1.0.0"
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else

View File

@ -28,6 +28,8 @@
#include <stddef.h>
#include <stdio.h>
#define SC_OPTION_VERSION "1.0.0"
struct sc_option_item
{
const char letter;

View File

@ -28,6 +28,8 @@
#include <linux/perf_event.h>
#include <stdint.h>
#define SC_PERF_VERSION "1.0.0"
#define SC_PERF_HW_CACHE(CACHE, OP, RESULT) \
((PERF_COUNT_HW_CACHE_##CACHE) | (PERF_COUNT_HW_CACHE_OP_##OP << 8u) | \
(PERF_COUNT_HW_CACHE_RESULT_##RESULT << 16u))

View File

@ -32,6 +32,8 @@
#include <stdlib.h>
#include <string.h>
#define SC_QUEUE_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -28,6 +28,8 @@
#include <stddef.h>
#include <stdint.h>
#define SC_VERSION "1.0.0"
#define sc_max(a, b) (((a) > (b)) ? (a) : (b))
#define sc_min(a, b) (((a) > (b)) ? (b) : (a))

View File

@ -29,6 +29,8 @@
#include <stddef.h>
#include <stdarg.h>
#define SC_SIGNAL_VERSION "1.0.0"
/**
* Set shutdown fd here. When shutdown signal is received e.g SIGINT, SIGTERM.
* Signal handler will write 1 byte to shutdown fd. So, your app can detect

View File

@ -83,15 +83,36 @@ int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
return rc == 0 ? 0 : -1;
}
int sc_sock_startup()
{
int rc;
WSADATA data;
rc = WSAStartup(MAKEWORD(2, 2), &data);
if (rc != 0 || (LOBYTE(data.wVersion) != 2 || HIBYTE(data.wVersion) != 2)) {
return -1;
}
return 0;
}
int sc_sock_cleanup()
{
int rc;
rc = WSACleanup();
return rc != 0 ? -1 : 0;
}
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include <sys/time.h>
#define sc_close(n) close(n)
#define sc_unlink(n) unlink(n)
@ -101,6 +122,16 @@ int sc_sock_set_blocking(struct sc_sock *sock, bool blocking)
#define SC_EINPROGRESS EINPROGRESS
#define SC_EINTR EINTR
int sc_sock_startup()
{
return 0;
}
int sc_sock_cleanup()
{
return 0;
}
static int sc_sock_err()
{
return errno;
@ -1336,11 +1367,11 @@ int sc_sock_poll_term(struct sc_sock_poll *p)
return 0;
}
static int sc_sock_poll_expand(struct sc_sock_poll* p)
static int sc_sock_poll_expand(struct sc_sock_poll *p)
{
int cap, rc = 0;
void** data = NULL;
struct pollfd* ev = NULL;
void **data = NULL;
struct pollfd *ev = NULL;
if (p->count == p->cap) {
if (p->cap >= SC_SIZE_MAX / 2) {
@ -1362,7 +1393,7 @@ static int sc_sock_poll_expand(struct sc_sock_poll* p)
p->data = data;
for (int i = p->cap; i < cap; i++) {
p->events[i].fd = SC_INVALID;
p->events[i].fd = SC_INVALID;
}
p->cap = cap;
@ -1483,14 +1514,14 @@ uint32_t sc_sock_poll_event(struct sc_sock_poll *p, int i)
return events;
}
int sc_sock_poll_wait(struct sc_sock_poll* p, int timeout)
int sc_sock_poll_wait(struct sc_sock_poll *p, int timeout)
{
int n, rc = p->cap;
timeout = (timeout == -1) ? 16 : timeout;
do {
n = WSAPoll(p->events, (ULONG)p->cap, timeout);
n = WSAPoll(p->events, (ULONG) p->cap, timeout);
} while (n < 0 && errno == EINTR);
if (n == SC_INVALID) {

View File

@ -28,6 +28,8 @@
#include <stddef.h>
#include <stdint.h>
#define SC_SOCK_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else
@ -90,6 +92,18 @@ struct sc_sock
char err[128];
};
/**
* Call once when your application starts.
* @return 0 on success, negative on failure.
*/
int sc_sock_startup();
/**
* Call once before your application terminates
* @return 0 on success, negative on failure.
*/
int sc_sock_cleanup();
/**
* Initialize sock
*

View File

@ -1471,13 +1471,8 @@ int main()
assert(sc_mutex_init(&mutex) == 0);
#endif
#if defined(_WIN32) || defined(_WIN64)
WSADATA data;
assert(sc_sock_startup() == 0);
int rc = WSAStartup(MAKEWORD(2, 2), &data);
assert(rc == 0);
assert(LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) == 2);
#endif
test1();
test_ip4();
@ -1498,10 +1493,7 @@ int main()
test_err();
test_poll_mass();
#if defined(_WIN32) || defined(_WIN64)
rc = WSACleanup();
assert(rc == 0);
#endif
assert(sc_sock_cleanup() == 0);
#ifdef SC_HAVE_WRAP
assert(sc_mutex_term(&mutex) == 0);

View File

@ -29,6 +29,8 @@
#include <stdbool.h>
#include <stdint.h>
#define SC_STR_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else

View File

@ -24,6 +24,8 @@
#ifndef SC_THREAD_H
#define SC_THREAD_H
#define SC_THREAD_VERSION "1.0.0"
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>

View File

@ -24,6 +24,8 @@
#ifndef SC_TIME_H
#define SC_TIME_H
#define SC_TIME_VERSION "1.0.0"
#include <stdint.h>
/**

View File

@ -25,6 +25,8 @@
#ifndef SC_TIMER_H
#define SC_TIMER_H
#define SC_TIMER_VERSION "1.0.0"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

View File

@ -24,6 +24,8 @@
#ifndef SC_URI_H
#define SC_URI_H
#define SC_URI_VERSION "1.0.0"
#ifdef SC_HAVE_CONFIG_H
#include "config.h"
#else