Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
README.md
.vscode

# Prerequisites
*.d
Expand Down Expand Up @@ -32,3 +33,5 @@ README.md
*.exe
*.out
*.app
.claude/
.opencode/
55 changes: 14 additions & 41 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,21 @@ set(CMAKE_MACOSX_RPATH 0)
set(LIBCO_VERSION 0.5)

# Set cflags
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} -g -fno-strict-aliasing -O2 -Wall -export-dynamic -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated -m64)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fno-strict-aliasing -O2 -Wall -export-dynamic -Wall -pipe -D_GNU_SOURCE -D_REENTRANT -fPIC -Wno-deprecated")

if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
message(STATUS "x86_64 architecture detected")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64 -mtune=generic")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64")
message(STATUS "ARM64 architecture detected")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a -mtune=generic")
else()
message(STATUS "Unknown architecture: ${CMAKE_SYSTEM_PROCESSOR}")
endif()
# Use c and asm
enable_language(C ASM)

# Add source files
set(SOURCE_FILES
co_epoll.cpp
co_hook_sys_call.cpp
co_routine.cpp
coctx.cpp
coctx_swap.S)

# Add static and shared library target
add_library(colib_static STATIC ${SOURCE_FILES})
add_library(colib_shared SHARED ${SOURCE_FILES})

# Set library output name
set_target_properties(colib_static PROPERTIES OUTPUT_NAME colib)
set_target_properties(colib_shared PROPERTIES OUTPUT_NAME colib)

set_target_properties(colib_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(colib_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# Set shared library version, will generate libcolib.${LIBCO_VERSION}.so and a symbol link named libcolib.so
# For mac osx, the extension name will be .dylib
set_target_properties(colib_shared PROPERTIES VERSION ${LIBCO_VERSION} SOVERSION ${LIBCO_VERSION})



# Macro for add example target
macro(add_example_target EXAMPLE_TARGET)
add_executable("example_${EXAMPLE_TARGET}" "example_${EXAMPLE_TARGET}.cpp")
target_link_libraries("example_${EXAMPLE_TARGET}" colib_static pthread dl)
endmacro(add_example_target)
set(LIBCO_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")

add_example_target(closure)
add_example_target(cond)
add_example_target(copystack)
add_example_target(echocli)
add_example_target(echosvr)
add_example_target(poll)
add_example_target(setenv)
add_example_target(specific)
add_example_target(thread)
add_subdirectory(lib)
add_subdirectory(examples)
19 changes: 19 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cmake_minimum_required(VERSION 2.8)

macro(add_example_target EXAMPLE_TARGET)
get_filename_component(EXAMPLE_NAME ${EXAMPLE_TARGET} NAME)
if(${EXAMPLE_TARGET} MATCHES "\\.cpp$")
string(REPLACE ".cpp" "" EXAMPLE_NAME ${EXAMPLE_NAME})
endif()

add_executable(${EXAMPLE_NAME} ${EXAMPLE_TARGET})
target_include_directories(${EXAMPLE_NAME} PUBLIC ${LIBCO_INCLUDE_DIR})
target_link_libraries(${EXAMPLE_NAME} PUBLIC colib_static pthread dl)

add_test(NAME ${EXAMPLE_NAME} COMMAND ${EXAMPLE_NAME})
endmacro()

file(GLOB_RECURSE EXAMPLE_TARGETS "example_*")
foreach(EXAMPLE_TARGET ${EXAMPLE_TARGETS})
add_example_target(${EXAMPLE_TARGET})
endforeach()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 2.8)

# Use c and asm
enable_language(C ASM)

# Add source files
set(SOURCE_FILES
co_epoll.cpp
co_hook_sys_call.cpp
co_routine.cpp
coctx.cpp
co_comm.cpp
co_memcpy.c
coctx_swap.S)

# Add static and shared library target
add_library(colib_static STATIC ${SOURCE_FILES})
add_library(colib_shared SHARED ${SOURCE_FILES})

target_compile_definitions(colib_static PRIVATE _GNU_SOURCE _REENTRANT)
target_compile_definitions(colib_shared PRIVATE _GNU_SOURCE _REENTRANT)

# Set library output name
set_target_properties(colib_static PROPERTIES OUTPUT_NAME colib)
set_target_properties(colib_shared PROPERTIES OUTPUT_NAME colib)

set_target_properties(colib_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(colib_shared PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# Set shared library version, will generate libcolib.${LIBCO_VERSION}.so and a symbol link named libcolib.so
# For mac osx, the extension name will be .dylib
set_target_properties(colib_shared PROPERTIES VERSION ${LIBCO_VERSION} SOVERSION ${LIBCO_VERSION})
9 changes: 8 additions & 1 deletion co_closure.h → lib/co_closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

#ifndef __CO_CLOSURE_H__
#define __CO_CLOSURE_H__

#ifdef __cplusplus
extern "C" {
#endif

struct stCoClosure_t
{
public:
Expand Down Expand Up @@ -92,6 +97,8 @@ public:\

#define co_func_end }


#ifdef __cplusplus
}
#endif

#endif
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 1 addition & 10 deletions co_hook_sys_call.cpp → lib/co_hook_sys_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ typedef int (*unsetenv_pfn_t)(const char *name);
typedef char *(*getenv_pfn_t)(const char *name);
typedef hostent* (*gethostbyname_pfn_t)(const char *name);
typedef res_state (*__res_state_pfn_t)();
typedef int (*__poll_pfn_t)(struct pollfd fds[], nfds_t nfds, int timeout);
typedef int (*gethostbyname_r_pfn_t)(const char* __restrict name, struct hostent* __restrict __result_buf, char* __restrict __buf, size_t __buflen, struct hostent** __restrict __result, int* __restrict __h_errnop);

static socket_pfn_t g_sys_socket_func = (socket_pfn_t)dlsym(RTLD_NEXT,"socket");
Expand Down Expand Up @@ -128,9 +127,6 @@ static __res_state_pfn_t g_sys___res_state_func = (__res_state_pfn_t)dlsym(RTLD
static gethostbyname_pfn_t g_sys_gethostbyname_func = (gethostbyname_pfn_t)dlsym(RTLD_NEXT, "gethostbyname");
static gethostbyname_r_pfn_t g_sys_gethostbyname_r_func = (gethostbyname_r_pfn_t)dlsym(RTLD_NEXT, "gethostbyname_r");

static __poll_pfn_t g_sys___poll_func = (__poll_pfn_t)dlsym(RTLD_NEXT, "__poll");


/*
static pthread_getspecific_pfn_t g_sys_pthread_getspecific_func
= (pthread_getspecific_pfn_t)dlsym(RTLD_NEXT,"pthread_getspecific");
Expand Down Expand Up @@ -970,10 +966,6 @@ extern "C"

return &(__co_state_wrap->state);
}
int __poll(struct pollfd fds[], nfds_t nfds, int timeout)
{
return poll(fds, nfds, timeout);
}
}

struct hostbuf_wrap
Expand Down Expand Up @@ -1035,5 +1027,4 @@ void co_enable_hook_sys() //这函数必须在这里,否则本文件会被忽略
{
co->cEnableSysHook = 1;
}
}

}
138 changes: 138 additions & 0 deletions lib/co_memcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "co_memcpy.h"
#include <string.h>
#include "memcpy_def.h"
#include "memcpy_neon_impl.h"

#define SIZE_128B 128
#define SIZE_1KB 1024
#define SIZE_2KB 2048
#define SIZE_4KB 4096
#define SIZE_8KB 8192
#define SIZE_12KB 12288

static void *co_memcpy_save_aarch64(void *restrict dst, const void *restrict src, unsigned long len)
{
void *dst_ret = dst;
#ifdef __aarch64__
if (len < SIZE_128B) {
memcpy_rem(dst, src, len);
return dst_ret;
}

unsigned long align_offset = (unsigned long)dst & 0x3f;
if (align_offset) {
memcpy_rem(dst, src, align_offset);
dst += align_offset;
src += align_offset;
len -= align_offset;
}

int len_count = len / BLOCK_SIZE;
int rem = len % BLOCK_SIZE;
if (len <= SIZE_1KB) {
memcpy_blk(dst, src, len_count, memcpy_blk_save);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_2KB) {
memcpy_256B_save(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_4KB) {
memcpy_384B_save(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_8KB) {
memcpy_512B_save(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_12KB) {
memcpy_1024B_save(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else {
memcpy_1536B_save(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
}
#endif
return dst_ret;
}

static void *co_memcpy_swap_aarch64(void *restrict dst, const void *restrict src, unsigned long len)
{
void *dst_ret = dst;
#ifdef __aarch64__
if (len < SIZE_128B) {
memcpy_rem(dst, src, len);
return dst_ret;
}

unsigned long align_offset = (unsigned long)dst & 0x3f;
if (align_offset) {
memcpy_rem(dst, src, align_offset);
dst += align_offset;
src += align_offset;
len -= align_offset;
}

int len_count = len / BLOCK_SIZE;
int rem = len % BLOCK_SIZE;
if (len <= SIZE_1KB) {
memcpy_blk(dst, src, len_count, memcpy_blk_swap);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_2KB) {
memcpy_384B_swap(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_8KB) {
memcpy_512B_swap(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else if (len <= SIZE_12KB) {
memcpy_1024B_swap(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
} else {
memcpy_1536B_swap(dst, src, len_count, rem);
if (rem) {
memcpy_rem(dst, src, rem);
}
}
#endif
return dst_ret;
}

typedef void *(*memcpy_func_name)(void *restrict dst, const void *restrict src, unsigned long len);

static memcpy_func_name memcpy_save_func = (memcpy_func_name)memcpy;
static memcpy_func_name memcpy_swap_func = (memcpy_func_name)memcpy;

static void __attribute__((constructor)) model_init(void)
{
#ifdef __aarch64__
memcpy_save_func = (memcpy_func_name)co_memcpy_save_aarch64;
memcpy_swap_func = (memcpy_func_name)co_memcpy_swap_aarch64;
#endif
return;
}

void *co_memcpy_save(void *restrict dst, const void *restrict src, unsigned long len)
{
return memcpy_save_func(dst, src, len);
}

void *co_memcpy_swap(void *restrict dst, const void *restrict src, unsigned long len)
{
return memcpy_swap_func(dst, src, len);
}
15 changes: 15 additions & 0 deletions lib/co_memcpy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CO_MEMCPY_H
#define CO_MEMCPY_H

#ifdef __cplusplus
extern "C" {
#endif

void *co_memcpy_save(void *__restrict dst, const void *__restrict src, unsigned long len);
void *co_memcpy_swap(void *__restrict dst, const void *__restrict src, unsigned long len);

#ifdef __cplusplus
}
#endif

#endif /* CO_MEMCPY_H */
Loading