Skip to content

Commit

Permalink
chore(*): replace predefined macros in Win and define usleep only whe…
Browse files Browse the repository at this point in the history
…n compiling with c99 on Linux
  • Loading branch information
Water-Melon committed Mar 7, 2024
1 parent 7e739f3 commit 0bd0074
Show file tree
Hide file tree
Showing 45 changed files with 136 additions and 132 deletions.
6 changes: 5 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ func_flag=""
# C99
c99_flag=""

# compile_flags_for_mingw
mingw_cflags=""


#Functions
set_melang_default_paths() {
if ! case $sysname in MINGW*) false;; esac; then
melang_script_path=$HOME/lib/melang
melang_dylib_path=$HOME/lib/melang_dynamic
mingw_cflags="-Wno-error=use-after-free -Wno-use-after-free"
else
melang_script_path=/usr/local/lib/melang
melang_dylib_path=/usr/local/lib/melang_dynamic
Expand Down Expand Up @@ -454,7 +458,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" >> Makefile
else
echo -e "FLAGS\t\t= -Iinclude -c -Wall $debug -Werror $olevel -fPIC $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag $func_flag $c99_flag" >> Makefile
echo -e "FLAGS\t\t= -Iinclude -c -Wall $debug -Werror $mingw_cflags $olevel -fPIC $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag $func_flag $c99_flag" >> Makefile
fi
if ! case $sysname in MINGW*) false;; esac; then
if [ $wasm -eq 0 ]; then
Expand Down
4 changes: 2 additions & 2 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3405,7 +3405,7 @@ Their definitions can be found in melon/include/mln_types.h.
return -1;
}
memset(&msg, 0, sizeof(msg));
#if defined(WIN32)
#if defined(____WIN32____)
int n = recv(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = recv(fd, &msg, sizeof(msg), 0);
Expand Down Expand Up @@ -3439,7 +3439,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(____WIN32____)
int n = send(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = send(fd, &msg, sizeof(msg), 0);
Expand Down
4 changes: 2 additions & 2 deletions docs/book/cn/multithread.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
return -1;
}
memset(&msg, 0, sizeof(msg));
#if defined(WIN32)
#if defined(__WIN32__)
int n = recv(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = recv(fd, &msg, sizeof(msg), 0);
Expand Down Expand Up @@ -74,7 +74,7 @@
msg.c = 'N';
msg.type = ITC_REQUEST;
msg.need_clear = 1;
#if defined(WIN32)
#if defined(__WIN32__)
int n = send(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = send(fd, &msg, sizeof(msg), 0);
Expand Down
2 changes: 1 addition & 1 deletion docs/book/cn/tcp_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(__WIN32__)
mln_u32_t mmap:1;//是否是mmap创建的内存,win下暂不支持
#endif
mln_u32_t in_memory:1;//数据是否在内存中
Expand Down
4 changes: 2 additions & 2 deletions docs/book/en/multithread.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Different from the thread pool described above, the multi-threaded framework is
return -1;
}
memset(&msg, 0, sizeof(msg));
#if defined(WIN32)
#if defined(__WIN32__)
int n = recv(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = recv(fd, &msg, sizeof(msg), 0);
Expand Down Expand Up @@ -73,7 +73,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)
#if defined(__WIN32__)
int n = send(fd, (char *)&msg, sizeof(msg), 0);
#else
int n = send(fd, &msg, sizeof(msg), 0);
Expand Down
2 changes: 1 addition & 1 deletion docs/book/en/tcp_io.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(__WIN32__)
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
Expand Down
4 changes: 2 additions & 2 deletions include/mln_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef __MLN_ALLOC_H
#define __MLN_ALLOC_H

#if defined(WIN32)
#if defined(__WIN32__)
#include <windows.h>
#include <winbase.h>
#else
Expand Down Expand Up @@ -88,7 +88,7 @@ struct mln_alloc_s {
void *locker;
mln_alloc_shm_lock_cb_t lock;
mln_alloc_shm_lock_cb_t unlock;
#if defined(WIN32)
#if defined(__WIN32__)
HANDLE map_handle;
#endif
struct mln_alloc_s *parent;
Expand Down
2 changes: 1 addition & 1 deletion include/mln_chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(__WIN32__)
mln_u32_t mmap:1;
#endif
mln_u32_t in_memory:1;
Expand Down
2 changes: 1 addition & 1 deletion include/mln_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __MLN_CONNECTION_H
#define __MLN_CONNECTION_H

#if defined(WIN32)
#if defined(__WIN32__)
#include <winsock2.h>
#else
#include <sys/socket.h>
Expand Down
2 changes: 1 addition & 1 deletion include/mln_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#elif defined(MLN_KQUEUE)
#include <sys/event.h>
#else
#if defined(WIN32)
#if defined(__WIN32__)
#include <winsock2.h>
#include <winsock2.h>
#else
Expand Down
2 changes: 1 addition & 1 deletion include/mln_fheap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(__WIN32__)
#define FH_LGN 33
#else
#define FH_LGN 65
Expand Down
2 changes: 1 addition & 1 deletion include/mln_fork.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
#ifndef __MLN_FORK_H
#define __MLN_FORK_H
#if !defined(WIN32)
#if !defined(__WIN32__)
#include <sys/types.h>
#include "mln_types.h"
#include "mln_event.h"
Expand Down
4 changes: 2 additions & 2 deletions include/mln_framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#include "mln_event.h"

typedef int (*mln_framework_init_t)(void);
#if !defined(WIN32)
#if !defined(__WIN32__)
typedef void (*mln_framework_process_t)(mln_event_t *);
#endif

struct mln_framework_attr {
int argc;
char **argv;
mln_framework_init_t global_init;
#if !defined(WIN32)
#if !defined(__WIN32__)
mln_framework_process_t main_thread;
mln_framework_process_t master_process;
mln_framework_process_t worker_process;
Expand Down
2 changes: 1 addition & 1 deletion include/mln_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef __MLN_IPC_H
#define __MLN_IPC_H

#if !defined(WIN32)
#if !defined(__WIN32__)

#include "mln_types.h"
#include "mln_fork.h"
Expand Down
6 changes: 3 additions & 3 deletions include/mln_span.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <sys/time.h>
#include "mln_func.h"
#if defined(WIN32)
#if defined(__WIN32__)
#include <windows.h>
#else
#include <pthread.h>
Expand Down Expand Up @@ -37,7 +37,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(__WIN32__)
extern DWORD mln_span_registered_thread;
#else
extern pthread_t mln_span_registered_thread;
Expand All @@ -49,7 +49,7 @@ extern void mln_span_free(mln_span_t *s);
extern void mln_span_entry(const char *file, const char *func, int line);
extern void mln_span_exit(const char *file, const char *func, int line);

#if defined(WIN32)
#if defined(__WIN32__)
#define mln_span_start() ({\
int r = 0;\
mln_func_entry_callback_set(mln_span_entry);\
Expand Down
2 changes: 1 addition & 1 deletion include/mln_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct utctime {


extern int mln_sys_limit_modify(void);
#if !defined(WIN32)
#if !defined(__WIN32__)
extern int mln_daemon(void);
#endif
extern int mln_boot_params(int argc, char *argv[]);
Expand Down
6 changes: 3 additions & 3 deletions include/mln_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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(__WIN32__) || defined(__wasm__)
typedef unsigned long long mln_u64_t;
typedef long long mln_s64_t;
#else
Expand All @@ -37,7 +37,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(__WIN32__) || defined(__wasm__)
typedef long long * mln_s64ptr_t;
typedef unsigned long long * mln_u64ptr_t;
#else
Expand All @@ -46,7 +46,7 @@ typedef unsigned long * mln_u64ptr_t;
#endif
typedef size_t mln_size_t;
typedef off_t mln_off_t;
#if defined(WIN32)
#if defined(__WIN32__)
#if defined(i386) || defined(__arm__)
typedef long mln_sptr_t;
typedef unsigned long mln_uptr_t;
Expand Down
6 changes: 3 additions & 3 deletions include/mln_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/*
* nonnull attribute
*/
#if defined(__APPLE__) || defined(WIN32) || defined(__wasm__)
#if defined(__APPLE__) || defined(__WIN32__) || defined(__wasm__)
#define __NONNULL1(x)
#define __NONNULL2(x,y)
#define __NONNULL3(x,y,z)
Expand Down Expand Up @@ -142,7 +142,7 @@ extern int spin_trylock(void *lock);
}\
}

#if defined(WIN32)
#if defined(__WIN32__)
extern int pipe(int fds[2]);
extern int socketpair(int domain, int type, int protocol, int sv[2]);
#define mln_socket_close closesocket
Expand All @@ -156,7 +156,7 @@ extern int socketpair(int domain, int type, int protocol, int sv[2]);
#define mln_isprint(x) ((x) >= 32 && (x) <= 126)
#define mln_iswhitespace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\f' || (x) == '\r' || (x) == '\v')

#if defined(MLN_C99)
#if defined(MLN_C99) && defined(__linux__)
extern void usleep(unsigned long usec);
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/mln_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ MLN_FUNC(, mln_alloc_t *, mln_alloc_shm_init, \
(size, locker, lock, unlock), \
{
mln_alloc_t *pool;
#if defined(WIN32)
#if defined(__WIN32__)
HANDLE handle;
#endif

if (size < M_ALLOC_SHM_DEFAULT_SIZE+1024 || locker == NULL || lock == NULL || unlock == NULL)
return NULL;

#if defined(WIN32)
#if defined(__WIN32__)
if ((handle = CreateFileMapping(INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
Expand Down Expand Up @@ -235,7 +235,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(__WIN32__)
HANDLE handle = pool->map_handle;
UnmapViewOfFile(pool->mem);
CloseHandle(handle);
Expand Down
14 changes: 7 additions & 7 deletions src/mln_bignum.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* Copyright (C) Niklaus F.Schen.
*/
#if defined(WIN32)
#if defined(__WIN32__)
#define _CRT_RAND_S
#endif
#include <stdio.h>
Expand Down Expand Up @@ -911,9 +911,9 @@ MLN_FUNC_VOID(, void, mln_bignum_dump, (mln_bignum_t *bn), (bn), {
mln_u32_t i;
fprintf(stderr, "Data:\n");
for (i = 0; i < M_BIGNUM_SIZE; ++i) {
#if defined(WIN32) && defined(__pentiumpro__)
#if defined(__WIN32__) && defined(__pentiumpro__)
fprintf(stderr, "\t%I64x\n", bn->data[i]);
#elif defined(WIN32) || defined(i386) || defined(__arm__) || defined(__wasm__)
#elif defined(__WIN32__) || defined(i386) || defined(__arm__) || defined(__wasm__)
fprintf(stderr, "\t%llx\n", bn->data[i]);
#else
fprintf(stderr, "\t%lx\n", bn->data[i]);
Expand Down Expand Up @@ -994,7 +994,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(__WIN32__)
rand_s(&val);
#else
val = (mln_u32_t)rand_r(&val);
Expand All @@ -1003,7 +1003,7 @@ MLN_FUNC_VOID(static inline, void, mln_bignum_random_prime, \
}

if ((off = bitwidth % 32)) {
#if defined(WIN32)
#if defined(__WIN32__)
rand_s(&val);
data[i] = ((mln_u64_t)val * 0xfdfd) & 0xffffffff;
#else
Expand All @@ -1021,7 +1021,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(__WIN32__)
rand_s(&val);
data[i-1] = (mln_u32_t)val;
#else
Expand All @@ -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(__WIN32__)
rand_s(&val);
width = val % bitwidth;
#else
Expand Down
6 changes: 3 additions & 3 deletions src/mln_chain.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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(__WIN32__) && defined(MLN_MMAP)
b->mmap = 0;
#endif
b->flush = b->sync = b->last_buf = b->last_in_chain = 0;
Expand All @@ -37,7 +37,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(__WIN32__) && defined(MLN_MMAP)
if (b->mmap) {
if (b->start != NULL) {
munmap(b->start, b->end - b->start);
Expand All @@ -51,7 +51,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(__WIN32__) && defined(MLN_MMAP)
}
#endif
mln_alloc_free(b);
Expand Down
Loading

0 comments on commit 0bd0074

Please sign in to comment.