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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,33 @@ jobs:
run: |
cd $GITHUB_WORKSPACE
make --debug test

openbsd:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: build + test
uses: vmactions/openbsd-vm@v1
with:
usesh: true
prepare: |
pkg_add cmake
run: |
cmake -B build
cmake --build build -j $(sysctl -n hw.ncpu)
./build/qjs -qd

freebsd:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: build + test
uses: vmactions/freebsd-vm@v1
with:
usesh: true
prepare: |
pkg install -y cmake
run: |
cmake -B build
cmake --build build -j $(sysctl -n hw.ncpu)
./build/qjs -qd
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,7 @@ if(BUILD_QJS_LIBC)
list(APPEND qjs_sources quickjs-libc.c)
endif()
list(APPEND qjs_defines _GNU_SOURCE)
list(APPEND qjs_libs qjs)
if(NOT WIN32)
list(APPEND qjs_libs dl)
endif()
list(APPEND qjs_libs qjs ${CMAKE_DL_LIBS})
if(NOT MSVC)
list(APPEND qjs_libs m pthread)
endif()
Expand Down
20 changes: 20 additions & 0 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
#define alloca _alloca
#define ssize_t ptrdiff_t
#endif
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
Expand Down Expand Up @@ -331,4 +338,17 @@ void rqsort(void *base, size_t nmemb, size_t size,
int64_t js__gettimeofday_us(void);
uint64_t js__hrtime_ns(void);

static inline size_t js__malloc_usable_size(const void *ptr)
{
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(_WIN32)
return _msize((void *)ptr);
#elif defined(__linux__) || defined(__FreeBSD__)
return malloc_usable_size((void *)ptr);
#else
return 0;
#endif
}

#endif /* CUTILS_H */
45 changes: 6 additions & 39 deletions qjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#endif

#include "cutils.h"
#include "quickjs-libc.h"
Expand Down Expand Up @@ -128,23 +123,6 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
return ptr - dp->base;
}

/* default memory allocation functions with memory limitation */
static inline size_t js_trace_malloc_usable_size(void *ptr)
{
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(_WIN32)
return _msize(ptr);
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__)
return malloc_usable_size(ptr);
#else
/* change this to `return 0;` if compilation fails */
return malloc_usable_size(ptr);
#endif
}

static void
#if defined(_WIN32) && !defined(__clang__)
/* mingw printf is used */
Expand All @@ -168,7 +146,7 @@ __attribute__((format(printf, 2, 3)))
} else {
printf("H%+06lld.%zd",
js_trace_malloc_ptr_offset(ptr, s->opaque),
js_trace_malloc_usable_size(ptr));
js__malloc_usable_size(ptr));
}
fmt++;
continue;
Expand Down Expand Up @@ -203,7 +181,7 @@ static void *js_trace_malloc(JSMallocState *s, size_t size)
js_trace_malloc_printf(s, "A %zd -> %p\n", size, ptr);
if (ptr) {
s->malloc_count++;
s->malloc_size += js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
}
return ptr;
}
Expand All @@ -215,7 +193,7 @@ static void js_trace_free(JSMallocState *s, void *ptr)

js_trace_malloc_printf(s, "F %p\n", ptr);
s->malloc_count--;
s->malloc_size -= js_trace_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
free(ptr);
}

Expand All @@ -228,7 +206,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL;
return js_trace_malloc(s, size);
}
old_size = js_trace_malloc_usable_size(ptr);
old_size = js__malloc_usable_size(ptr);
if (size == 0) {
js_trace_malloc_printf(s, "R %zd %p\n", size, ptr);
s->malloc_count--;
Expand All @@ -244,7 +222,7 @@ static void *js_trace_realloc(JSMallocState *s, void *ptr, size_t size)
ptr = realloc(ptr, size);
js_trace_malloc_printf(s, " -> %p\n", ptr);
if (ptr) {
s->malloc_size += js_trace_malloc_usable_size(ptr) - old_size;
s->malloc_size += js__malloc_usable_size(ptr) - old_size;
}
return ptr;
}
Expand All @@ -253,18 +231,7 @@ static const JSMallocFunctions trace_mf = {
js_trace_malloc,
js_trace_free,
js_trace_realloc,
#if defined(__APPLE__)
malloc_size,
#elif defined(_WIN32)
(size_t (*)(const void *))_msize,
#elif defined(EMSCRIPTEN)
NULL,
#elif defined(__linux__) || defined(__CYGWIN__)
(size_t (*)(const void *))malloc_usable_size,
#else
/* change this to `NULL,` if compilation fails */
malloc_usable_size,
#endif
js__malloc_usable_size
};

#define PROG_NAME "qjs"
Expand Down
17 changes: 12 additions & 5 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,19 @@

#if defined(__APPLE__)
typedef sig_t sighandler_t;
#if !defined(environ)
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#endif
#endif /* __APPLE__ */

#if defined(__OpenBSD__) || defined(__FreeBSD__)
typedef sig_t sighandler_t;
extern char **environ;
#endif

#if !defined(_WIN32)
/* enable the os.Worker API. IT relies on POSIX threads */
#define USE_WORKER
#endif

#endif /* _WIN32 */

#ifdef USE_WORKER
#include <pthread.h>
Expand Down Expand Up @@ -3584,8 +3585,14 @@ void js_std_set_worker_new_context_func(JSContext *(*func)(JSRuntime *rt))
#define OS_PLATFORM "js"
#elif defined(__CYGWIN__)
#define OS_PLATFORM "cygwin"
#else
#elif defined(__linux__)
#define OS_PLATFORM "linux"
#elif defined(__OpenBSD__)
#define OS_PLATFORM "openbsd"
#elif defined(__FreeBSD__)
#define OS_PLATFORM "freebsd"
#else
#define OS_PLATFORM "unknown"
#endif

#define OS_FLAG(x) JS_PROP_INT32_DEF(#x, x, JS_PROP_CONFIGURABLE )
Expand Down
37 changes: 5 additions & 32 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@
#include <time.h>
#include <fenv.h>
#include <math.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#elif defined(__linux__) || defined(__CYGWIN__)
#include <malloc.h>
#elif defined(__FreeBSD__)
#include <malloc_np.h>
#endif

#include "cutils.h"
#include "list.h"
Expand Down Expand Up @@ -1639,23 +1632,6 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque)
rt->user_opaque = opaque;
}

/* default memory allocation functions with memory limitation */
static inline size_t js_def_malloc_usable_size(void *ptr)
{
#if defined(__APPLE__)
return malloc_size(ptr);
#elif defined(_WIN32)
return _msize(ptr);
#elif defined(EMSCRIPTEN)
return 0;
#elif defined(__linux__)
return malloc_usable_size(ptr);
#else
/* change this to `return 0;` if compilation fails */
return malloc_usable_size(ptr);
#endif
}

static void *js_def_malloc(JSMallocState *s, size_t size)
{
void *ptr;
Expand All @@ -1671,7 +1647,7 @@ static void *js_def_malloc(JSMallocState *s, size_t size)
return NULL;

s->malloc_count++;
s->malloc_size += js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
return ptr;
}

Expand All @@ -1681,7 +1657,7 @@ static void js_def_free(JSMallocState *s, void *ptr)
return;

s->malloc_count--;
s->malloc_size -= js_def_malloc_usable_size(ptr) + MALLOC_OVERHEAD;
s->malloc_size -= js__malloc_usable_size(ptr) + MALLOC_OVERHEAD;
free(ptr);
}

Expand All @@ -1694,7 +1670,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
return NULL;
return js_def_malloc(s, size);
}
old_size = js_def_malloc_usable_size(ptr);
old_size = js__malloc_usable_size(ptr);
if (size == 0) {
s->malloc_count--;
s->malloc_size -= old_size + MALLOC_OVERHEAD;
Expand All @@ -1708,7 +1684,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size)
if (!ptr)
return NULL;

s->malloc_size += js_def_malloc_usable_size(ptr) - old_size;
s->malloc_size += js__malloc_usable_size(ptr) - old_size;
return ptr;
}

Expand All @@ -1720,13 +1696,10 @@ static const JSMallocFunctions def_malloc_funcs = {
malloc_size,
#elif defined(_WIN32)
(size_t (*)(const void *))_msize,
#elif defined(EMSCRIPTEN)
NULL,
#elif defined(__linux__) || defined (__CYGWIN__)
(size_t (*)(const void *))malloc_usable_size,
#else
/* change this to `NULL,` if compilation fails */
malloc_usable_size,
NULL,
#endif
};

Expand Down