Skip to content

Commit

Permalink
fix configure and add a macro for mmap detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Sep 27, 2023
1 parent c6c5ba9 commit 5d3442a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 101 deletions.
184 changes: 89 additions & 95 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,90 @@ else
debug=''
fi


#event
event_flag="-DMLN_SELECT"

echo "#include<stdio.h>
#include<sys/epoll.h>
int main(void){epoll_create(10);return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
event_flag="-DMLN_EPOLL"
echo -e "event\t\t\t[EPOLL]"
rm -f ev_test ev_test.c
fi

echo "#include<stdio.h>
#include<sys/types.h>
#include<sys/event.h>
#include<sys/time.h>
int main(void){kqueue();return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
event_flag="-DMLN_KQUEUE"
echo -e "event\t\t\t[KQUEUE]"
rm -f ev_test ev_test.c
fi
rm -f ev_test ev_test.c
echo -e "event\t\t\t[SELECT]"


#sendfile
sendfile_flag=""
echo "#include <sys/sendfile.h>
int main(void){sendfile(1,0,0,1);return 0;}" > sendfile_test.c
$cc -o sendfile_test sendfile_test.c 2>/dev/null
if [ "$?" == "0" ]; then
sendfile_flag="-DMLN_SENDFILE"
echo -e "sendfile\t\t[support]"
else
echo -e "sendfile\t\t[NOT support]"
fi
rm -f sendfile_test sendfile_test.c


#writev
writev_flag=""
echo -e "#include <stdio.h>\n#include <sys/uio.h>" > writev_test.c
echo "int main(void){writev(0,NULL,0);return 0;}" >> writev_test.c
$cc -o writev_test writev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
writev_flag="-DMLN_WRITEV"
echo -e "writev\t\t\t[support]"
else
echo -e "writev\t\t\t[NOT support]"
fi
rm -f writev_test writev_test.c


#unix98
unix98_flag=""
echo -e "#ifndef __USE_UNIX98\n#define __USE_UNIX98\n#endif\n#include <pthread.h>\n" > unix98_test.c
echo "int main(void){pthread_setconcurrency(0);return 0;}" >> unix98_test.c
$cc -o unix98_test unix98_test.c -lpthread 2>/dev/null
if [ "$?" == "0" ]; then
echo -e "__USE_UNIX98\t\t[support]"
unix98_flag="-DMLN_USE_UNIX98"
else
echo -e "__USE_UNIX98\t\t[not support]"
fi
rm -fr unix98_test unix98_test.c


#mmap
mmap_flag=""
echo -e "#include <stdio.h>\n#include <sys/mman.h>" >> mmap_test.c
echo "int main(void){mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0);return 0;}" >> mmap_test.c
$cc -o mmap_test mmap_test.c 2>/dev/null
if [ "$?" == "0" ]; then
mmap_flag="-DMLN_MMAP"
echo -e "mmap\t\t\t[support]"
else
echo -e "mmap\t\t\t[NOT support]"
fi
rm -f mmap_test mmap_test.c

#output installation path
echo -e "Installation Path \t[$install_path]"
echo -e "Melang script Path \t[$melang_script_path]"
Expand Down Expand Up @@ -210,7 +294,7 @@ echo "}" >> src/mln_path.c
get_source_files() {
files=()
if [ $1 = 'all' ]; then
for f in `find src/ -name "*.c"`
for f in `find src -name "*.c"`
do
files+=$f" "
done
Expand All @@ -221,7 +305,7 @@ get_source_files() {
do
IFS=$old_ifs
fname='mln_'$f'.c'
fname=`find src/ -name $fname`
fname=`find src -name $fname`
if [ -z $fname ]; then
echo "[$f] not found"
exit 1
Expand All @@ -232,7 +316,7 @@ get_source_files() {
suffix=`echo $header | cut -d '.' -f 2`
if [ $suffix == 'h' ]; then
source_file=`echo $header | cut -d '.' -f 1`'.c'
source_file=`find src/ -name $source_file`
source_file=`find src -name $source_file`
if [ ! -z $source_file ]; then
if [[ ! "${files[@]}" =~ $source_file ]]; then
files+=$source_file" "
Expand Down Expand Up @@ -342,99 +426,9 @@ do

if [ $wasm -eq 1 ]; then
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname" >> Makefile
continue
fi

test $fname = "./src/mln_event.c"
if [ $? -eq 0 ]; then
#test event system call
echo "#include<stdio.h>
#include<sys/epoll.h>
int main(void){epoll_create(10);return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
echo -e "event\t\t\t[EPOLL]"
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_EPOLL" >> Makefile
rm -f ev_test ev_test.c
continue
fi

echo "#include<stdio.h>
#include<sys/types.h>
#include<sys/event.h>
#include<sys/time.h>
int main(void){kqueue();return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
echo -e "event\t\t\t[KQUEUE]"
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_KQUEUE" >> Makefile
rm -f ev_test ev_test.c
continue
fi
rm -f ev_test ev_test.c

echo -e "event\t\t\t[SELECT]"
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_SELECT" >> Makefile
continue
fi

#test sendfile & writev
test $fname = "./src/mln_connection.c"
if [ $? -eq 0 ]; then
sendfile=0
echo "#include <sys/sendfile.h>
int main(void){sendfile(1,0,0,1);return 0;}" > sendfile_test.c
$cc -o sendfile_test sendfile_test.c 2>/dev/null
if [ "$?" == "0" ]; then
sendfile=1
echo -e "sendfile\t\t[support]"
else
echo -e "sendfile\t\t[NOT support]"
fi
rm -f sendfile_test sendfile_test.c

#test writev
echo -e "#include <stdio.h>\n#include <sys/uio.h>" > writev_test.c
echo "int main(void){writev(0,NULL,0);return 0;}" >> writev_test.c
$cc -o writev_test writev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
echo -e "writev\t\t\t[support]"
if [ $sendfile -eq "1" ]; then
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_SENDFILE -DMLN_WRITEV" >> Makefile
else
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_WRITEV" >> Makefile
fi
else
echo -e "writev\t\t\t[NOT support]"
if [ $sendfile -eq "1" ]; then
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_SENDFILE" >> Makefile
else
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname" >> Makefile
fi
fi
rm -f writev_test writev_test.c
continue
fi

#test __USE_UNIX98
test $fname = "./src/mln_thread_pool.c"
if [ $? -eq 0 ]; then
unix98=0
echo -e "#ifndef __USE_UNIX98\n#define __USE_UNIX98\n#endif\n#include <pthread.h>\n" > unix98_test.c
echo "int main(void){pthread_setconcurrency(0);return 0;}" >> unix98_test.c
$cc -o unix98_test unix98_test.c -lpthread 2>/dev/null
if [ "$?" == "0" ]; then
echo -e "__USE_UNIX98\t\t[support]"
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname -DMLN_USE_UNIX98" >> Makefile
else
echo -e "__USE_UNIX98\t\t[not support]"
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname" >> Makefile
fi
rm -fr unix98_test unix98_test.c
continue
else
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag" >> Makefile
fi

echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname" >> Makefile
done

#generate conf file
Expand Down
3 changes: 0 additions & 3 deletions include/mln_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
#else
#include <sys/mman.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mln_types.h"

typedef int (*mln_alloc_shm_lock_cb_t)(void *);
Expand Down
8 changes: 8 additions & 0 deletions src/mln_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "mln_alloc.h"
#include "mln_utils.h"
#include <stdlib.h>
#include <string.h>


MLN_CHAIN_FUNC_DECLARE(mln_blk, \
Expand Down Expand Up @@ -119,8 +121,12 @@ mln_alloc_t *mln_alloc_shm_init(struct mln_alloc_shm_attr_s *attr)
pool->map_handle = handle;
#else

#if defined(MLN_MMAP)
pool = (mln_alloc_t *)mmap(NULL, attr->size, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
if (pool == NULL) return NULL;
#else
return NULL;
#endif
#endif
pool->parent = NULL;
pool->large_used_head = pool->large_used_tail = NULL;
Expand Down Expand Up @@ -219,7 +225,9 @@ void mln_alloc_destroy(mln_alloc_t *pool)
UnmapViewOfFile(pool->mem);
CloseHandle(handle);
#else
#if defined(MLN_MMAP)
munmap(pool->mem, pool->shm_size);
#endif
#endif
}
if (parent != NULL && mln_alloc_is_shm(parent))
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_buf_t *mln_buf_new(mln_alloc_t *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)
#if !defined(WIN32) && defined(MLN_MMAP)
b->mmap = 0;
#endif
b->flush = b->sync = b->last_buf = b->last_in_chain = 0;
Expand All @@ -39,7 +39,7 @@ void mln_buf_pool_release(mln_buf_t *b)
}

if (b->in_memory) {
#if !defined(WIN32)
#if !defined(WIN32) && defined(MLN_MMAP)
if (b->mmap) {
if (b->start != NULL) {
munmap(b->start, b->end - b->start);
Expand All @@ -53,7 +53,7 @@ void mln_buf_pool_release(mln_buf_t *b)
} else {
mln_alloc_free(b->pos);
}
#if !defined(WIN32)
#if !defined(WIN32) && defined(MLN_MMAP)
}
#endif
mln_alloc_free(b);
Expand Down

0 comments on commit 5d3442a

Please sign in to comment.