From f2e553bc98ad6a37540bd05d5b20cd4caee8d72c Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Sat, 16 Mar 2024 16:08:06 +0800 Subject: [PATCH] Build acl by cosmocc. --- build_by_cosmocc | 16 ++++ lib_acl/Makefile | 8 ++ lib_acl/include/thread/acl_pthread.h | 4 +- lib_acl/samples/Makefile.in | 7 ++ lib_acl/samples/Makefile_cpp.in | 8 ++ lib_acl/samples/aio/Makefile.in | 2 +- lib_acl/samples/dbuf/valgrind.sh | 0 lib_acl/src/event/events_epoll.h | 7 +- lib_acl/src/event/events_epoll_thr.c | 8 +- lib_acl/src/master/template/ifmonitor.c | 4 + lib_acl/src/net/acl_sane_inet.c | 8 +- lib_acl/src/net/dns/acl_rfc1035.c | 2 + lib_acl/src/net/listen/acl_fifo_listen.c | 9 +++ lib_acl/src/stdlib/acl_mbox.c | 2 +- lib_acl/src/stdlib/acl_mylog.c | 4 +- lib_acl/src/stdlib/acl_vstream_popen.c | 18 ++--- lib_acl/src/stdlib/filedir/acl_fhandle.c | 8 +- lib_acl/src/stdlib/iostuff/acl_read_wait.c | 5 ++ lib_acl/src/stdlib/memory/acl_dbuf_pool.c | 48 +++++++----- lib_acl/src/stdlib/sys/unix/acl_safe_open.c | 75 ++++++++++--------- lib_acl_cpp/Makefile | 19 ++++- .../include/acl_cpp/stdlib/dbuf_pool.hpp | 6 +- lib_acl_cpp/samples/Makefile.in | 9 +++ lib_acl_cpp/samples/dbuf/alloc/main.cpp | 3 +- lib_acl_cpp/samples/dbuf/dbuf1/valgrind.sh | 0 lib_acl_cpp/samples/dbuf/dbuf3/valgrind.sh | 0 lib_acl_cpp/src/acl_stdafx.hpp | 11 ++- lib_acl_cpp/src/http/http_client.cpp | 2 + lib_acl_cpp/src/stdlib/dbuf_pool.cpp | 14 ++-- lib_acl_cpp/src/stdlib/zlib_stream.cpp | 20 ++++- lib_fiber/c/Makefile | 9 +++ lib_fiber/c/src/common/pthread_patch.c | 2 +- lib_fiber/c/src/define.h | 2 +- lib_fiber/c/src/dns/rfc1035.c | 2 + lib_fiber/c/src/event.h | 7 +- lib_fiber/c/src/event/event_epoll.c | 9 ++- lib_fiber/c/src/hook/getaddrinfo.c | 2 + lib_fiber/c/src/hook/hook.h | 13 +++- lib_fiber/c/src/hook/io.h | 2 +- lib_fiber/c/src/stdafx.h | 9 ++- lib_fiber/samples/Makefile.in | 7 ++ lib_protocol/Makefile | 8 ++ 42 files changed, 297 insertions(+), 102 deletions(-) create mode 100755 build_by_cosmocc mode change 100644 => 100755 lib_acl/samples/dbuf/valgrind.sh mode change 100644 => 100755 lib_acl_cpp/samples/dbuf/dbuf1/valgrind.sh mode change 100644 => 100755 lib_acl_cpp/samples/dbuf/dbuf3/valgrind.sh diff --git a/build_by_cosmocc b/build_by_cosmocc new file mode 100755 index 000000000..58ed86806 --- /dev/null +++ b/build_by_cosmocc @@ -0,0 +1,16 @@ +#!/bin/sh +export ENV_CC=cosmocc +export ENV_CPP=cosmoc++ + +cd lib_acl; make -j 4; +cd samples; make; +cd ../.. + +cd lib_protocol; make -j 4; +cd samples; make; +cd ../.. + +cd lib_acl_cpp; make -j 10 +cd samples; make; +cd ../.. + diff --git a/lib_acl/Makefile b/lib_acl/Makefile index ef2e44a1d..f4c46ff2a 100644 --- a/lib_acl/Makefile +++ b/lib_acl/Makefile @@ -95,6 +95,14 @@ ifeq ($(findstring clang++, $(CC)), clang) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmocc, $(CC)), cosmocc) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + AR = cosmoar + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) UNIXTYPE = FREEBSD diff --git a/lib_acl/include/thread/acl_pthread.h b/lib_acl/include/thread/acl_pthread.h index ab52da41b..5ee55680d 100644 --- a/lib_acl/include/thread/acl_pthread.h +++ b/lib_acl/include/thread/acl_pthread.h @@ -19,8 +19,10 @@ extern "C" { # if defined(ACL_FREEBSD) || defined(ACL_SUNOS5) || defined(ACL_MACOSX) || defined(ALPINE) || defined(MINGW) # define ACL_PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE -# else +# elif defined(PTHREAD_MUTEX_RECURSIVE_NP) # define ACL_PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP +# else +# define ACL_PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE # endif typedef pthread_t acl_pthread_t; diff --git a/lib_acl/samples/Makefile.in b/lib_acl/samples/Makefile.in index e29d39cd9..144b67d1f 100644 --- a/lib_acl/samples/Makefile.in +++ b/lib_acl/samples/Makefile.in @@ -49,6 +49,13 @@ ifeq ($(findstring gcc, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmocc, $(CC)), cosmocc) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) CFLAGS += -DFREEBSD diff --git a/lib_acl/samples/Makefile_cpp.in b/lib_acl/samples/Makefile_cpp.in index 35ba0a15a..403ec582f 100644 --- a/lib_acl/samples/Makefile_cpp.in +++ b/lib_acl/samples/Makefile_cpp.in @@ -41,6 +41,14 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmoc+, $(CC)), cosmoc+) + CFLAGS += -DCOSMOCC + CFLAGS += -DLINUX2 + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -fexceptions + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) CFLAGS += -DFREEBSD -pedantic diff --git a/lib_acl/samples/aio/Makefile.in b/lib_acl/samples/aio/Makefile.in index 08d41b7ab..c0573f97e 100644 --- a/lib_acl/samples/aio/Makefile.in +++ b/lib_acl/samples/aio/Makefile.in @@ -1,4 +1,4 @@ -CC = $(MY_ENV_CC) +CC = $(ENV_CC) #CC = gcc CFLAGS = -c -g -W -Wall -Wcast-qual -Wcast-align \ diff --git a/lib_acl/samples/dbuf/valgrind.sh b/lib_acl/samples/dbuf/valgrind.sh old mode 100644 new mode 100755 diff --git a/lib_acl/src/event/events_epoll.h b/lib_acl/src/event/events_epoll.h index 6d354bdeb..9ffcf6b98 100644 --- a/lib_acl/src/event/events_epoll.h +++ b/lib_acl/src/event/events_epoll.h @@ -24,7 +24,12 @@ extern "C" { */ #if (ACL_EVENTS_KERNEL_STYLE == ACL_EVENTS_STYLE_EPOLL) #include -#include +#ifdef COSMOCC +# include +# include +#else +# include +#endif #define EVENT_NAME "epoll" diff --git a/lib_acl/src/event/events_epoll_thr.c b/lib_acl/src/event/events_epoll_thr.c index 9b93754c9..c10dee196 100644 --- a/lib_acl/src/event/events_epoll_thr.c +++ b/lib_acl/src/event/events_epoll_thr.c @@ -33,7 +33,13 @@ #ifdef ACL_EVENTS_KERNEL_STYLE #if (ACL_EVENTS_KERNEL_STYLE == ACL_EVENTS_STYLE_EPOLL) -#include +#ifdef COSMOCC +# include +# include +#else +# include +#endif + #include "events_fdtable.h" #include "events.h" diff --git a/lib_acl/src/master/template/ifmonitor.c b/lib_acl/src/master/template/ifmonitor.c index 5cd24764d..0b5ffa199 100644 --- a/lib_acl/src/master/template/ifmonitor.c +++ b/lib_acl/src/master/template/ifmonitor.c @@ -7,6 +7,9 @@ #include #include + +#ifndef COSMOCC + #include //#include #include @@ -117,5 +120,6 @@ void netlink_monitor(ACL_EVENT *event, monitor_callback callback, void *ctx) acl_event_enable_read(event, stream, 0, netlink_callback, nc); } +#endif /* COSMOCC */ #endif /* ACL_LINUX */ diff --git a/lib_acl/src/net/acl_sane_inet.c b/lib_acl/src/net/acl_sane_inet.c index 354e3192d..887c33b61 100644 --- a/lib_acl/src/net/acl_sane_inet.c +++ b/lib_acl/src/net/acl_sane_inet.c @@ -222,12 +222,14 @@ size_t acl_inet_ntop(const struct sockaddr *sa, char *buf, size_t size) return 0; } -#if defined(ACL_UNIX) || (defined(ACL_WINDOWS) && _MSC_VER >= 1600) +#if defined(COSMOCC) + ifname[0] = 0; +#elif defined(ACL_UNIX) || (defined(ACL_WINDOWS) && _MSC_VER >= 1600) ptr = (char*) if_indextoname(in6->sin6_scope_id, ifname); if (ptr == NULL) { ifname[0] = 0; } -# else +#else ifname[0] = 0; #endif @@ -323,11 +325,13 @@ size_t acl_inet_pton(int af, const char *src, struct sockaddr *dst) in6->sin6_family = AF_INET6; in6->sin6_port = htons(port); #if defined(ACL_UNIX) || (defined(ACL_WINDOWS) && _MSC_VER >= 1600) +# ifndef COSMOCC if (ptr && *ptr && !(in6->sin6_scope_id = if_nametoindex(ptr))) { acl_msg_error("%s(%d): if_nametoindex error %s", __FUNCTION__, __LINE__, acl_last_serror()); return 0; } +# endif #endif if (inet_pton(af, buf, &in6->sin6_addr) == 0) { diff --git a/lib_acl/src/net/dns/acl_rfc1035.c b/lib_acl/src/net/dns/acl_rfc1035.c index 822b46b2c..74f880c0f 100644 --- a/lib_acl/src/net/dns/acl_rfc1035.c +++ b/lib_acl/src/net/dns/acl_rfc1035.c @@ -1000,11 +1000,13 @@ static size_t save_addr2rr(int type, const char *src, ACL_RFC1035_RR *rr) in6.sin6_port = htons(0); #if defined(ACL_UNIX) || (defined(ACL_WINDOWS) && _MSC_VER >= 1600) +# ifndef COSMOCC if (ptr && *ptr && !(in6.sin6_scope_id = if_nametoindex(ptr))) { acl_msg_error("%s(%d): if_nametoindex error %s", __FUNCTION__, __LINE__, acl_last_serror()); return 0; } +# endif #endif if (inet_pton(AF_INET6, buf, &in6.sin6_addr) == 0) { acl_msg_error("%s(%d): invalid addr=%s", diff --git a/lib_acl/src/net/listen/acl_fifo_listen.c b/lib_acl/src/net/listen/acl_fifo_listen.c index 4d4c0ce36..06c8e15af 100644 --- a/lib_acl/src/net/listen/acl_fifo_listen.c +++ b/lib_acl/src/net/listen/acl_fifo_listen.c @@ -18,6 +18,10 @@ #include #include +#ifdef COSMOCC +#include +#endif + /* Utility library. */ #include "stdlib/acl_msg.h" @@ -53,7 +57,12 @@ int acl_fifo_listen(const char *path, int permissions, int block_mode) acl_last_strerror(tbuf, sizeof(tbuf))); return -1; } + +#ifdef COSMOCC + if (sys_mkfifo(path, permissions) < 0) { +#else if (mkfifo(path, permissions) < 0) { +#endif acl_msg_error("%s: create fifo %s: %s", myname, path, acl_last_strerror(tbuf, sizeof(tbuf))); return -1; diff --git a/lib_acl/src/stdlib/acl_mbox.c b/lib_acl/src/stdlib/acl_mbox.c index 8145e4ad6..a04c56af6 100644 --- a/lib_acl/src/stdlib/acl_mbox.c +++ b/lib_acl/src/stdlib/acl_mbox.c @@ -19,7 +19,7 @@ # if defined(ALPINE) # include # define HAS_EVENTFD -# else +# elif !defined(COSMOCC) # include # if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) # include diff --git a/lib_acl/src/stdlib/acl_mylog.c b/lib_acl/src/stdlib/acl_mylog.c index d7cca4f5a..de0fd908f 100644 --- a/lib_acl/src/stdlib/acl_mylog.c +++ b/lib_acl/src/stdlib/acl_mylog.c @@ -154,8 +154,10 @@ static void init_log_mutex(acl_pthread_mutex_t *lock) n2 = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # elif defined(MINGW) n2 = 0; -# else +# elif defined(PTHREAD_MUTEX_RECURSIVE_NP) n2 = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); +# else + n2 = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # endif thread_mutex_init(lock, !n1 && !n2 ? &attr : NULL); #else diff --git a/lib_acl/src/stdlib/acl_vstream_popen.c b/lib_acl/src/stdlib/acl_vstream_popen.c index e306400a2..124540914 100644 --- a/lib_acl/src/stdlib/acl_vstream_popen.c +++ b/lib_acl/src/stdlib/acl_vstream_popen.c @@ -30,7 +30,7 @@ typedef int (*ACL_VSTREAM_WAITPID_FN) (pid_t, ACL_WAIT_STATUS_T *, int); typedef struct ACL_VSTREAM_POPEN_ARGS { char **argv; char *command; - int privileged; + int priv; char **env; char **export; char *shell; @@ -53,7 +53,7 @@ static void vstream_parse_args(ACL_VSTREAM_POPEN_ARGS *args, va_list ap) */ args->argv = 0; args->command = 0; - args->privileged = 0; + args->priv = 0; args->env = 0; args->export = 0; args->shell = 0; @@ -93,7 +93,7 @@ static void vstream_parse_args(ACL_VSTREAM_POPEN_ARGS *args, va_list ap) break; #ifdef ACL_UNIX case ACL_VSTREAM_POPEN_UID: - args->privileged = 1; + args->priv = 1; #ifdef MINGW args->uid = (uid_t) va_arg(ap, int); #else @@ -101,7 +101,7 @@ static void vstream_parse_args(ACL_VSTREAM_POPEN_ARGS *args, va_list ap) #endif break; case ACL_VSTREAM_POPEN_GID: - args->privileged = 1; + args->priv = 1; #ifdef MINGW args->gid = (gid_t) va_arg(ap, int); #else @@ -122,11 +122,11 @@ static void vstream_parse_args(ACL_VSTREAM_POPEN_ARGS *args, va_list ap) " or ACL_VSTREAM_POPEN_COMMAND", myname); } #ifdef ACL_UNIX - if (args->privileged != 0 && args->uid == 0) { - acl_msg_panic("%s: privileged uid", myname); + if (args->priv != 0 && args->uid == 0) { + acl_msg_panic("%s: priv uid", myname); } - if (args->privileged != 0 && args->gid == 0) { - acl_msg_panic("%s: privileged gid", myname); + if (args->priv != 0 && args->gid == 0) { + acl_msg_panic("%s: priv gid", myname); } #endif } @@ -214,7 +214,7 @@ ACL_VSTREAM *acl_vstream_popen(int flags,...) /* * Don't try to become someone else unless the user specified it. */ - if (args.privileged) { + if (args.priv) { acl_set_ugid(args.uid, args.gid); } diff --git a/lib_acl/src/stdlib/filedir/acl_fhandle.c b/lib_acl/src/stdlib/filedir/acl_fhandle.c index a86d73d5e..a28f02ece 100644 --- a/lib_acl/src/stdlib/filedir/acl_fhandle.c +++ b/lib_acl/src/stdlib/filedir/acl_fhandle.c @@ -84,8 +84,10 @@ static ACL_FHANDLE *__fhandle_alloc(size_t size, unsigned int oflags) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # elif defined(MINGW) || defined(ALPINE) pthread_mutex_init(&fs->mutex, &attr); -# else +# elif defined(PTHREAD_MUTEX_RECURSIVE_NP) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); +# else + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # endif pthread_mutex_init(&fs->mutex, &attr); #else @@ -430,8 +432,10 @@ void acl_fhandle_init(int cache_size, int debug_section, unsigned int flags) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # elif defined(MINGW) || defined(ALPINE) pthread_mutex_init(&__fhandle_mutex, &attr); -# else +# elif defined(PTHREAD_MUTEX_RECURSIVE_NP) pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP); +# else + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); # endif pthread_mutex_init(&__fhandle_mutex, &attr); #else diff --git a/lib_acl/src/stdlib/iostuff/acl_read_wait.c b/lib_acl/src/stdlib/iostuff/acl_read_wait.c index fce650ac2..d7f4e7ea9 100644 --- a/lib_acl/src/stdlib/iostuff/acl_read_wait.c +++ b/lib_acl/src/stdlib/iostuff/acl_read_wait.c @@ -26,7 +26,12 @@ #include "init/acl_init.h" #include "thread/acl_pthread.h" +#ifdef COSMOCC +#include +#include +#else #include +#endif typedef struct EPOLL_CTX { diff --git a/lib_acl/src/stdlib/memory/acl_dbuf_pool.c b/lib_acl/src/stdlib/memory/acl_dbuf_pool.c index 21d658047..9569da1f9 100644 --- a/lib_acl/src/stdlib/memory/acl_dbuf_pool.c +++ b/lib_acl/src/stdlib/memory/acl_dbuf_pool.c @@ -120,13 +120,15 @@ int acl_dbuf_pool_reset(ACL_DBUF_POOL *pool, size_t off) acl_msg_warn("warning: %s(%d) off(%ld) > pool->off(%ld)", __FUNCTION__, __LINE__, (long) off, (long) pool->off); return -1; - } else if (off == pool->off) + } else if (off == pool->off) { return 0; + } while (1) { /* 如果当前内存块有保留内存区,则保留整个内存块 */ - if (iter->keep) + if (iter->keep) { break; + } /* 计算当前内存块被使用的内存大小 */ n = iter->addr - iter->buf; @@ -157,8 +159,9 @@ int acl_dbuf_pool_reset(ACL_DBUF_POOL *pool, size_t off) /* off 为下一个内存块的 addr 所在的相对偏移位置 */ pool->off -=n; - if (tmp->size > pool->block_size) + if (tmp->size > pool->block_size) { pool->huge--; + } #ifdef USE_VALLOC free(tmp); @@ -198,20 +201,23 @@ int acl_dbuf_pool_free(ACL_DBUF_POOL *pool, const void *addr) return -1; } - if (iter->used > 0 || iter->keep) + if (iter->used > 0 || iter->keep) { return 0; + } /* should free the ACL_DBUF block */ - if (iter == pool->head) + if (iter == pool->head) { pool->head = iter->next; - else + } else { prev->next = iter->next; + } pool->off -= iter->addr - iter->buf; - if (iter->size > pool->block_size) + if (iter->size > pool->block_size) { pool->huge--; + } acl_myfree(iter); @@ -234,8 +240,9 @@ static ACL_DBUF *acl_dbuf_alloc(ACL_DBUF_POOL *pool, size_t length) dbuf->addr = dbuf->buf; pool->head = dbuf; - if (length > pool->block_size) + if (length > pool->block_size) { pool->huge++; + } pool->count++; return dbuf; @@ -248,17 +255,16 @@ void *acl_dbuf_pool_alloc(ACL_DBUF_POOL *pool, size_t length) length += 4 - length % 4; - if (length > pool->block_size) + if (length > pool->block_size) { dbuf = acl_dbuf_alloc(pool, length); - else if (pool->head == NULL) + } else if (pool->head == NULL) { dbuf = acl_dbuf_alloc(pool, pool->block_size); - else if (pool->block_size < ((char*) pool->head->addr - - (char*) pool->head->buf) + length) - { + } else if (pool->block_size < ((char*) pool->head->addr + - (char*) pool->head->buf) + length) { dbuf = acl_dbuf_alloc(pool, pool->block_size); - } - else + } else { dbuf = pool->head; + } ptr = dbuf->addr; dbuf->addr = (char*) dbuf->addr + length; @@ -273,8 +279,9 @@ void *acl_dbuf_pool_calloc(ACL_DBUF_POOL *pool, size_t length) void *ptr; ptr = acl_dbuf_pool_alloc(pool, length); - if (ptr) + if (ptr) { memset(ptr, 0, length); + } return ptr; } @@ -293,8 +300,9 @@ char *acl_dbuf_pool_strndup(ACL_DBUF_POOL *pool, const char *s, size_t len) char *ptr; size_t n = strlen(s); - if (n > len) + if (n > len) { n = len; + } ptr = (char*) acl_dbuf_pool_alloc(pool, n + 1); memcpy(ptr, s, n); ptr[n] = 0; @@ -317,8 +325,9 @@ int acl_dbuf_pool_keep(ACL_DBUF_POOL *pool, const void *addr) while (iter) { if (ptr < iter->addr && ptr >= iter->buf) { iter->keep++; - if (iter->keep <= iter->used) + if (iter->keep <= iter->used) { return 0; + } acl_msg_warn("warning: %s(%d), keep(%d) > used(%d)", __FUNCTION__, __LINE__, @@ -342,8 +351,9 @@ int acl_dbuf_pool_unkeep(ACL_DBUF_POOL *pool, const void *addr) while (iter) { if (ptr < iter->addr && ptr >= iter->buf) { iter->keep--; - if (iter->keep >= 0) + if (iter->keep >= 0) { return 0; + } acl_msg_warn("warning: %s(%d), keep(%d) < 0", __FUNCTION__, __LINE__, iter->keep); diff --git a/lib_acl/src/stdlib/sys/unix/acl_safe_open.c b/lib_acl/src/stdlib/sys/unix/acl_safe_open.c index 70cc5a7a8..6312b4371 100644 --- a/lib_acl/src/stdlib/sys/unix/acl_safe_open.c +++ b/lib_acl/src/stdlib/sys/unix/acl_safe_open.c @@ -34,9 +34,9 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, { struct stat local_statbuf; struct stat lstat_st; - int saved_error; + int saved_error; + char tbuf[256]; ACL_VSTREAM *fp; - char tbuf[256]; /* * Open an existing file. @@ -49,7 +49,7 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, path, acl_last_strerror(tbuf, sizeof(tbuf))); } acl_set_error(saved_error); - return (0); + return 0; } /* @@ -57,19 +57,23 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, * link (so that someone can't lure us into clobbering a sensitive file * by making a hard link to it), and it must be a non-symlink file. */ - if (fstat_st == 0) + if (fstat_st == 0) { fstat_st = &local_statbuf; + } + if (fstat(ACL_VSTREAM_FILE(fp), fstat_st) < 0) { acl_msg_fatal("%s: bad open file status: %s", path, acl_last_strerror(tbuf, sizeof(tbuf))); } else if (S_ISDIR(fstat_st->st_mode)) { - if (why) + if (why) { acl_vstring_sprintf(why, "file is a directory"); + } acl_set_error(EISDIR); } else if (fstat_st->st_nlink != 1) { - if (why) + if (why) { acl_vstring_sprintf(why, "file has %d hard links", - (int) fstat_st->st_nlink); + (int) fstat_st->st_nlink); + } acl_set_error(EPERM); } @@ -90,15 +94,18 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, * delivering mail to a world-writable mailbox directory. */ else if (lstat(path, &lstat_st) < 0) { - if (why) + if (why) { acl_vstring_sprintf(why, "file status changed unexpectedly: %s", acl_last_strerror(tbuf, sizeof(tbuf))); + } acl_set_error(EPERM); } else if (S_ISLNK(lstat_st.st_mode)) { - if (lstat_st.st_uid == 0) - return (fp); - if (why) + if (lstat_st.st_uid == 0) { + return fp; + } + if (why) { acl_vstring_sprintf(why, "file is a symbolic link"); + } acl_set_error(EPERM); } else if (fstat_st->st_dev != lstat_st.st_dev || fstat_st->st_ino != lstat_st.st_ino @@ -107,8 +114,9 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, #endif || fstat_st->st_nlink != lstat_st.st_nlink || fstat_st->st_mode != lstat_st.st_mode) { - if (why) + if (why) { acl_vstring_sprintf(why, "file status changed unexpectedly"); + } acl_set_error(EPERM); } @@ -116,14 +124,14 @@ static ACL_VSTREAM *acl_safe_open_exist(const char *path, int flags, * We are almost there... */ else { - return (fp); + return fp; } /* * End up here in case of fstat()/lstat() problems or inconsistencies. */ acl_vstream_fclose(fp); - return (0); + return 0; } /* acl_safe_open_create - create new file */ @@ -140,10 +148,11 @@ static ACL_VSTREAM *acl_safe_open_create(const char *path, int flags, int mode, */ fp = acl_vstream_fopen(path, flags | (O_CREAT | O_EXCL), mode, 4096); if (fp == 0) { - if (why) + if (why) { acl_vstring_sprintf(why, "cannot create file exclusively: %s", acl_last_strerror(tbuf, sizeof(tbuf))); - return (0); + } + return 0; } /* @@ -161,22 +170,23 @@ static ACL_VSTREAM *acl_safe_open_create(const char *path, int flags, int mode, /* * Optionally look up the file attributes. */ - if (st != 0 && fstat(ACL_VSTREAM_FILE(fp), st) < 0) + if (st != 0 && fstat(ACL_VSTREAM_FILE(fp), st) < 0) { acl_msg_fatal("%s: bad open file status: %s", path, acl_last_strerror(tbuf, sizeof(tbuf))); + } /* * We are almost there... */ else { - return (fp); + return fp; } /* * End up here in case of trouble. */ acl_vstream_fclose(fp); - return (0); + return 0; } /* acl_safe_open - safely open or create file */ @@ -185,22 +195,20 @@ ACL_VSTREAM *acl_safe_open(const char *path, int flags, int mode, struct stat * st, uid_t user, gid_t group, ACL_VSTRING *why) { ACL_VSTREAM *fp; - - switch (flags & (O_CREAT | O_EXCL)) { + unsigned f = ((unsigned) flags) & (O_CREAT | O_EXCL); /* * Open an existing file, carefully. */ - case 0: + if (f == 0) { return (acl_safe_open_exist(path, flags, st, why)); - + } else if (f == (O_CREAT | O_EXCL)) { /* * Create a new file, carefully. */ - case O_CREAT | O_EXCL: - return (acl_safe_open_create(path, flags, mode, - st, user, group, why)); - + return acl_safe_open_create(path, flags, mode, + st, user, group, why); + } else if (f == O_CREAT) { /* * Open an existing file or create a new one, carefully. * When opening an existing file, we are prepared to deal @@ -208,26 +216,25 @@ ACL_VSTREAM *acl_safe_open(const char *path, int flags, int mode, * are prepared for "file exists" errors only. Any other * error means we better give up trying. */ - case O_CREAT: fp = acl_safe_open_exist(path, flags, st, why); if (fp == 0 && acl_last_error() == ENOENT) { fp = acl_safe_open_create(path, flags, mode, st, - user, group, why); - if (fp == 0 && acl_last_error() == EEXIST) + user, group, why); + if (fp == 0 && acl_last_error() == EEXIST) { fp = acl_safe_open_exist(path, flags, st, why); + } } - return (fp); - + return fp; + } else { /* * Interface violation. Sorry, but we must be strict. */ - default: acl_msg_panic("acl_safe_open: O_EXCL flag without O_CREAT flag"); } /* no reache here */ - return (NULL); + return NULL; } #endif /* ACL_UNIX */ diff --git a/lib_acl_cpp/Makefile b/lib_acl_cpp/Makefile index 28846cd4c..35056cdf0 100644 --- a/lib_acl_cpp/Makefile +++ b/lib_acl_cpp/Makefile @@ -73,6 +73,15 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmoc+, $(CC)), cosmoc+) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + CFLAGS += -fexceptions + AR = cosmoar + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) UNIXTYPE = FREEBSD @@ -159,10 +168,12 @@ ifeq ($(findstring Linux, $(UNIXNAME)), Linux) CFLAGS += -DHAS_POLARSSL_DLL endif - ifeq ($(MBEDTLS_STATIC), yes) - CFLAGS += -DHAS_MBEDTLS - else - CFLAGS += -DHAS_MBEDTLS_DLL + ifneq ($(findstring cosmoc+, $(CC)), cosmoc+) + ifeq ($(MBEDTLS_STATIC), yes) + CFLAGS += -DHAS_MBEDTLS + else + CFLAGS += -DHAS_MBEDTLS_DLL + endif endif shared_ldflags = -lprotocol -lacl -lrt -lpthread -lz -ldl diff --git a/lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp b/lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp index 53b6e298b..692295039 100644 --- a/lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp +++ b/lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp @@ -34,12 +34,12 @@ public: * @param size {size_t} 由编译传入的 dbuf_pool 对象的长度大小 * @param nblock {size_t} 内部采用的内存块(4096)的倍数 */ - void *operator new(size_t size, size_t nblock = 2); + static void *operator new(size_t size, size_t nblock = 2); #if defined(_WIN32) || defined(_WIN64) - void operator delete(void* ptr, size_t); + static void operator delete(void* ptr, size_t); #endif - void operator delete(void* ptr); + static void operator delete(void* ptr); /** * 重置内存池的状态以便于重复使用该内存池对象 diff --git a/lib_acl_cpp/samples/Makefile.in b/lib_acl_cpp/samples/Makefile.in index fe3aa2275..277c73080 100644 --- a/lib_acl_cpp/samples/Makefile.in +++ b/lib_acl_cpp/samples/Makefile.in @@ -47,6 +47,15 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmoc+, $(CC)), cosmoc+) + CFLAGS += -DCOSMOCC + CFLAGS += -DLINUX2 + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -fexceptions + +endif + + # For FreeBSD ifeq ($(findstring FreeBSD, $(OSNAME)), FreeBSD) CFLAGS += -DFREEBSD diff --git a/lib_acl_cpp/samples/dbuf/alloc/main.cpp b/lib_acl_cpp/samples/dbuf/alloc/main.cpp index 680fe323d..0da1a0638 100644 --- a/lib_acl_cpp/samples/dbuf/alloc/main.cpp +++ b/lib_acl_cpp/samples/dbuf/alloc/main.cpp @@ -35,6 +35,7 @@ static void test1(int nblock, int capacity, int incr, int max, int pos) { dbuf.set_increment((size_t) incr); myobj* o; std::list > objs; + //std::list objs; for (int i = 0; i < max; i++) { o = dbuf.create(&dbuf, i); @@ -66,8 +67,6 @@ static void test1(int nblock, int capacity, int incr, int max, int pos) { objs.clear(); dbuf.dbuf_reset(); - printf("\r\n----------------Again---------------\r\n"); - for (int i = 0; i < max; i++) { o = dbuf.create(&dbuf, i); if (i < 10) { diff --git a/lib_acl_cpp/samples/dbuf/dbuf1/valgrind.sh b/lib_acl_cpp/samples/dbuf/dbuf1/valgrind.sh old mode 100644 new mode 100755 diff --git a/lib_acl_cpp/samples/dbuf/dbuf3/valgrind.sh b/lib_acl_cpp/samples/dbuf/dbuf3/valgrind.sh old mode 100644 new mode 100755 diff --git a/lib_acl_cpp/src/acl_stdafx.hpp b/lib_acl_cpp/src/acl_stdafx.hpp index 662f91ffe..c35910e3e 100644 --- a/lib_acl_cpp/src/acl_stdafx.hpp +++ b/lib_acl_cpp/src/acl_stdafx.hpp @@ -51,9 +51,13 @@ #endif // ACL_CPP_DEBUG_MEM #if defined(ACL_UNIX) -#include -#include -#include +# include +# include +# ifdef COSMOCC +# include +# else +# include +# endif #endif // 加入下面一行可以加快在 VC 下的编译速度 @@ -66,4 +70,3 @@ #define ACL_CPP_DEBUG_HTTP_NET 42 #define DEBUG_MQTT 43 #define ACL_CPP_DEBUG_MAX 70 - diff --git a/lib_acl_cpp/src/http/http_client.cpp b/lib_acl_cpp/src/http/http_client.cpp index f980a0c4a..1a2ab77d1 100644 --- a/lib_acl_cpp/src/http/http_client.cpp +++ b/lib_acl_cpp/src/http/http_client.cpp @@ -1,10 +1,12 @@ #include "acl_stdafx.hpp" +#if 0 #if defined(_WIN32) || defined(_WIN64) # include "zlib-1.2.11/zlib.h" #else # include #endif +#endif #ifndef ACL_PREPARE_COMPILE #include "acl_cpp/stdlib/log.hpp" diff --git a/lib_acl_cpp/src/stdlib/dbuf_pool.cpp b/lib_acl_cpp/src/stdlib/dbuf_pool.cpp index f131fd5ef..511a8b514 100644 --- a/lib_acl_cpp/src/stdlib/dbuf_pool.cpp +++ b/lib_acl_cpp/src/stdlib/dbuf_pool.cpp @@ -25,10 +25,15 @@ void *dbuf_pool::operator new(size_t size, size_t nblock /* = 2 */) if (nblock == 0) { nblock = 2; } + ACL_DBUF_POOL* pool = acl_dbuf_pool_create(4096 * nblock); - dbuf_pool* dbuf = (dbuf_pool*) acl_dbuf_pool_alloc(pool, size); - dbuf->pool_ = pool; - dbuf->mysize_ = size; + assert(pool); + + dbuf_pool* dbuf = (dbuf_pool*) acl_dbuf_pool_alloc(pool, size); + assert(dbuf); + + dbuf->pool_ = pool; + dbuf->mysize_ = size; return dbuf; } @@ -49,8 +54,7 @@ void dbuf_pool::operator delete(void* ptr) bool dbuf_pool::dbuf_reset(size_t reserve /* = 0 */) { - return acl_dbuf_pool_reset(pool_, mysize_ + reserve) == 0 - ? true : false; + return acl_dbuf_pool_reset(pool_, mysize_ + reserve) == 0; } void* dbuf_pool::dbuf_alloc(size_t len) diff --git a/lib_acl_cpp/src/stdlib/zlib_stream.cpp b/lib_acl_cpp/src/stdlib/zlib_stream.cpp index d4546d225..025a0eefb 100644 --- a/lib_acl_cpp/src/stdlib/zlib_stream.cpp +++ b/lib_acl_cpp/src/stdlib/zlib_stream.cpp @@ -2,6 +2,8 @@ #if defined(_WIN32) || defined(_WIN64) # include "zlib-1.2.11/zlib.h" +#elif defined(COSMOCC) +# include #else # include #endif @@ -187,11 +189,16 @@ static void __zlib_dll_load(void) # else //# define __deflateInit deflateInit_ -# define __deflateInit2 deflateInit2_ +# ifdef COSMOCC +# define __deflateInit2 deflateInit2 +# define __inflateInit2 inflateInit2 +# else +# define __deflateInit2 deflateInit2_ +# define __inflateInit2 inflateInit2_ +# endif # define __deflate deflate # define __deflateReset deflateReset # define __deflateEnd deflateEnd -# define __inflateInit2 inflateInit2_ # define __inflate inflate # define __inflateReset inflateReset # define __inflateEnd inflateEnd @@ -551,9 +558,14 @@ bool zlib_stream::zip_begin(zlib_level_t level /* = zlib_default */, is_compress_ = true; // int ret = __deflateInit(zstream_, level, ZLIB_VERSION, sizeof(z_stream)); +#ifdef COSMOCC + int ret = __deflateInit2(zstream_, level, Z_DEFLATED, + wbits, mlevel, Z_DEFAULT_STRATEGY); +#else int ret = __deflateInit2(zstream_, level, Z_DEFLATED, wbits, mlevel, Z_DEFAULT_STRATEGY, ZLIB_VERSION, (int) sizeof(z_stream)); +#endif if (ret != Z_OK) { logger_error("deflateInit error"); return false; @@ -639,8 +651,12 @@ bool zlib_stream::unzip_begin(bool have_zlib_header /* = true */, finished_ = false; is_compress_ = false; +#ifdef COSMOCC + int ret = __inflateInit2(zstream_, have_zlib_header ? wsize : -wsize); +#else int ret = __inflateInit2(zstream_, have_zlib_header ? wsize : -wsize, ZLIB_VERSION, sizeof(z_stream)); +#endif if (ret != Z_OK) { logger_error("inflateInit error"); return (false); diff --git a/lib_fiber/c/Makefile b/lib_fiber/c/Makefile index d7bc9587f..8f4651e9a 100644 --- a/lib_fiber/c/Makefile +++ b/lib_fiber/c/Makefile @@ -90,6 +90,14 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmocc, $(CC)), cosmocc) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + AR = cosmoar + +endif + ifeq ($(findstring arm64, $(UNIXNAME)), arm64) JMP_CTX = USE_BOOST_JMP endif @@ -222,6 +230,7 @@ SRC = $(wildcard $(SRC_PATH_SRC)/*.c) \ $(wildcard $(SRC_PATH_SRC)/sync/*.c) #ASM_OBJ=$(OBJ_PATH_DST)/setjmp.o $(OBJ_PATH_DST)/make_gas.o $(OBJ_PATH_DST)/jump_gas.o + ASM_OBJ=$(OBJ_PATH_DST)/make_gas.o $(OBJ_PATH_DST)/jump_gas.o OBJ = $(patsubst %.c, $(OBJ_PATH_DST)/%.o, $(notdir $(SRC))) $(ASM_OBJ) diff --git a/lib_fiber/c/src/common/pthread_patch.c b/lib_fiber/c/src/common/pthread_patch.c index 117dbe383..ba3650e16 100644 --- a/lib_fiber/c/src/common/pthread_patch.c +++ b/lib_fiber/c/src/common/pthread_patch.c @@ -326,7 +326,7 @@ long thread_self(void) return (long) GetCurrentThreadId(); } -#elif defined(__linux__) +#elif defined(__linux__) || defined(COSMOCC) #include diff --git a/lib_fiber/c/src/define.h b/lib_fiber/c/src/define.h index 1065d11b4..55747e2e1 100644 --- a/lib_fiber/c/src/define.h +++ b/lib_fiber/c/src/define.h @@ -1,7 +1,7 @@ #ifndef __DEFINE_INCLUDE_H__ #define __DEFINE_INCLUDE_H__ -#if defined(__linux__) +#if defined(__linux__) || defined(LINUX2) # define LINUX # define SYS_UNIX # define HAS_SELECT diff --git a/lib_fiber/c/src/dns/rfc1035.c b/lib_fiber/c/src/dns/rfc1035.c index 1dcbdfb14..fbe9d5e2f 100644 --- a/lib_fiber/c/src/dns/rfc1035.c +++ b/lib_fiber/c/src/dns/rfc1035.c @@ -1018,11 +1018,13 @@ static size_t save_addr2rr(int type, const char *src, RFC1035_RR *rr) in6.sin6_port = htons(0); #if defined(SYS_UNIX) || (defined(SYS_WIN) && _MSC_VER >= 1600) +# if !defined(COSMOCC) if (ptr && *ptr && !(in6.sin6_scope_id = if_nametoindex(ptr))) { msg_error("%s(%d): if_nametoindex error %s", __FUNCTION__, __LINE__, last_serror()); return 0; } +# endif #endif if (inet_pton(AF_INET6, buf, &in6.sin6_addr) == 0) { msg_error("%s(%d): invalid addr=%s", diff --git a/lib_fiber/c/src/event.h b/lib_fiber/c/src/event.h index eaa4e95a3..62825c144 100644 --- a/lib_fiber/c/src/event.h +++ b/lib_fiber/c/src/event.h @@ -6,7 +6,12 @@ #include "common/timer_cache.h" #ifdef HAS_EPOLL -#include +# ifdef COSMOCC +# include +# include +# else +# include +# endif #endif #ifdef HAS_IO_URING diff --git a/lib_fiber/c/src/event/event_epoll.c b/lib_fiber/c/src/event/event_epoll.c index 8034b9d08..65b275179 100644 --- a/lib_fiber/c/src/event/event_epoll.c +++ b/lib_fiber/c/src/event/event_epoll.c @@ -7,7 +7,14 @@ #define __USE_GNU #endif #include -#include + +#ifdef COSMOCC +# include +# include +#else +# include +#endif + #include "event.h" #include "event_epoll.h" diff --git a/lib_fiber/c/src/hook/getaddrinfo.c b/lib_fiber/c/src/hook/getaddrinfo.c index 77a6d3375..d2393b541 100644 --- a/lib_fiber/c/src/hook/getaddrinfo.c +++ b/lib_fiber/c/src/hook/getaddrinfo.c @@ -41,11 +41,13 @@ static struct addrinfo *create_addrinfo(const char *ip, short port, sa.in6.sin6_family = AF_INET6; sa.in6.sin6_port = htons(port); +#ifndef COSMOCC if (ptr && *ptr) { if (!(in6->sin6_scope_id = if_nametoindex(ptr))) { return NULL; } } +#endif if (inet_pton(AF_INET6, buf, &sa.in6.sin6_addr) <= 0) { return NULL; } diff --git a/lib_fiber/c/src/hook/hook.h b/lib_fiber/c/src/hook/hook.h index 2e3988660..09f7c7e2c 100644 --- a/lib_fiber/c/src/hook/hook.h +++ b/lib_fiber/c/src/hook/hook.h @@ -3,6 +3,13 @@ #include "fiber/fiber_define.h" +#ifdef COSMOCC +# include +# include +#elif defined(__linux__) || defined(LINUX2) +# include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -35,7 +42,7 @@ typedef int (WSAAPI *WSARecv_fn)(socket_t, LPWSABUF, DWORD, LPDWORD, LPDWORD, typedef socket_t (WSAAPI *WSAAccept_fn)(SOCKET, struct sockaddr FAR *, LPINT, LPCONDITIONPROC, DWORD_PTR); -#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) +#elif defined(__linux__) || defined(LINUX2) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) typedef int (*fcntl_fn)(int, int, ...); typedef int (*setsockopt_fn)(socket_t, int, int, const void *, socklen_t); @@ -143,7 +150,7 @@ extern gethostbyname_fn *sys_gethostbyname; extern WSARecv_fn *sys_WSARecv; extern WSAAccept_fn *sys_WSAAccept; -#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) // SYS_UNIX +#elif defined(__linux__) || defined(LINUX2) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) // SYS_UNIX extern fcntl_fn *sys_fcntl; extern setsockopt_fn *sys_setsockopt; @@ -197,7 +204,7 @@ extern pwrite_fn *sys_pwrite; // in hook.c void hook_once(void); -#if defined(__linux__) +#if defined(__linux__) || defined(LINUX2) // in epoll.c int epoll_try_register(int epfd); int epoll_close(int epfd); diff --git a/lib_fiber/c/src/hook/io.h b/lib_fiber/c/src/hook/io.h index 62f262301..9e0001200 100644 --- a/lib_fiber/c/src/hook/io.h +++ b/lib_fiber/c/src/hook/io.h @@ -20,7 +20,7 @@ ssize_t fiber_sendto(FILE_EVENT *fe, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); ssize_t fiber_sendmsg(FILE_EVENT *fe, const struct msghdr *msg, int flags); -#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) // SYS_UNIX +#if defined(__linux__) || defined(LINUX2) || defined(__APPLE__) || defined(__FreeBSD__) || defined(MINGW) // SYS_UNIX // in fiber_read.c int fiber_iocp_read(FILE_EVENT *fe, char *buf, int len); diff --git a/lib_fiber/c/src/stdafx.h b/lib_fiber/c/src/stdafx.h index 5a2a34efd..2cf55b67f 100644 --- a/lib_fiber/c/src/stdafx.h +++ b/lib_fiber/c/src/stdafx.h @@ -62,9 +62,14 @@ #define GETPID _getpid #endif -#if defined(__linux__) +#if defined(__linux__) || defined(LINUX2) # include -# include +# ifdef COSMOCC +# include +# include +# else +# include +# endif /* # if !defined(__aarch64__) && !defined(__arm__) diff --git a/lib_fiber/samples/Makefile.in b/lib_fiber/samples/Makefile.in index a677ce337..0ea261660 100644 --- a/lib_fiber/samples/Makefile.in +++ b/lib_fiber/samples/Makefile.in @@ -56,6 +56,13 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmocc, $(CC)), cosmocc) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) CFLAGS += -DFREEBSD diff --git a/lib_protocol/Makefile b/lib_protocol/Makefile index ef1e261c7..d586446e8 100644 --- a/lib_protocol/Makefile +++ b/lib_protocol/Makefile @@ -62,6 +62,14 @@ ifeq ($(findstring clang++, $(CC)), clang++) -Wno-invalid-offsetof endif +ifeq ($(findstring cosmocc, $(CC)), cosmocc) + CFLAGS += -DCOSMOCC + CFLAGS += -Wno-implicit-fallthrough + CFLAGS += -DLINUX2 + AR = cosmoar + +endif + # For FreeBSD ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) UNIXTYPE = FREEBSD