diff --git a/README.md b/README.md index 16d43032..2f077434 100644 --- a/README.md +++ b/README.md @@ -66,31 +66,18 @@ Melon is a generic cross-platform C library. It contains many algorithms, data s ### Platform Support -Melon was originally written for UNIX systems, so it is suitable for UNIX-like systems such as Linux and MacOS. And there are a few optimizations for Intel CPUs. +- Linux -At present, Melon has also completed the preliminary porting to Windows, so it can be used on Windows. However, because Windows differs greatly from the UNIX system in the creation process, some functions of the above `framework` are temporarily not supported in Windows. +- MacOSX +- Windows + - `msys2` (Fully supported) + - `msvc` (Partially supported) -### Installation -On Windows, please install `mingw` or `msys2`. -If you install `mingw`, you may want to install `git bash` and `make` either. Install [MingW-W64-builds](https://www.mingw-w64.org/downloads/#mingw-builds) with the installation settings: - -- `Version`: `8.1.0` - -- `Architecture`: `i686` - -- `Threads`: `posix` - -- `Exception`: `dwarf` - -- `Build revision`: `0` - -If you install `msys2`, you can use `pacman` to install any tool softwares you may need. - -Then execute these shell commands on git bash or terminal (on UNIX). +### Quick Installation ``` git clone https://github.com/Water-Melon/Melon.git @@ -100,7 +87,7 @@ make make install ``` -For more `configure` options, please refer to [Official Document: Installation](http://doc.melonc.io/en/install.html). +For installation on other operating systems or for more configure options, please refer to [Official Document: Installation](http://doc.melonc.io/en/install.html). diff --git a/build.bat b/build.bat new file mode 100644 index 00000000..1d601a55 --- /dev/null +++ b/build.bat @@ -0,0 +1,74 @@ +@echo off + +set "HOME=%HOMEDRIVE%%HOMEPATH%" + +echo Installation Path: [%HOME%/libmelon] +echo Generating files and directories ... + +setlocal enabledelayedexpansion + +set "old=MLN_ROOT" +set "new=!%HOME%!\libmelon" +set "new=%new:\=\\%" + +set "file=src/mln_path.c" + +powershell -Command "(Get-Content '%file%') -replace '%old%', '\"%new%\"' | Set-Content '%file%'" + + +set "old=MLN_NULL" +set "new=!%HOME%!\libmelon\null" +set "new=%new:\=\\%" + +set "file=src/mln_path.c" + +powershell -Command "(Get-Content '%file%') -replace '%old%', '\"%new%\"' | Set-Content '%file%'" + + +set "old=MLN_LANG_LIB" +set "new=!%HOME%!\libmelon\lang\lib" +set "new=%new:\=\\%" + +set "file=src/mln_path.c" + +powershell -Command "(Get-Content '%file%') -replace '%old%', '\"%new%\"' | Set-Content '%file%'" + + +set "old=MLN_LANG_DYLIB" +set "new=!%HOME%!\libmelon\lang\dylib" +set "new=%new:\=\\%" + +set "file=src/mln_path.c" + +powershell -Command "(Get-Content '%file%') -replace '%old%', '\"%new%\"' | Set-Content '%file%'" + + +set "old={{ROOT}}" +set "new=!%HOME%!\libmelon" +set "new=%new:\=\\%" + +set "file=conf/melon.conf.msvc.template" + +powershell -Command "(Get-Content '%file%') -replace '%old%', '%new%' | Set-Content 'conf/melon.conf'" + +endlocal + + +mkdir lib objs\src "%HOME%\libmelon\logs" "%HOME%\libmelon\lang\lib" "%HOME%\libmelon\lang\dylib" "%HOME%\libmelon\include" "%HOME%\libmelon\conf" "%HOME%\libmelon\lib" "%HOME%\libmelon\tmp" +echo "" > "%HOME%\libmelon\null" + +for %%f in (src\*.c) do ( + cl /c /DMSVC /I include "%%f" /Fo:objs\%%f.o /O2 +) + +lib /OUT:lib\libmelon_static.lib objs\src\*.o + +setlocal + +xcopy include "%HOME%/libmelon/include" /E /I /Y +xcopy lib "%HOME%/libmelon/lib" /E /I /Y +xcopy conf "%HOME%/libmelon/conf" /E /I /Y + +endlocal + +echo Done diff --git a/conf/melon.conf.msvc.template b/conf/melon.conf.msvc.template new file mode 100644 index 00000000..4546f7e4 --- /dev/null +++ b/conf/melon.conf.msvc.template @@ -0,0 +1,35 @@ +log_level "none"; +//user "root"; +daemon off; +core_file_size "unlimited"; +//max_nofile 1024; +worker_proc 1; +framework off; +log_path "{{ROOT}}\\logs\\melon.log"; +/* + * Configurations in the 'proc_exec' are the + * processes which are customized by user. + * + * Here is an example to show you how to + * spawn a program. + * keepalive "/tmp/a.out" ["arg1" "arg2" ...] + * The command in this example is 'keepalive' that + * indicate master process to supervise this + * process. If process is killed, master process + * would restart this program. + * If you don't want master to restart it, you can + * default "/tmp/a.out" ["arg1" "arg2" ...] + * + * But you should know that there is another + * arugment after the last argument you write here. + * That is the file descriptor which is used to + * communicate with master process. + */ +proc_exec { + // keepalive "/tmp/a"; +} +thread_exec { +// restart "hello" "hello" "world"; +// default "haha"; +} +//trace_mode "trace/trace.m"; /* path or off */ diff --git a/configure b/configure index 00e23bbc..1575d03f 100755 --- a/configure +++ b/configure @@ -63,6 +63,9 @@ mingw_cflags="" # constructor constructor_flag="" +# msys2 +msys2_flag="" + #Functions set_melang_default_paths() { if ! case $sysname in MINGW*) false;; esac; then @@ -80,6 +83,7 @@ set_c_compilers() { install_path=$HOME/libmelon cc="gcc" tcc="gcc" + msys2_flag="-DMSYS2" else install_path=`echo "/usr/local/melon"` cc="cc" @@ -400,7 +404,7 @@ generate_Makefile() { if [ $wasm -eq 1 ]; then echo -e "FLAGS\t\t= -Iinclude -c $debug $olevel $llvm_flag -s -mmutable-globals -mnontrapping-fptoint -msign-ext -Wemcc -DMLN_ROOT=\\\"$realpath\\\" -DMLN_NULL=\\\"$nullpath\\\" -DMLN_LANG_LIB=\\\"$melang_script_path\\\" -DMLN_LANG_DYLIB=\\\"$melang_dylib_path\\\" $CFLAGS" >> Makefile else - echo -e "FLAGS\t\t= -Iinclude -c -Wall $debug -Werror $mingw_cflags $olevel -fPIC -DMLN_ROOT=\\\"$realpath\\\" -DMLN_NULL=\\\"$nullpath\\\" -DMLN_LANG_LIB=\\\"$melang_script_path\\\" -DMLN_LANG_DYLIB=\\\"$melang_dylib_path\\\" $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag $func_flag $c99_flag $constructor_flag $CFLAGS" >> Makefile + echo -e "FLAGS\t\t= -Iinclude -c -Wall $debug -Werror $mingw_cflags $olevel -fPIC -DMLN_ROOT=\\\"$realpath\\\" -DMLN_NULL=\\\"$nullpath\\\" -DMLN_LANG_LIB=\\\"$melang_script_path\\\" -DMLN_LANG_DYLIB=\\\"$melang_dylib_path\\\" $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag $func_flag $c99_flag $constructor_flag $msys2_flag $CFLAGS" >> Makefile fi if ! case $sysname in MINGW*) false;; esac; then if [ $wasm -eq 0 ]; then diff --git a/docs/Melon Developer Guide.txt b/docs/Melon Developer Guide.txt index d167e0f5..89dc8972 100644 --- a/docs/Melon Developer Guide.txt +++ b/docs/Melon Developer Guide.txt @@ -3430,7 +3430,7 @@ Their definitions can be found in melon/include/mln_types.h. return -1; } memset(&msg, 0, sizeof(msg)); - #if defined(____WIN32____) + #if defined(MSVC) int n = recv(fd, (char *)&msg, sizeof(msg), 0); #else int n = recv(fd, &msg, sizeof(msg), 0); @@ -3464,7 +3464,7 @@ Their definitions can be found in melon/include/mln_types.h. msg.c = 'N'; msg.type = ITC_REQUEST; msg.need_clear = 1; - #if defined(____WIN32____) + #if defined(MSVC) int n = send(fd, (char *)&msg, sizeof(msg), 0); #else int n = send(fd, &msg, sizeof(msg), 0); diff --git a/docs/book/cn/array.md b/docs/book/cn/array.md index c1f5ec64..fe22141b 100644 --- a/docs/book/cn/array.md +++ b/docs/book/cn/array.md @@ -2,12 +2,6 @@ -### 视频介绍 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/class.md b/docs/book/cn/class.md index f726ad97..cf198ff2 100644 --- a/docs/book/cn/class.md +++ b/docs/book/cn/class.md @@ -2,6 +2,8 @@ 类模板用于提供一种类似面向对象编程的编程体验。模块提供了若干宏来实现面向对象的编程风格。 +本模块在MSVC环境中暂不支持。 + ### 头文件 @@ -104,4 +106,4 @@ int main(void) } ``` -注意,这里函数指针、构造函数和析构函数的声明必须放在宏`class`前。而所有函数定义必须放在`class`之后。 \ No newline at end of file +注意,这里函数指针、构造函数和析构函数的声明必须放在宏`class`前。而所有函数定义必须放在`class`之后。 diff --git a/docs/book/cn/conf.md b/docs/book/cn/conf.md index 25a2d618..66124b15 100644 --- a/docs/book/cn/conf.md +++ b/docs/book/cn/conf.md @@ -2,11 +2,6 @@ -### 视频介绍 - - - - ### 说明 这里仅给出开发者所需要的结构定义和函数声明。 diff --git a/docs/book/cn/cron.md b/docs/book/cn/cron.md index 59bda61e..d05eac35 100644 --- a/docs/book/cn/cron.md +++ b/docs/book/cn/cron.md @@ -2,11 +2,6 @@ -### 视频介绍 - - - - ### 头文件 diff --git a/docs/book/cn/error.md b/docs/book/cn/error.md index 0aeb59f3..d46d707d 100644 --- a/docs/book/cn/error.md +++ b/docs/book/cn/error.md @@ -13,11 +13,6 @@ -### 视频讲解 - - - - ### 头文件 @@ -63,7 +58,7 @@ RET(code) 描述:根据给定的错误码`code`生成返回值。如果`code`为0,则表示无错。若`code`小于0则是不合法的错误值。若`code`大于0,则表示一个合法错误码。如果再此之前使用了`mln_error_callback_set`设置了回调函数,那么在返回值拼装完成后,回调函数会被调用。 -返回值:0或一个负值,0表示无错误,负值表示出错 +返回值:0或一个负值,0表示无错误,负值表示出错。`msvc`环境下,返回值将直接赋值给参数`code`,而不是像函数一样返回。 diff --git a/docs/book/cn/event.md b/docs/book/cn/event.md index 4f8be838..a70c0238 100644 --- a/docs/book/cn/event.md +++ b/docs/book/cn/event.md @@ -6,12 +6,7 @@ - kqueue - select - - -### 视频介绍 - - - +`msvc`环境中,本模块不是线程安全的。 ### 头文件 diff --git a/docs/book/cn/fheap.md b/docs/book/cn/fheap.md index 1767f2a0..8ff1c818 100644 --- a/docs/book/cn/fheap.md +++ b/docs/book/cn/fheap.md @@ -5,7 +5,7 @@ Melon中实现的是**最小堆**。 与红黑树组件类似,斐波那契堆组件有三种用法: - 基本用法 -- 内联用法 +- 内联用法 (`msvc`中不支持) - 容器用法 diff --git a/docs/book/cn/framework_init.md b/docs/book/cn/framework_init.md index 25dfffb9..2f7abaae 100644 --- a/docs/book/cn/framework_init.md +++ b/docs/book/cn/framework_init.md @@ -32,6 +32,8 @@ `master_process`/`worker_process`是在多进程模型下,在主/工作进程中被调用的,用于针对单个进程进行一些全局设置。 +`msvc`中,不包含`main_thread`,`master_process`和`worker_process`。初始化流程仅对配置和日志等初始化后返回。 + ### 头文件 @@ -55,9 +57,11 @@ struct mln_framework_attr { int argc; //一般为main的argc char **argv; //一般为main的argv mln_framework_init_t global_init; //初始化回调函数,一般用于初始化全局变量,该回调会在配置加载完成后被调用 +#if !defined(MSVC) mln_framework_process_t main_thread; //主线程处理函数,我们将在多线程框架部分深入 mln_framework_process_t master_process; //主进程处理函数,我们将在多进程框架部分深入 mln_framework_process_t worker_process; //工作进程处理函数,我们将在多进程框架部分深入 +#endif }; typedef int (*mln_framework_init_t)(void); diff --git a/docs/book/cn/index.md b/docs/book/cn/index.md index cb7136bb..b90bb5cc 100644 --- a/docs/book/cn/index.md +++ b/docs/book/cn/index.md @@ -70,9 +70,25 @@ Melon当前提供了如下功能: ### 平台支持 -Melon最初是为UNIX系统编写,因此适用于Linux、MacOS等类UNIX系统,并在针对Intel CPU有少量优化。 +- Linux -目前Melon也已经完成了向Windows的初步移植,因此可以在Windows上进行使用。但由于Windows在创建进程上与UNIX系统差异较大,因此导致上述`框架`部分功能在Windows中暂时不支持。 +- MacOSX + +- Windows + - `msys2` (完全支持) + - `msvc` (部分支持,详情参考下一小节的安装文档) + + +### 快速安装 +``` +git clone https://github.com/Water-Melon/Melon.git +cd Melon +./configure [--prefix=LIB_INSTALL_PATH | ...] +make +make install +``` + +关于如何在其他平台安装以及configure的更多选项,请参考[安装](http://doc.melonc.io/cn/install.html)章节内容。 diff --git a/docs/book/cn/install.md b/docs/book/cn/install.md index 67e9c174..2b2ce8bd 100644 --- a/docs/book/cn/install.md +++ b/docs/book/cn/install.md @@ -1,33 +1,10 @@ ## 安装 -### 视频介绍 - +### UNIX - - -Windows与UNIX环境的安装并无差异,仅需要先行安装并配置`mingw`或`msys2`。 - -如果你安装`mingw`, 那你也许也会想要安装`git bash`以及`make`,后续步骤与UNIX的完全一致。 - -安装[MingW-W64-builds](https://www.mingw-w64.org/downloads/#mingw-builds)时请选择如下设置: - -- `Version`: `8.1.0` - -- `Architecture`: `i686` - -- `Threads`: `posix` - -- `Exception`: `dwarf` - -- `Build revision`: `0` - -如果你安装的是`msys2`,那么你可以使用`pacman`命令来安装一些你可能需要的工具,例如`git`,`make`等。 - - - -UNIX环境下Melon的安装执行如下命令: +UNIX环境(Linux和MacOSX)下Melon的安装执行如下命令: ```bash $ git clone https://github.com/Water-Melon/Melon.git @@ -57,7 +34,7 @@ $ sudo make install -Melon会同时生成动态库与静态库。对于Linux系统,在使用Melon的动态库时,需要将该库的路径加入到系统配置中: +Melon会同时生成动态库(`libmelon.so`)与静态库(`libmelon_static.a`)。对于Linux系统,在使用Melon的动态库时,需要将该库的路径加入到系统配置中: ```bash $ sudo echo "/usr/local/melon/lib/" >> /etc/ld.so.conf @@ -76,7 +53,73 @@ $ export LD_LIBRARY_PATH=/path/to/melon/libdir:$LD_LIBRARY_PATH -#### Docker +### Windows + +Windows平台支持两种安装方式: + +- `msys2` +- `msvc` + + + +#### msys2 + +`msys2`完全支持Melon的特性,因此也是Windows上的推荐安装方式。 + +##### 安装msys2及依赖项 + +`msys2`可以在 https://www.msys2.org/ 中进行下载和安装。随后选择`MSYS2 UCRT64`启动msys2命令行环境,并输入如下命令安装所需依赖: + +``` +pacman -S vim make gcc git +``` + +##### 安装Melon + +这一步与UNIX环境中的安装步骤就完全一样了。安装好后将得到`libmelon.dll`和`libmelon_static.lib`库。 + + + +#### msvc + +`msvc`目前仅支持部分模块功能,如下是暂不支持的模块: + +- `class` +- `iothread` +- `thread_pool` +- `自旋锁` +- `多进程框架` +- `多线程框架` + +要使用`msvc`,需要先行安装[Visual Studio C/C++ IDE](https://visualstudio.microsoft.com/vs/features/cplusplus/)。安装好后,可以使用跟随其一同安装好的Powershell启动MSVC命令行环境。 + +拉取Melon库并切换至Melon代码库后,执行 + +``` +build.bat +``` + +即可进行编译安装。安装好后,在`lib`子目录中将看到编译生成的静态库`libmelon_static.lib`。MSVC环境暂不支持生成动态库。 + +额外需要注意的一点是,MSVC编译出来的Melon库,硬编码了`path`模块内的路径,因此安装路径被强制放在`HOME/libmelon`中。 + +MSVC目前仅支持完全安装,暂不支持模块选择性编译。 + + + +### CMake + +目前Melon支持了使用CMake完全编译安装库,执行如下命令即可: + +```bash +mkdir build +cd build +cmake .. +``` + + + +### Docker 可以直接使用如下命令拉取已构建好的Melon环境,其中也包含了Melang脚本所使用到的系统库 diff --git a/docs/book/cn/iothread.md b/docs/book/cn/iothread.md index d38d89c9..c2ee77df 100644 --- a/docs/book/cn/iothread.md +++ b/docs/book/cn/iothread.md @@ -2,12 +2,9 @@ I/O线程算是一种另类线程池结构。但是这个组件主要用于图形界面类的应用。通常情况下,图形界面应用都会存在一个用户线程和一个I/O线程,这样当I/O处理时就不会无法响应用户的操作(如:点击)了。 +本模块在MSVC环境中暂不支持。 -### 视频介绍 - - - ### 头文件 diff --git a/docs/book/cn/ipc.md b/docs/book/cn/ipc.md index e1e6c54d..5724262f 100644 --- a/docs/book/cn/ipc.md +++ b/docs/book/cn/ipc.md @@ -1,5 +1,7 @@ ## IPC模块开发 +本模块在MSVC环境中暂不支持。 + ### 头文件 diff --git a/docs/book/cn/json.md b/docs/book/cn/json.md index 66d5d1fa..e3a6fd37 100644 --- a/docs/book/cn/json.md +++ b/docs/book/cn/json.md @@ -2,11 +2,6 @@ -### 视频介绍 - - - - ### 头文件 diff --git a/docs/book/cn/lex.md b/docs/book/cn/lex.md index adcb6460..4a381ed2 100644 --- a/docs/book/cn/lex.md +++ b/docs/book/cn/lex.md @@ -4,11 +4,6 @@ -### 视频介绍 - - - - ### 头文件 diff --git a/docs/book/cn/log.md b/docs/book/cn/log.md index 7048aca1..51c3466d 100644 --- a/docs/book/cn/log.md +++ b/docs/book/cn/log.md @@ -1,10 +1,7 @@ ## 日志 - -### 视频介绍 - - +日志模块在`msvc`环境中将不是多线程安全的。 diff --git a/docs/book/cn/mpool.md b/docs/book/cn/mpool.md index 3956d093..5e2a805c 100644 --- a/docs/book/cn/mpool.md +++ b/docs/book/cn/mpool.md @@ -9,12 +9,6 @@ Melon中,内存池分为两类: -### 视频介绍 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/multiprocess.md b/docs/book/cn/multiprocess.md index dff0af2d..40f78d87 100644 --- a/docs/book/cn/multiprocess.md +++ b/docs/book/cn/multiprocess.md @@ -2,19 +2,12 @@ -**注意**:Windows下目前不支持本功能。 +本功能在MSVC环境中暂不支持。 Melon开发之初便是要支持多进程模型的,这一点也主要源于Nginx以及以往用户态网络程序开发经历。因此,Melon中的多进程也延续了类似Nginx的异步事件模式。 -### 视频介绍 - - - - - - 下面我们来使用Melon来完成一个多进程例子。这个例子虽然简单,但用户会发现,事实上Melon并不干涉甚至是给予用户极大的自由发挥空间。 diff --git a/docs/book/cn/multithread.md b/docs/book/cn/multithread.md index 1085a45b..4b5bd032 100644 --- a/docs/book/cn/multithread.md +++ b/docs/book/cn/multithread.md @@ -2,7 +2,7 @@ -**注意**:Windows下目前不支持本功能。 +本功能在MSVC环境中暂不支持。 多线程框架与前面介绍的线程池不同,是一种模块化线程。模块化线程是指,每一个线程都是一个独立的代码模块,都有各自对应的入口函数(类似于每一个 C 语言程序有一个 main 函数一样)。 @@ -40,11 +40,7 @@ return -1; } memset(&msg, 0, sizeof(msg)); - #if defined(__WIN32__) - int n = recv(fd, (char *)&msg, sizeof(msg), 0); - #else int n = recv(fd, &msg, sizeof(msg), 0); - #endif if (n != sizeof(msg)) { mln_log(debug, "recv error. n=%d. %s\n", n, strerror(errno)); return -1; @@ -74,11 +70,7 @@ msg.c = 'N'; msg.type = ITC_REQUEST; msg.need_clear = 1; - #if defined(__WIN32__) - int n = send(fd, (char *)&msg, sizeof(msg), 0); - #else int n = send(fd, &msg, sizeof(msg), 0); - #endif if (n != sizeof(msg)) { mln_log(debug, "send error. n=%d. %s\n", n, strerror(errno)); mln_string_free(msg.dest); diff --git a/docs/book/cn/parser_generator.md b/docs/book/cn/parser_generator.md index 2bb2053a..dac9dec2 100644 --- a/docs/book/cn/parser_generator.md +++ b/docs/book/cn/parser_generator.md @@ -6,12 +6,6 @@ -### 视频介绍 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/quickstart.md b/docs/book/cn/quickstart.md index dfcbe1ca..d76263da 100644 --- a/docs/book/cn/quickstart.md +++ b/docs/book/cn/quickstart.md @@ -1,12 +1,6 @@ ## 快速入门 -### 视频介绍 - - - - - Melon的使用并不繁琐,大致步骤可分为: 1. 在使用前初始化库 diff --git a/docs/book/cn/rbtree.md b/docs/book/cn/rbtree.md index 66587d2e..ab7bd7d1 100644 --- a/docs/book/cn/rbtree.md +++ b/docs/book/cn/rbtree.md @@ -5,17 +5,11 @@ 在红黑树组件中,有两种使用模式: - 基础用法 -- 内联用法 +- 内联用法 (`msvc`不支持) - 容器用法 -### 视频讲解 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/regex.md b/docs/book/cn/regex.md index 2b1634a1..af7b4015 100644 --- a/docs/book/cn/regex.md +++ b/docs/book/cn/regex.md @@ -2,12 +2,6 @@ -### 视频介绍 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/span.md b/docs/book/cn/span.md index 539d3ac6..2937e959 100644 --- a/docs/book/cn/span.md +++ b/docs/book/cn/span.md @@ -10,12 +10,6 @@ Melon中的资源开销(span)组件是用来测量C语言函数开销的, -### 视频介绍 - - - - - ### 头文件 ```c diff --git a/docs/book/cn/spinlock.md b/docs/book/cn/spinlock.md index c2d28d20..172d09b9 100644 --- a/docs/book/cn/spinlock.md +++ b/docs/book/cn/spinlock.md @@ -2,6 +2,7 @@ Melon中的自旋锁会根据不同编译器和不同CPU架构选用不同的实现。 +本模块在MSVC环境中暂不支持。 ### 头文件 diff --git a/docs/book/cn/tcp_io.md b/docs/book/cn/tcp_io.md index 63f4edb3..20701c39 100644 --- a/docs/book/cn/tcp_io.md +++ b/docs/book/cn/tcp_io.md @@ -35,7 +35,7 @@ typedef struct mln_buf_s {//用于存放数据,且根据不同标识量指定 mln_off_t file_last;//数据在本文件内的结束偏移 mln_file_t *file;//文件结构,参见文件集合部分的介绍 mln_u32_t temporary:1;//start、pos等内存指针指向的内存是否是临时的(即不需要释放的) -#if !defined(__WIN32__) +#if !defined(MSVC) mln_u32_t mmap:1;//是否是mmap创建的内存,win下暂不支持 #endif mln_u32_t in_memory:1;//数据是否在内存中 diff --git a/docs/book/cn/threadpool.md b/docs/book/cn/threadpool.md index d4ab93ed..85b5864d 100644 --- a/docs/book/cn/threadpool.md +++ b/docs/book/cn/threadpool.md @@ -4,6 +4,8 @@ **注意**:在每个进程中仅允许存在一个线程池。 +本模块在MSVC环境中暂不支持。 + ### 头文件 diff --git a/docs/book/cn/trace.md b/docs/book/cn/trace.md index e4f87aa7..cd70e8a1 100644 --- a/docs/book/cn/trace.md +++ b/docs/book/cn/trace.md @@ -1,11 +1,6 @@ ## 跟踪模式 -### 视频介绍 - - - - 跟踪模式用于收集使用Melon库的程序信息。大致过程如下: @@ -49,9 +44,12 @@ ```c mln_trace(fmt, ...); + +//msvc中,原型为 +mln_trace(ret, fmt, ...); ``` -描述:发送若干数据给指定的处理脚本。这里的参数,与`mln_lang_ctx_pipe_send`函数([详见此章节](https://water-melon.github.io/Melon/cn/melang.html))的`fmt`及其可变参数的内容完全一致,因为本宏内部就是调用该函数完成的消息传递。 +描述:发送若干数据给指定的处理脚本。这里的参数,与`mln_lang_ctx_pipe_send`函数([详见此章节](https://water-melon.github.io/Melon/cn/melang.html))的`fmt`及其可变参数的内容完全一致,因为本宏内部就是调用该函数完成的消息传递。msvc中,返回值会被赋予给参数`ret`。 返回值: diff --git a/docs/book/en/class.md b/docs/book/en/class.md index 3dfa47d6..d2707a03 100644 --- a/docs/book/en/class.md +++ b/docs/book/en/class.md @@ -2,6 +2,7 @@ Class template is used to provide a programming experience similar to object-oriented programming. The module provides several macros to implement object-oriented programming style. +This module is not supported in the MSVC. ### Header file @@ -104,4 +105,4 @@ int main(void) } ``` -Note that function typedef, and constructor and destructor declarations must be given before the `class` macro. All function definitions must come after the `class` macro. \ No newline at end of file +Note that function typedef, and constructor and destructor declarations must be given before the `class` macro. All function definitions must come after the `class` macro. diff --git a/docs/book/en/error.md b/docs/book/en/error.md index 8733c049..a297fd26 100644 --- a/docs/book/en/error.md +++ b/docs/book/en/error.md @@ -58,7 +58,7 @@ RET(code) Description: Generate a return value based on the given error code `code`. No error if `code` is 0. An invalid error value if `code` is less than 0. If `code` is greater than 0, it indicates a valid error code. If mln_error_callback_set was used to set a callback function before this, the callback function will be invoked after the return value is composed. -Return value: 0 or a negative value, 0 means no error, negative value means error +Return value: 0 or a negative value, 0 means no error, negative value means error. In the MSVC environment, the return value will be directly assigned to the `code`. diff --git a/docs/book/en/event.md b/docs/book/en/event.md index 6a4c6392..83e87ac0 100644 --- a/docs/book/en/event.md +++ b/docs/book/en/event.md @@ -6,6 +6,7 @@ The system calls used by events vary according to different operating system pla - kqueue - select +This module is not thread-safe in the `MSVC` environment. ### Header file diff --git a/docs/book/en/fheap.md b/docs/book/en/fheap.md index 18eb028a..9183a05a 100644 --- a/docs/book/en/fheap.md +++ b/docs/book/en/fheap.md @@ -5,7 +5,7 @@ What is implemented in Melon is **minimum heap**. Similar to the red-black tree component, the Fibonacci heap component has three usages: - Basic Usage -- Inline Usage +- Inline Usage (not supported in `MSVC`) - Container Usage diff --git a/docs/book/en/framework_init.md b/docs/book/en/framework_init.md index 7f7328b5..5e43d036 100644 --- a/docs/book/en/framework_init.md +++ b/docs/book/en/framework_init.md @@ -32,6 +32,7 @@ The approximate calling sequence of these callback functions is as follows: `master_process`/`worker_process` is called in the master/worker process under the multi-process model, and is used to make some global settings for a single process. +In `MSVC`, there is no `main_thread`, `master_process`, or `worker_process`. The initialization process only returns after initializing configurations, logging, etc. ### Header file @@ -55,9 +56,11 @@ struct mln_framework_attr { int argc; //Usually the argc of main char **argv; //Usually the argv of main mln_framework_init_t global_init; //The initialization callback function is generally used to initialize global variables. The callback will be called after the configuration is loaded. +#if !defined(MSVC) mln_framework_process_t main_thread; //The main thread handler function, which we will dive into in the mutli-thread framework section mln_framework_process_t master_process; //The main process handler function, which we will dive into in the multi-process framework section mln_framework_process_t worker_process; //Worker process handlers, which we'll dive into in the multiprocessing framework section +#endif }; typedef int (*mln_framework_init_t)(void); diff --git a/docs/book/en/install.md b/docs/book/en/install.md index c9d99dd4..4547fd57 100644 --- a/docs/book/en/install.md +++ b/docs/book/en/install.md @@ -1,28 +1,10 @@ ## Installation - +### UNIX -On Windows, there is no difference between the installation of Windows and UNIX environments, you only need to install and configure `mingw` or `msys2`. - -If you install `mingw`, you may want to install `git bash` and `make` either. Install [MingW-W64-builds](https://www.mingw-w64.org/downloads/#mingw-builds) with the installation settings: - -- `Version`: `8.1.0` - -- `Architecture`: `i686` - -- `Threads`: `posix` - -- `Exception`: `dwarf` - -- `Build revision`: `0` - -If you install `msys2`, you can use `pacman` to install any tool softwares you may need (e.g. `make`, `git`). - - - -To install Melon on UNIX, execute the following commands: +To install Melon on UNIX environments (Linux and MacOSX), execute the following commands: ```bash $ git clone https://github.com/Water-Melon/Melon.git @@ -31,34 +13,34 @@ $ make $ sudo make install ``` -Shell script `configure` has the following parameters: +The `configure` script supports the following parameters: -- `--prefix` The installation path of Melon library -- `--melang-prefix` The installation path of the Melang script files that Melon used -- `--cc` Set the C compiler that used to compile Melon -- `--enable-wasm` Enable `webassembly` mode to generate webassembly format library -- `--debug` Enable `debug` mode. If omited the generated library will not contain symbol information and macro `__DEBUG__` -- `--func` Enable `func` mode. When enabled, the functions defined by `MLN_FUNC` and `MLN_FUNC_VOID` will enable entry and exit callbacks when called. -- `--olevel=[O|O1|O2|O3|...]` The level of compilation optimization, the default is `O3`. The optimization is disabled if no content after `=`. -- `--select=[all | module1,module2,...]` Selectively compile some modules. The default is `all` which means compiling all modules. Module names can be given in the document for each module. -- `--disable-macro=[macro1,macro2,...]` disables the system calls or macros supported by the current operating system detected by `configure`. Currently, only the following is supported: - - `event`: used to control whether to disable detection of event-related system calls supported by a specific operating system platform. If disabled, `select` is used by default. - - `sendfile`: Controls whether to disable the `sendfile` system call. - - `writev`: Controls whether the `writev` system call is disabled. - - `unix98`: Controls whether to disable the `__USE_UNIX98` macro. - - `mmap`: Controls whether to disable `mmap` and `munmap` system calls. -- `--help` Show help information +- `--prefix`: Set the installation path for Melon. +- `--melang-prefix`: Set the installation path for Melang scripts used in the Melon library. +- `--cc`: Set the C compiler used for compiling Melon components. +- `--enable-wasm`: Enable `webassembly` mode, which compiles and installs the Melon library in webassembly format. +- `--debug`: Enable `debug` mode. If not enabled, the generated library will not include symbol information, and the `__DEBUG__` macro will not be enabled. +- `--func`: Enable `func` mode, which enables entry and exit callbacks for functions defined using `MLN_FUNC` and `MLN_FUNC_VOID`. +- `--olevel=[O|O1|O2|O3|...]`: Set the optimization level for compilation, default is `O3`. If nothing follows the `=`, optimization is disabled. +- `--select=[all | module1,module2,...]`: Selectively compile specific modules. Default is `all` to compile all modules. Module names can be found in the respective module documentation. +- `--disable-macro=[macro1,macro2,...]`: Disable system calls or macros detected by `configure` for the current operating system. Currently supported: + - `event`: Disable detection of event-related system calls supported by specific operating system platforms. If disabled, `select` is used by default. + - `sendfile`: Control whether to disable the `sendfile` system call. + - `writev`: Control whether to disable the `writev` system call. + - `unix98`: Control whether to disable the `__USE_UNIX98` macro. + - `mmap`: Control whether to disable the `mmap` and `munmap` system calls. +- `--help`: Display help information for the `configure` script. -Melon generates both dynamic and static libraries at the same time. For Linux systems, when using Melon's dynamic library, the path to the library needs to be added to the system configuration: +Melon generates both a dynamic library (`libmelon.so`) and a static library (`libmelon_static.a`). For Linux systems, when using the dynamic library of Melon, you need to add the path of this library to the system configuration: ```bash $ sudo echo "/usr/local/melon/lib/" >> /etc/ld.so.conf $ sudo ldconfig ``` -Or use the command given below to solve dynamic library not found problem: +Alternatively, you can use the `LD_LIBRARY_PATH` environment variable to solve the problem of not finding the dynamic library at runtime: ```shell $ export LD_LIBRARY_PATH=/path/to/melon/libdir:$LD_LIBRARY_PATH @@ -66,7 +48,75 @@ $ export LD_LIBRARY_PATH=/path/to/melon/libdir:$LD_LIBRARY_PATH -By default, Melon is installed in `/usr/local/melon` on UNIX and `$HOME/libmelon` on Windows. +By default, Melon will be installed in `/usr/local/melon` on UNIX systems and in `$HOME/libmelon` on Windows. + + + +### Windows + +Windows platform supports two installation environments: + +- `msys2` +- `msvc` + + + +#### msys2 + +`msys2` fully supports Melon's features and is therefore the recommended installation method on Windows. + +##### Installing msys2 and dependencies + +`msys2` can be downloaded and installed from https://www.msys2.org/. Then, choose `MSYS2 UCRT64` to start the msys2 command line environment, and enter the following command to install the required dependencies: + +``` +pacman -S vim make gcc git +``` + +##### Installing Melon + +This step is exactly the same as the installation steps in the UNIX environment. After installation, you will get the `libmelon.dll` and `libmelon_static.lib` libraries. + + + +#### msvc + +`msvc` currently only supports partial module functionality. The following modules are currently not supported: + +- `class` +- `iothread` +- `thread_pool` +- `spinlock` +- `multi-process framework` +- `multi-thread framework` + +To use `msvc`, you need to first install the [Visual Studio C/C++ IDE](https://visualstudio.microsoft.com/vs/features/cplusplus/). After installation, you can use the Powershell installed along with it to start the MSVC command line environment. + +After pulling the Melon library and change directory to the Melon codebase, execute: + +``` +build.bat +``` + +to compile and install. + +After installation, you will see the compiled static library `libmelon_static.lib` in the `lib` subdirectory. MSVC environment currently does not support generating dynamic libraries. + +One additional point to note is that the Melon library compiled with MSVC hard-codes the paths inside the `path` module, so the installation path is `HOME/libmelon`. + +MSVC currently only supports full installation and does not support selective module installation. + + + +### CMake + +Melon now supports full compilation and installation of the library using CMake. Simply execute the following command: + +```bash +mkdir build +cd build +cmake .. +``` diff --git a/docs/book/en/iothread.md b/docs/book/en/iothread.md index 0adbe47b..c187201d 100644 --- a/docs/book/en/iothread.md +++ b/docs/book/en/iothread.md @@ -2,6 +2,7 @@ I/O thread is another kind of thread pool. But this component is mainly used for GUI applications. Usually, GUI applications will have one user thread and one I/O thread, so that when I/O is processed, it will not prevent to respond to user operations (such as click). +This module is not supported in the MSVC. ### Header file diff --git a/docs/book/en/ipc.md b/docs/book/en/ipc.md index e062e2cc..4bb3c675 100644 --- a/docs/book/en/ipc.md +++ b/docs/book/en/ipc.md @@ -1,5 +1,6 @@ ## IPC module development +This module is not supported in the MSVC. ### Header file diff --git a/docs/book/en/log.md b/docs/book/en/log.md index e5907159..2d602c2a 100644 --- a/docs/book/en/log.md +++ b/docs/book/en/log.md @@ -1,6 +1,9 @@ ## Log +This module is not thread-safe in the `MSVC`. + + ### Header file diff --git a/docs/book/en/multiprocess.md b/docs/book/en/multiprocess.md index b0b90faf..955089f7 100644 --- a/docs/book/en/multiprocess.md +++ b/docs/book/en/multiprocess.md @@ -2,12 +2,11 @@ -**Note**: This function is currently not supported under Windows. - At the beginning of Melon's development, it was necessary to support the multi-process model, which was mainly due to Nginx and previous user-mode network program development experience. Therefore, multiprocessing in Melon also continues the asynchronous event pattern similar to Nginx. Let's use Melon to complete a multi-process example. Although this example is simple, users will find that in fact Melon does not interfere or even gives to the user great freedom of development. +This feature is not supported in the MSVC. After installation, we first need to create a source file called `hello.c` to complete the desired function: diff --git a/docs/book/en/multithread.md b/docs/book/en/multithread.md index 43c5b27a..7010fdc9 100644 --- a/docs/book/en/multithread.md +++ b/docs/book/en/multithread.md @@ -2,12 +2,11 @@ -**Note**: This function is currently not supported under Windows. - Different from the thread pool described above, the multi-threaded framework is a modular thread. Modular threads mean that each thread is an independent code module with its own corresponding entry function (similar to the fact that every C language program has a main function). > Since version 2.3.0, the multi-threading module is no longer integrated by writing code files in the threads directory under the Melon directory, but using the registration function to be registered and loaded by the user in the program. This decouples the thread module code from the Melon library in the directory structure. +This feature is not supported in the MSVC. ### Development steps @@ -39,11 +38,7 @@ Different from the thread pool described above, the multi-threaded framework is return -1; } memset(&msg, 0, sizeof(msg)); - #if defined(__WIN32__) - int n = recv(fd, (char *)&msg, sizeof(msg), 0); - #else int n = recv(fd, &msg, sizeof(msg), 0); - #endif if (n != sizeof(msg)) { mln_log(debug, "recv error. n=%d. %s\n", n, strerror(errno)); return -1; @@ -73,11 +68,7 @@ Different from the thread pool described above, the multi-threaded framework is msg.c = 'N'; msg.type = ITC_REQUEST; msg.need_clear = 1; - #if defined(__WIN32__) - int n = send(fd, (char *)&msg, sizeof(msg), 0); - #else int n = send(fd, &msg, sizeof(msg), 0); - #endif if (n != sizeof(msg)) { mln_log(debug, "send error. n=%d. %s\n", n, strerror(errno)); mln_string_free(msg.dest); diff --git a/docs/book/en/rbtree.md b/docs/book/en/rbtree.md index 8d46575a..7f82e4e7 100644 --- a/docs/book/en/rbtree.md +++ b/docs/book/en/rbtree.md @@ -5,7 +5,7 @@ This document explains how to use the red-black tree component. In the red-black tree component, there are two usage modes: - Basic Usage -- Inline Usage +- Inline Usage (not supported in `MSVC`) - Container Usage diff --git a/docs/book/en/spinlock.md b/docs/book/en/spinlock.md index 11dffbba..fb09b52e 100644 --- a/docs/book/en/spinlock.md +++ b/docs/book/en/spinlock.md @@ -2,6 +2,7 @@ The spinlock in Melon will use different implementations according to different compilers and different CPU architectures. +This module is not supported in the MSVC. ### Header file diff --git a/docs/book/en/tcp_io.md b/docs/book/en/tcp_io.md index 9fddb758..e7809364 100644 --- a/docs/book/en/tcp_io.md +++ b/docs/book/en/tcp_io.md @@ -35,7 +35,7 @@ typedef struct mln_buf_s {//Used to store data, and specify the data storage loc mln_off_t file_last;//end offset of data within this file mln_file_t *file;//File structure, see the introduction of the file collection section mln_u32_t temporary:1;//Whether the memory pointed to by memory pointers such as start and pos is temporary (that is, does not need to be released) -#if !defined(__WIN32__) +#if !defined(MSVC) mln_u32_t mmap:1;//Whether it is the memory created by mmap, it is not supported under win #endif mln_u32_t in_memory:1;//Is the data in memory diff --git a/docs/book/en/threadpool.md b/docs/book/en/threadpool.md index 80e3f441..79696204 100644 --- a/docs/book/en/threadpool.md +++ b/docs/book/en/threadpool.md @@ -4,6 +4,7 @@ There are two multi-threading modes supported in Melon, one of which is the thre **Note**: Only one thread pool is allowed per process. +This module is not supported in the MSVC. ### Header file diff --git a/docs/book/en/trace.md b/docs/book/en/trace.md index 50517751..8e250275 100644 --- a/docs/book/en/trace.md +++ b/docs/book/en/trace.md @@ -42,9 +42,12 @@ For Melang and its supporting library, please refer to [Melang Warehouse](https: ```c mln_trace(fmt, ...); + +//in msvc, the prototype will be +mln_trace(ret, fmt, ...); ``` -Description: Send some data to the specified processing script. The parameters here are exactly the same as `fmt` and its variable parameters of the `mln_lang_ctx_pipe_send` function ([see this chapter for details](https://water-melon.github.io/Melon/en/melang.html)), Because the inside of this macro is to call the function to complete the message delivery. +Description: Send some data to the specified processing script. The parameters here are exactly the same as `fmt` and its variable parameters of the `mln_lang_ctx_pipe_send` function ([see this chapter for details](https://water-melon.github.io/Melon/en/melang.html)), Because the inside of this macro is to call the function to complete the message delivery. In MSVC, the return value will be assigned to the parameter `ret`. return value: - `0` - on success diff --git a/include/mln_alloc.h b/include/mln_alloc.h index 2b0c54fc..b0823a31 100644 --- a/include/mln_alloc.h +++ b/include/mln_alloc.h @@ -6,7 +6,7 @@ #ifndef __MLN_ALLOC_H #define __MLN_ALLOC_H -#if defined(__WIN32__) +#if defined(MSVC) #include #include #else @@ -74,7 +74,11 @@ typedef struct mln_alloc_shm_s { struct mln_alloc_shm_s *prev; struct mln_alloc_shm_s *next; mln_alloc_t *pool; +#if defined(MSVC) + mln_u8ptr_t addr; +#else void *addr; +#endif mln_size_t size; mln_u32_t nfree; mln_u32_t base:31; @@ -83,12 +87,16 @@ typedef struct mln_alloc_shm_s { } mln_alloc_shm_t; struct mln_alloc_s { +#if defined(MSVC) + mln_u8ptr_t mem; +#else void *mem; +#endif mln_size_t shm_size; void *locker; mln_alloc_shm_lock_cb_t lock; mln_alloc_shm_lock_cb_t unlock; -#if defined(__WIN32__) +#if defined(MSVC) HANDLE map_handle; #endif struct mln_alloc_s *parent; diff --git a/include/mln_array.h b/include/mln_array.h index 2a6b17a8..81222472 100644 --- a/include/mln_array.h +++ b/include/mln_array.h @@ -13,7 +13,11 @@ typedef void (*array_pool_free_handler)(void *); typedef void (*array_free)(void *); typedef struct { +#if defined(MSVC) + mln_u8ptr_t elts; +#else void *elts; +#endif mln_size_t size; mln_size_t nalloc; mln_size_t nelts; diff --git a/include/mln_bignum.h b/include/mln_bignum.h index ce0efd82..cfc0d0db 100644 --- a/include/mln_bignum.h +++ b/include/mln_bignum.h @@ -22,11 +22,19 @@ typedef struct { mln_u64_t data[M_BIGNUM_SIZE]; } mln_bignum_t; +#if defined(MSVC) +#define mln_bignum_init(bn) do {\ + bn.tag = M_BIGNUM_POSITIVE;\ + bn.length = 0;\ + memset((bn).data, 0, sizeof(mln_u64_t)*M_BIGNUM_SIZE);\ +} while (0) +#else #define mln_bignum_init(bn) ({\ bn.tag = M_BIGNUM_POSITIVE;\ bn.length = 0;\ memset((bn).data, 0, sizeof(mln_u64_t)*M_BIGNUM_SIZE);\ }) +#endif #define mln_bignum_positive(pbn) ((pbn)->tag = M_BIGNUM_POSITIVE) #define mln_bignum_negative(pbn) ((pbn)->tag = M_BIGNUM_NEGATIVE) #define mln_bignum_is_positive(pbn) ((pbn)->tag == M_BIGNUM_POSITIVE) diff --git a/include/mln_chain.h b/include/mln_chain.h index 58028f18..9dfa8560 100644 --- a/include/mln_chain.h +++ b/include/mln_chain.h @@ -23,7 +23,7 @@ typedef struct mln_buf_s { mln_off_t file_last; mln_file_t *file; mln_u32_t temporary:1; -#if !defined(__WIN32__) +#if !defined(MSVC) mln_u32_t mmap:1; #endif mln_u32_t in_memory:1; diff --git a/include/mln_class.h b/include/mln_class.h index 045a14b2..63717ba1 100644 --- a/include/mln_class.h +++ b/include/mln_class.h @@ -7,6 +7,8 @@ #include +#if !defined(MSVC) + #define new(type, ...) ({\ type *_o = (type *)malloc(sizeof(type));\ if (_o != NULL) {\ @@ -34,6 +36,8 @@ #endif +#endif + #if 0 diff --git a/include/mln_conf.h b/include/mln_conf.h index d89ad804..1ed81cbe 100644 --- a/include/mln_conf.h +++ b/include/mln_conf.h @@ -71,7 +71,9 @@ struct mln_conf_s { mln_conf_domain_cb_t search; mln_conf_domain_cb_t insert; mln_conf_domain_cb_t remove; +#if !defined(MSVC) mln_ipc_cb_t *cb; +#endif }; typedef struct mln_conf_hook_s { diff --git a/include/mln_connection.h b/include/mln_connection.h index 8923612d..4732510c 100644 --- a/include/mln_connection.h +++ b/include/mln_connection.h @@ -5,7 +5,7 @@ #ifndef __MLN_CONNECTION_H #define __MLN_CONNECTION_H -#if defined(__WIN32__) +#if defined(MSVC) #include #else #include diff --git a/include/mln_error.h b/include/mln_error.h index 94656f34..4359c73c 100644 --- a/include/mln_error.h +++ b/include/mln_error.h @@ -19,6 +19,32 @@ extern mln_error_cb_t mln_error_callback; #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +#if defined(MSVC) +#define RET(code) do {\ + int r, l = __LINE__ >= 0x3fff? 0x3fff: (__LINE__ & 0x3fff);\ + int c = ((code) < 0 || (code) >= 0xff)? 0xff: (((code) & 0xff) >= mln_error_nmsg? 0xff: ((code) & 0xff));\ + if (c > 0) {\ + if (mln_error_filenames == NULL) {\ + r = 0x1ff;\ + } else {\ + mln_string_t tmp = mln_string(__FILENAME__);\ + for (r = 0; r < mln_error_nfile; ++r) {\ + if (!mln_string_strcmp(&tmp, &mln_error_filenames[r]))\ + break;\ + }\ + if (r >= 0x1ff || r >= mln_error_nfile) {\ + r = 0x1ff;\ + }\ + }\ + r = (r << 22) | (l << 8) | c;\ + r = -r;\ + } else {\ + r = 0;\ + }\ + if (mln_error_callback != NULL) mln_error_callback(r, mln_error_udata);\ + (code) = r;\ +} while (0) +#else #define RET(code) ({\ int r, l = __LINE__ >= 0x3fff? 0x3fff: (__LINE__ & 0x3fff);\ int c = ((code) < 0 || (code) >= 0xff)? 0xff: (((code) & 0xff) >= mln_error_nmsg? 0xff: ((code) & 0xff));\ @@ -44,6 +70,7 @@ extern mln_error_cb_t mln_error_callback; if (mln_error_callback != NULL) mln_error_callback(r, mln_error_udata);\ r;\ }) +#endif #define CODE(r) ((-(r)) & 0xff) diff --git a/include/mln_event.h b/include/mln_event.h index 2d131eaa..90eae06c 100644 --- a/include/mln_event.h +++ b/include/mln_event.h @@ -11,7 +11,7 @@ #elif defined(MLN_KQUEUE) #include #else -#if defined(__WIN32__) +#if defined(MSVC) #include #include #else @@ -19,8 +19,12 @@ #endif #endif #include -#include +#if !defined(MSVC) #include +#include +#else +#include "mln_utils.h" +#endif #include #include "mln_rbtree.h" #include "mln_fheap.h" @@ -107,9 +111,11 @@ struct mln_event_desc_s { }; struct mln_event_s { +#if !defined(MSVC) pthread_mutex_t fd_lock; pthread_mutex_t timer_lock; pthread_mutex_t cb_lock; +#endif dispatch_callback callback; void *callback_data; mln_u32_t is_break:1; diff --git a/include/mln_fec.h b/include/mln_fec.h index 427fb963..3a7980a9 100644 --- a/include/mln_fec.h +++ b/include/mln_fec.h @@ -10,7 +10,9 @@ #include #include #include +#if !defined(MSVC) #include +#endif #include "mln_string.h" #define M_FEC_RTP_FIXEDLEN 12 diff --git a/include/mln_fheap.h b/include/mln_fheap.h index f0c86644..617c02eb 100644 --- a/include/mln_fheap.h +++ b/include/mln_fheap.h @@ -10,7 +10,7 @@ #include "mln_types.h" #include "mln_func.h" -#if defined(i386) || defined(__arm__) || defined(__WIN32__) +#if defined(i386) || defined(__arm__) || defined(MSVC) #define FH_LGN 33 #else #define FH_LGN 65 @@ -202,6 +202,7 @@ MLN_FUNC(static inline, mln_fheap_node_t *, mln_fheap_remove_child, \ return ret; }) +#if !defined(MSVC) #define mln_fheap_inline_insert(fh, fn, compare) ({\ fheap_cmp cmp = (fheap_cmp)(compare);\ if (cmp == NULL) cmp = (fh)->cmp;\ @@ -301,7 +302,9 @@ MLN_FUNC(static inline, mln_fheap_node_t *, mln_fheap_remove_child, \ (fn)->mark = FHEAP_FALSE;\ (fn);\ }) - +#else +extern mln_fheap_node_t *mln_fheap_node_init(mln_fheap_node_t *fn, void *k); +#endif /* * external diff --git a/include/mln_file.h b/include/mln_file.h index 182bd422..f0a1ee5d 100644 --- a/include/mln_file.h +++ b/include/mln_file.h @@ -39,7 +39,7 @@ struct mln_fileset_s { #define mln_file_fd(pfile) ((pfile)->fd) extern mln_fileset_t *mln_fileset_init(mln_size_t max_file); extern void mln_fileset_destroy(mln_fileset_t *fs); -extern mln_file_t *mln_file_open(mln_fileset_t *fs, const char *filepath) __NONNULL2(1,2); +extern mln_file_t *mln_file_open(mln_fileset_t *fs, char *filepath) __NONNULL2(1,2); extern void mln_file_close(mln_file_t *pfile); extern mln_file_t *mln_file_tmp_open(mln_alloc_t *pool) __NONNULL1(1); diff --git a/include/mln_fork.h b/include/mln_fork.h index 9e22fa1c..afebe797 100644 --- a/include/mln_fork.h +++ b/include/mln_fork.h @@ -4,7 +4,7 @@ */ #ifndef __MLN_FORK_H #define __MLN_FORK_H -#if !defined(__WIN32__) +#if !defined(MSVC) #include #include "mln_types.h" #include "mln_event.h" diff --git a/include/mln_framework.h b/include/mln_framework.h index e06d0e63..0c2d1b75 100644 --- a/include/mln_framework.h +++ b/include/mln_framework.h @@ -8,7 +8,7 @@ #include "mln_event.h" typedef int (*mln_framework_init_t)(void); -#if !defined(__WIN32__) +#if !defined(MSVC) typedef void (*mln_framework_process_t)(mln_event_t *); #endif @@ -16,7 +16,7 @@ struct mln_framework_attr { int argc; char **argv; mln_framework_init_t global_init; -#if !defined(__WIN32__) +#if !defined(MSVC) mln_framework_process_t main_thread; mln_framework_process_t master_process; mln_framework_process_t worker_process; diff --git a/include/mln_iothread.h b/include/mln_iothread.h index cf71040e..16364832 100644 --- a/include/mln_iothread.h +++ b/include/mln_iothread.h @@ -5,6 +5,7 @@ #ifndef __MLN_IOTHREAD_H #define __MLN_IOTHREAD_H +#if !defined(MSVC) #include "mln_types.h" #include @@ -58,3 +59,5 @@ extern int mln_iothread_send(mln_iothread_t *t, mln_u32_t type, void *data, mln_ extern int mln_iothread_recv(mln_iothread_t *t, mln_iothread_ep_type_t from); #endif + +#endif diff --git a/include/mln_ipc.h b/include/mln_ipc.h index b7f043e2..e0553399 100644 --- a/include/mln_ipc.h +++ b/include/mln_ipc.h @@ -5,7 +5,7 @@ #ifndef __MLN_IPC_H #define __MLN_IPC_H -#if !defined(__WIN32__) +#if !defined(MSVC) #include "mln_types.h" #include "mln_fork.h" diff --git a/include/mln_json.h b/include/mln_json.h index a8b52e03..ca29e700 100644 --- a/include/mln_json.h +++ b/include/mln_json.h @@ -86,6 +86,33 @@ struct mln_json_call_attr { #define mln_json_null_type_set(json) (json)->type = M_JSON_NULL #define mln_json_init(j) mln_json_none_type_set(j) +#if defined(MSVC) +#define mln_json_string_init(j, s) do {\ + mln_json_t *json = (j);\ + mln_json_string_type_set(json);\ + json->data.m_j_string = (s);\ +} while (0) +#define mln_json_number_init(j, n) do {\ + mln_json_t *json = (j);\ + mln_json_number_type_set(json);\ + json->data.m_j_number = (double)(n);\ +} while (0) +#define mln_json_true_init(j) do {\ + mln_json_t *json = (j);\ + mln_json_true_type_set(json);\ + json->data.m_j_true = 1;\ +} while (0) +#define mln_json_false_init(j) do {\ + mln_json_t *json = (j);\ + mln_json_false_type_set(json);\ + json->data.m_j_false = 1;\ +} while (0) +#define mln_json_null_init(j) do {\ + mln_json_t *json = (j);\ + mln_json_null_type_set(json);\ + json->data.m_j_null = NULL;\ +} while (0) +#else #define mln_json_string_init(j, s) ({\ mln_json_t *json = (j);\ mln_json_string_type_set(json);\ @@ -111,14 +138,10 @@ struct mln_json_call_attr { mln_json_null_type_set(json);\ json->data.m_j_null = NULL;\ }) +#endif extern int mln_json_obj_init(mln_json_t *j) __NONNULL1(1); extern int mln_json_array_init(mln_json_t *j) __NONNULL1(1); extern void mln_json_destroy(mln_json_t *j); -#define mln_json_reset(j) ({\ - mln_json_t *json = (j);\ - mln_json_destroy(json);\ - mln_json_none_type_set((json));\ -}) extern void mln_json_dump(mln_json_t *j, int n_space, char *prefix); extern int mln_json_obj_update(mln_json_t *j, mln_json_t *key, mln_json_t *val) __NONNULL3(1,2,3); extern mln_json_t *mln_json_obj_search(mln_json_t *j, mln_string_t *key) __NONNULL2(1,2); @@ -133,7 +156,15 @@ extern mln_string_t *mln_json_encode(mln_json_t *j); extern int mln_json_parse(mln_json_t *j, mln_string_t *exp, mln_json_iterator_t iterator, void *data) __NONNULL2(1,2); extern int mln_json_generate(mln_json_t *j, char *fmt, ...) __NONNULL2(1,2); extern int mln_json_object_iterate(mln_json_t *j, mln_json_object_iterator_t it, void *data) __NONNULL2(1,2); - +#if defined(MSVC) +extern void mln_json_reset(mln_json_t *j); +extern int mln_json_array_iterate(mln_json_t *j, mln_json_array_iterator_t it, void *data); +#else +#define mln_json_reset(j) ({\ + mln_json_t *json = (j);\ + mln_json_destroy(json);\ + mln_json_none_type_set((json));\ +}) #define mln_json_array_iterate(j, it, data) ({\ mln_json_t *json = (mln_json_t *)(j), *end;\ mln_json_array_iterator_t iterator = (mln_json_array_iterator_t)(it);\ @@ -149,6 +180,7 @@ extern int mln_json_object_iterate(mln_json_t *j, mln_json_object_iterator_t it, }\ rc;\ }) +#endif #endif diff --git a/include/mln_lang.h b/include/mln_lang.h index c1a818bf..fd1779b2 100644 --- a/include/mln_lang.h +++ b/include/mln_lang.h @@ -112,7 +112,9 @@ struct mln_lang_s { mln_lang_run_ctl_t clear; ev_fd_handler launcher; mln_rbtree_t *alias_set; +#if !defined(MSVC) pthread_mutex_t lock; +#endif }; typedef enum { @@ -218,7 +220,9 @@ struct mln_lang_ctx_s { struct mln_lang_ctx_s *next; mln_lang_symbol_node_t *sym_head; mln_lang_symbol_node_t *sym_tail; +#if !defined(MSVC) pthread_t owner; +#endif mln_string_t *alias; mln_u32_t sym_count:16; mln_u32_t ret_flag:1; @@ -420,7 +424,9 @@ typedef struct { typedef struct { mln_lang_ctx_t *ctx; mln_array_t list; +#if !defined(MSVC) pthread_mutex_t lock; +#endif mln_lang_ctx_pipe_recv_cb_t recv_handler; mln_u32_t subscribed:1; mln_u32_t padding:31; @@ -439,8 +445,10 @@ extern mln_lang_method_t *mln_lang_methods[]; #define mln_lang_ctx_is_quit(ctx) ((ctx)->quit) +#if !defined(MSVC) #define mln_lang_mutex_lock(lang) pthread_mutex_lock(&(lang)->lock) #define mln_lang_mutex_unlock(lang) pthread_mutex_unlock(&(lang)->lock) +#endif #define mln_lang_task_empty(lang) ((lang)->run_head == NULL && (lang)->wait_head == NULL) #define mln_lang_signal_get(lang) ((lang)->signal) #define mln_lang_event_get(lang) ((lang)->ev) @@ -536,13 +544,13 @@ extern mln_lang_array_t *mln_lang_array_new(mln_lang_ctx_t *ctx) __NONNULL1(1); extern void mln_lang_array_free(mln_lang_array_t *array); extern int mln_lang_array_elem_exist(mln_lang_array_t *array, mln_lang_var_t *key) __NONNULL2(1,2); extern int mln_lang_ctx_resource_register(mln_lang_ctx_t *ctx, char *name, void *data, mln_lang_resource_free free_handler) __NONNULL2(1,2); -extern void *mln_lang_ctx_resource_fetch(mln_lang_ctx_t *ctx, const char *name) __NONNULL2(1,2); +extern void *mln_lang_ctx_resource_fetch(mln_lang_ctx_t *ctx, char *name) __NONNULL2(1,2); extern void mln_lang_ctx_set_ret_var(mln_lang_ctx_t *ctx, mln_lang_var_t *var) __NONNULL1(1); extern void mln_lang_ctx_suspend(mln_lang_ctx_t *ctx) __NONNULL1(1); extern void mln_lang_ctx_continue(mln_lang_ctx_t *ctx) __NONNULL1(1); extern int mln_lang_resource_register(mln_lang_t *lang, char *name, void *data, mln_lang_resource_free free_handler) __NONNULL2(1,2); -extern void mln_lang_resource_cancel(mln_lang_t *lang, const char *name) __NONNULL2(1,2); -extern void *mln_lang_resource_fetch(mln_lang_t *lang, const char *name) __NONNULL2(1,2); +extern void mln_lang_resource_cancel(mln_lang_t *lang, char *name) __NONNULL2(1,2); +extern void *mln_lang_resource_fetch(mln_lang_t *lang, char *name) __NONNULL2(1,2); extern int mln_lang_ctx_pipe_send(mln_lang_ctx_t *ctx, char *fmt, ...); extern int mln_lang_ctx_pipe_recv_handler_set(mln_lang_ctx_t *ctx, mln_lang_ctx_pipe_recv_cb_t recv_handler) __NONNULL1(1); diff --git a/include/mln_log.h b/include/mln_log.h index 7b9f1a66..c3fb7111 100644 --- a/include/mln_log.h +++ b/include/mln_log.h @@ -21,7 +21,9 @@ typedef enum { } mln_log_level_t; typedef struct { +#if !defined(MSVC) mln_spin_t thread_lock; +#endif int fd; mln_u32_t in_daemon:1; mln_u32_t init:1; @@ -55,7 +57,7 @@ extern void _mln_sys_log(mln_log_level_t level, \ ...); #define mln_log(err_lv,msg,...) \ _mln_sys_log(err_lv, __FILE__, __FUNCTION__, __LINE__, msg, ## __VA_ARGS__) -extern ssize_t mln_log_writen(void *buf, mln_size_t size); +extern int mln_log_writen(void *buf, mln_size_t size); extern int mln_log_fd(void); extern char *mln_log_dir_path(void); extern char *mln_log_logfile_path(void); diff --git a/include/mln_rbtree.h b/include/mln_rbtree.h index cd53cf0f..2aa50784 100644 --- a/include/mln_rbtree.h +++ b/include/mln_rbtree.h @@ -143,6 +143,7 @@ MLN_FUNC_VOID(static inline, void, rbtree_insert_fixup, \ t->root->color = M_RB_BLACK; }) +#if !defined(MSVC) /*Insert*/ #define mln_rbtree_inline_insert(t, n, compare) ({\ mln_rbtree_t *tree = (t);\ @@ -246,6 +247,7 @@ MLN_FUNC_VOID(static inline, void, rbtree_insert_fixup, \ (n)->nofree = 1;\ (n);\ }) +#endif #define mln_rbtree_node_num(ptree) ((ptree)->nr_node) @@ -254,6 +256,9 @@ MLN_FUNC_VOID(static inline, void, rbtree_insert_fixup, \ #define mln_rbtree_node_data_set(node,ud) ((node)->data = (ud)) #define mln_rbtree_root(ptree) ((ptree)->root) +#if defined(MSVC) +extern mln_rbtree_node_t *mln_rbtree_node_init(mln_rbtree_node_t *n, void *data); +#endif extern mln_rbtree_t *mln_rbtree_new(struct mln_rbtree_attr *attr); extern void mln_rbtree_free(mln_rbtree_t *t); extern void mln_rbtree_insert(mln_rbtree_t *t, mln_rbtree_node_t *node) __NONNULL2(1,2); diff --git a/include/mln_rs.h b/include/mln_rs.h index 0d3718a4..ce4e5d28 100644 --- a/include/mln_rs.h +++ b/include/mln_rs.h @@ -10,7 +10,9 @@ #include #include #include +#if !defined(MSVC) #include +#endif typedef struct { mln_size_t row; diff --git a/include/mln_span.h b/include/mln_span.h index f5322e03..39c1c79b 100644 --- a/include/mln_span.h +++ b/include/mln_span.h @@ -5,13 +5,14 @@ #ifndef __MLN_SPAN_H #define __MLN_SPAN_H -#include -#include "mln_func.h" -#if defined(__WIN32__) +#if defined(MSVC) +#include "mln_utils.h" #include #else +#include #include #endif +#include "mln_func.h" typedef struct mln_span_s { struct timeval begin; @@ -37,7 +38,7 @@ typedef void (*mln_span_dump_cb_t)(mln_span_t *s, int level, void *data); extern mln_span_stack_node_t *__mln_span_stack_top; extern mln_span_stack_node_t *__mln_span_stack_bottom; extern mln_span_t *mln_span_root; -#if defined(__WIN32__) +#if defined(MSVC) extern DWORD mln_span_registered_thread; #else extern pthread_t mln_span_registered_thread; @@ -49,30 +50,27 @@ extern void mln_span_free(mln_span_t *s); extern int mln_span_entry(void *fptr, const char *file, const char *func, int line, ...); extern void mln_span_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...); -#if defined(__WIN32__) -#define mln_span_start() ({\ - int r = 0;\ +#if defined(MSVC) +#define mln_span_start() do {\ mln_func_entry_callback_set(mln_span_entry);\ mln_func_exit_callback_set(mln_span_exit);\ mln_span_registered_thread = GetCurrentThreadId();\ mln_span_root = NULL;\ __mln_span_stack_top = __mln_span_stack_bottom = NULL;\ mln_span_entry(__FILE__, __FUNCTION__, __LINE__);\ - r;\ -}) +} while (0) #else #define mln_span_start() ({\ - int r = 0;\ mln_func_entry_callback_set(mln_span_entry);\ mln_func_exit_callback_set(mln_span_exit);\ mln_span_registered_thread = pthread_self();\ __mln_span_stack_top = __mln_span_stack_bottom = NULL;\ mln_span_root = NULL;\ mln_span_entry(__FILE__, __FUNCTION__, __LINE__);\ - r;\ }) #endif +#if !defined(MSVC) #define mln_span_stop() ({\ mln_span_exit(__FILE__, __FUNCTION__, __LINE__, NULL);\ mln_func_entry_callback_set(NULL);\ @@ -91,14 +89,26 @@ extern void mln_span_exit(void *fptr, const char *file, const char *func, int li span;\ }) -#define mln_span_file(s) ((s)->file) -#define mln_span_func(s) ((s)->func) -#define mln_span_line(s) ((s)->line) #define mln_span_time_cost(s) ({\ mln_span_t *_s = (s);\ mln_u64_t r = (_s->end.tv_sec * 1000000 + _s->end.tv_usec) - (_s->begin.tv_sec * 1000000 + _s->begin.tv_usec);\ r;\ }) +#else +#define mln_span_stop() do {\ + mln_span_exit(__FILE__, __FUNCTION__, __LINE__, NULL);\ + mln_func_entry_callback_set(NULL);\ + mln_func_exit_callback_set(NULL);\ + mln_span_stack_free();\ +} while (0) + +extern void mln_span_release(void); +extern mln_span_t *mln_span_move(void); +extern mln_u64_t mln_span_time_cost(mln_span_t *s); +#endif +#define mln_span_file(s) ((s)->file) +#define mln_span_func(s) ((s)->func) +#define mln_span_line(s) ((s)->line) extern void mln_span_dump(mln_span_dump_cb_t cb, void *data); #endif diff --git a/include/mln_string.h b/include/mln_string.h index fff51c0d..06c4a24e 100644 --- a/include/mln_string.h +++ b/include/mln_string.h @@ -22,6 +22,7 @@ typedef struct { * init & free */ #define mln_string(s) {(mln_u8ptr_t)s, sizeof(s)-1, 1, 0, 1} +#if !defined(MSVC) #define mln_string_set(pstring,s) \ ({\ (pstring)->data = (mln_u8ptr_t)(s);\ @@ -60,6 +61,12 @@ typedef struct { }\ }\ }) +#else +extern mln_string_t *mln_string_set(mln_string_t *str, char *s); +extern mln_string_t *mln_string_nset(mln_string_t *str, char *s, mln_u64_t n); +extern mln_string_t *mln_string_ref(mln_string_t *s); +extern void mln_string_free(mln_string_t *s); +#endif extern mln_string_t *mln_string_new(const char *s); diff --git a/include/mln_thread.h b/include/mln_thread.h index 169ecde8..cc4b154c 100644 --- a/include/mln_thread.h +++ b/include/mln_thread.h @@ -4,6 +4,9 @@ */ #ifndef __MLN_THREAD_H #define __MLN_THREAD_H + +#if !defined(MSVC) + #include "mln_connection.h" #include "mln_types.h" #include "mln_string.h" @@ -101,3 +104,5 @@ extern void mln_thread_cleanup_set(void (*tcleanup)(void *), void *data); extern void mln_thread_module_set(mln_thread_module_t *modules, mln_size_t num); #endif +#endif + diff --git a/include/mln_thread_pool.h b/include/mln_thread_pool.h index 326e63cc..142b6201 100644 --- a/include/mln_thread_pool.h +++ b/include/mln_thread_pool.h @@ -5,6 +5,8 @@ #ifndef __MLN_THREAD_POOL_H #define __MLN_THREAD_POOL_H +#if !defined(MSVC) + #ifdef MLN_USE_UNIX98 #ifndef __USE_UNIX98 #define __USE_UNIX98 @@ -76,3 +78,5 @@ extern int mln_thread_pool_resource_add(void *data) __NONNULL1(1); extern void mln_thread_quit(void); extern void mln_thread_resource_info(struct mln_thread_pool_info *info); #endif + +#endif diff --git a/include/mln_tools.h b/include/mln_tools.h index 51dd98fe..ede70b03 100644 --- a/include/mln_tools.h +++ b/include/mln_tools.h @@ -33,7 +33,7 @@ struct utctime { extern int mln_sys_limit_modify(void); -#if !defined(__WIN32__) +#if !defined(MSVC) extern int mln_daemon(void); #endif extern int mln_boot_params(int argc, char *argv[]); diff --git a/include/mln_trace.h b/include/mln_trace.h index 67aae403..732f8bba 100644 --- a/include/mln_trace.h +++ b/include/mln_trace.h @@ -9,6 +9,13 @@ typedef int (*mln_trace_init_cb_t)(mln_lang_ctx_t *ctx); +#if defined(MSVC) +#define mln_trace(ret, fmt, ...) do {\ + ret = 0;\ + mln_lang_ctx_t *__trace_ctx = mln_trace_task_get();\ + if (__trace_ctx != NULL) ret = mln_lang_ctx_pipe_send(__trace_ctx, fmt, ## __VA_ARGS__);\ + } while (0) +#else #define mln_trace(fmt, ...) \ ({\ int ret = 0;\ @@ -16,6 +23,7 @@ typedef int (*mln_trace_init_cb_t)(mln_lang_ctx_t *ctx); if (__trace_ctx != NULL) ret = mln_lang_ctx_pipe_send(__trace_ctx, fmt, ## __VA_ARGS__);\ ret;\ }) +#endif extern mln_string_t *mln_trace_path(void); extern int mln_trace_init(mln_event_t *ev, mln_string_t *path); diff --git a/include/mln_types.h b/include/mln_types.h index 33b7abb7..54229dba 100644 --- a/include/mln_types.h +++ b/include/mln_types.h @@ -7,8 +7,9 @@ #define __MLN_TYPES #include -#include #include "mln_utils.h" +#if !defined(MSVC) +#include #if defined(__GNUC__) && (__GNUC__ >= 4 && __GNUC_MINOR__ > 1) typedef long mln_spin_t; #elif defined(i386) || defined(__x86_64) @@ -17,6 +18,7 @@ typedef unsigned long mln_spin_t; #include typedef pthread_spinlock_t mln_spin_t; #endif +#endif typedef unsigned char mln_u8_t; typedef char mln_s8_t; @@ -24,7 +26,7 @@ typedef unsigned short mln_u16_t; typedef short mln_s16_t; typedef unsigned int mln_u32_t; typedef int mln_s32_t; -#if defined(i386) || defined(__arm__) || defined(__WIN32__) || defined(__wasm__) +#if defined(i386) || defined(__arm__) || defined(MSVC) || defined(__wasm__) typedef unsigned long long mln_u64_t; typedef long long mln_s64_t; #else @@ -37,7 +39,7 @@ typedef short * mln_s16ptr_t; typedef unsigned short * mln_u16ptr_t; typedef int * mln_s32ptr_t; typedef unsigned int * mln_u32ptr_t; -#if defined(i386) || defined(__arm__) || defined(__WIN32__) || defined(__wasm__) +#if defined(i386) || defined(__arm__) || defined(MSVC) || defined(__wasm__) typedef long long * mln_s64ptr_t; typedef unsigned long long * mln_u64ptr_t; #else @@ -46,24 +48,10 @@ typedef unsigned long * mln_u64ptr_t; #endif typedef size_t mln_size_t; typedef off_t mln_off_t; -#if defined(__WIN32__) - #if defined(i386) || defined(__arm__) typedef long mln_sptr_t; typedef unsigned long mln_uptr_t; typedef long mln_sauto_t; typedef unsigned long mln_uauto_t; - #else -typedef long long mln_sptr_t; -typedef unsigned long long mln_uptr_t; -typedef long long mln_sauto_t; -typedef unsigned long long mln_uauto_t; - #endif -#else -typedef long mln_sptr_t; -typedef unsigned long mln_uptr_t; -typedef long mln_sauto_t; -typedef unsigned long mln_uauto_t; -#endif #endif diff --git a/include/mln_utils.h b/include/mln_utils.h index a47df1a4..561c30a7 100644 --- a/include/mln_utils.h +++ b/include/mln_utils.h @@ -5,7 +5,11 @@ #ifndef __MLN_DEFS_H #define __MLN_DEFS_H +#if !defined(MSVC) #include +#else +#include "mln_types.h" +#endif #include "mln_func.h" #ifdef __DEBUG__ @@ -19,6 +23,15 @@ * container_of and offsetof */ #define mln_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#if defined(MSVC) +#define mln_container_of(ptr, type, member, ret) do {\ + type *__rptr = NULL;\ + if ((ptr) != NULL) {\ + __rptr = (type *)((char *)((const typeof(((type *)0)->member) *)(ptr)) - mln_offsetof(type, member));\ + }\ + (ret) = __rptr;\ +} while (0) +#else #define mln_container_of(ptr, type, member) ({\ type *__rptr = NULL;\ if ((ptr) != NULL) {\ @@ -26,11 +39,12 @@ }\ __rptr;\ }) +#endif /* * nonnull attribute */ -#if defined(__APPLE__) || defined(__WIN32__) || defined(__wasm__) +#if defined(__APPLE__) || defined(MSVC) || defined(__wasm__) #define __NONNULL1(x) #define __NONNULL2(x,y) #define __NONNULL3(x,y,z) @@ -71,6 +85,7 @@ extern int spin_trylock(void *lock); #else +#if !defined(MSVC) /* * In FreeBSD, pthread_spin_init() will call calloc(), * so we cannot use these interfaces in FreeBSD to @@ -85,6 +100,7 @@ extern int spin_trylock(void *lock); pthread_spin_destroy((lock_ptr)) #endif +#endif /* * Chain @@ -142,7 +158,7 @@ extern int spin_trylock(void *lock); }\ } -#if defined(__WIN32__) +#if defined(MSVC) extern int pipe(int fds[2]); extern int socketpair(int domain, int type, int protocol, int sv[2]); #define mln_socket_close closesocket @@ -160,6 +176,28 @@ extern int socketpair(int domain, int type, int protocol, int sv[2]); extern void usleep(unsigned long usec); #endif +#if defined(MSVC) +#include +#include +#define MAX_PATH_LEN 260 + +struct dirent { + char d_name[MAX_PATH_LEN]; +}; + +typedef struct { + HANDLE hFind; + WIN32_FIND_DATAA findFileData; + char* path; + CRITICAL_SECTION lock; +} DIR; + +extern DIR* opendir(const char* path); +extern struct dirent* readdir(DIR* dirp); +extern int closedir(DIR* dirp); +extern int gettimeofday(struct timeval *tv, void *tz); +#endif + #define MLN_AUTHOR "Niklaus F.Schen" #endif diff --git a/src/mln_alloc.c b/src/mln_alloc.c index 860ca52f..35b0392a 100644 --- a/src/mln_alloc.c +++ b/src/mln_alloc.c @@ -33,9 +33,7 @@ static inline void *mln_alloc_shm_set_bitmap(mln_alloc_shm_t *as, mln_off_t Boff static inline mln_alloc_shm_t *mln_alloc_shm_new_block(mln_alloc_t *pool, mln_off_t *Boff, mln_off_t *boff, mln_size_t size); static inline void mln_alloc_free_shm(void *ptr); -MLN_FUNC(static inline, mln_alloc_shm_t *, mln_alloc_shm_new, \ - (mln_alloc_t *pool, mln_size_t size, int is_large), \ - (pool, size, is_large), \ +static inline mln_alloc_shm_t *mln_alloc_shm_new(mln_alloc_t *pool, mln_size_t size, int is_large) { int n, i, j; mln_alloc_shm_t *shm, *tmp; @@ -88,21 +86,19 @@ MLN_FUNC(static inline, mln_alloc_shm_t *, mln_alloc_shm_new, \ } return shm; -}) +} -MLN_FUNC(, mln_alloc_t *, mln_alloc_shm_init, \ - (mln_size_t size, void *locker, mln_alloc_shm_lock_cb_t lock, mln_alloc_shm_lock_cb_t unlock), \ - (size, locker, lock, unlock), \ +mln_alloc_t *mln_alloc_shm_init(mln_size_t size, void *locker, mln_alloc_shm_lock_cb_t lock, mln_alloc_shm_lock_cb_t unlock) { mln_alloc_t *pool; -#if defined(__WIN32__) +#if defined(MSVC) HANDLE handle; #endif if (size < M_ALLOC_SHM_DEFAULT_SIZE+1024 || locker == NULL || lock == NULL || unlock == NULL) return NULL; -#if defined(__WIN32__) +#if defined(MSVC) if ((handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, @@ -141,15 +137,20 @@ MLN_FUNC(, mln_alloc_t *, mln_alloc_shm_init, \ pool->parent = NULL; pool->large_used_head = pool->large_used_tail = NULL; pool->shm_head = pool->shm_tail = NULL; +#if defined(MSVC) + pool->mem = (mln_u8ptr_t)pool; +#else pool->mem = pool; +#endif pool->shm_size = size; pool->locker = locker; pool->lock = lock; pool->unlock = unlock; return pool; -}) +} -MLN_FUNC(, mln_alloc_t *, mln_alloc_init, (mln_alloc_t *parent), (parent), { +mln_alloc_t *mln_alloc_init(mln_alloc_t *parent) +{ mln_alloc_t *pool; #ifdef __DEBUG__ @@ -183,7 +184,7 @@ MLN_FUNC(, mln_alloc_t *, mln_alloc_init, (mln_alloc_t *parent), (parent), { pool->lock = NULL; pool->unlock = NULL; return pool; -}) +} MLN_FUNC_VOID(static inline, void, mln_alloc_mgr_table_init, (mln_alloc_mgr_t *tbl), (tbl), { int i, j; @@ -209,7 +210,8 @@ MLN_FUNC_VOID(static inline, void, mln_alloc_mgr_table_init, (mln_alloc_mgr_t *t } }) -MLN_FUNC_VOID(, void, mln_alloc_destroy, (mln_alloc_t *pool), (pool), { +void mln_alloc_destroy(mln_alloc_t *pool) +{ if (pool == NULL) return; mln_alloc_t *parent = pool->parent; @@ -235,7 +237,7 @@ MLN_FUNC_VOID(, void, mln_alloc_destroy, (mln_alloc_t *pool), (pool), { if (parent != NULL) mln_alloc_free(pool); else free(pool); } else { -#if defined(__WIN32__) +#if defined(MSVC) HANDLE handle = pool->map_handle; UnmapViewOfFile(pool->mem); CloseHandle(handle); @@ -247,9 +249,10 @@ MLN_FUNC_VOID(, void, mln_alloc_destroy, (mln_alloc_t *pool), (pool), { } if (parent != NULL && mln_alloc_is_shm(parent)) (void)parent->unlock(parent->locker); -}) +} -MLN_FUNC(, void *, mln_alloc_m, (mln_alloc_t *pool, mln_size_t size), (pool, size), { +void *mln_alloc_m(mln_alloc_t *pool, mln_size_t size) +{ #ifdef __DEBUG__ return malloc(size); #else @@ -346,10 +349,9 @@ MLN_FUNC(, void *, mln_alloc_m, (mln_alloc_t *pool, mln_size_t size), (pool, siz ++(blk->chunk->refer); return blk->data; #endif -}) +} -MLN_FUNC(static inline, mln_alloc_mgr_t *, mln_alloc_get_mgr_by_size, \ - (mln_alloc_mgr_t *tbl, mln_size_t size), (tbl, size), \ +static inline mln_alloc_mgr_t *mln_alloc_get_mgr_by_size(mln_alloc_mgr_t *tbl, mln_size_t size) { if (size > tbl[M_ALLOC_MGR_LEN-1].blk_size) return NULL; @@ -373,9 +375,10 @@ MLN_FUNC(static inline, mln_alloc_mgr_t *, mln_alloc_get_mgr_by_size, \ if (am[off].blk_size >= size) return &am[off]; if (am[off+1].blk_size >= size) return &am[off+1]; return &am[off+2]; -}) +} -MLN_FUNC(, void *, mln_alloc_c, (mln_alloc_t *pool, mln_size_t size), (pool, size), { +void *mln_alloc_c(mln_alloc_t *pool, mln_size_t size) +{ #ifdef __DEBUG__ return calloc(1, size); #else @@ -384,9 +387,10 @@ MLN_FUNC(, void *, mln_alloc_c, (mln_alloc_t *pool, mln_size_t size), (pool, siz memset(ptr, 0, size); return ptr; #endif -}) +} -MLN_FUNC(, void *, mln_alloc_re, (mln_alloc_t *pool, void *ptr, mln_size_t size), (pool, ptr, size), { +void *mln_alloc_re(mln_alloc_t *pool, void *ptr, mln_size_t size) +{ #ifdef __DEBUG__ return realloc(ptr, size); #else @@ -407,9 +411,10 @@ MLN_FUNC(, void *, mln_alloc_re, (mln_alloc_t *pool, void *ptr, mln_size_t size) return new_ptr; #endif -}) +} -MLN_FUNC_VOID(, void, mln_alloc_free, (void *ptr), (ptr), { +void mln_alloc_free(void *ptr) +{ if (ptr == NULL) { return; } @@ -428,7 +433,8 @@ MLN_FUNC_VOID(, void, mln_alloc_free, (void *ptr), (ptr), { pool = blk->pool; if (pool->mem) { - return mln_alloc_free_shm(ptr); + mln_alloc_free_shm(ptr); + return; } if (blk->is_large) { @@ -472,7 +478,7 @@ MLN_FUNC_VOID(, void, mln_alloc_free, (void *ptr), (ptr), { free(ch); } #endif -}) +} MLN_FUNC(static inline, void *, mln_alloc_shm_m, (mln_alloc_t *pool, mln_size_t size), (pool, size), { mln_alloc_shm_t *as; @@ -499,7 +505,8 @@ MLN_FUNC(static inline, void *, mln_alloc_shm_m, (mln_alloc_t *pool, mln_size_t return mln_alloc_shm_set_bitmap(as, Boff, boff, size); }) -MLN_FUNC(static inline, void *, mln_alloc_shm_large_m, (mln_alloc_t *pool, mln_size_t size), (pool, size), { +static inline void *mln_alloc_shm_large_m(mln_alloc_t *pool, mln_size_t size) +{ mln_alloc_shm_t *as; mln_alloc_blk_t *blk; @@ -515,7 +522,7 @@ MLN_FUNC(static inline, void *, mln_alloc_shm_large_m, (mln_alloc_t *pool, mln_s blk->is_large = 1; blk->in_used = 1; return blk->data; -}) +} MLN_FUNC(static inline, mln_alloc_shm_t *, mln_alloc_shm_new_block, \ (mln_alloc_t *pool, mln_off_t *Boff, mln_off_t *boff, mln_size_t size), \ diff --git a/src/mln_asn1.c b/src/mln_asn1.c index fd3feca3..f2b428ce 100644 --- a/src/mln_asn1.c +++ b/src/mln_asn1.c @@ -69,7 +69,7 @@ MLN_FUNC(, mln_asn1_deresult_t *, mln_asn1_decode_chain, \ mln_chain_t *c; mln_buf_t *b; mln_asn1_deresult_t *res; - ssize_t n; + int n; for (c = in; c != NULL; c = c->next) { if (c->buf == NULL) continue; diff --git a/src/mln_bignum.c b/src/mln_bignum.c index 38566604..f679b192 100644 --- a/src/mln_bignum.c +++ b/src/mln_bignum.c @@ -2,14 +2,18 @@ /* * Copyright (C) Niklaus F.Schen. */ -#if defined(__WIN32__) +#if defined(MSVC) #define _CRT_RAND_S #endif #include #include #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include #include "mln_bignum.h" #include "mln_func.h" @@ -905,15 +909,14 @@ MLN_FUNC(static inline, int, __mln_bignum_div_word, \ return 0; }) -MLN_FUNC_VOID(, void, mln_bignum_dump, (mln_bignum_t *bn), (bn), { +void mln_bignum_dump(mln_bignum_t *bn) +{ fprintf(stderr, "Tag: %s\n", bn->tag==M_BIGNUM_POSITIVE?"+":"-"); fprintf(stderr, "Length: %u\n", bn->length); mln_u32_t i; fprintf(stderr, "Data:\n"); for (i = 0; i < M_BIGNUM_SIZE; ++i) { -#if defined(__WIN32__) && defined(__pentiumpro__) - fprintf(stderr, "\t%I64x\n", bn->data[i]); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) fprintf(stderr, "\t%llx\n", bn->data[i]); #else fprintf(stderr, "\t%lx\n", bn->data[i]); @@ -921,7 +924,7 @@ MLN_FUNC_VOID(, void, mln_bignum_dump, (mln_bignum_t *bn), (bn), { } fprintf(stderr, "\n"); fflush(stderr); -}) +} /* * prime @@ -982,8 +985,7 @@ MLN_FUNC(static inline, mln_u32_t, mln_bignum_witness, \ return 0; }) -MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \ - (mln_bignum_t *bn, mln_u32_t bitwidth), (bn, bitwidth), \ +static inline void mln_bignum_random_prime(mln_bignum_t *bn, mln_u32_t bitwidth) { struct timeval tv; memset(bn, 0, sizeof(mln_bignum_t)); @@ -994,7 +996,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \ mln_s32_t i; for (i = 0; i < times; ++i) { -#if defined(__WIN32__) +#if defined(MSVC) rand_s(&val); #else val = (mln_u32_t)rand_r(&val); @@ -1003,7 +1005,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \ } if ((off = bitwidth % 32)) { -#if defined(__WIN32__) +#if defined(MSVC) rand_s(&val); data[i] = ((mln_u64_t)val * 0xfdfd) & 0xffffffff; #else @@ -1021,7 +1023,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \ if (data[i-1] == 0) { gettimeofday(&tv, NULL); val = tv.tv_sec*1000000+tv.tv_usec; -#if defined(__WIN32__) +#if defined(MSVC) rand_s(&val); data[i-1] = (mln_u32_t)val; #else @@ -1033,11 +1035,9 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \ data[0] |= 1; } } -}) +} -MLN_FUNC_VOID(static inline, void, mln_bignum_random_scope, \ - (mln_bignum_t *bn, mln_u32_t bitwidth, mln_bignum_t *max), \ - (bn, bitwidth, max), \ +static inline void mln_bignum_random_scope(mln_bignum_t *bn, mln_u32_t bitwidth, mln_bignum_t *max) { mln_u32_t width; struct timeval tv; @@ -1046,7 +1046,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_scope, \ lp: gettimeofday(&tv, NULL); val = tv.tv_sec*1000000+tv.tv_usec; -#if defined(__WIN32__) +#if defined(MSVC) rand_s(&val); width = val % bitwidth; #else @@ -1054,7 +1054,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_scope, \ #endif if (width < 2) goto lp; mln_bignum_random_prime(bn, width); -}) +} MLN_FUNC(, int, mln_bignum_prime, (mln_bignum_t *res, mln_u32_t bitwidth), (res, bitwidth), { if (bitwidth > M_BIGNUM_BITS>>1 || bitwidth < 3) return -1; diff --git a/src/mln_chain.c b/src/mln_chain.c index 49c296cb..df1bc360 100644 --- a/src/mln_chain.c +++ b/src/mln_chain.c @@ -6,7 +6,8 @@ #include "mln_chain.h" #include "mln_func.h" -MLN_FUNC(, mln_buf_t *, mln_buf_new, (mln_alloc_t *pool), (pool), { +mln_buf_t *mln_buf_new(mln_alloc_t *pool) +{ mln_buf_t *b = mln_alloc_m(pool, sizeof(mln_buf_t)); b->left_pos = b->pos = b->last = NULL; b->start = b->end = NULL; @@ -14,12 +15,12 @@ MLN_FUNC(, mln_buf_t *, mln_buf_new, (mln_alloc_t *pool), (pool), { b->file_left_pos = b->file_pos = b->file_last = 0; b->file = NULL; b->temporary = b->in_memory = b->in_file = 0; -#if !defined(__WIN32__) && defined(MLN_MMAP) +#if !defined(MSVC) && defined(MLN_MMAP) b->mmap = 0; #endif b->flush = b->sync = b->last_buf = b->last_in_chain = 0; return b; -}) +} MLN_FUNC(, mln_chain_t *, mln_chain_new, (mln_alloc_t *pool), (pool), { mln_chain_t *c = mln_alloc_m(pool, sizeof(mln_chain_t)); @@ -28,7 +29,8 @@ MLN_FUNC(, mln_chain_t *, mln_chain_new, (mln_alloc_t *pool), (pool), { return c; }) -MLN_FUNC_VOID(, void, mln_buf_pool_release, (mln_buf_t *b), (b), { +void mln_buf_pool_release(mln_buf_t *b) +{ if (b == NULL) return; if (b->shadow != NULL || b->temporary) { @@ -37,7 +39,7 @@ MLN_FUNC_VOID(, void, mln_buf_pool_release, (mln_buf_t *b), (b), { } if (b->in_memory) { -#if !defined(__WIN32__) && defined(MLN_MMAP) +#if !defined(MSVC) && defined(MLN_MMAP) if (b->mmap) { if (b->start != NULL) { munmap(b->start, b->end - b->start); @@ -51,7 +53,7 @@ MLN_FUNC_VOID(, void, mln_buf_pool_release, (mln_buf_t *b), (b), { } else { mln_alloc_free(b->pos); } -#if !defined(__WIN32__) && defined(MLN_MMAP) +#if !defined(MSVC) && defined(MLN_MMAP) } #endif mln_alloc_free(b); @@ -65,7 +67,7 @@ MLN_FUNC_VOID(, void, mln_buf_pool_release, (mln_buf_t *b), (b), { } mln_alloc_free(b); -}) +} MLN_FUNC_VOID(, void, mln_chain_pool_release, (mln_chain_t *c), (c), { if (c == NULL) return; diff --git a/src/mln_conf.c b/src/mln_conf.c index 9fb536ff..35bea5d5 100644 --- a/src/mln_conf.c +++ b/src/mln_conf.c @@ -12,14 +12,11 @@ #define CONF_ERR(lex,TK,MSG); \ {\ - int rc = 1;\ mln_string_t *path = mln_lex_get_cur_filename(lex);\ fprintf(stderr, "Configuration error. ");\ if (path != NULL) {\ - rc = write(STDERR_FILENO, path->data, path->len);\ - rc = write(STDERR_FILENO, ":", 1);\ + fprintf(stderr, "%s:", (char *)(path->data));\ }\ - if (rc <= 0) rc = 1;/*do nothing*/\ fprintf(stderr, "%d: \"%s\" %s.\n", (TK)->line, (char *)((TK)->text->data), MSG);\ } @@ -106,7 +103,7 @@ static int mln_conf_dump_domain_iterate_handler(mln_rbtree_node_t *node, void *u /*for hook*/ static mln_conf_hook_t *mln_conf_hook_init(void); static void mln_conf_hook_destroy(mln_conf_hook_t *ch); -#if !defined(__WIN32__) +#if !defined(MSVC) static void mln_conf_reload_master_handler(mln_event_t *ev, void *f_ptr, void *buf, mln_u32_t len, void **udata_ptr); static void mln_conf_reload_worker_handler(mln_event_t *ev, void *f_ptr, void *buf, mln_u32_t len, void **udata_ptr); #endif @@ -132,7 +129,8 @@ MLN_FUNC(static, mln_conf_lex_struct_t *, mln_conf_lex_sglq_handler, (mln_lex_t return mln_conf_lex_new(lex, CONF_TK_CHAR); }) -MLN_FUNC(static inline, int, mln_get_char, (mln_lex_t *lex, char c), (lex, c), { +static inline int mln_get_char(mln_lex_t *lex, char c) +{ if (c == '\\') { char n; if ((n = mln_lex_getchar(lex)) == MLN_ERR) return -1; @@ -161,9 +159,11 @@ MLN_FUNC(static inline, int, mln_get_char, (mln_lex_t *lex, char c), (lex, c), { case 'r': if (mln_lex_putchar(lex, '\r') == MLN_ERR) return -1; break; +#if !defined(MSVC) case 'e': if (mln_lex_putchar(lex, '\e') == MLN_ERR) return -1; break; +#endif case 'v': if (mln_lex_putchar(lex, '\v') == MLN_ERR) return -1; break; @@ -178,7 +178,7 @@ MLN_FUNC(static inline, int, mln_get_char, (mln_lex_t *lex, char c), (lex, c), { if (mln_lex_putchar(lex, c) == MLN_ERR) return -1; } return 0; -}) +} MLN_FUNC(static, mln_conf_lex_struct_t *, mln_conf_lex_dblq_handler, (mln_lex_t *lex, void *data), (lex, data), { mln_lex_result_clean(lex); @@ -303,7 +303,8 @@ MLN_FUNC(static, mln_conf_lex_struct_t *, mln_conf_token, (mln_lex_t *lex), (lex /*mln_conf_t*/ -MLN_FUNC(static inline, mln_conf_t *, mln_conf_init, (void), (), { +static inline mln_conf_t *mln_conf_init(void) +{ mln_conf_t *cf; cf = (mln_conf_t *)malloc(sizeof(mln_conf_t)); if (cf == NULL) { @@ -350,7 +351,11 @@ MLN_FUNC(static inline, mln_conf_t *, mln_conf_init, (void), (), { conf_file_path[path_len] = '\0'; mln_string_nset(&path, conf_file_path, path_len); +#if defined(MSVC) + if (!_access(conf_file_path, 0)) { +#else if (!access(conf_file_path, F_OK)) { +#endif lattr.pool = pool; lattr.keywords = conf_keywords; memset(&hooks, 0, sizeof(hooks)); @@ -381,20 +386,25 @@ MLN_FUNC(static inline, mln_conf_t *, mln_conf_init, (void), (), { mln_conf_destroy(cf); return NULL; } +#if !defined(MSVC) cf->cb = NULL; +#endif return cf; -}) +} -MLN_FUNC_VOID(static, void, mln_conf_destroy, (mln_conf_t *cf), (cf), { +static void mln_conf_destroy(mln_conf_t *cf) +{ if (cf == NULL) return; mln_conf_destroy_lex(cf); if (cf->domain != NULL) { mln_rbtree_free(cf->domain); cf->domain = NULL; } +#if !defined(MSVC) if (cf->cb != NULL) mln_ipc_handler_unregister(cf->cb); +#endif free(cf); -}) +} MLN_FUNC_VOID(static inline, void, mln_conf_destroy_lex, (mln_conf_t *cf), (cf), { if (cf == NULL) return; @@ -822,7 +832,8 @@ MLN_FUNC(static, int, _mln_conf_load, (mln_conf_t *cf, mln_conf_domain_t *curren return 0; }) -MLN_FUNC(, int, mln_conf_load, (void), (), { +int mln_conf_load(void) +{ mln_string_t dname; mln_rbtree_node_t *rn; mln_conf_domain_t *cd, tmp; @@ -847,7 +858,7 @@ MLN_FUNC(, int, mln_conf_load, (void), (), { return -1; } } -#if !defined(__WIN32__) +#if !defined(MSVC) if ((g_conf->cb = mln_ipc_handler_register(M_IPC_TYPE_CONF, mln_conf_reload_master_handler, mln_conf_reload_worker_handler, NULL, NULL)) == NULL) { mln_conf_destroy(g_conf); g_conf = NULL; @@ -855,7 +866,7 @@ MLN_FUNC(, int, mln_conf_load, (void), (), { } #endif return 0; -}) +} MLN_FUNC_VOID(, void, mln_conf_free, (void), (), { if (g_conf == NULL) return; @@ -973,9 +984,7 @@ MLN_FUNC(static, int, mln_conf_dump_conf_iterate_handler, (mln_rbtree_node_t *no return 0; }) -MLN_FUNC(static, int, mln_conf_dump_domain_iterate_handler, \ - (mln_rbtree_node_t *node, void *udata), (node, udata), \ -{ +MLN_FUNC(static, int, mln_conf_dump_domain_iterate_handler, (mln_rbtree_node_t *node, void *udata), (node, udata), { mln_conf_cmd_t *cc = (mln_conf_cmd_t *)mln_rbtree_node_data_get(node); printf("\t\tCOMMAND [%s]:\n", (char *)(cc->cmd_name->data)); mln_s32_t i; @@ -994,17 +1003,7 @@ MLN_FUNC(static, int, mln_conf_dump_domain_iterate_handler, \ printf("BOOL [%u]\n", ci->val.b); break; case CONF_INT: -#if defined(__WIN32__) - #if defined(i386) || defined(__arm__) printf("INT [%ld]\n", ci->val.i); - #elif defined(__pentiumpro__) - printf("INT [%I64d]\n", ci->val.i); - #else - printf("INT [%lld]\n", ci->val.i); - #endif -#else - printf("INT [%ld]\n", ci->val.i); -#endif break; case CONF_FLOAT: printf("FLOAT [%f]\n", ci->val.f); @@ -1027,7 +1026,7 @@ MLN_CHAIN_FUNC_DEFINE(static inline, \ /* * ipc handlers */ -#if !defined(__WIN32__) +#if !defined(MSVC) MLN_FUNC_VOID(static, void, mln_conf_reload_master_handler, \ (mln_event_t *ev, void *f_ptr, void *buf, mln_u32_t len, void **udata_ptr), \ (ev, f_ptr, buf, len, udata_ptr), \ diff --git a/src/mln_connection.c b/src/mln_connection.c index 17ddbc67..14644630 100644 --- a/src/mln_connection.c +++ b/src/mln_connection.c @@ -2,16 +2,22 @@ /* * Copyright (C) Niklaus F.Schen. */ +#include "mln_connection.h" #include #include +#if !defined(MSVC) #include +#endif #include #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include #include -#include "mln_connection.h" #include "mln_utils.h" #include "mln_file.h" #include "mln_func.h" @@ -35,21 +41,22 @@ mln_tcp_conn_recv_chain_file(int sockfd, \ mln_buf_t *last); static inline int mln_tcp_conn_recv_chain_mem(int sockfd, mln_alloc_t *pool, mln_buf_t *b); -static inline ssize_t +static inline int mln_tcp_conn_send_chain_memory(mln_tcp_conn_t *tc); -static inline ssize_t +static inline int mln_tcp_conn_send_chain_file(mln_tcp_conn_t *tc); -MLN_FUNC(static inline, int, mln_fd_is_nonblock, (int fd), (fd), { -#if defined(__WIN32__) +static inline int mln_fd_is_nonblock(int fd) +{ +#if defined(MSVC) return 0; /* no useful API for getting this flag from socket */ #else int flg = fcntl(fd, F_GETFL, NULL); ASSERT(flg >= 0); return flg & O_NONBLOCK; #endif -}) +} /* @@ -209,7 +216,7 @@ MLN_FUNC(, mln_chain_t *, mln_tcp_conn_tail, (mln_tcp_conn_t *tc, int type), (tc }) MLN_FUNC(, int, mln_tcp_conn_send, (mln_tcp_conn_t *tc), (tc), { - ssize_t n; + int n; if (tc->snd_head == NULL) return M_C_NOTYET; @@ -242,10 +249,10 @@ MLN_FUNC(, int, mln_tcp_conn_send, (mln_tcp_conn_t *tc), (tc), { #if defined(MLN_WRITEV) -MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t *tc), (tc), { +MLN_FUNC(static inline, int, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t *tc), (tc), { mln_chain_t *c; mln_buf_t *b; - ssize_t n, is_done = 0; + int n, is_done = 0; register mln_size_t buf_left_size; int proc_vec, nvec = 256; struct iovec vector[256]; @@ -359,13 +366,14 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t return is_done; }) #else -MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t *tc), (tc), { +static inline int mln_tcp_conn_send_chain_memory(mln_tcp_conn_t *tc) +{ mln_u8_t buf[8192], *p; mln_chain_t *c; mln_buf_t *b; mln_size_t left_size; register mln_size_t buf_left_size; - ssize_t n, is_done = 0; + int n, is_done = 0; if (mln_fd_is_nonblock(tc->sockfd)) { while (1) { @@ -395,7 +403,7 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t if (left_size == sizeof(buf)) return 0; non: -#if defined(__WIN32__) +#if defined(MSVC) n = send(tc->sockfd, (char *)buf, sizeof(buf) - left_size, 0); #else n = send(tc->sockfd, buf, sizeof(buf) - left_size, 0); @@ -458,7 +466,7 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t if (left_size == sizeof(buf)) return 0; blk: -#if defined(__WIN32__) +#if defined(MSVC) n = send(tc->sockfd, (char *)buf, sizeof(buf) - left_size, 0); #else n = send(tc->sockfd, buf, sizeof(buf) - left_size, 0); @@ -489,14 +497,14 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_memory, (mln_tcp_conn_t } return is_done; -}) +} #endif #if defined(MLN_SENDFILE) -MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t *tc), (tc), { +MLN_FUNC(static inline, int, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t *tc), (tc), { int sockfd = tc->sockfd; - ssize_t n, is_done = 0; + int n, is_done = 0; mln_chain_t *c; mln_buf_t *b; mln_size_t buf_left_size; @@ -565,9 +573,10 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t * return is_done; }) #else -MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t *tc), (tc), { +static inline int mln_tcp_conn_send_chain_file(mln_tcp_conn_t *tc) +{ int sockfd = tc->sockfd; - ssize_t n; + int n; mln_buf_t *b; mln_chain_t *c; mln_u8_t buf[4096]; @@ -601,7 +610,7 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t * len = n; non_snd: -#if defined(__WIN32__) +#if defined(MSVC) n = send(sockfd, (char *)buf, len, 0); #else n = send(sockfd, buf, len, 0); @@ -652,7 +661,7 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t * len = n; blk_snd: -#if defined(__WIN32__) +#if defined(MSVC) n = send(sockfd, (char *)buf, len, 0); #else n = send(sockfd, buf, len, 0); @@ -668,7 +677,7 @@ MLN_FUNC(static inline, ssize_t, mln_tcp_conn_send_chain_file, (mln_tcp_conn_t * mln_tcp_conn_append(tc, c, M_C_SENT); return b->last_in_chain == 0? 0: 1; -}) +} #endif MLN_FUNC(static inline, mln_chain_t *, mln_tcp_conn_pop_inline, \ @@ -772,14 +781,12 @@ MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain, \ return n; }) -MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain_file, \ - (int sockfd, mln_alloc_t *pool, mln_buf_t *b, mln_buf_t *last), \ - (sockfd, pool, b, last), \ +static inline int mln_tcp_conn_recv_chain_file(int sockfd, mln_alloc_t *pool, mln_buf_t *b, mln_buf_t *last) { int n; mln_u8_t buf[1024]; -#if defined(__WIN32__) +#if defined(MSVC) n = recv(sockfd, (char *)buf, sizeof(buf), 0); #else n = recv(sockfd, buf, sizeof(buf), 0); @@ -805,10 +812,9 @@ MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain_file, \ } return n; -}) +} -MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain_mem, \ - (int sockfd, mln_alloc_t *pool, mln_buf_t *b), (sockfd, pool, b), \ +static inline int mln_tcp_conn_recv_chain_mem(int sockfd, mln_alloc_t *pool, mln_buf_t *b) { mln_u8ptr_t buf; int n; @@ -819,7 +825,7 @@ MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain_mem, \ return -1; } -#if defined(__WIN32__) +#if defined(MSVC) n = recv(sockfd, (char *)buf, 1024, 0); #else n = recv(sockfd, buf, 1024, 0); @@ -835,5 +841,5 @@ MLN_FUNC(static inline, int, mln_tcp_conn_recv_chain_mem, \ b->last_buf = 1; return n; -}) +} diff --git a/src/mln_event.c b/src/mln_event.c index 3ba75579..faf2381f 100644 --- a/src/mln_event.c +++ b/src/mln_event.c @@ -3,6 +3,7 @@ * Copyright (C) Niklaus F.Schen. */ +#include "mln_event.h" #include #include #include @@ -10,9 +11,8 @@ #include #include #include "mln_utils.h" -#include "mln_event.h" #include "mln_func.h" -#if !defined(__WIN32__) +#if !defined(MSVC) #include #endif @@ -70,11 +70,19 @@ mln_event_fd_timeout_set(mln_event_t *ev, mln_event_desc_t *ed, int timeout_ms); mln_event_desc_t fheap_min = { NULL, NULL, NULL, NULL, M_EV_TM, 0, +#if defined(MSVC) + {0}, +#else {(mln_event_tm_t){NULL, NULL, 0}}, +#endif }; -MLN_FUNC(, mln_event_t *, mln_event_new, (void), (), { +mln_event_t *mln_event_new(void) +{ int rc; +#if defined(MSVC) + struct mln_rbtree_attr rbattr; +#endif mln_event_t *ev; ev = (mln_event_t *)malloc(sizeof(mln_event_t)); if (ev == NULL) { @@ -82,7 +90,16 @@ MLN_FUNC(, mln_event_t *, mln_event_new, (void), (), { } ev->callback = NULL; ev->callback_data = NULL; +#if defined(MSVC) + rbattr.pool = NULL; + rbattr.pool_alloc = NULL; + rbattr.pool_free = NULL; + rbattr.cmp = mln_event_rbtree_fd_cmp; + rbattr.data_free = NULL; + ev->ev_fd_tree = mln_rbtree_new(&rbattr); +#else ev->ev_fd_tree = mln_rbtree_new(NULL); +#endif if (ev->ev_fd_tree == NULL) { goto err1; } @@ -131,15 +148,21 @@ MLN_FUNC(, mln_event_t *, mln_event_new, (void), (), { FD_ZERO(&(ev->err_set)); #endif +#if !defined(MSVC) rc = pthread_mutex_init(&ev->fd_lock, NULL); if (pthread_mutex_init(&ev->timer_lock, NULL) != 0) rc = -1; if (pthread_mutex_init(&ev->cb_lock, NULL) != 0) rc = -1; +#else + rc = 0; +#endif if (rc) { +#if !defined(MSVC) pthread_mutex_destroy(&ev->fd_lock); pthread_mutex_destroy(&ev->timer_lock); pthread_mutex_destroy(&ev->cb_lock); +#endif #if defined(MLN_EPOLL) close(ev->epollfd); #elif defined(MLN_KQUEUE) @@ -159,9 +182,10 @@ MLN_FUNC(, mln_event_t *, mln_event_new, (void), (), { err1: free(ev); return NULL; -}) +} -MLN_FUNC_VOID(, void, mln_event_free, (mln_event_t *ev), (ev), { +void mln_event_free(mln_event_t *ev) +{ if (ev == NULL) return; mln_event_desc_t *ed; mln_fheap_inline_free(ev->ev_fd_timeout_heap, mln_event_fd_timeout_cmp, NULL); @@ -182,11 +206,13 @@ MLN_FUNC_VOID(, void, mln_event_free, (mln_event_t *ev), (ev), { #else /*select do nothing.*/ #endif +#if !defined(MSVC) pthread_mutex_destroy(&ev->fd_lock); pthread_mutex_destroy(&ev->timer_lock); pthread_mutex_destroy(&ev->cb_lock); +#endif free(ev); -}) +} MLN_FUNC_VOID(static inline, void, mln_event_desc_free, (void *data), (data), { if (data == NULL) return; @@ -196,9 +222,7 @@ MLN_FUNC_VOID(static inline, void, mln_event_desc_free, (void *data), (data), { /* * ev_timer */ -MLN_FUNC(, mln_event_timer_t *, mln_event_timer_set, \ - (mln_event_t *event, mln_u32_t msec, void *data, ev_tm_handler tm_handler), \ - (event, msec, data, tm_handler), \ +mln_event_timer_t *mln_event_timer_set(mln_event_t *event, mln_u32_t msec, void *data, ev_tm_handler tm_handler) { struct timeval tv; gettimeofday(&tv, NULL); @@ -222,23 +246,30 @@ MLN_FUNC(, mln_event_timer_t *, mln_event_timer_set, \ free(ed); return NULL; } +#if !defined(MSVC) pthread_mutex_lock(&event->timer_lock); +#endif mln_fheap_inline_insert(event->ev_timer_heap, fn, mln_event_fheap_timer_cmp); +#if !defined(MSVC) pthread_mutex_unlock(&event->timer_lock); +#endif return fn; -}) +} -MLN_FUNC_VOID(, void, mln_event_timer_cancel, \ - (mln_event_t *event, mln_event_timer_t *timer), \ - (event, timer), \ +void mln_event_timer_cancel(mln_event_t *event, mln_event_timer_t *timer) { +#if !defined(MSVC) pthread_mutex_lock(&event->timer_lock); +#endif mln_fheap_inline_delete(event->ev_timer_heap, timer, mln_event_fheap_timer_copy, mln_event_fheap_timer_cmp); mln_fheap_inline_node_free(event->ev_timer_heap, timer, mln_event_desc_free); +#if !defined(MSVC) pthread_mutex_unlock(&event->timer_lock); -}) +#endif +} -MLN_FUNC_VOID(static inline, void, mln_event_timer_process, (mln_event_t *event), (event), { +static inline void mln_event_timer_process(mln_event_t *event) +{ mln_uauto_t now; struct timeval tv; gettimeofday(&tv, NULL); @@ -247,24 +278,36 @@ MLN_FUNC_VOID(static inline, void, mln_event_timer_process, (mln_event_t *event) mln_fheap_node_t *fn; lp: +#if !defined(MSVC) if (pthread_mutex_trylock(&event->timer_lock)) return; +#endif fn = mln_fheap_minimum(event->ev_timer_heap); if (fn == NULL) { +#if !defined(MSVC) pthread_mutex_unlock(&event->timer_lock); +#endif return; } ed = (mln_event_desc_t *)mln_fheap_node_key(fn); if (ed->data.tm.end_tm > now) { +#if !defined(MSVC) pthread_mutex_unlock(&event->timer_lock); +#endif return; } +#if defined(MSVC) + fn = mln_fheap_extract_min(event->ev_timer_heap); +#else fn = mln_fheap_inline_extract_min(event->ev_timer_heap, mln_event_fheap_timer_cmp); +#endif +#if !defined(MSVC) pthread_mutex_unlock(&event->timer_lock); +#endif if (ed->data.tm.handler != NULL) ed->data.tm.handler(event, ed->data.tm.data); @@ -273,38 +316,46 @@ MLN_FUNC_VOID(static inline, void, mln_event_timer_process, (mln_event_t *event) if (!event->is_break) goto lp; -}) +} /* * ev_fd */ -MLN_FUNC_VOID(, void, mln_event_fd_timeout_handler_set, \ - (mln_event_t *event, int fd, void *data, ev_fd_handler timeout_handler), \ - (event, fd, data, timeout_handler), \ +void mln_event_fd_timeout_handler_set(mln_event_t *event, int fd, void *data, ev_fd_handler timeout_handler) { +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif mln_event_desc_t tmp; memset(&tmp, 0, sizeof(tmp)); tmp.type = M_EV_FD; tmp.data.fd.fd = fd; +#if defined(MSVC) + mln_rbtree_node_t *rn = mln_rbtree_search(event->ev_fd_tree, &tmp); +#else mln_rbtree_node_t *rn = mln_rbtree_inline_search(event->ev_fd_tree, &tmp, mln_event_rbtree_fd_cmp); +#endif ASSERT(!mln_rbtree_null(rn, event->ev_fd_tree)); mln_event_desc_t *ed = (mln_event_desc_t *)mln_rbtree_node_data_get(rn); ed->data.fd.timeout_data = data; ed->data.fd.timeout_handler = timeout_handler; +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); -}) +#endif +} -MLN_FUNC(, int, mln_event_fd_set, \ - (mln_event_t *event, int fd, mln_u32_t flag, int timeout_ms, void *data, ev_fd_handler fd_handler), \ - (event, fd, flag, timeout_ms, data, fd_handler), \ +int mln_event_fd_set(mln_event_t *event, int fd, mln_u32_t flag, int timeout_ms, void *data, ev_fd_handler fd_handler) { ASSERT(fd >= 0 && !(flag & ~M_EV_FD_MASK) && flag <= M_EV_CLR && !((flag & M_EV_NONBLOCK) && (flag & M_EV_BLOCK))); +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif if (flag == M_EV_CLR) { mln_event_fd_clr_set(event, fd); +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return 0; } mln_event_desc_t tmp; @@ -312,7 +363,11 @@ MLN_FUNC(, int, mln_event_fd_set, \ tmp.type = M_EV_FD; tmp.data.fd.fd = fd; mln_rbtree_node_t *rn; +#if defined(MSVC) + rn = mln_rbtree_search(event->ev_fd_tree, &tmp); +#else rn = mln_rbtree_inline_search(event->ev_fd_tree, &tmp, mln_event_rbtree_fd_cmp); +#endif if (!mln_rbtree_null(rn, event->ev_fd_tree)) { if (flag & M_EV_APPEND) { if (flag & M_EV_NONBLOCK) mln_event_fd_nonblock_set(fd); @@ -329,7 +384,9 @@ MLN_FUNC(, int, mln_event_fd_set, \ fd_handler, \ 1) < 0) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return -1; } } else { @@ -347,11 +404,15 @@ MLN_FUNC(, int, mln_event_fd_set, \ fd_handler, \ ((mln_event_desc_t *)(rn->data))->data.fd.is_clear?0:1) < 0) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return -1; } } +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return 0; } if (flag & M_EV_NONBLOCK) { @@ -360,12 +421,16 @@ MLN_FUNC(, int, mln_event_fd_set, \ mln_event_fd_block_set(fd); } if (mln_event_fd_normal_set(event, NULL, fd, flag, timeout_ms, data, fd_handler, 0) < 0) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return -1; } +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return 0; -}) +} MLN_FUNC(static inline, int, mln_event_fd_normal_set, \ (mln_event_t *event, mln_event_desc_t *ed, int fd, mln_u32_t flag, \ @@ -428,10 +493,13 @@ MLN_FUNC(static inline, int, mln_event_fd_normal_set, \ other_mark); }) -MLN_FUNC(static inline, int, mln_event_fd_append_set, \ - (mln_event_t *event, mln_event_desc_t *ed, int fd, mln_u32_t flag, \ - int timeout_ms, void *data, ev_fd_handler fd_handler, int other_mark), \ - (event, ed, fd, flag, timeout_ms, data, fd_handler, other_mark), \ +static inline int mln_event_fd_append_set(mln_event_t *event, \ + mln_event_desc_t *ed, \ + int fd, mln_u32_t flag, \ + int timeout_ms, \ + void *data, \ + ev_fd_handler fd_handler, \ + int other_mark) { if (mln_event_fd_timeout_set(event, ed, timeout_ms) < 0) return -1; @@ -581,7 +649,7 @@ MLN_FUNC(static inline, int, mln_event_fd_append_set, \ } #endif return 0; -}) +} MLN_FUNC(static, int, mln_event_fd_timeout_set, \ (mln_event_t *ev, mln_event_desc_t *ed, int timeout_ms), \ @@ -619,15 +687,18 @@ MLN_FUNC(static, int, mln_event_fd_timeout_set, \ return 0; }) -MLN_FUNC_VOID(static inline, void, mln_event_fd_clr_set, \ - (mln_event_t *event, int fd), (event, fd), \ +static inline void mln_event_fd_clr_set(mln_event_t *event, int fd) { mln_event_desc_t tmp, *ed; memset(&tmp, 0, sizeof(tmp)); tmp.type = M_EV_FD; tmp.data.fd.fd = fd; mln_rbtree_node_t *rn; +#if defined(MSVC) + rn = mln_rbtree_search(event->ev_fd_tree, &tmp); +#else rn = mln_rbtree_inline_search(event->ev_fd_tree, &tmp, mln_event_rbtree_fd_cmp); +#endif if (mln_rbtree_null(rn, event->ev_fd_tree)) { return; } @@ -674,26 +745,29 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_clr_set, \ &(event->ev_fd_wait_tail), \ ed); mln_event_desc_free(ed); -}) +} /* * set dispatch callback */ -MLN_FUNC_VOID(, void, mln_event_callback_set, \ - (mln_event_t *ev, dispatch_callback dc, void *dc_data), \ - (ev, dc, dc_data), \ +void mln_event_callback_set(mln_event_t *ev, dispatch_callback dc, void *dc_data) { +#if !defined(MSVC) pthread_mutex_lock(&ev->cb_lock); +#endif ev->callback = dc; ev->callback_data = dc_data; +#if !defined(MSVC) pthread_mutex_unlock(&ev->cb_lock); -}) +#endif +} /* * tools */ -MLN_FUNC_VOID(static inline, void, mln_event_fd_nonblock_set, (int fd), (fd), { -#if defined(__WIN32__) +static inline void mln_event_fd_nonblock_set(int fd) +{ +#if defined(MSVC) u_long opt = 1; ioctlsocket(fd, FIONBIO, &opt); #else @@ -703,10 +777,11 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_nonblock_set, (int fd), (fd), { flg = fcntl(fd, F_SETFL, flg | O_NONBLOCK); ASSERT(flg >= 0); #endif -}) +} -MLN_FUNC_VOID(static inline, void, mln_event_fd_block_set, (int fd), (fd), { -#if defined(__WIN32__) +static inline void mln_event_fd_block_set(int fd) +{ +#if defined(MSVC) u_long opt = 0; ioctlsocket(fd, FIONBIO, &opt); #else @@ -716,7 +791,7 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_block_set, (int fd), (fd), { flg = fcntl(fd, F_SETFL, flg & ~(O_NONBLOCK)); ASSERT(flg >= 0); #endif -}) +} /* * dispatch @@ -1011,7 +1086,8 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { } }) #else -MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { +void mln_event_dispatch(mln_event_t *event) +{ int nfds, fd; mln_event_desc_t *ed; fd_set *rd_set = &(event->rd_set); @@ -1021,6 +1097,7 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { mln_u32_t move; while (1) { +#if !defined(MSVC) if (!pthread_mutex_trylock(&event->cb_lock)) { dispatch_callback cb = event->callback; void *data = event->callback_data; @@ -1031,6 +1108,9 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { pthread_mutex_unlock(&event->cb_lock); } } +#else + if (event->callback != NULL) event->callback(event, event->callback_data); +#endif BREAK_OUT(); mln_event_timer_process(event); BREAK_OUT(); @@ -1045,11 +1125,13 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { FD_ZERO(wr_set); FD_ZERO(err_set); +#if !defined(MSVC) if (pthread_mutex_trylock(&event->fd_lock)) { tm.tv_sec = 0; tm.tv_usec = M_EV_NOLOCK_TIMEOUT_US; select(event->select_fd, rd_set, wr_set, err_set, &tm); } else { +#endif for (ed = event->ev_fd_wait_head; \ ed != NULL; \ ed = ed->next) @@ -1065,18 +1147,20 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { tm.tv_usec = M_EV_TIMEOUT_US; nfds = select(event->select_fd, rd_set, wr_set, err_set, &tm); if (nfds < 0) { -#if !defined(__WIN32__) +#if !defined(MSVC) if (errno == EINTR || errno == ENOMEM) { -#endif pthread_mutex_unlock(&event->fd_lock); continue; -#if !defined(__WIN32__) } else { ASSERT(0); } +#else + continue; #endif } else if (nfds == 0) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif tm.tv_sec = 0; tm.tv_usec = M_EV_NOLOCK_TIMEOUT_US; select(event->select_fd, rd_set, wr_set, err_set, &tm); @@ -1123,13 +1207,16 @@ MLN_FUNC_VOID(, void, mln_event_dispatch, (mln_event_t *event), (event), { ed->data.fd.in_active = 1; } } +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); } +#endif } -}) +} #endif -MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *event), (event), { +static inline void mln_event_active_fd_process(mln_event_t *event) +{ mln_event_desc_t *ed; mln_event_fd_t *ef; ev_fd_handler h; @@ -1137,8 +1224,10 @@ MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *ev int fd; lp: +#if !defined(MSVC) if (pthread_mutex_trylock(&event->fd_lock)) return; +#endif ed = event->ev_fd_active_head; if (ed != NULL) { @@ -1162,9 +1251,13 @@ MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *ev h = ef->rcv_handler; data = ef->rcv_data; fd = ef->fd; +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif h(event, fd, data); +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif } ef->active_flag &= (~M_EV_RECV); } @@ -1174,9 +1267,13 @@ MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *ev h = ef->snd_handler; data = ef->snd_data; fd = ef->fd; +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif h(event, fd, data); +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif } ef->active_flag &= (~M_EV_SEND); } @@ -1186,9 +1283,13 @@ MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *ev h = ef->err_handler; data = ef->err_data; fd = ef->fd; +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif h(event, fd, data); +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif } ef->active_flag &= (~M_EV_ERROR); } @@ -1196,16 +1297,21 @@ MLN_FUNC_VOID(static inline, void, mln_event_active_fd_process, (mln_event_t *ev if (ef->is_clear) mln_event_fd_clr_set(event, ef->fd); +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif if (event->is_break) return; goto lp; } else { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif } -}) +} -MLN_FUNC_VOID(static inline, void, mln_event_fd_timeout_process, (mln_event_t *event), (event), { +static inline void mln_event_fd_timeout_process(mln_event_t *event) +{ mln_u64_t now; struct timeval tv; gettimeofday(&tv, NULL); @@ -1218,12 +1324,16 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_timeout_process, (mln_event_t *e int fd; lp: +#if !defined(MSVC) if (pthread_mutex_trylock(&event->fd_lock)) return; +#endif fn = mln_fheap_minimum(event->ev_fd_timeout_heap); if (fn == NULL) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return; } ed = (mln_event_desc_t *)mln_fheap_node_key(fn); @@ -1235,7 +1345,9 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_timeout_process, (mln_event_t *e ef->in_active = 0; } if (ef->end_us > now) { +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif return; } ef->in_process = 1; @@ -1247,20 +1359,26 @@ MLN_FUNC_VOID(static inline, void, mln_event_fd_timeout_process, (mln_event_t *e h = ed->data.fd.timeout_handler; fd = ed->data.fd.fd; data = ed->data.fd.timeout_data; +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif h(event, fd, data); +#if !defined(MSVC) pthread_mutex_lock(&event->fd_lock); +#endif } ef->in_process = 0; if (ef->is_clear) mln_event_fd_clr_set(event, ef->fd); +#if !defined(MSVC) pthread_mutex_unlock(&event->fd_lock); +#endif if (event->is_break) return; goto lp; -}) +} /* * rbtree functions diff --git a/src/mln_expr.c b/src/mln_expr.c index ccb3a804..ce8d3994 100644 --- a/src/mln_expr.c +++ b/src/mln_expr.c @@ -131,7 +131,8 @@ MLN_FUNC(static, mln_expr_struct_t *, mln_expr_sglq_handler, (mln_lex_t *lex, vo return mln_expr_new(lex, EXPR_TK_STRING); }) -MLN_FUNC(static inline, int, mln_expr_val_init, (mln_expr_val_t *v, mln_expr_struct_t *token), (v, token), { +static inline int mln_expr_val_init(mln_expr_val_t *v, mln_expr_struct_t *token) +{ char num[1024]; mln_size_t len = token->text->len >= sizeof(num)-1? sizeof(num)-1: token->text->len; memcpy(num, token->text->data, len); @@ -140,9 +141,7 @@ MLN_FUNC(static inline, int, mln_expr_val_init, (mln_expr_val_t *v, mln_expr_str switch (token->type) { case EXPR_TK_OCT: v->type = mln_expr_type_int; -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64o", &(v->data.i)); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%llo", &(v->data.i)); #else sscanf(num, "%lo", &(v->data.i)); @@ -150,9 +149,7 @@ MLN_FUNC(static inline, int, mln_expr_val_init, (mln_expr_val_t *v, mln_expr_str break; case EXPR_TK_DEC: v->type = mln_expr_type_int; -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64d", &(v->data.i)); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%lld", &(v->data.i)); #else sscanf(num, "%ld", &(v->data.i)); @@ -160,9 +157,7 @@ MLN_FUNC(static inline, int, mln_expr_val_init, (mln_expr_val_t *v, mln_expr_str break; case EXPR_TK_HEX: v->type = mln_expr_type_int; -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64x", &(v->data.i)); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%llx", &(v->data.i)); #else sscanf(num, "%lx", &(v->data.i)); @@ -193,7 +188,7 @@ MLN_FUNC(static inline, int, mln_expr_val_init, (mln_expr_val_t *v, mln_expr_str v->free = NULL; return 0; -}) +} MLN_FUNC(, mln_expr_val_t *, mln_expr_val_new, (mln_expr_typ_t type, void *data, mln_expr_udata_free free), (type, data, free), { mln_expr_val_t *ev; diff --git a/src/mln_fheap.c b/src/mln_fheap.c index d6da4e6c..f40662b9 100644 --- a/src/mln_fheap.c +++ b/src/mln_fheap.c @@ -66,23 +66,67 @@ MLN_FUNC(, mln_fheap_t *, mln_fheap_new_fast, \ }) MLN_FUNC_VOID(, void, mln_fheap_insert, (mln_fheap_t *fh, mln_fheap_node_t *fn), (fh, fn), { - mln_fheap_inline_insert(fh, fn, NULL); + mln_fheap_add_child(&(fh->root_list), fn); + fn->parent = NULL; + if (fh->min == NULL) { + fh->min = fn; + } else { + if (!fh->cmp(fn->key, fh->min->key)) + fh->min = fn; + } + ++(fh->num); }) MLN_FUNC(, mln_fheap_node_t *, mln_fheap_extract_min, (mln_fheap_t *fh), (fh), { - return mln_fheap_inline_extract_min(fh, NULL); + mln_fheap_node_t *z = fh->min; + if (z != NULL) { + mln_fheap_node_t *child; + while ((child = mln_fheap_remove_child(&(z->child))) != NULL) { + mln_fheap_add_child(&(fh->root_list), child); + child->parent = NULL; + } + mln_fheap_node_t *right = z->right; + mln_fheap_del_child(&(fh->root_list), z); + if (z == right) { + fh->min = NULL; + } else { + fh->min = right; + mln_fheap_consolidate(fh, fh->cmp); + } + --((fh)->num); + } + return z; }) MLN_FUNC(, int, mln_fheap_decrease_key, (mln_fheap_t *fh, mln_fheap_node_t *node, void *key), (fh, node, key), { - return mln_fheap_inline_decrease_key(fh, node, key, NULL, NULL); + if (!fh->cmp(node->key, key)) { + return -1; + } + fh->copy(node->key, key); + mln_fheap_node_t *y = node->parent; + if (y != NULL && !fh->cmp(node->key, y->key)) { + mln_fheap_cut(fh, node, y); + mln_fheap_cascading_cut(fh, y); + } + if (node != fh->min && !fh->cmp(node->key, fh->min->key)) + fh->min = node; + return 0; }) MLN_FUNC_VOID(, void, mln_fheap_delete, (mln_fheap_t *fh, mln_fheap_node_t *node), (fh, node), { - mln_fheap_inline_delete(fh, node, NULL, NULL); + mln_fheap_decrease_key(fh, node, fh->min_val); + mln_fheap_extract_min(fh); }) MLN_FUNC_VOID(, void, mln_fheap_free, (mln_fheap_t *fh), (fh), { - mln_fheap_inline_free(fh, NULL, NULL); + if (fh == NULL) return; + + mln_fheap_node_t *fn; + while ((fn = mln_fheap_extract_min(fh)) != NULL) { + mln_fheap_node_free(fh, fn); + } + if (fh->pool != NULL) fh->pool_free(fh); + else free(fh); }) /*mln_fheap_node_t*/ @@ -107,6 +151,27 @@ MLN_FUNC(, mln_fheap_node_t *, mln_fheap_node_new, (mln_fheap_t *fh, void *key), }) MLN_FUNC_VOID(, void, mln_fheap_node_free, (mln_fheap_t *fh, mln_fheap_node_t *fn), (fh, fn), { - mln_fheap_inline_node_free(fh, fn, NULL); + if (fn == NULL) return; + + if (fh->key_free != NULL && fn->key != NULL) + fh->key_free(fn->key); + if (!fn->nofree) { + if (fh->pool != NULL) fh->pool_free(fn); + else free(fn); + } }) +#if defined(MSVC) +mln_fheap_node_t *mln_fheap_node_init(mln_fheap_node_t *fn, void *k) +{ + fn->key = k; + fn->parent = NULL; + fn->child = NULL; + fn->right = fn->left = fn; + fn->degree = 0; + fn->nofree = 1; + fn->mark = FHEAP_FALSE; + return fn; +} +#endif + diff --git a/src/mln_file.c b/src/mln_file.c index 9db16654..7db5e07b 100644 --- a/src/mln_file.c +++ b/src/mln_file.c @@ -12,7 +12,11 @@ #include #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif MLN_CHAIN_FUNC_DECLARE(static inline, \ reg_file, \ @@ -69,7 +73,7 @@ MLN_FUNC_VOID(, void, mln_fileset_destroy, (mln_fileset_t *fs), (fs), { }) -MLN_FUNC(, mln_file_t *, mln_file_open, (mln_fileset_t *fs, const char *filepath), (fs, filepath), { +MLN_FUNC(, mln_file_t *, mln_file_open, (mln_fileset_t *fs, char *filepath), (fs, filepath), { mln_rbtree_node_t *rn; mln_file_t *f, tmpf; mln_string_t path; @@ -162,7 +166,8 @@ MLN_FUNC_VOID(static, void, mln_file_free, (void *pfile), (pfile), { mln_alloc_free(f); }) -MLN_FUNC(, mln_file_t *, mln_file_tmp_open, (mln_alloc_t *pool), (pool), { +mln_file_t *mln_file_tmp_open(mln_alloc_t *pool) +{ char dir_path[512] = {0}; char tmp_path[1024] = {0}; struct timeval now; @@ -170,7 +175,7 @@ MLN_FUNC(, mln_file_t *, mln_file_tmp_open, (mln_alloc_t *pool), (pool), { mln_file_t *f; snprintf(dir_path, sizeof(dir_path)-1, "%s", mln_path_tmpfile()); -#if defined(__WIN32__) +#if defined(MSVC) if (mkdir(dir_path) < 0) { #else if (mkdir(dir_path, S_IRWXU) < 0) { @@ -197,7 +202,11 @@ MLN_FUNC(, mln_file_t *, mln_file_tmp_open, (mln_alloc_t *pool), (pool), { gettimeofday(&now, NULL); suffix = now.tv_sec * 1000000 + now.tv_usec; snprintf(tmp_path, sizeof(tmp_path)-1, "%s/mln_tmp_%lu.tmp", dir_path, suffix); +#if defined(MSVC) + f->fd = open(tmp_path, O_RDWR|O_CREAT|O_EXCL, _S_IREAD|_S_IWRITE|_S_IEXEC); +#else f->fd = open(tmp_path, O_RDWR|O_CREAT|O_EXCL, S_IRWXU); +#endif if (f->fd < 0) { if (errno == EEXIST) { memset(tmp_path, 0, sizeof(tmp_path)); @@ -210,7 +219,7 @@ MLN_FUNC(, mln_file_t *, mln_file_tmp_open, (mln_alloc_t *pool), (pool), { f->mtime = f->ctime = f->atime = now.tv_sec; return f; -}) +} MLN_CHAIN_FUNC_DEFINE(static inline, \ diff --git a/src/mln_fork.c b/src/mln_fork.c index b1ed6cac..b301707d 100644 --- a/src/mln_fork.c +++ b/src/mln_fork.c @@ -2,7 +2,7 @@ /* * Copyright (C) Niklaus F.Schen. */ -#if !defined(__WIN32__) +#if !defined(MSVC) #include #include #include diff --git a/src/mln_framework.c b/src/mln_framework.c index 7371f832..e0bc9570 100644 --- a/src/mln_framework.c +++ b/src/mln_framework.c @@ -2,23 +2,23 @@ /* * Copyright (C) Niklaus F.Schen. */ +#include "mln_framework.h" #include "mln_thread.h" #include "mln_fork.h" -#include -#include -#include -#include -#include #include "mln_types.h" #include "mln_tools.h" #include "mln_log.h" #include "mln_string.h" #include "mln_conf.h" -#include "mln_framework.h" #include "mln_trace.h" #include "mln_func.h" +#include +#include +#include +#include +#if !defined(MSVC) +#include -#if !defined(__WIN32__) static int mln_master_trace_init(mln_lang_ctx_t *ctx); static void mln_worker_routine(struct mln_framework_attr *attr); static void mln_master_routine(struct mln_framework_attr *attr); @@ -30,7 +30,8 @@ static mln_event_t *_ev = NULL; #endif -MLN_FUNC(, int, mln_framework_init, (struct mln_framework_attr *attr), (attr), { +int mln_framework_init(struct mln_framework_attr *attr) +{ /*Init configurations*/ if (mln_conf_load() < 0) { return -1; @@ -43,7 +44,7 @@ MLN_FUNC(, int, mln_framework_init, (struct mln_framework_attr *attr), (attr), { if (attr->global_init != NULL && attr->global_init() < 0) return -1; -#if !defined(__WIN32__) +#if !defined(MSVC) if (mln_get_framework_status()) { if (mln_boot_params(attr->argc, attr->argv) < 0) return -1; @@ -78,13 +79,13 @@ MLN_FUNC(, int, mln_framework_init, (struct mln_framework_attr *attr), (attr), { } else { #endif if (mln_log_init(NULL) < 0) return -1; -#if !defined(__WIN32__) +#if !defined(MSVC) } #endif return 0; -}) +} -#if !defined(__WIN32__) +#if !defined(MSVC) MLN_FUNC_VOID(static, void, mln_master_routine, (struct mln_framework_attr *attr), (attr), { mln_event_t *ev = mln_event_new(); if (ev == NULL) exit(1); diff --git a/src/mln_func.c b/src/mln_func.c index 84fd43cc..ef17c4b0 100644 --- a/src/mln_func.c +++ b/src/mln_func.c @@ -32,7 +32,7 @@ mln_func_exit_cb_t mln_func_exit_callback_get(void) #include "mln_rc.h" #include #include -#if defined(__WIN32__) +#if defined(MSVC) #include #include #else @@ -85,7 +85,7 @@ static void mln_notice(char *suffix) freeaddrinfo(host_result); return; } -#if defined(__WIN32__) +#if defined(MSVC) u_long opt = 1; ioctlsocket(fd, FIONBIO, &opt); #else @@ -96,7 +96,7 @@ static void mln_notice(char *suffix) freeaddrinfo(host_result); } usleep(400000); -#if defined(__WIN32__) +#if defined(MSVC) n = send(fd, (char *)buf, sizeof(buf) - 1, 0); #else n = send(fd, buf, sizeof(buf) - 1, 0); diff --git a/src/mln_http.c b/src/mln_http.c index 695ac9cd..798b476e 100644 --- a/src/mln_http.c +++ b/src/mln_http.c @@ -3,10 +3,12 @@ * Copyright (C) Niklaus F.Schen. */ +#include "mln_http.h" #include +#if !defined(MSVC) #include +#endif #include "mln_types.h" -#include "mln_http.h" #include "mln_func.h" @@ -38,7 +40,11 @@ mln_http_generate_uri(struct mln_http_chain_s *hc); static int mln_http_generate_fields_hash_iterate_handler(mln_hash_t *h, void *key, void *val, void *data); static inline int +#if defined(MSVC) +mln_http_generate_write(struct mln_http_chain_s *hc, mln_u8ptr_t buf, mln_size_t size); +#else mln_http_generate_write(struct mln_http_chain_s *hc, void *buf, mln_size_t size); +#endif static inline int mln_http_generate_set_last_in_chain(struct mln_http_chain_s *hc); @@ -601,9 +607,11 @@ MLN_FUNC(static inline, int, mln_http_generate_set_last_in_chain, \ return M_HTTP_RET_OK; }) -MLN_FUNC(static inline, int, mln_http_generate_write, \ - (struct mln_http_chain_s *hc, void *buf, mln_size_t size), \ - (hc, buf, size), \ +#if defined(MSVC) +static inline int mln_http_generate_write(struct mln_http_chain_s *hc, mln_u8ptr_t buf, mln_size_t size) +#else +static inline int mln_http_generate_write(struct mln_http_chain_s *hc, void *buf, mln_size_t size) +#endif { mln_buf_t *cur; mln_http_t *http = hc->http; @@ -668,7 +676,7 @@ MLN_FUNC(static inline, int, mln_http_generate_write, \ } return M_HTTP_RET_OK; -}) +} MLN_FUNC(static inline, int, mln_http_generate_version, \ (struct mln_http_chain_s *hc), (hc), \ @@ -998,24 +1006,37 @@ MLN_FUNC(static, int, mln_http_hash_cmp, \ /* * dump */ -MLN_FUNC_VOID(, void, mln_http_dump, (mln_http_t *http), (http), { +void mln_http_dump(mln_http_t *http) +{ int rc = 1; printf("HTTP Dump:\n"); if (http == NULL) return; if (http->uri != NULL) { printf("\tURI:[");fflush(stdout); +#if defined(MSVC) + rc = write(_fileno(stdout), http->uri->data, http->uri->len); +#else rc = write(STDOUT_FILENO, http->uri->data, http->uri->len); +#endif printf("]\n"); } if (http->args != NULL) { printf("\tARGS:[");fflush(stdout); +#if defined(MSVC) + rc = write(_fileno(stdout), http->args->data, http->args->len); +#else rc = write(STDOUT_FILENO, http->args->data, http->args->len); +#endif printf("]\n"); } if (http->response_msg != NULL) { printf("\tRESPONSE_MSG:[");fflush(stdout); +#if defined(MSVC) + rc = write(_fileno(stdout), http->response_msg->data, http->response_msg->len); +#else rc = write(STDOUT_FILENO, http->response_msg->data, http->response_msg->len); +#endif printf("]\n"); } printf("\tstatus_code:%u\n", http->status); @@ -1025,7 +1046,7 @@ MLN_FUNC_VOID(, void, mln_http_dump, (mln_http_t *http), (http), { printf("\tfields:\n"); if (rc <= 0) rc = 1;/*do nothing*/ mln_hash_iterate(http->header_fields, mln_http_dump_iterate_handler, NULL); -}) +} MLN_FUNC(static, int, mln_http_dump_iterate_handler, \ (mln_hash_t *h, void *key, void *val, void *data), (h, key, val, data), \ diff --git a/src/mln_iothread.c b/src/mln_iothread.c index ba218620..7ccc9283 100644 --- a/src/mln_iothread.c +++ b/src/mln_iothread.c @@ -2,6 +2,7 @@ /* * Copyright (C) Niklaus F.Schen. */ +#if !defined(MSVC) #include "mln_iothread.h" #include #include @@ -10,11 +11,7 @@ #include #include #include "mln_func.h" -#if defined(__WIN32__) -#include -#else #include -#endif static inline void mln_iothread_fd_nonblock_set(int fd); static inline mln_iothread_msg_t *mln_iothread_msg_new(mln_u32_t type, void *data, int feedback); @@ -207,11 +204,9 @@ MLN_FUNC_VOID(static inline, void, mln_iothread_msg_free, (mln_iothread_msg_t *m }) MLN_FUNC_VOID(static inline, void, mln_iothread_fd_nonblock_set, (int fd), (fd), { -#if defined(__WIN32__) - u_long opt = 1; - ioctlsocket(fd, FIONBIO, &opt); -#else int flg = fcntl(fd, F_GETFL, NULL); fcntl(fd, F_SETFL, flg | O_NONBLOCK); -#endif }) + +#endif + diff --git a/src/mln_ipc.c b/src/mln_ipc.c index 5fac9a1a..a34dd151 100644 --- a/src/mln_ipc.c +++ b/src/mln_ipc.c @@ -2,7 +2,7 @@ /* * Copyright (C) Niklaus F.Schen. */ -#if !defined(__WIN32__) +#if !defined(MSVC) #include "mln_ipc.h" #include "mln_func.h" diff --git a/src/mln_json.c b/src/mln_json.c index 513b6d01..feff256e 100644 --- a/src/mln_json.c +++ b/src/mln_json.c @@ -61,11 +61,22 @@ MLN_FUNC_VOID(static inline, void, mln_json_kv_free, (mln_json_kv_t *kv), (kv), }) -MLN_FUNC(static inline, int, __mln_json_obj_init, (mln_json_t *j), (j), { +static inline int __mln_json_obj_init(mln_json_t *j) +{ j->type = M_JSON_OBJECT; +#if defined(MSVC) + struct mln_rbtree_attr rbattr; + rbattr.pool = NULL; + rbattr.pool_alloc = NULL; + rbattr.pool_free = NULL; + rbattr.cmp = (rbtree_cmp)mln_json_kv_cmp; + rbattr.data_free = (rbtree_free_data)mln_json_kv_free; + if ((j->data.m_j_obj = mln_rbtree_new(&rbattr)) == NULL) return -1; +#else if ((j->data.m_j_obj = mln_rbtree_new(NULL)) == NULL) return -1; +#endif return 0; -}) +} MLN_FUNC(, int, mln_json_obj_init, (mln_json_t *j), (j), { return __mln_json_obj_init(j); @@ -77,8 +88,7 @@ MLN_FUNC(, int, mln_json_obj_update, \ return __mln_json_obj_update(j, key, val); }) -MLN_FUNC(static inline, int, __mln_json_obj_update, \ - (mln_json_t *j, mln_json_t *key, mln_json_t *val), (j, key, val), \ +static inline int __mln_json_obj_update(mln_json_t *j, mln_json_t *key, mln_json_t *val) { mln_rbtree_node_t *rn; mln_json_kv_t kv, *pkv; @@ -86,7 +96,11 @@ MLN_FUNC(static inline, int, __mln_json_obj_update, \ if (!mln_json_is_string(key) || !mln_json_is_object(j)) return -1; kv.key = *key; +#if defined(MSVC) + rn = mln_rbtree_search(mln_json_object_data_get(j), &kv); +#else rn = mln_rbtree_inline_search(mln_json_object_data_get(j), &kv, mln_json_kv_cmp); +#endif if (mln_rbtree_null(rn, mln_json_object_data_get(j))) { pkv = (mln_json_kv_t *)malloc(sizeof(mln_json_kv_t)); if (pkv == NULL) return -1; @@ -94,7 +108,11 @@ MLN_FUNC(static inline, int, __mln_json_obj_update, \ pkv->key = *key; pkv->val = *val; rn = mln_rbtree_node_init(&pkv->node, pkv); +#if defined(MSVC) + mln_rbtree_insert(mln_json_object_data_get(j), rn); +#else mln_rbtree_inline_insert(mln_json_object_data_get(j), rn, mln_json_kv_cmp); +#endif } else { pkv = (mln_json_kv_t *)mln_rbtree_node_data_get(rn); mln_json_destroy(&(pkv->key)); @@ -104,36 +122,50 @@ MLN_FUNC(static inline, int, __mln_json_obj_update, \ } return 0; -}) +} -MLN_FUNC(, mln_json_t *, mln_json_obj_search, (mln_json_t *j, mln_string_t *key), (j, key), { +mln_json_t *mln_json_obj_search(mln_json_t *j, mln_string_t *key) +{ if (!mln_json_is_object(j)) return NULL; mln_rbtree_node_t *rn; mln_json_kv_t kv; mln_json_string_init(&(kv.key), key); +#if defined(MSVC) + rn = mln_rbtree_search(mln_json_object_data_get(j), &kv); +#else rn = mln_rbtree_inline_search(mln_json_object_data_get(j), &kv, mln_json_kv_cmp); +#endif if (mln_rbtree_null(rn, mln_json_object_data_get(j))) { return NULL; } return &(((mln_json_kv_t *)mln_rbtree_node_data_get(rn))->val); -}) +} -MLN_FUNC_VOID(, void, mln_json_obj_remove, (mln_json_t *j, mln_string_t *key), (j, key), { +void mln_json_obj_remove(mln_json_t *j, mln_string_t *key) +{ if (!mln_json_is_object(j)) return; mln_json_kv_t kv; mln_rbtree_node_t *rn; mln_json_string_init(&(kv.key), key); +#if defined(MSVC) + rn = mln_rbtree_search(mln_json_object_data_get(j), &kv); +#else rn = mln_rbtree_inline_search(mln_json_object_data_get(j), &kv, mln_json_kv_cmp); +#endif if (mln_rbtree_null(rn, mln_json_object_data_get(j))) { return; } mln_rbtree_delete(mln_json_object_data_get(j), rn); +#if defined(MSVC) + mln_rbtree_node_free(mln_json_object_data_get(j), rn); +#else mln_rbtree_inline_node_free(mln_json_object_data_get(j), rn, mln_json_kv_free); -}) +#endif +} MLN_FUNC(static inline, int, __mln_json_array_init, (mln_json_t *j), (j), { @@ -196,12 +228,17 @@ MLN_FUNC_VOID(, void, mln_json_array_remove, (mln_json_t *j, mln_uauto_t index), mln_array_pop(mln_json_array_data_get(j)); }) -MLN_FUNC_VOID(, void, mln_json_destroy, (mln_json_t *j), (j), { +void mln_json_destroy(mln_json_t *j) +{ if (j == NULL) return; switch (j->type) { case M_JSON_OBJECT: +#if defined(MSVC) + mln_rbtree_free(mln_json_object_data_get(j)); +#else mln_rbtree_inline_free(mln_json_object_data_get(j), mln_json_kv_free); +#endif break; case M_JSON_ARRAY: mln_array_free(mln_json_array_data_get(j)); @@ -219,7 +256,7 @@ MLN_FUNC_VOID(, void, mln_json_destroy, (mln_json_t *j), (j), { default: break; } -}) +} /* @@ -245,7 +282,7 @@ MLN_FUNC_VOID(, void, mln_json_dump, \ case M_JSON_ARRAY: { printf("type:array\n"); - mln_json_t *elem = mln_array_elts(mln_json_array_data_get(j)); + mln_json_t *elem = (mln_json_t *)mln_array_elts(mln_json_array_data_get(j)); mln_json_t *end = elem + mln_array_nelts(mln_json_array_data_get(j)); for (; elem < end; ++elem) { mln_json_dump(elem, space, "Array member:"); @@ -859,14 +896,20 @@ MLN_FUNC(static inline, int, mln_json_write_byte, \ return 0; }) +#if defined(MSVC) +static inline void mln_json_write_back(mln_size_t *off, mln_size_t n) +{ + *off = *off < n? 0: *off - n; +} +#else #define mln_json_write_back(_off, _n) ({\ mln_size_t o = (_off), n = (_n);\ (_off) = o < n? 0: (o - n);\ }) +#endif -MLN_FUNC(static inline, int, mln_json_write_content, \ - (mln_json_t *j, mln_s8ptr_t *buf, mln_size_t *size, mln_size_t *off), \ - (j, buf, size, off), \ +static inline int +mln_json_write_content(mln_json_t *j, mln_s8ptr_t *buf, mln_size_t *size, mln_size_t *off) { if (j == NULL) return 0; @@ -882,7 +925,11 @@ MLN_FUNC(static inline, int, mln_json_write_content, \ tmp.ptr3 = off; save = *off; mln_rbtree_iterate(mln_json_object_data_get(j), mln_json_write_content_obj_iterate_handler, &tmp); +#if defined(MSVC) + if (save < *off) mln_json_write_back(off, 1); +#else if (save < *off) mln_json_write_back(*off, 1); +#endif if (mln_json_write_byte(buf, size, off, (mln_u8ptr_t)"}", 1) < 0) return -1; break; } @@ -897,7 +944,11 @@ MLN_FUNC(static inline, int, mln_json_write_content, \ if (mln_json_write_content(el, buf, size, off) < 0) return-1; if (mln_json_write_byte(buf, size, off, (mln_u8ptr_t)",", 1) < 0) return -1; } +#if defined(MSVC) + if (save < *off) mln_json_write_back(off, 1); +#else if (save < *off) mln_json_write_back(*off, 1); +#endif if (mln_json_write_byte(buf, size, off, (mln_u8ptr_t)"]", 1) < 0) return -1; break; } @@ -925,9 +976,7 @@ MLN_FUNC(static inline, int, mln_json_write_content, \ char tmp[M_JSON_BUFLEN]; mln_s64_t i = (mln_s64_t)(j->data.m_j_number); if (i == j->data.m_j_number) -#if defined(__WIN32__) && defined(__pentiumpro__) - n = snprintf(tmp, sizeof(tmp) - 1, "%I64d", i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) n = snprintf(tmp, sizeof(tmp) - 1, "%lld", i); #else n = snprintf(tmp, sizeof(tmp) - 1, "%ld", i); @@ -951,7 +1000,7 @@ MLN_FUNC(static inline, int, mln_json_write_content, \ } return 0; -}) +} MLN_FUNC(static, int, mln_json_write_content_obj_iterate_handler, \ (mln_rbtree_node_t *node, void *data), (node, data), \ @@ -1360,3 +1409,26 @@ MLN_FUNC(static inline, int, mln_json_object_iterator, \ return it(&(kv->key), &(kv->val), tmp->ptr2); }) +#if defined(MSVC) +void mln_json_reset(mln_json_t *j) +{ + mln_json_destroy(j); + mln_json_none_type_set(j); +} + +int mln_json_array_iterate(mln_json_t *j, mln_json_array_iterator_t it, void *data) +{ + mln_json_t *end; + int rc = -1; + mln_array_t *a = mln_json_array_data_get(j); + if (mln_json_is_array(j)) { + j = (mln_json_t *)mln_array_elts(a); + end = j + mln_array_nelts(a); + for (; j < end; ++j) { + if ((rc = it(j, data))) break; + } + } + return rc; +} +#endif + diff --git a/src/mln_lang.c b/src/mln_lang.c index e3f991e7..31800f7a 100644 --- a/src/mln_lang.c +++ b/src/mln_lang.c @@ -3,14 +3,18 @@ * Copyright (C) Niklaus F.Schen. */ #include "mln_lang.h" -#if !defined(__WIN32__) +#if !defined(MSVC) #include #include #include #endif #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include #include "mln_lex.h" #include "mln_log.h" @@ -24,7 +28,7 @@ #include "mln_lang_array.h" #include "mln_path.h" #include "mln_func.h" -#if defined(__WIN32__) +#if defined(MSVC) #include #else #include @@ -305,6 +309,147 @@ static inline int mln_lang_ctx_pipe_elem_init(mln_lang_ctx_pipe_elem_t *pe, int static void mln_lang_ctx_pipe_elem_destroy(mln_lang_ctx_pipe_elem_t *pe); #define mln_lang_stack_top(ctx) ((ctx)->run_stack_top) +#if defined(MSVC) +static inline mln_lang_stack_node_t * +mln_lang_stack_push(mln_lang_ctx_t *ctx, mln_lang_stack_node_type_t _type, void *_data) +{ + mln_lang_stack_node_t *n = NULL; + if (ctx->run_stack_top == NULL) { + n = ctx->run_stack_top = ctx->run_stack; + } else if (ctx->run_stack_top - ctx->run_stack < M_LANG_RUN_STACK_LEN) { + n = ++(ctx->run_stack_top); + } + if (n != NULL) { + n->type = _type; + n->pos = NULL; + n->ret_var = n->ret_var2 = NULL; + n->step = 0; + n->call = 0; + switch (_type) { + case M_LSNT_STM: + n->data.stm = (mln_lang_stm_t *)_data; + break; + case M_LSNT_FUNCDEF: + n->data.funcdef = (mln_lang_funcdef_t *)_data; + break; + case M_LSNT_SET: + n->data.set = (mln_lang_set_t *)_data; + break; + case M_LSNT_SETSTM: + n->data.set_stm = (mln_lang_setstm_t *)_data; + break; + case M_LSNT_BLOCK: + n->data.block = (mln_lang_block_t *)_data; + break; + case M_LSNT_WHILE: + n->data.w = (mln_lang_while_t *)_data; + break; + case M_LSNT_SWITCH: + n->data.sw = (mln_lang_switch_t *)_data; + break; + case M_LSNT_SWITCHSTM: + n->data.sw_stm = (mln_lang_switchstm_t *)_data; + break; + case M_LSNT_FOR: + n->data.f = (mln_lang_for_t *)_data; + break; + case M_LSNT_IF: + n->data.i = (mln_lang_if_t *)_data; + break; + case M_LSNT_EXP: + n->data.exp = (mln_lang_exp_t *)_data; + break; + case M_LSNT_ASSIGN: + n->data.assign = (mln_lang_assign_t *)_data; + break; + case M_LSNT_LOGICLOW: + n->data.logiclow = (mln_lang_logiclow_t *)_data; + break; + case M_LSNT_LOGICHIGH: + n->data.logichigh = (mln_lang_logichigh_t *)_data; + n->pos = n->data.logichigh; + break; + case M_LSNT_RELATIVELOW: + n->data.relativelow = (mln_lang_relativelow_t *)_data; + n->pos = n->data.relativelow; + break; + case M_LSNT_RELATIVEHIGH: + n->data.relativehigh = (mln_lang_relativehigh_t *)_data; + n->pos = n->data.relativehigh; + break; + case M_LSNT_MOVE: + n->data.move = (mln_lang_move_t *)_data; + n->pos = n->data.move; + break; + case M_LSNT_ADDSUB: + n->data.addsub = (mln_lang_addsub_t *)_data; + n->pos = n->data.addsub; + break; + case M_LSNT_MULDIV: + n->data.muldiv = (mln_lang_muldiv_t *)_data; + n->pos = n->data.muldiv; + break; + case M_LSNT_NOT: + n->data.not = (mln_lang_not_t *)_data; + n->pos = n->data.not; + break; + case M_LSNT_SUFFIX: + n->data.suffix = (mln_lang_suffix_t *)_data; + break; + case M_LSNT_LOCATE: + n->data.locate = (mln_lang_locate_t *)_data; + break; + case M_LSNT_SPEC: + n->data.spec = (mln_lang_spec_t *)_data; + break; + case M_LSNT_FACTOR: + n->data.factor = (mln_lang_factor_t *)_data; + break; + default: /* M_LSNT_ELEMLIST */ + n->data.elemlist = (mln_lang_elemlist_t *)_data; + break; + } + } + return n; +} + +static inline mln_lang_stack_node_t *mln_lang_stack_pop(mln_lang_ctx_t *ctx) +{ + mln_lang_stack_node_t *n = ctx->run_stack_top; + if (n != NULL && --ctx->run_stack_top < ctx->run_stack) { + ctx->run_stack_top = NULL; + } + return n; +} + +static inline void mln_lang_stack_withdraw(mln_lang_ctx_t *ctx) +{ + if (ctx->run_stack_top == NULL) { + ctx->run_stack_top = ctx->run_stack; + } else if (ctx->run_stack_top - ctx->run_stack < M_LANG_RUN_STACK_LEN) { + ++(ctx->run_stack_top); + } else { + ASSERT(0); + } +} + +static inline void mln_lang_stack_node_free(void *data) +{ + mln_lang_stack_node_t *n = data; + if (n != NULL) { + if (n->ret_var != NULL) { + __mln_lang_var_free(n->ret_var); + n->ret_var = NULL; + } + if (n->ret_var2 != NULL) { + __mln_lang_var_free(n->ret_var2); + n->ret_var2 = NULL; + } + } +} + +#else + #define mln_lang_stack_push(ctx,_type,_data) ({\ mln_lang_stack_node_t *n = NULL;\ if ((ctx)->run_stack_top == NULL) {\ @@ -421,11 +566,6 @@ static void mln_lang_ctx_pipe_elem_destroy(mln_lang_ctx_pipe_elem_t *pe); ASSERT(0);\ }\ }) -#define mln_lang_stack_popuntil(ctx); \ - while (mln_lang_stack_top(ctx) != NULL && mln_lang_stack_top(ctx)->step == M_LANG_STEP_OUT) {\ - mln_lang_stack_node_free(mln_lang_stack_pop(ctx));\ - } - #define mln_lang_stack_node_free(_data) ({\ mln_lang_stack_node_t *n = (_data);\ if (n != NULL) {\ @@ -439,11 +579,77 @@ static void mln_lang_ctx_pipe_elem_destroy(mln_lang_ctx_pipe_elem_t *pe); }\ }\ }) +#endif + +#define mln_lang_stack_popuntil(ctx); \ + while (mln_lang_stack_top(ctx) != NULL && mln_lang_stack_top(ctx)->step == M_LANG_STEP_OUT) {\ + mln_lang_stack_node_free(mln_lang_stack_pop(ctx));\ + } #define mln_lang_scope_top(_ctx) ((_ctx)->scope_top) #define mln_lang_scope_base(_ctx) ((_ctx)->scopes) +#if defined(MSVC) +static inline int mln_lang_scope_in(mln_lang_ctx_t *ctx, mln_lang_scope_t *s) +{ + if (s != NULL && s - ctx->scopes <= M_LANG_SCOPE_LEN) { + return 1; + } + return 0; +} + +static inline mln_lang_scope_t * +mln_lang_scope_push(mln_lang_ctx_t *ctx, \ + mln_string_t *name, \ + mln_lang_scope_type_t type, \ + mln_lang_stack_node_t *cur_stack, \ + mln_lang_stm_t *entry_stm) +{ + mln_lang_scope_t *s = NULL, *last; + last = ctx->scope_top; + if (last == NULL) { + s = ctx->scope_top = ctx->scopes; + } else if (last - ctx->scopes < M_LANG_SCOPE_LEN) { + s = (++ctx->scope_top); + } + if (s != NULL) { + s->ctx = ctx; + s->type = type; + if (name != NULL) { + s->name = mln_string_ref(name); + } else { + s->name = NULL; + } + s->cur_stack = cur_stack; + s->entry = entry_stm; + s->layer = last == NULL? 1: last->layer + 1; + s->sym_head = s->sym_tail = NULL; + } + return s; +} + +static inline void mln_lang_scope_pop(mln_lang_ctx_t *ctx) +{ + mln_lang_scope_t *s = ctx->scope_top; + mln_lang_symbol_node_t *sym; + if (s != NULL) { + if (s->name != NULL) { + mln_string_free(s->name); + } + while ((sym = s->sym_head) != NULL) { + mln_lang_sym_scope_chain_del(&s->sym_head, &s->sym_tail, sym); + mln_lang_sym_chain_del(&sym->bucket->head, &sym->bucket->tail, sym); + mln_lang_symbol_node_free(sym); + } + if (--ctx->scope_top < ctx->scopes) { + ctx->scope_top = NULL; + } + } +} + +#else + #define mln_lang_scope_in(_ctx,_scope) ({\ int in = 0;\ mln_lang_scope_t *s = (_scope);\ @@ -494,6 +700,7 @@ static void mln_lang_ctx_pipe_elem_destroy(mln_lang_ctx_pipe_elem_t *pe); }\ }\ }) +#endif mln_lang_method_t *mln_lang_methods[] = { @@ -536,9 +743,7 @@ mln_lang_stack_handler mln_lang_stack_map[] = { mln_lang_stack_handler_elemlist }; -MLN_FUNC(, mln_lang_t *, mln_lang_new, \ - (mln_event_t *ev, mln_lang_run_ctl_t signal, mln_lang_run_ctl_t clear), \ - (ev, signal, clear), \ +mln_lang_t *mln_lang_new(mln_event_t *ev, mln_lang_run_ctl_t signal, mln_lang_run_ctl_t clear) { mln_lang_t *lang; struct mln_rbtree_attr rbattr; @@ -566,10 +771,12 @@ MLN_FUNC(, mln_lang_t *, mln_lang_new, \ lang->launcher = mln_lang_run_handler; lang->clear = clear; lang->alias_set = NULL; +#if !defined(MSVC) if (pthread_mutex_init(&lang->lock, NULL) != 0) { mln_alloc_destroy(pool); return NULL; } +#endif rbattr.pool = pool; rbattr.pool_alloc = (rbtree_pool_alloc_handler)mln_alloc_m; rbattr.pool_free = (rbtree_pool_free_handler)mln_alloc_free; @@ -590,18 +797,23 @@ MLN_FUNC(, mln_lang_t *, mln_lang_new, \ return NULL; } return lang; -}) +} -MLN_FUNC_VOID(, void, mln_lang_free, (mln_lang_t *lang), (lang), { +void mln_lang_free(mln_lang_t *lang) +{ if (lang == NULL) return; mln_alloc_t *pool = lang->pool; +#if !defined(MSVC) pthread_mutex_lock(&lang->lock); +#endif if (lang->wait) { lang->quit = 1; +#if !defined(MSVC) pthread_mutex_unlock(&lang->lock); +#endif return; } mln_lang_ctx_t *ctx; @@ -622,29 +834,38 @@ MLN_FUNC_VOID(, void, mln_lang_free, (mln_lang_t *lang), (lang), { } if (lang->alias_set != NULL) mln_rbtree_free(lang->alias_set); +#if !defined(MSVC) pthread_mutex_unlock(&lang->lock); pthread_mutex_destroy(&lang->lock); +#endif mln_alloc_free(lang); mln_alloc_destroy(pool); -}) +} -MLN_FUNC_VOID(static, void, mln_lang_run_handler, \ - (mln_event_t *ev, int fd, void *data), (ev, fd, data), \ +static void mln_lang_run_handler(mln_event_t *ev, int fd, void *data) { int n; mln_lang_t *lang = (mln_lang_t *)data; mln_lang_ctx_t *ctx; mln_lang_stack_node_t *node; +#if !defined(MSVC) if (pthread_mutex_trylock(&lang->lock) != 0) return; +#endif +#if !defined(MSVC) if ((ctx = lang->run_head) != NULL && ctx->owner == 0) { +#else + if ((ctx = lang->run_head) != NULL) { +#endif mln_lang_ctx_chain_del(&lang->run_head, &lang->run_tail, ctx); mln_lang_ctx_chain_add(&lang->run_head, &lang->run_tail, ctx); +#if !defined(MSVC) ctx->owner = pthread_self(); pthread_mutex_unlock(&lang->lock); +#endif for (n = 0; n < M_LANG_DEFAULT_STEP; ++n) { if ((node = mln_lang_stack_top(ctx)) == NULL) goto quit; @@ -656,7 +877,9 @@ MLN_FUNC_VOID(static, void, mln_lang_run_handler, \ ctx->return_handler(ctx); } mln_lang_job_free(ctx); +#if !defined(MSVC) pthread_mutex_lock(&lang->lock); +#endif goto out; } } @@ -670,16 +893,20 @@ MLN_FUNC_VOID(static, void, mln_lang_run_handler, \ if (!ctx->ref && (mln_lang_stack_top(ctx) == NULL || mln_lang_stack_top(ctx)->type != M_LSNT_ASSIGN)) { mln_gc_collect(ctx->gc, ctx); } +#if !defined(MSVC) pthread_mutex_lock(&lang->lock); ctx->owner = 0; +#endif out: if (lang->run_head != NULL) lang->signal(lang); } else { lang->clear(lang); } +#if !defined(MSVC) pthread_mutex_unlock(&lang->lock); -}) +#endif +} MLN_FUNC(static inline, mln_lang_ast_cache_t *, mln_lang_ast_cache_new, \ @@ -786,9 +1013,8 @@ MLN_FUNC(static inline, mln_lang_ast_cache_t *, mln_lang_ast_cache_search, \ }) -MLN_FUNC(static inline, mln_lang_ctx_t *, mln_lang_ctx_new, \ - (mln_lang_t *lang, void *data, mln_string_t *filename, mln_u32_t type, mln_string_t *content, mln_string_t *alias), \ - (lang, data, filename, type, content, alias), \ +static inline mln_lang_ctx_t * +mln_lang_ctx_new(mln_lang_t *lang, void *data, mln_string_t *filename, mln_u32_t type, mln_string_t *content, mln_string_t *alias) { mln_lang_ctx_t *ctx; struct mln_rbtree_attr rbattr; @@ -854,7 +1080,9 @@ MLN_FUNC(static inline, mln_lang_ctx_t *, mln_lang_ctx_new, \ ctx->return_handler = NULL; ctx->prev = ctx->next = NULL; ctx->sym_head = ctx->sym_tail = NULL; +#if !defined(MSVC) ctx->owner = 0; +#endif ctx->sym_count = 0; ctx->ret_flag = ctx->op_array_flag = ctx->op_bool_flag = ctx->op_func_flag = ctx->op_int_flag = \ ctx->op_nil_flag = ctx->op_obj_flag = ctx->op_real_flag = ctx->op_str_flag = 0; @@ -930,7 +1158,7 @@ MLN_FUNC(static inline, mln_lang_ctx_t *, mln_lang_ctx_new, \ mln_rbtree_insert(ctx->lang->alias_set, rn); } return ctx; -}) +} MLN_FUNC_VOID(static inline, void, mln_lang_ctx_free, (mln_lang_ctx_t *ctx), (ctx), { if (ctx == NULL) return; @@ -1053,17 +1281,18 @@ MLN_FUNC(, int, mln_lang_ctx_global_var_add, \ return 0; }) -MLN_FUNC(, mln_lang_ctx_t *, mln_lang_job_new, \ - (mln_lang_t *lang, mln_string_t *alias, mln_u32_t type, \ - mln_string_t *data, void *udata, mln_lang_return_handler handler), \ - (lang, alias, type, data, udata, handler), \ +mln_lang_ctx_t *mln_lang_job_new(mln_lang_t *lang, mln_string_t *alias, mln_u32_t type, mln_string_t *data, void *udata, mln_lang_return_handler handler) { mln_lang_ctx_t *ctx; +#if !defined(MSVC) pthread_mutex_lock(&lang->lock); +#endif ctx = __mln_lang_job_new(lang, alias, type, data, udata, handler); +#if !defined(MSVC) pthread_mutex_unlock(&lang->lock); +#endif return ctx; -}) +} MLN_FUNC(static inline, mln_lang_ctx_t *, __mln_lang_job_new, \ (mln_lang_t *lang, mln_string_t *alias, mln_u32_t type, \ @@ -1088,13 +1317,18 @@ MLN_FUNC(static inline, mln_lang_ctx_t *, __mln_lang_job_new, \ return ctx; }) -MLN_FUNC_VOID(, void, mln_lang_job_free, (mln_lang_ctx_t *ctx), (ctx), { +void mln_lang_job_free(mln_lang_ctx_t *ctx) +{ if (ctx == NULL) return; mln_lang_t *lang = ctx->lang; +#if !defined(MSVC) pthread_mutex_lock(&lang->lock); +#endif __mln_lang_job_free(ctx); +#if !defined(MSVC) pthread_mutex_unlock(&lang->lock); -}) +#endif +} MLN_FUNC_VOID(static inline, void, __mln_lang_job_free, (mln_lang_ctx_t *ctx), (ctx), { if (ctx == NULL) return; @@ -1779,7 +2013,7 @@ MLN_FUNC(static inline, mln_lang_var_t *, mln_lang_var_transform, \ }) MLN_FUNC_VOID(, void, mln_lang_var_free, (void *data), (data), { - return __mln_lang_var_free(data); + __mln_lang_var_free(data); }) MLN_FUNC_VOID(static, void, mln_lang_var_pfree, (mln_lang_var_t **v), (v), { @@ -1866,7 +2100,7 @@ MLN_FUNC(static, int, mln_lang_var_name_cmp, \ MLN_FUNC_VOID(, void, mln_lang_var_assign, \ (mln_lang_var_t *var, mln_lang_val_t *val), (var, val), \ { - return __mln_lang_var_assign(var, val); + __mln_lang_var_assign(var, val); }) MLN_FUNC_VOID(static inline, void, __mln_lang_var_assign, \ @@ -2145,7 +2379,7 @@ MLN_FUNC(static inline, mln_lang_func_detail_t *, __mln_lang_func_detail_new, \ }) MLN_FUNC_VOID(, void, mln_lang_func_detail_free, (mln_lang_func_detail_t *lfd), (lfd), { - return __mln_lang_func_detail_free(lfd); + __mln_lang_func_detail_free(lfd); }) MLN_FUNC_VOID(static inline, void, __mln_lang_func_detail_free, \ @@ -2333,7 +2567,7 @@ MLN_FUNC(static inline, mln_lang_val_t *, __mln_lang_val_new, \ }) MLN_FUNC_VOID(, void, mln_lang_val_free, (mln_lang_val_t *val), (val), { - return __mln_lang_val_free(val); + __mln_lang_val_free(val); }) MLN_FUNC_VOID(static inline, void, __mln_lang_val_free, (void *data), (data), { @@ -2836,7 +3070,7 @@ MLN_FUNC(, mln_lang_funccall_val_t *, mln_lang_funccall_val_new, \ }) MLN_FUNC_VOID(, void, mln_lang_funccall_val_free, (mln_lang_funccall_val_t *func), (func), { - return __mln_lang_funccall_val_free(func); + __mln_lang_funccall_val_free(func); }) MLN_FUNC_VOID(static inline, void, __mln_lang_funccall_val_free, \ @@ -2974,7 +3208,7 @@ MLN_FUNC(static inline, int, mln_lang_funccall_val_operator_overload_test, \ MLN_FUNC_VOID(, void, mln_lang_errmsg, (mln_lang_ctx_t *ctx, char *msg), (ctx, msg), { - return __mln_lang_errmsg(ctx, msg); + __mln_lang_errmsg(ctx, msg); }) MLN_FUNC_VOID(static, void, __mln_lang_errmsg, (mln_lang_ctx_t *ctx, char *msg), (ctx, msg), { @@ -3109,7 +3343,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_stm, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goon1: if (!ctx->ret_flag && stm->next != NULL) { @@ -3226,7 +3461,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_set, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { mln_lang_stack_node_free(mln_lang_stack_pop(ctx)); } @@ -3281,7 +3517,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_setstm, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else { goon1: @@ -3602,7 +3839,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_while, (mln_lang_ctx_t *ctx), ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else { goon1: @@ -3614,7 +3852,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_while, (mln_lang_ctx_t *ctx), ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else { mln_lang_ctx_reset_ret_var(ctx); @@ -3675,7 +3914,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_switch, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else if (node->step == 1) { node->step = 2; @@ -3685,7 +3925,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_switch, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { mln_lang_stack_node_free(mln_lang_stack_pop(ctx)); } @@ -3708,7 +3949,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_switchstm, (mln_lang_ctx_t *c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { if (sw_stm->stm == NULL) { node->step = 4; @@ -3785,7 +4027,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_switchstm, (mln_lang_ctx_t *c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goon4: mln_lang_stack_node_free(mln_lang_stack_pop(ctx)); @@ -3795,7 +4038,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_switchstm, (mln_lang_ctx_t *c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } }) @@ -3812,7 +4056,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_for, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goto goon1; } @@ -3825,7 +4070,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_for, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goto goon2; } @@ -3838,7 +4084,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_for, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { mln_lang_stack_node_free(mln_lang_stack_pop(ctx)); } @@ -3850,7 +4097,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_for, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goto goon1; } @@ -3870,7 +4118,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_if, (mln_lang_ctx_t *ctx), (c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { goto goon1; } @@ -3894,8 +4143,10 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_if, (mln_lang_ctx_t *ctx), (c return; } } - if (node != NULL) - return mln_lang_stack_map[node->type](ctx); + if (node != NULL) { + mln_lang_stack_map[node->type](ctx); + return; + } mln_lang_ctx_reset_ret_var(ctx); } }) @@ -4152,7 +4403,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_exp, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { if (exp->next != NULL) { node->data.exp = exp->next; @@ -4185,7 +4437,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_assign, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(assign->right != NULL); @@ -4195,7 +4448,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_assign, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 2) { node->step = 3; mln_lang_op handler = NULL; @@ -4341,7 +4595,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_logiclow, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(logiclow->right != NULL); @@ -4352,7 +4607,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_logiclow, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else { again: @@ -4362,7 +4618,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_logiclow, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { logiclow = logiclow->right; if (logiclow != NULL) { @@ -4396,7 +4653,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_logichigh, (mln_lang_ctx_t *c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(logichigh->right != NULL); @@ -4412,7 +4670,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_logichigh, (mln_lang_ctx_t *c ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -4503,7 +4762,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_relativelow, (mln_lang_ctx_t ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(relativelow->right != NULL); @@ -4519,7 +4779,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_relativelow, (mln_lang_ctx_t ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -4607,7 +4868,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_relativehigh, (mln_lang_ctx_t ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(relativehigh->right != NULL); @@ -4623,7 +4885,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_relativehigh, (mln_lang_ctx_t ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -4717,7 +4980,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_move, (mln_lang_ctx_t *ctx), ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(move->right != NULL); @@ -4733,7 +4997,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_move, (mln_lang_ctx_t *ctx), ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -4821,7 +5086,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_addsub, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(addsub->right != NULL); @@ -4837,7 +5103,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_addsub, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -4925,7 +5192,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_muldiv, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(muldiv->right != NULL); @@ -4941,7 +5209,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_muldiv, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 3) { node->step = 4; mln_lang_op handler = NULL; @@ -5028,7 +5297,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_not, (mln_lang_ctx_t *ctx), ( ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; mln_lang_op handler = NULL; @@ -5099,7 +5369,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_suffix, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; mln_lang_op handler = NULL; @@ -5223,7 +5494,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_locate, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 1) { node->step = 2; ASSERT(locate->op == M_LOCATE_INDEX); @@ -5242,7 +5514,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_locate, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else if (node->step == 2) { mln_lang_method_t *method; @@ -5402,7 +5675,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_locate, (mln_lang_ctx_t *ctx) ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { node->call = 1; node->step = 6; @@ -5448,17 +5722,18 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_locate, (mln_lang_ctx_t *ctx) node = NULL; } mln_lang_stack_node_free(&tmp); - if (node != NULL) - return mln_lang_stack_map[node->type](ctx); + if (node != NULL) { + mln_lang_stack_map[node->type](ctx); + return; + } } else { mln_lang_stack_node_free(mln_lang_stack_pop(ctx)); mln_lang_stack_popuntil(ctx); } }) -MLN_FUNC(static inline, int, mln_lang_stack_handler_funccall_run, \ - (mln_lang_ctx_t *ctx, mln_lang_stack_node_t *node, mln_lang_funccall_val_t *funccall), \ - (ctx, node, funccall), \ +static inline int +mln_lang_stack_handler_funccall_run(mln_lang_ctx_t *ctx, mln_lang_stack_node_t *node, mln_lang_funccall_val_t *funccall) { mln_lang_scope_t *scope; mln_lang_func_detail_t *prototype = funccall->prototype; @@ -5590,13 +5865,19 @@ MLN_FUNC(static inline, int, mln_lang_stack_handler_funccall_run, \ __mln_lang_errmsg(ctx, "Not implemented."); return -1; } +#if !defined(MSVC) pthread_mutex_lock(&ctx->lang->lock); +#endif if ((var = prototype->data.process(ctx)) == NULL) { +#if !defined(MSVC) pthread_mutex_unlock(&ctx->lang->lock); +#endif return -1; } __mln_lang_ctx_set_ret_var(ctx, var); +#if !defined(MSVC) pthread_mutex_unlock(&ctx->lang->lock); +#endif } else { if (prototype->data.stm != NULL) { scope->entry = prototype->data.stm; @@ -5607,7 +5888,7 @@ MLN_FUNC(static inline, int, mln_lang_stack_handler_funccall_run, \ } } return 0; -}) +} MLN_FUNC(static inline, int, mln_lang_funcall_run_add_args, \ (mln_lang_ctx_t *ctx, mln_lang_array_t *array, mln_lang_var_t *arg), \ @@ -5709,7 +5990,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_spec, (mln_lang_ctx_t *ctx), ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else { ASSERT(spec->op == M_SPEC_NEW || spec->op == M_SPEC_PARENTH); } @@ -6000,7 +6282,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_elemlist, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } else if (node->step == 1) { node->step = 2; @@ -6012,7 +6295,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_elemlist, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } else if (node->step == 2) { mln_lang_var_t *key, *val; mln_lang_stack_node_t *array_node; @@ -6046,7 +6330,8 @@ MLN_FUNC_VOID(static, void, mln_lang_stack_handler_elemlist, (mln_lang_ctx_t *ct ctx->quit = 1; return; } - return mln_lang_stack_map[node->type](ctx); + mln_lang_stack_map[node->type](ctx); + return; } } }) @@ -6693,12 +6978,13 @@ MLN_FUNC(static, int, mln_lang_func_import_cmp, \ return mln_string_strcmp(i1->name, i2->name); }) -MLN_FUNC_VOID(static, void, mln_lang_func_import_free, (mln_lang_import_t *i), (i), { +static void mln_lang_func_import_free(mln_lang_import_t *i) +{ import_destroy_t destroy; if (i == NULL) return; -#if defined(__WIN32__) +#if defined(MSVC) destroy = (import_destroy_t)GetProcAddress(i->handle, "destroy"); #else destroy = (import_destroy_t)dlsym(i->handle, "destroy"); @@ -6709,14 +6995,14 @@ MLN_FUNC_VOID(static, void, mln_lang_func_import_free, (mln_lang_import_t *i), ( if (i->name != NULL) mln_string_free(i->name); if (i->handle != NULL) { -#if defined(__WIN32__) +#if defined(MSVC) FreeLibrary((HMODULE)(i->handle)); #else dlclose(i->handle); #endif } mln_alloc_free(i); -}) +} MLN_FUNC(static, mln_lang_ctx_import_t *, mln_lang_func_ctx_import_new, \ (mln_lang_ctx_t *ctx, mln_lang_import_t *i), (ctx, i), \ @@ -6796,7 +7082,8 @@ MLN_FUNC(static, int, mln_lang_func_import_handler, (mln_lang_ctx_t *ctx), (ctx) return 0; }) -MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t *ctx), (ctx), { +static mln_lang_var_t *mln_lang_func_import_process(mln_lang_ctx_t *ctx) +{ mln_lang_var_t *ret_var; mln_string_t v1 = mln_string("name"); mln_lang_ctx_import_t ci, *pci; @@ -6829,7 +7116,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t if (!mln_rbtree_null(rn, tree)) { pi = (mln_lang_import_t *)mln_rbtree_node_data_get(rn); } else { -#if defined(__WIN32__) +#if defined(MSVC) if (name->len > 1 && name->data[1] == ':') { n = snprintf(path, sizeof(path)-1, "%s.dll", (char *)(name->data)); #else @@ -6839,20 +7126,24 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t path[n] = 0; } else { if (name->len > 0 && name->data[0] != '.') { -#if defined(__WIN32__) +#if defined(MSVC) n = snprintf(path, sizeof(path)-1, "%s.dll", (char *)(name->data)); #else n = snprintf(path, sizeof(path)-1, "./%s.so", (char *)(name->data)); #endif } else { -#if defined(__WIN32__) +#if defined(MSVC) n = snprintf(path, sizeof(path)-1, "%s.dll", (char *)(name->data)); #else n = snprintf(path, sizeof(path)-1, "%s.so", (char *)(name->data)); #endif } path[n] = 0; +#if defined(MSVC) + if (!_access(path, 0)) { +#else if (!access(path, F_OK)) { +#endif /* do nothing */ } else if ((melang_dy_path = getenv("MELANG_DYNAMIC_PATH")) != NULL) { char *end = strchr(melang_dy_path, ';'); @@ -6860,7 +7151,11 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t while (end != NULL) { *end = 0; n = snprintf(tmp_path, sizeof(tmp_path)-1, "%s/%s", melang_dy_path, path); +#if defined(MSVC) + if (!_access(tmp_path, 0)) { +#else if (!access(tmp_path, F_OK)) { +#endif memcpy(path, tmp_path, n); path[n] = 0; found = 1; @@ -6887,7 +7182,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t } } -#if defined(__WIN32__) +#if defined(MSVC) handle = LoadLibrary(TEXT(path)); #else handle = dlopen(path, RTLD_LAZY); @@ -6900,7 +7195,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t } if ((pi = mln_lang_func_import_new(ctx, name, handle)) == NULL) { -#if defined(__WIN32__) +#if defined(MSVC) FreeLibrary((HMODULE)(pi->handle)); #else dlclose(pi->handle); @@ -6937,7 +7232,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t mln_rbtree_insert(ctx_tree, ctx_rn); } -#if defined(__WIN32__) +#if defined(MSVC) init = (import_init_t)GetProcAddress(pi->handle, "init"); #else init = (import_init_t)dlsym(pi->handle, "init"); @@ -6957,7 +7252,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_import_process, (mln_lang_ctx_t } return ret_var; -}) +} MLN_FUNC(static, int, mln_lang_func_eval, (mln_lang_ctx_t *ctx), (ctx), { mln_lang_val_t *val; @@ -7668,7 +7963,7 @@ MLN_FUNC_VOID(static, void, mln_lang_ctx_resource_free_handler, (mln_lang_resour mln_alloc_free(lr); }) -MLN_FUNC(, void *, mln_lang_ctx_resource_fetch, (mln_lang_ctx_t *ctx, const char *name), (ctx, name), { +MLN_FUNC(, void *, mln_lang_ctx_resource_fetch, (mln_lang_ctx_t *ctx, char *name), (ctx, name), { mln_rbtree_node_t *rn; mln_lang_resource_t lr; mln_string_t s; @@ -7717,7 +8012,7 @@ MLN_FUNC_VOID(static, void, mln_lang_resource_free_handler, (mln_lang_resource_t free(lr); }) -MLN_FUNC_VOID(, void, mln_lang_resource_cancel, (mln_lang_t *lang, const char *name), (lang, name), { +MLN_FUNC_VOID(, void, mln_lang_resource_cancel, (mln_lang_t *lang, char *name), (lang, name), { mln_rbtree_node_t *rn; mln_lang_resource_t lr; mln_string_t s; @@ -7730,7 +8025,7 @@ MLN_FUNC_VOID(, void, mln_lang_resource_cancel, (mln_lang_t *lang, const char *n mln_rbtree_node_free(lang->resource_set, rn); }) -MLN_FUNC(, void *, mln_lang_resource_fetch, (mln_lang_t *lang, const char *name), (lang, name), { +MLN_FUNC(, void *, mln_lang_resource_fetch, (mln_lang_t *lang, char *name), (lang, name), { mln_rbtree_node_t *rn; mln_lang_resource_t lr; mln_string_t s; @@ -7785,18 +8080,19 @@ MLN_FUNC(static inline, mln_lang_hash_bucket_t *, mln_lang_hash_get_bucket, \ /* * pipe for communication between C code and script */ -MLN_FUNC(static, mln_lang_ctx_pipe_t *, mln_lang_ctx_pipe_new, \ - (mln_lang_ctx_t *ctx), (ctx), \ +static mln_lang_ctx_pipe_t *mln_lang_ctx_pipe_new(mln_lang_ctx_t *ctx) { mln_lang_ctx_pipe_t *p; if ((p = (mln_lang_ctx_pipe_t *)mln_alloc_m(ctx->pool, sizeof(mln_lang_ctx_pipe_t))) == NULL) { return NULL; } +#if !defined(MSVC) if (pthread_mutex_init(&p->lock, NULL)) { mln_alloc_free(p); return NULL; } +#endif p->ctx = ctx; if (mln_array_pool_init(&p->list, \ (array_free)mln_array_destroy, \ @@ -7806,7 +8102,9 @@ MLN_FUNC(static, mln_lang_ctx_pipe_t *, mln_lang_ctx_pipe_new, \ (array_pool_alloc_handler)mln_alloc_m, \ (array_pool_free_handler)mln_alloc_free) < 0) { +#if !defined(MSVC) pthread_mutex_destroy(&p->lock); +#endif mln_alloc_free(p); return NULL; } @@ -7814,20 +8112,25 @@ MLN_FUNC(static, mln_lang_ctx_pipe_t *, mln_lang_ctx_pipe_new, \ p->subscribed = 0; return p; -}) +} -MLN_FUNC_VOID(static, void, mln_lang_ctx_pipe_free, (mln_lang_ctx_pipe_t *p), (p), { +static void mln_lang_ctx_pipe_free(mln_lang_ctx_pipe_t *p) +{ if (p == NULL) return; +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif mln_array_destroy(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); pthread_mutex_destroy(&p->lock); +#endif mln_alloc_free(p); -}) +} MLN_FUNC(static inline, int, mln_lang_ctx_pipe_elem_init, \ (mln_lang_ctx_pipe_elem_t *pe, int type, void *value), (pe, type, value), \ @@ -7909,7 +8212,8 @@ MLN_FUNC(static, int, mln_lang_func_pipe, (mln_lang_ctx_t *ctx), (ctx), { return 0; }) -MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_pipe_process, (mln_lang_ctx_t *ctx), (ctx), { +static mln_lang_var_t *mln_lang_func_pipe_process(mln_lang_ctx_t *ctx) +{ mln_lang_var_t *ret_var = NULL; mln_lang_var_t *var = NULL; mln_string_t v1 = mln_string("op"); @@ -7960,30 +8264,46 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_pipe_process, (mln_lang_ctx_t * return NULL; } } +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif p->subscribed = 1; +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif ret_var = mln_lang_var_create_nil(ctx, NULL); } else if (!mln_string_strcmp(op, &op_unsub)) { if ((p = (mln_lang_ctx_pipe_t *)mln_lang_ctx_resource_fetch(ctx, "pipe")) != NULL) { +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif p->subscribed = 0; +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif } ret_var = mln_lang_var_create_nil(ctx, NULL); } else if (!mln_string_strcmp(op, &op_recv)) { if ((p = (mln_lang_ctx_pipe_t *)mln_lang_ctx_resource_fetch(ctx, "pipe")) != NULL) { +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif ret_var = mln_lang_func_pipe_process_array_generate(ctx, p); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif } else { ret_var = mln_lang_var_create_false(ctx, NULL); } } else if (!mln_string_strcmp(op, &op_send)) { if ((p = (mln_lang_ctx_pipe_t *)mln_lang_ctx_resource_fetch(ctx, "pipe")) != NULL) { +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif cb = p->recv_handler; +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif if (cb != NULL) { rc = cb(ctx, mln_lang_var_val_get(var)); } @@ -8000,7 +8320,7 @@ MLN_FUNC(static, mln_lang_var_t *, mln_lang_func_pipe_process, (mln_lang_ctx_t * mln_lang_errmsg(ctx, "No memory."); } return ret_var; -}) +} MLN_FUNC(static inline, mln_lang_var_t *, mln_lang_func_pipe_process_array_generate, \ (mln_lang_ctx_t *ctx, mln_lang_ctx_pipe_t *pipe), (ctx, pipe), \ @@ -8078,8 +8398,7 @@ int mln_lang_ctx_pipe_send(mln_lang_ctx_t *ctx, char *fmt, ...) return rc; } -MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ - (mln_lang_ctx_t *ctx, char *fmt, va_list arg), (ctx, fmt, arg), \ +static inline int mln_lang_ctx_pipe_do_send(mln_lang_ctx_t *ctx, char *fmt, va_list arg) { mln_lang_ctx_pipe_t *p; mln_lang_ctx_pipe_elem_t *pe; @@ -8090,14 +8409,20 @@ MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ if (p->ctx != ctx) return -1; +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif if (!p->subscribed) { +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } if ((a = (mln_array_t *)mln_array_push(&p->list)) == NULL) { +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } if (mln_array_pool_init(a, \ @@ -8109,14 +8434,18 @@ MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ (array_pool_free_handler)mln_alloc_free) < 0) { mln_array_pop(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } while (*fmt) { if ((pe = (mln_lang_ctx_pipe_elem_t *)mln_array_push(a)) == NULL) { mln_array_pop(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } pe->data.s= NULL; @@ -8134,7 +8463,9 @@ MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ mln_string_set(&tmp, str); if (mln_lang_ctx_pipe_elem_init(pe, M_LANG_VAL_TYPE_STRING, &tmp) < 0) { mln_array_pop(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } break; @@ -8144,7 +8475,9 @@ MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ mln_string_t *tmp = va_arg(arg, mln_string_t *); if (mln_lang_ctx_pipe_elem_init(pe, M_LANG_VAL_TYPE_STRING, tmp) < 0) { mln_array_pop(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } break; @@ -8157,20 +8490,22 @@ MLN_FUNC(static inline, int, mln_lang_ctx_pipe_do_send, \ } default: mln_array_pop(&p->list); +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return -1; } ++fmt; } +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return 0; -}) +} -MLN_FUNC(, int, mln_lang_ctx_pipe_recv_handler_set, \ - (mln_lang_ctx_t *ctx, mln_lang_ctx_pipe_recv_cb_t recv_handler), \ - (ctx, recv_handler), \ +int mln_lang_ctx_pipe_recv_handler_set(mln_lang_ctx_t *ctx, mln_lang_ctx_pipe_recv_cb_t recv_handler) { mln_lang_ctx_pipe_t *p; @@ -8186,10 +8521,14 @@ MLN_FUNC(, int, mln_lang_ctx_pipe_recv_handler_set, \ if (p->ctx != ctx) return -1; +#if !defined(MSVC) pthread_mutex_lock(&p->lock); +#endif p->recv_handler = recv_handler; +#if !defined(MSVC) pthread_mutex_unlock(&p->lock); +#endif return 0; -}) +} diff --git a/src/mln_lang_ast.c b/src/mln_lang_ast.c index 1e8a9936..12f22041 100644 --- a/src/mln_lang_ast.c +++ b/src/mln_lang_ast.c @@ -732,8 +732,7 @@ MLN_FUNC(static, mln_lang_struct_t *, mln_lang_sglq_handler, \ return mln_lang_new(lex, LANG_TK_STRING); }) -MLN_FUNC(static inline, int, mln_get_char, \ - (mln_lex_t *lex, char c), (lex, c), \ +static inline int mln_get_char(mln_lex_t *lex, char c) { if (c == '\\') { char n; @@ -763,9 +762,11 @@ MLN_FUNC(static inline, int, mln_get_char, \ case 'r': if (mln_lex_putchar(lex, '\r') == MLN_ERR) return -1; break; +#if !defined(MSVC) case 'e': if (mln_lex_putchar(lex, '\e') == MLN_ERR) return -1; break; +#endif case 'v': if (mln_lex_putchar(lex, '\v') == MLN_ERR) return -1; break; @@ -780,7 +781,7 @@ MLN_FUNC(static inline, int, mln_get_char, \ if (mln_lex_putchar(lex, c) == MLN_ERR) return -1; } return 0; -}) +} MLN_FUNC(static, mln_lang_struct_t *, mln_lang_dblq_handler, \ (mln_lex_t *lex, void *data), (lex, data), \ @@ -3538,9 +3539,7 @@ MLN_FUNC(static, int, mln_lang_semantic_factorid, \ return 0; }) -MLN_FUNC(static, int, mln_lang_semantic_factorint, \ - (mln_factor_t *left, mln_factor_t **right, void *data), \ - (left, right, data), \ +static int mln_lang_semantic_factorint(mln_factor_t *left, mln_factor_t **right, void *data) { mln_alloc_t *pool = (mln_alloc_t *)data; mln_lang_struct_t *ls = (mln_lang_struct_t *)(right[0]->data); @@ -3554,26 +3553,20 @@ MLN_FUNC(static, int, mln_lang_semantic_factorint, \ mln_s64_t i; if (ls->text->len > 1 && num[0] == '0') { if (num[1] == 'x' || num[1] == 'X') { -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64x", &i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%llx", &i); #else sscanf(num, "%lx", &i); #endif } else { -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64o", &i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%llo", &i); #else sscanf(num, "%lo", &i); #endif } } else { -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf(num, "%I64d", &i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf(num, "%lld", &i); #else sscanf(num, "%ld", &i); @@ -3586,7 +3579,7 @@ MLN_FUNC(static, int, mln_lang_semantic_factorint, \ left->data = lf; left->nonterm_free_handler = mln_lang_factor_free; return 0; -}) +} MLN_FUNC(static, int, mln_lang_semantic_factorreal, \ (mln_factor_t *left, mln_factor_t **right, void *data), \ @@ -3697,21 +3690,26 @@ MLN_FUNC(static, int, mln_lang_semantic_elemnext, \ /* * APIs */ -MLN_FUNC(, int, mln_lang_ast_file_open, (mln_string_t *file_path), (file_path), { +int mln_lang_ast_file_open(mln_string_t *file_path) +{ int fd, n; size_t len = file_path->len >= 1024? 1023: file_path->len; char path[1024], *melang_path = NULL, tmp_path[1024]; memcpy(path, file_path->data, len); path[len] = 0; -#if defined(__WIN32__) +#if defined(MSVC) if (len > 1 && path[1] == ':') { #else if (path[0] == '/') { #endif fd = open(path, O_RDONLY); } else { +#if defined(MSVC) + if (!_access(path, 0)) { +#else if (!access(path, F_OK)) { +#endif fd = open(path, O_RDONLY); } else if ((melang_path = getenv((char *)(mln_lang_env.data))) != NULL) { char *end = strchr(melang_path, ';'); @@ -3720,7 +3718,11 @@ MLN_FUNC(, int, mln_lang_ast_file_open, (mln_string_t *file_path), (file_path), *end = 0; n = snprintf(tmp_path, sizeof(tmp_path)-1, "%s/%s", melang_path, path); tmp_path[n] = 0; +#if defined(MSVC) + if (!_access(tmp_path, 0)) { +#else if (!access(tmp_path, F_OK)) { +#endif fd = open(tmp_path, O_RDONLY); found = 1; break; @@ -3746,7 +3748,7 @@ MLN_FUNC(, int, mln_lang_ast_file_open, (mln_string_t *file_path), (file_path), } return fd; -}) +} MLN_FUNC(, void *, mln_lang_ast_parser_generate, (void), (), { return mln_lang_parser_generate(prod_tbl, sizeof(prod_tbl)/sizeof(mln_production_t), &mln_lang_env); diff --git a/src/mln_lang_func.c b/src/mln_lang_func.c index b57fe2b4..66bd96b3 100644 --- a/src/mln_lang_func.c +++ b/src/mln_lang_func.c @@ -667,9 +667,7 @@ MLN_FUNC(static, int, mln_lang_func_reverse, \ return -1; }) -MLN_FUNC(static, int, mln_lang_func_not, \ - (mln_lang_ctx_t *ctx, mln_lang_var_t **ret, mln_lang_var_t *op1, mln_lang_var_t *op2), \ - (ctx, ret, op1, op2), \ +static int mln_lang_func_not(mln_lang_ctx_t *ctx, mln_lang_var_t **ret, mln_lang_var_t *op1, mln_lang_var_t *op2) { if (ctx->op_func_flag) { int rc = mln_lang_funccall_val_operator(ctx, &mln_lang_func_opr_names[33], ret, op1, op2); @@ -688,7 +686,7 @@ MLN_FUNC(static, int, mln_lang_func_not, \ return -1; } return 0; -}) +} MLN_FUNC(static, int, mln_lang_func_pinc, \ (mln_lang_ctx_t *ctx, mln_lang_var_t **ret, mln_lang_var_t *op1, mln_lang_var_t *op2), \ diff --git a/src/mln_lang_int.c b/src/mln_lang_int.c index 819d8323..e5f4b3bf 100644 --- a/src/mln_lang_int.c +++ b/src/mln_lang_int.c @@ -166,7 +166,8 @@ static mln_string_t mln_lang_int_opr_names[] = { mln_string("__int_pdec_operator__"), }; -MLN_FUNC(static inline, mln_s64_t, mln_lang_int_var_toint, (mln_lang_var_t *var), (var), { +static inline mln_s64_t mln_lang_int_var_toint(mln_lang_var_t *var) +{ ASSERT(var != NULL && var->val != NULL); mln_s64_t i = 0; mln_lang_val_t *val = var->val; @@ -193,9 +194,7 @@ MLN_FUNC(static inline, mln_s64_t, mln_lang_int_var_toint, (mln_lang_var_t *var) if (buf == NULL) break; memcpy(buf, s->data, s->len); buf[s->len] = 0; -#if defined(__WIN32__) && defined(__pentiumpro__) - sscanf((char *)buf, "%I64d", &i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) sscanf((char *)buf, "%lld", &i); #else sscanf((char *)buf, "%ld", &i); @@ -208,7 +207,7 @@ MLN_FUNC(static inline, mln_s64_t, mln_lang_int_var_toint, (mln_lang_var_t *var) break; } return i; -}) +} MLN_FUNC(static inline, double, mln_lang_int_var_toreal, (mln_lang_var_t *var), (var), { ASSERT(var != NULL && var->val != NULL); diff --git a/src/mln_lang_str.c b/src/mln_lang_str.c index 3c508625..d85d2e69 100644 --- a/src/mln_lang_str.c +++ b/src/mln_lang_str.c @@ -172,8 +172,7 @@ MLN_FUNC(, mln_string_t *, mln_lang_str_var_tostring, \ return __mln_lang_str_var_tostring(pool, var); }) -MLN_FUNC(static inline, mln_string_t *, __mln_lang_str_var_tostring, \ - (mln_alloc_t *pool, mln_lang_var_t *var), (pool, var), \ +static inline mln_string_t *__mln_lang_str_var_tostring(mln_alloc_t *pool, mln_lang_var_t *var) { ASSERT(var != NULL && var->val != NULL); char buf[1024] = {0}; @@ -193,9 +192,7 @@ MLN_FUNC(static inline, mln_string_t *, __mln_lang_str_var_tostring, \ n = snprintf(buf, sizeof(buf)-1, "Array"); break; case M_LANG_VAL_TYPE_INT: -#if defined(__WIN32__) && defined(__pentiumpro__) - n = snprintf(buf, sizeof(buf)-1, "%I64d", val->data.i); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) n = snprintf(buf, sizeof(buf)-1, "%lld", val->data.i); #else n = snprintf(buf, sizeof(buf)-1, "%ld", val->data.i); @@ -219,7 +216,7 @@ MLN_FUNC(static inline, mln_string_t *, __mln_lang_str_var_tostring, \ mln_string_t tmp; mln_string_nset(&tmp, buf, n); return mln_string_pool_dup(pool, &tmp); -}) +} MLN_FUNC(static, int, mln_lang_str_assign, \ (mln_lang_ctx_t *ctx, mln_lang_var_t **ret, mln_lang_var_t *op1, mln_lang_var_t *op2), \ diff --git a/src/mln_lex.c b/src/mln_lex.c index a09d317a..75a5d713 100644 --- a/src/mln_lex.c +++ b/src/mln_lex.c @@ -12,9 +12,13 @@ #if defined(MLN_C99) #define _GNU_SOURCE #endif -#include #include +#if !defined(MSVC) +#include #include +#else +#include "mln_utils.h" +#endif #include "mln_string.h" #include "mln_lex.h" #include "mln_path.h" @@ -77,9 +81,7 @@ MLN_FUNC(static inline, int, mln_lex_base_dir, \ return 0; }) -MLN_FUNC(, mln_lex_input_t *, mln_lex_input_new, \ - (mln_lex_t *lex, mln_u32_t type, mln_string_t *data, int *err, mln_u64_t line), \ - (lex, type, data, err, line), \ +mln_lex_input_t *mln_lex_input_new(mln_lex_t *lex, mln_u32_t type, mln_string_t *data, int *err, mln_u64_t line) { int r; mln_lex_input_t *li; @@ -117,7 +119,7 @@ MLN_FUNC(, mln_lex_input_t *, mln_lex_input_new, \ len = data->len >= 1024? 1023: data->len; memcpy(path, data->data, len); } -#if defined(__WIN32__) +#if defined(MSVC) if (len > 1 && path[1] == ':') { #else if (path[0] == '/') { @@ -125,7 +127,11 @@ MLN_FUNC(, mln_lex_input_t *, mln_lex_input_new, \ li->fd = open(path, O_RDONLY); r = mln_lex_base_dir(lex, li, path, err); } else { +#if defined(MSVC) + if (!_access(path, 0)) { +#else if (!access(path, F_OK)) { +#endif li->fd = open(path, O_RDONLY); r = mln_lex_base_dir(lex, li, path, err); } else if (lex->env != NULL && (melang_path = getenv((char *)(lex->env->data))) != NULL) { @@ -135,7 +141,11 @@ MLN_FUNC(, mln_lex_input_t *, mln_lex_input_new, \ *end = 0; n = snprintf(tmp_path, sizeof(tmp_path)-1, "%s/%s", melang_path, path); tmp_path[n] = 0; +#if defined(MSVC) + if (!_access(tmp_path, 0)) { +#else if (!access(tmp_path, F_OK)) { +#endif li->fd = open(tmp_path, O_RDONLY); r = mln_lex_base_dir(lex, li, tmp_path, err); found = 1; @@ -177,7 +187,7 @@ MLN_FUNC(, mln_lex_input_t *, mln_lex_input_new, \ } return li; -}) +} MLN_FUNC_VOID(, void, mln_lex_input_free, (void *in), (in), { if (in == NULL) return; @@ -387,7 +397,8 @@ MLN_FUNC_VOID(, void, mln_lex_destroy, (mln_lex_t *lex), (lex), { mln_alloc_free(lex); }) -MLN_FUNC(, char *, mln_lex_strerror, (mln_lex_t *lex), (lex), { +char *mln_lex_strerror(mln_lex_t *lex) +{ if (lex->err_msg != NULL) mln_alloc_free(lex->err_msg); int len = 0; if (lex->cur != NULL) { @@ -404,9 +415,7 @@ MLN_FUNC(, char *, mln_lex_strerror, (mln_lex_t *lex), (lex), { int n = 0; if (lex->cur != NULL && lex->cur->fd >= 0) n += snprintf(lex->err_msg + n, len - n, "%s:", (char *)(lex->cur->data->data)); -#if defined(__WIN32__) && defined(__pentiumpro__) - n += snprintf(lex->err_msg + n, len - n, "%I64u: %s", lex->line, mln_lex_errmsg[lex->error]); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) || defined(__wasm__) n += snprintf(lex->err_msg + n, len - n, "%llu: %s", lex->line, mln_lex_errmsg[lex->error]); #else n += snprintf(lex->err_msg + n, len - n, "%lu: %s", lex->line, mln_lex_errmsg[lex->error]); @@ -423,10 +432,9 @@ MLN_FUNC(, char *, mln_lex_strerror, (mln_lex_t *lex), (lex), { n += snprintf(lex->err_msg + n, len - n, ". %s", strerror(errno)); lex->err_msg[n] = 0; return lex->err_msg; -}) +} -MLN_FUNC(, int, mln_lex_push_input_file_stream, \ - (mln_lex_t *lex, mln_string_t *path), (lex, path), \ +int mln_lex_push_input_file_stream(mln_lex_t *lex, mln_string_t *path) { int err = MLN_LEX_SUCCEED; mln_lex_input_t *in; @@ -451,7 +459,11 @@ MLN_FUNC(, int, mln_lex_push_input_file_stream, \ memcpy(p, path->data, n); } p[n] = 0; +#if defined(MSVC) + if (_access(p, 0)) { +#else if (access(p, F_OK)) { +#endif mln_lex_error_set(lex, MLN_LEX_EFPATH); return -1; } @@ -465,7 +477,7 @@ MLN_FUNC(, int, mln_lex_push_input_file_stream, \ mln_string_t tmp; struct dirent *entry; DIR *directory; -#if defined(__WIN32__) +#if defined(MSVC) WIN32_FIND_DATA fileData; #endif if ((directory = opendir(p)) == NULL) { @@ -481,7 +493,7 @@ MLN_FUNC(, int, mln_lex_push_input_file_stream, \ p[n + m] = 0; mln_string_nset(&tmp, p, n + m); path = &tmp; -#if defined(__WIN32__) +#if defined(MSVC) FindClose(FindFirstFile(p, &fileData)); if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || entry->d_name[0] == '.') { #else @@ -544,7 +556,7 @@ MLN_FUNC(, int, mln_lex_push_input_file_stream, \ lex->line = 1; } return 0; -}) +} MLN_FUNC(, int, mln_lex_push_input_buf_stream, \ (mln_lex_t *lex, mln_string_t *buf), (lex, buf), \ @@ -582,8 +594,7 @@ MLN_FUNC(static, int, mln_lex_check_file_loop_iterate_handler, \ return 0; }) -MLN_FUNC(, int, mln_lex_check_file_loop, \ - (mln_lex_t *lex, mln_string_t *path), (lex, path), \ +int mln_lex_check_file_loop(mln_lex_t *lex, mln_string_t *path) { char p[1024]; struct stat path_stat; @@ -607,7 +618,11 @@ MLN_FUNC(, int, mln_lex_check_file_loop, \ } p[n] = 0; +#if defined(MSVC) + if (_access(p, 0)) { +#else if (access(p, F_OK)) { +#endif mln_lex_error_set(lex, MLN_LEX_EFPATH); return -1; } @@ -620,7 +635,7 @@ MLN_FUNC(, int, mln_lex_check_file_loop, \ DIR *directory; mln_string_t tmp; struct dirent *entry; -#if defined(__WIN32__) +#if defined(MSVC) WIN32_FIND_DATA fileData; #endif @@ -638,7 +653,7 @@ MLN_FUNC(, int, mln_lex_check_file_loop, \ p[n + m] = 0; mln_string_nset(&tmp, p, n + m); path = &tmp; -#if defined(__WIN32__) +#if defined(MSVC) FindClose(FindFirstFile(p, &fileData)); if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY || entry->d_name[0] == '.') { #else @@ -681,7 +696,7 @@ MLN_FUNC(, int, mln_lex_check_file_loop, \ } } return 0; -}) +} MLN_FUNC(, int, mln_lex_condition_test, (mln_lex_t *lex), (lex), { mln_lex_result_clean(lex); diff --git a/src/mln_log.c b/src/mln_log.c index 20a6466a..827f7961 100644 --- a/src/mln_log.c +++ b/src/mln_log.c @@ -7,11 +7,15 @@ #include #include #include +#if !defined(MSVC) #include +#include +#else +#include "mln_utils.h" +#endif #include #include #include -#include #include #include "mln_log.h" #include "mln_path.h" @@ -31,8 +35,8 @@ _mln_sys_log_process(mln_log_t *log, \ static inline void mln_file_lock(int fd); static inline void mln_file_unlock(int fd); static int mln_log_set_level(mln_log_t *log, mln_conf_t *cf, int is_init); -static inline ssize_t mln_log_write(mln_log_t *log, void *buf, mln_size_t size); -#if !defined(__WIN32__) +static inline int mln_log_write(mln_log_t *log, void *buf, mln_size_t size); +#if !defined(MSVC) static void mln_log_atfork_lock(void); static void mln_log_atfork_unlock(void); #endif @@ -45,14 +49,18 @@ static mln_logger_t _logger = _mln_sys_log_process; char log_err_level[] = "Log level permission deny."; char log_err_fmt[] = "Log message format error."; char log_path_cmd[] = "log_path"; +#if defined(MSVC) +mln_log_t g_log = {2, 0, 0, 0, none, {0},{0},{0}}; +#else mln_log_t g_log = {(mln_spin_t)0, STDERR_FILENO, 0, 0, 0, none, {0},{0},{0}}; +#endif /* * file lock */ static inline void mln_file_lock(int fd) { -#if !defined(__WIN32__) +#if !defined(MSVC) struct flock fl; memset(&fl, 0, sizeof(fl)); fl.l_type = F_WRLCK; @@ -65,7 +73,7 @@ static inline void mln_file_lock(int fd) static inline void mln_file_unlock(int fd) { -#if !defined(__WIN32__) +#if !defined(MSVC) struct flock fl; memset(&fl, 0, sizeof(fl)); fl.l_type = F_UNLCK; @@ -129,6 +137,7 @@ int mln_log_init(mln_conf_t *cf) log->init = 1; log->level = none; int ret = 0; +#if !defined(MSVC) if ((ret = mln_spin_init(&(log->thread_lock))) != 0) { fprintf(stderr, "%s(): Init log's thread_lock failed. %s\n", __FUNCTION__, strerror(ret)); return -1; @@ -141,6 +150,7 @@ int mln_log_init(mln_conf_t *cf) mln_spin_destroy(&(log->thread_lock)); return -1; } +#endif if (mln_log_get_log(log, cf, 1) < 0) { fprintf(stderr, "%s(): Get log file failed.\n", __FUNCTION__); @@ -186,7 +196,7 @@ mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) __FUNCTION__, log_path_cmd); return -1; } -#if defined(__WIN32__) +#if defined(MSVC) if (ci->val.s->len <= 1 || (ci->val.s->data)[1] != ':') { #else if ((ci->val.s->data)[0] != '/' && (ci->val.s->data)[0] != '.') { @@ -202,7 +212,7 @@ mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) ; memcpy(log->dir_path, path_str, p - path_str); log->dir_path[p - path_str] = 0; -#if defined(__WIN32__) +#if defined(MSVC) if (mkdir(log->dir_path) < 0) { #else if (mkdir(log->dir_path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0) { @@ -213,16 +223,28 @@ mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) } } +#if defined(MSVC) + fd = open(path_str, O_WRONLY|O_CREAT|O_APPEND, _S_IREAD|_S_IWRITE); +#else fd = open(path_str, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); +#endif if (fd < 0) { fprintf(stderr, "%s(): open '%s' failed. %s\n", __FUNCTION__, path_str, strerror(errno)); return -1; } +#if defined(MSVC) + if (!is_init && \ + log->fd > 0 && \ + log->fd != _fileno(stdin) && \ + log->fd != _fileno(stdout) && \ + log->fd != _fileno(stderr)) +#else if (!is_init && \ log->fd > 0 && \ log->fd != STDIN_FILENO && \ log->fd != STDOUT_FILENO && \ log->fd != STDERR_FILENO) +#endif { close(log->fd); } @@ -233,7 +255,11 @@ mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) if (is_init) { memset(log->pid_path, 0, M_LOG_PATH_LEN); snprintf(log->pid_path, M_LOG_PATH_LEN-1, "%s/melon.pid", log->dir_path); +#if defined(MSVC) + fd = open(log->pid_path, O_WRONLY|O_CREAT|O_TRUNC, _S_IREAD|_S_IWRITE); +#else fd = open(log->pid_path, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); +#endif if (fd < 0) { fprintf(stderr, "%s(): open pid file failed. %s\n", __FUNCTION__, strerror(errno)); return -1; @@ -249,7 +275,7 @@ mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) return 0; } -#if !defined(__WIN32__) +#if !defined(MSVC) static void mln_log_atfork_lock(void) { mln_spin_lock(&(g_log.thread_lock)); @@ -264,14 +290,23 @@ static void mln_log_atfork_unlock(void) void mln_log_destroy(void) { mln_log_t *log = &g_log; +#if defined(MSVC) + if (log->fd > 0 && \ + log->fd != _fileno(stdin) && \ + log->fd != _fileno(stdout) && \ + log->fd != _fileno(stderr)) +#else if (log->fd > 0 && \ log->fd != STDIN_FILENO && \ log->fd != STDOUT_FILENO && \ log->fd != STDERR_FILENO) +#endif { close(log->fd); } +#if !defined(MSVC) mln_spin_destroy(&(log->thread_lock)); +#endif } /* @@ -331,12 +366,16 @@ static int mln_log_set_level(mln_log_t *log, mln_conf_t *cf, int is_init) */ int mln_log_reload(void *data) { +#if !defined(MSVC) mln_spin_lock(&(g_log.thread_lock)); +#endif mln_log_get_log(&g_log, mln_conf(), 0); mln_file_lock(g_log.fd); int ret = mln_log_set_level(&g_log, mln_conf(), 0); mln_file_unlock(g_log.fd); +#if !defined(MSVC) mln_spin_unlock(&(g_log.thread_lock)); +#endif return ret; } @@ -350,7 +389,9 @@ void _mln_sys_log(mln_log_level_t level, \ char *msg, \ ...) { +#if !defined(MSVC) mln_spin_lock(&(g_log.thread_lock)); +#endif mln_file_lock(g_log.fd); va_list arg; va_start(arg, msg); @@ -358,42 +399,64 @@ void _mln_sys_log(mln_log_level_t level, \ _logger(&g_log, level, file, func, line, msg, arg); va_end(arg); mln_file_unlock(g_log.fd); +#if !defined(MSVC) mln_spin_unlock(&(g_log.thread_lock)); +#endif } -static inline ssize_t mln_log_write(mln_log_t *log, void *buf, mln_size_t size) +static inline int mln_log_write(mln_log_t *log, void *buf, mln_size_t size) { - ssize_t ret = write(log->fd, buf, size); + int ret = write(log->fd, buf, size); if (log->init && !log->in_daemon) { +#if defined(MSVC) + ret = write(_fileno(stderr), buf, size); +#else ret = write(STDERR_FILENO, buf, size); +#endif } return ret; } -static inline ssize_t mln_log_level_write(mln_log_t *log, mln_log_level_t level) +static inline int mln_log_level_write(mln_log_t *log, mln_log_level_t level) { - ssize_t ret = 0; + int ret = 0; switch (level) { case report: ret = write(log->fd, (void *)"REPORT: ", 8); if (log->init && !log->in_daemon) +#if defined(MSVC) + ret = write(_fileno(stderr), (void *)"REPORT: ", 17); +#else ret = write(STDERR_FILENO, (void *)"\e[34mREPORT\e[0m: ", 17); +#endif break; case debug: ret = write(log->fd, (void *)"DEBUG: ", 7); if (log->init && !log->in_daemon) +#if defined(MSVC) + ret = write(_fileno(stderr), (void *)"DEBUG: ", 16); +#else ret = write(STDERR_FILENO, (void *)"\e[32mDEBUG\e[0m: ", 16); +#endif break; case warn: ret = write(log->fd, (void *)"WARN: ", 6); if (log->init && !log->in_daemon) +#if defined(MSVC) + ret = write(_fileno(stderr), (void *)"WARN: ", 15); +#else ret = write(STDERR_FILENO, (void *)"\e[33mWARN\e[0m: ", 15); +#endif break; case error: ret = write(log->fd, (void *)"ERROR: ", 7); if (log->init && !log->in_daemon) +#if defined(MSVC) + ret = write(_fileno(stderr), (void *)"ERROR: ", 16); +#else ret = write(STDERR_FILENO, (void *)"\e[31mERROR\e[0m: ", 16); +#endif break; default: break; @@ -401,13 +464,17 @@ static inline ssize_t mln_log_level_write(mln_log_t *log, mln_log_level_t level) return ret; } -ssize_t mln_log_writen(void *buf, mln_size_t size) +int mln_log_writen(void *buf, mln_size_t size) { +#if !defined(MSVC) mln_spin_lock(&(g_log.thread_lock)); +#endif mln_file_lock(g_log.fd); - ssize_t n = mln_log_write(&g_log, buf, size); + int n = mln_log_write(&g_log, buf, size); mln_file_unlock(g_log.fd); +#if !defined(MSVC) mln_spin_unlock(&(g_log.thread_lock)); +#endif return n; } @@ -481,17 +548,7 @@ _mln_sys_log_process(mln_log_t *log, \ { memset(line_str, 0, sizeof(line_str)); mln_sauto_t num = va_arg(arg, long); -#if defined(__WIN32__) - #if defined(i386) || defined(__arm__) - int n = snprintf(line_str, sizeof(line_str)-1, "%ld", num); - #elif defined(__pentiumpro__) - int n = snprintf(line_str, sizeof(line_str)-1, "%I64d", num); - #else - int n = snprintf(line_str, sizeof(line_str)-1, "%lld", num); - #endif -#else int n = snprintf(line_str, sizeof(line_str)-1, "%ld", num); -#endif mln_log_write(log, (void *)line_str, n); break; } @@ -552,10 +609,7 @@ _mln_sys_log_process(mln_log_t *log, \ case 'i': { memset(line_str, 0, sizeof(line_str)); -#if defined(__WIN32__) && defined(__pentiumpro__) - long long num = va_arg(arg, long long); - int n = snprintf(line_str, sizeof(line_str)-1, "%I64d", num); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) long long num = va_arg(arg, long long); int n = snprintf(line_str, sizeof(line_str)-1, "%lld", num); #else @@ -568,10 +622,7 @@ _mln_sys_log_process(mln_log_t *log, \ case 'I': { memset(line_str, 0, sizeof(line_str)); -#if defined(__WIN32__) && defined(__pentiumpro__) - unsigned long long num = va_arg(arg, unsigned long long); - int n = snprintf(line_str, sizeof(line_str)-1, "%I64u", num); -#elif defined(__WIN32__) || defined(i386) || defined(__arm__) +#if defined(MSVC) || defined(i386) || defined(__arm__) unsigned long long num = va_arg(arg, unsigned long long); int n = snprintf(line_str, sizeof(line_str)-1, "%llu", num); #else diff --git a/src/mln_matrix.c b/src/mln_matrix.c index d501c22f..a4e571a7 100644 --- a/src/mln_matrix.c +++ b/src/mln_matrix.c @@ -136,12 +136,11 @@ MLN_FUNC(, mln_matrix_t *, mln_matrix_inverse, (mln_matrix_t *matrix), (matrix), return ret; }) -MLN_FUNC_VOID(, void, mln_matrix_dump, (mln_matrix_t *matrix), (matrix), { +void mln_matrix_dump(mln_matrix_t *matrix) +{ if (matrix == NULL) return; mln_size_t i, sum = matrix->row * matrix->col; -#if defined(__WIN32__) && !defined(__pentiumpro__) - printf("Matrix row:%llu col:%llu\n ", matrix->row, matrix->col); -#elif defined(i386) || defined(__arm__) || defined(__WIN32__) +#if defined(i386) || defined(__arm__) || defined(MSVC) printf("Matrix row:%u col:%u\n ", matrix->row, matrix->col); #else printf("Matrix row:%lu col:%lu\n ", matrix->row, matrix->col); @@ -153,5 +152,5 @@ MLN_FUNC_VOID(, void, mln_matrix_dump, (mln_matrix_t *matrix), (matrix), { printf("%f ", matrix->data[i]); } printf("\n"); -}) +} diff --git a/src/mln_parser_generator.c b/src/mln_parser_generator.c index e90115ed..932a24da 100644 --- a/src/mln_parser_generator.c +++ b/src/mln_parser_generator.c @@ -868,29 +868,10 @@ MLN_FUNC_VOID(, void, mln_pg_output_state, (mln_pg_state_t *s), (s), { mln_u32_t i; printf("STATES:\n"); for (; s != NULL; s = s->next) { -#if defined(__WIN32__) - #if defined(i386) || defined(__arm__) - printf("State %ld: input: [%s] nr_item:%I64u\n", \ - s->id, \ - s->input==NULL?"(null)":(char *)(s->input->token->data), \ - (unsigned long long)(s->nr_item)); - #elif defined(__pentiumpro__) - printf("State %I64d: input: [%s] nr_item:%I64u\n", \ - s->id, \ - s->input==NULL?"(null)":(char *)(s->input->token->data), \ - (unsigned long long)(s->nr_item)); - #else - printf("State %lld: input: [%s] nr_item:%llu\n", \ - s->id, \ - s->input==NULL?"(null)":(char *)(s->input->token->data), \ - (unsigned long long)(s->nr_item)); - #endif -#else printf("State %ld: input: [%s] nr_item:%llu\n", \ s->id, \ s->input==NULL?"(null)":(char *)(s->input->token->data), \ (unsigned long long)(s->nr_item)); -#endif printf("Items:\n"); for (it = s->head; it != NULL; it = it->next) { printf("rule: %s->", (char *)(it->rule->left->token->data)); @@ -898,11 +879,7 @@ MLN_FUNC_VOID(, void, mln_pg_output_state, (mln_pg_state_t *s), (s), { if (it->pos == i) printf("."); printf(" %s", (char *)((it->rule->rights[i])->token->data)); } -#if defined(__WIN32__) && !defined(__pentiumpro__) - printf("\t\tread:[%s] goto_id:%lld", it->read==NULL?"(null)":(char *)(it->read->token->data), it->goto_id); -#else printf("\t\tread:[%s] goto_id:%ld", it->read==NULL?"(null)":(char *)(it->read->token->data), it->goto_id); -#endif printf("\tLookahead:"); mln_rbtree_iterate(it->lookahead_set, mln_pg_output_state_iterate_handler, NULL); printf("\n"); diff --git a/src/mln_path.c b/src/mln_path.c index df6d2206..c837156f 100644 --- a/src/mln_path.c +++ b/src/mln_path.c @@ -8,6 +8,15 @@ static char install_path[] = MLN_ROOT; +#if defined(MSVC) +static char conf_path[] = MLN_ROOT"\\conf\\melon.conf"; + +static char tmpfile_path[] = MLN_ROOT"\\tmp"; + +static char pid_path[] = MLN_ROOT"\\logs\\melon.pid"; + +static char log_path[] = MLN_ROOT"\\logs\\melon.log"; +#else static char conf_path[] = MLN_ROOT"/conf/melon.conf"; static char tmpfile_path[] = MLN_ROOT"/tmp"; @@ -15,6 +24,7 @@ static char tmpfile_path[] = MLN_ROOT"/tmp"; static char pid_path[] = MLN_ROOT"/logs/melon.pid"; static char log_path[] = MLN_ROOT"/logs/melon.log"; +#endif static char null_path[] = MLN_NULL; diff --git a/src/mln_prime_generator.c b/src/mln_prime_generator.c index 825e5524..6dc85cd4 100644 --- a/src/mln_prime_generator.c +++ b/src/mln_prime_generator.c @@ -5,7 +5,11 @@ #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include "mln_types.h" #include "mln_func.h" diff --git a/src/mln_rbtree.c b/src/mln_rbtree.c index bcb2eea6..693b5df0 100644 --- a/src/mln_rbtree.c +++ b/src/mln_rbtree.c @@ -297,3 +297,12 @@ MLN_FUNC(, int, mln_rbtree_iterate, \ return 0; }) +#if defined(MSVC) +mln_rbtree_node_t *mln_rbtree_node_init(mln_rbtree_node_t *n, void *data) +{ + n->data = data; + n->nofree = 1; + return n; +} +#endif + diff --git a/src/mln_regexp.c b/src/mln_regexp.c index 624aa396..706e07c1 100644 --- a/src/mln_regexp.c +++ b/src/mln_regexp.c @@ -6,7 +6,6 @@ #include #include #include -#include #include "mln_regexp.h" #include "mln_func.h" diff --git a/src/mln_rs.c b/src/mln_rs.c index b87fd359..d3b8f7e8 100644 --- a/src/mln_rs.c +++ b/src/mln_rs.c @@ -356,12 +356,11 @@ MLN_FUNC(static, mln_rs_matrix_t *, mln_rs_matrix_data_matrix, \ return matrix; }) -MLN_FUNC_VOID(, void, mln_rs_matrix_dump, (mln_rs_matrix_t *matrix), (matrix), { +void mln_rs_matrix_dump(mln_rs_matrix_t *matrix) +{ if (matrix == NULL) return; mln_size_t i, sum = matrix->row * matrix->col; -#if defined(__WIN32__) && !defined(__pentiumpro__) - printf("Matrix row:%llu col:%llu\n ", matrix->row, matrix->col); -#elif defined(i386) || defined(__arm__) || defined(__WIN32__) +#if defined(i386) || defined(__arm__) || defined(MSVC) printf("Matrix row:%u col:%u\n ", matrix->row, matrix->col); #else printf("Matrix row:%lu col:%lu\n ", matrix->row, matrix->col); @@ -373,7 +372,7 @@ MLN_FUNC_VOID(, void, mln_rs_matrix_dump, (mln_rs_matrix_t *matrix), (matrix), { printf("%u ", matrix->data[i]); } printf("\n"); -}) +} /*mln_rs_result_t*/ MLN_FUNC(static, mln_rs_result_t *, mln_rs_result_new, \ diff --git a/src/mln_rsa.c b/src/mln_rsa.c index e94e43f3..a8bfcb97 100644 --- a/src/mln_rsa.c +++ b/src/mln_rsa.c @@ -2,13 +2,17 @@ /* * Copyright (C) Niklaus F.Schen. */ -#if defined(__WIN32__) +#if defined(MSVC) #define _CRT_RAND_S #endif #include #include #include +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include "mln_rsa.h" #include "mln_chain.h" #include "mln_md5.h" @@ -148,9 +152,7 @@ MLN_FUNC(static inline, int, mln_rsa_rsaep_rsadp, \ return 0; }) -MLN_FUNC_VOID(static inline, void, mln_rsa_pub_padding, \ - (mln_u8ptr_t in, mln_size_t inlen, mln_u8ptr_t out, mln_size_t keylen), \ - (in, inlen, out, keylen), \ +static inline void mln_rsa_pub_padding(mln_u8ptr_t in, mln_size_t inlen, mln_u8ptr_t out, mln_size_t keylen) { mln_size_t j; struct timeval tv; @@ -162,7 +164,7 @@ MLN_FUNC_VOID(static inline, void, mln_rsa_pub_padding, \ val = tv.tv_sec*1000000+tv.tv_usec; for (j = keylen - inlen - 3; j > 0; --j) { lp: -#if defined(__WIN32__) +#if defined(MSVC) rand_s(&val); *out = val; #else @@ -177,7 +179,7 @@ MLN_FUNC_VOID(static inline, void, mln_rsa_pub_padding, \ } *out++ = 0; memcpy(out, in, inlen); -}) +} MLN_FUNC_VOID(static inline, void, mln_rsa_pri_padding, \ (mln_u8ptr_t in, mln_size_t inlen, mln_u8ptr_t out, mln_size_t keylen), \ diff --git a/src/mln_span.c b/src/mln_span.c index 124a7760..8082ac58 100644 --- a/src/mln_span.c +++ b/src/mln_span.c @@ -10,7 +10,7 @@ mln_span_stack_node_t *__mln_span_stack_top = NULL; mln_span_stack_node_t *__mln_span_stack_bottom = NULL; mln_span_t *mln_span_root = NULL; -#if defined(__WIN32__) +#if defined(MSVC) DWORD mln_span_registered_thread; #else pthread_t mln_span_registered_thread; @@ -149,7 +149,7 @@ int mln_span_entry(void *fptr, const char *file, const char *func, int line, ... { mln_span_t *span; -#if defined(__WIN32__) +#if defined(MSVC) if (mln_span_registered_thread != GetCurrentThreadId()) return 0; #else if (!pthread_equal(mln_span_registered_thread, pthread_self())) return 0; @@ -170,7 +170,7 @@ int mln_span_entry(void *fptr, const char *file, const char *func, int line, ... void mln_span_exit(void *fptr, const char *file, const char *func, int line, void *ret, ...) { -#if defined(__WIN32__) +#if defined(MSVC) if (mln_span_registered_thread != GetCurrentThreadId()) return; #else if (!pthread_equal(mln_span_registered_thread, pthread_self())) return; @@ -197,6 +197,26 @@ static void __mln_span_dump(mln_span_t *s, mln_span_dump_cb_t cb, void *data, in void mln_span_dump(mln_span_dump_cb_t cb, void *data) { - return __mln_span_dump(mln_span_root, cb, data, 0); + __mln_span_dump(mln_span_root, cb, data, 0); } +#if defined(MSVC) +void mln_span_release(void) +{ + mln_span_free(mln_span_root); + mln_span_root = NULL; +} + +mln_span_t *mln_span_move(void) +{ + mln_span_t *span = mln_span_root; + mln_span_root = NULL; + return span; +} + +mln_u64_t mln_span_time_cost(mln_span_t *s) +{ + return (mln_u64_t)(s->end.tv_sec * 1000000 + s->end.tv_usec) - (s->begin.tv_sec * 1000000 + s->begin.tv_usec); +} +#endif + diff --git a/src/mln_string.c b/src/mln_string.c index 764b9f45..3a43dad9 100644 --- a/src/mln_string.c +++ b/src/mln_string.c @@ -39,6 +39,47 @@ int strncasecmp(const char *s1, const char *s2, size_t n) { #endif +#if defined(MSVC) +mln_string_t *mln_string_set(mln_string_t *str, char *s) +{ + str->data = (mln_u8ptr_t)s; + str->len = strlen(s); + str->data_ref = 1; + str->pool = 0; + str->ref = 1; + return str; +} + +mln_string_t *mln_string_nset(mln_string_t *str, char *s, mln_u64_t n) +{ + str->data = (mln_u8ptr_t)s; + str->len = n; + str->data_ref = 1; + str->pool = 0; + str->ref = 1; + return str; +} + +mln_string_t *mln_string_ref(mln_string_t *s) +{ + ++s->ref; + return s; +} + +void mln_string_free(mln_string_t *s) +{ + if (s == NULL) return; + if (s->ref-- > 1) return; + if (!s->data_ref && s->data != NULL) { + if (s->pool) mln_alloc_free(s->data); + else free(s->data); + } + if (s->pool) mln_alloc_free(s); + else free(s); +} + +#endif + MLN_FUNC(static inline, mln_string_t *, mln_string_assign, (char *s, mln_u32_t len), (s, len), { mln_string_t *str; str = (mln_string_t *)malloc(sizeof(mln_string_t)); diff --git a/src/mln_thread.c b/src/mln_thread.c index f4611b65..2b973241 100644 --- a/src/mln_thread.c +++ b/src/mln_thread.c @@ -2,13 +2,14 @@ /* * Copyright (C) Niklaus F.Schen. */ + +#if !defined(MSVC) + #include #include #include -#if !defined(__WIN32__) #include #include -#endif #include #include #include @@ -751,3 +752,5 @@ MLN_FUNC_VOID(static, void, mln_thread_msgq_destroy, (mln_thread_msgq_t *tmq), ( free(tmq); }) +#endif + diff --git a/src/mln_thread_pool.c b/src/mln_thread_pool.c index dc4419fc..fe5a81c2 100644 --- a/src/mln_thread_pool.c +++ b/src/mln_thread_pool.c @@ -2,6 +2,9 @@ /* * Copyright (C) Niklaus F.Schen. */ + +#if !defined(MSVC) + #include #include #include "mln_thread_pool.h" @@ -115,7 +118,6 @@ MLN_FUNC_VOID(static, void, mln_thread_pool_member_exit, (void *arg), (arg), { /* * thread_pool */ -#if !defined(__WIN32__) MLN_FUNC_VOID(static, void, mln_thread_pool_prepare, (void), (), { if (m_thread_pool_self == NULL) return; if (!m_thread_pool_self->locked) @@ -142,7 +144,6 @@ MLN_FUNC_VOID(static, void, mln_thread_pool_child, (void), (), { mln_thread_pool_member_exit(fr); } }) -#endif MLN_FUNC(static, mln_thread_pool_t *, mln_thread_pool_new, \ (struct mln_thread_pool_attr *tpattr, int *err), (tpattr, err), \ @@ -188,10 +189,9 @@ MLN_FUNC(static, mln_thread_pool_t *, mln_thread_pool_new, \ tp->process_handler = tpattr->child_process_handler; tp->free_handler = tpattr->free_handler; tp->max = tpattr->max; -#ifdef MLN_USE_UNIX98 +#if defined(MLN_USE_UNIX98) && !defined(MSYS2) if (tpattr->concurrency) pthread_setconcurrency(tpattr->concurrency); #endif -#if !defined(__WIN32__) if ((rc = pthread_atfork(mln_thread_pool_prepare, \ mln_thread_pool_parent, \ mln_thread_pool_child)) != 0) @@ -203,7 +203,6 @@ MLN_FUNC(static, mln_thread_pool_t *, mln_thread_pool_new, \ *err = rc; return NULL; } -#endif if ((m_thread_pool_self = mln_thread_pool_member_join(tp, 0)) == NULL) { pthread_attr_destroy(&(tp->attr)); pthread_cond_destroy(&(tp->cond)); @@ -430,3 +429,5 @@ MLN_CHAIN_FUNC_DEFINE(static inline, \ prev, \ next); +#endif + diff --git a/src/mln_tools.c b/src/mln_tools.c index a53fa240..bdabf8b5 100644 --- a/src/mln_tools.c +++ b/src/mln_tools.c @@ -3,14 +3,14 @@ * Copyright (C) Niklaus F.Schen. */ -#if !defined(__WIN32__) -#include -#include -#endif #include #include #include +#if !defined(MSVC) +#include +#include #include +#endif #include #include #include @@ -22,7 +22,7 @@ static int mln_boot_help(const char *boot_str, const char *alias); -#if !defined(__WIN32__) +#if !defined(MSVC) static int mln_set_id(void); static int mln_boot_reload(const char *boot_str, const char *alias); @@ -38,7 +38,7 @@ long mon_days[2][12] = { }; mln_boot_t boot_params[] = { {"--help", "-h", mln_boot_help, 0}, -#if !defined(__WIN32__) +#if !defined(MSVC) {"--reload", "-r", mln_boot_reload, 0}, {"--stop", "-s", mln_boot_stop, 0} #endif @@ -54,7 +54,8 @@ MLN_FUNC(, int, mln_sys_limit_modify, (void), (), { return mln_sys_nofile_modify(); }) -MLN_FUNC(static, int, mln_sys_core_modify, (void), (), { +static int mln_sys_core_modify(void) +{ #ifdef RLIMIT_CORE rlim_t core_file_size = 0; @@ -94,9 +95,10 @@ MLN_FUNC(static, int, mln_sys_core_modify, (void), (), { } #endif return 0; -}) +} -MLN_FUNC(static, int, mln_sys_nofile_modify, (void), (), { +static int mln_sys_nofile_modify(void) +{ #ifdef RLIMIT_NOFILE rlim_t nofile = 0; @@ -136,9 +138,9 @@ MLN_FUNC(static, int, mln_sys_nofile_modify, (void), (), { } #endif return 0; -}) +} -#if !defined(__WIN32__) +#if !defined(MSVC) MLN_FUNC(, int, mln_daemon, (void), (), { int ret = mln_log_init(NULL); if (ret < 0) return ret; @@ -284,7 +286,7 @@ MLN_FUNC(static, int, mln_boot_help, \ return 0; }) -#if !defined(__WIN32__) +#if !defined(MSVC) MLN_FUNC(static, int, mln_boot_reload, \ (const char *boot_str, const char *alias), \ (boot_str, alias), \ diff --git a/src/mln_utils.c b/src/mln_utils.c index ca74f722..e2806823 100644 --- a/src/mln_utils.c +++ b/src/mln_utils.c @@ -2,8 +2,7 @@ /* * Copyright (C) Niklaus F.Schen. */ -#include "mln_types.h" -#if defined(__WIN32__) +#if defined(MSVC) #include #include #include @@ -44,6 +43,8 @@ int socketpair(int domain, int type, int protocol, int sv[2]) } #endif +#include "mln_types.h" + #if defined(__GNUC__) && (__GNUC__ >= 4 && __GNUC_MINOR__ > 1) @@ -112,3 +113,80 @@ void usleep(unsigned long usec) nanosleep(&req, NULL); } #endif + +#if defined(MSVC) +#define EPOCH 116444736000000000ULL +int gettimeofday(struct timeval *tv, void *tz) +{ + FILETIME ft; + mln_u64_t tmpres = 0; + + if (tv != NULL) { + GetSystemTimeAsFileTime(&ft); + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + tmpres /= 10; + tmpres -= EPOCH; + tv->tv_sec = tmpres / 1000000; + tv->tv_usec = tmpres % 1000000; + } + + return 0; +} + +DIR* opendir(const char* path) +{ + DIR* dir = (DIR*)malloc(sizeof(DIR)); + if (dir == NULL) { + return NULL; + } + + char searchPath[MAX_PATH]; + snprintf(searchPath, MAX_PATH, "%s\\*", path); + dir->hFind = FindFirstFileA(searchPath, &dir->findFileData); + if (dir->hFind == INVALID_HANDLE_VALUE) { + free(dir); + return NULL; + } + + dir->path = _strdup(path); + InitializeCriticalSection(&dir->lock); + return dir; +} + +struct dirent* readdir(DIR* dirp) +{ + if (dirp == NULL || dirp->hFind == INVALID_HANDLE_VALUE) { + return NULL; + } + + EnterCriticalSection(&dirp->lock); + static struct dirent entry; + if (FindNextFileA(dirp->hFind, &dirp->findFileData) != 0) { + strncpy(entry.d_name, dirp->findFileData.cFileName, sizeof(entry.d_name)); + entry.d_name[sizeof(entry.d_name) - 1] = '\0'; + LeaveCriticalSection(&dirp->lock); + return &entry; + } + + LeaveCriticalSection(&dirp->lock); + return NULL; +} + +int closedir(DIR* dirp) +{ + if (dirp == NULL || dirp->hFind == INVALID_HANDLE_VALUE) { + return -1; + } + + EnterCriticalSection(&dirp->lock); + FindClose(dirp->hFind); + free(dirp->path); + LeaveCriticalSection(&dirp->lock); + DeleteCriticalSection(&dirp->lock); + free(dirp); + return 0; +} +#endif + diff --git a/src/mln_websocket.c b/src/mln_websocket.c index e6b047ac..70987d44 100644 --- a/src/mln_websocket.c +++ b/src/mln_websocket.c @@ -4,13 +4,16 @@ */ #include -#include #include #include "mln_websocket.h" #include "mln_regexp.h" #include "mln_sha.h" #include "mln_base64.h" +#if defined(MSVC) +#include "mln_utils.h" +#else #include +#endif #include "mln_func.h" static mln_u64_t mln_websocket_hash_calc(mln_hash_t *h, void *key);