diff --git a/CMakeLists.txt b/CMakeLists.txt index f965f2b3..37f24fc5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ project(pocketpy) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) +include(FetchContent) include(CMakeOptions.txt) if(WIN32) @@ -76,6 +77,25 @@ else() add_definitions(-DPK_ENABLE_CUSTOM_SNAME=0) endif() +if(PK_ENABLE_MIMALLOC) + message(">> Fetching mimalloc") + FetchContent_Declare( + mimalloc + GIT_REPOSITORY https://github.com/microsoft/mimalloc.git + GIT_TAG v3.1.5 + ) + + set(MI_OVERRIDE OFF CACHE BOOL "" FORCE) + set(MI_BUILD_SHARED OFF CACHE BOOL "" FORCE) + set(MI_BUILD_STATIC ON CACHE BOOL "" FORCE) + set(MI_BUILD_TESTS OFF CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(mimalloc) + + add_definitions(-DPK_ENABLE_MIMALLOC=1) +else() + add_definitions(-DPK_ENABLE_MIMALLOC=0) +endif() + if(PK_BUILD_MODULE_LZ4) add_subdirectory(3rd/lz4) @@ -87,6 +107,7 @@ if(PK_BUILD_MODULE_LIBHV) add_definitions(-DPK_BUILD_MODULE_LIBHV) endif() + if(PK_BUILD_SHARED_LIB) message(">> Building shared library") add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC}) @@ -149,4 +170,8 @@ endif() if(PK_BUILD_MODULE_LIBHV) target_link_libraries(${PROJECT_NAME} libhv_bindings) +endif() + +if(PK_ENABLE_MIMALLOC) + target_link_libraries(${PROJECT_NAME} mimalloc::mimalloc) endif() \ No newline at end of file diff --git a/CMakeOptions.txt b/CMakeOptions.txt index b5417efd..72fd38a4 100644 --- a/CMakeOptions.txt +++ b/CMakeOptions.txt @@ -11,6 +11,7 @@ option(PK_ENABLE_THREADS "" ON) option(PK_ENABLE_DETERMINISM "" OFF) option(PK_ENABLE_WATCHDOG "" OFF) option(PK_ENABLE_CUSTOM_SNAME "" OFF) +option(PK_ENABLE_MIMALLOC "" OFF) # modules option(PK_BUILD_MODULE_LZ4 "" OFF) diff --git a/include/pocketpy/common/chunkedvector.h b/include/pocketpy/common/chunkedvector.h index 682210b7..6468a930 100644 --- a/include/pocketpy/common/chunkedvector.h +++ b/include/pocketpy/common/chunkedvector.h @@ -1,6 +1,7 @@ #pragma once #include "pocketpy/common/vector.h" + typedef struct c11_chunkedvector_chunk { int length; int capacity; diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index 5a219ac9..96514e2a 100644 --- a/include/pocketpy/common/utils.h +++ b/include/pocketpy/common/utils.h @@ -1,6 +1,7 @@ #pragma once #include +#include #define PK_REGION(name) 1 diff --git a/include/pocketpy/common/vector.h b/include/pocketpy/common/vector.h index 1716e645..7f4e1808 100644 --- a/include/pocketpy/common/vector.h +++ b/include/pocketpy/common/vector.h @@ -1,7 +1,5 @@ #pragma once -#include -#include #include #include diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 7d48b24a..2d711333 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -27,24 +27,15 @@ #define PK_ENABLE_CUSTOM_SNAME 0 #endif +#ifndef PK_ENABLE_MIMALLOC // can be overridden by cmake +#define PK_ENABLE_MIMALLOC 0 +#endif + // GC min threshold #ifndef PK_GC_MIN_THRESHOLD // can be overridden by cmake #define PK_GC_MIN_THRESHOLD 32768 #endif -// Memory allocation functions -#ifndef PK_MALLOC - #ifndef __cplusplus - #define PK_MALLOC(size) malloc(size) - #define PK_REALLOC(ptr, size) realloc(ptr, size) - #define PK_FREE(ptr) free(ptr) - #else - #define PK_MALLOC(size) std::malloc(size) - #define PK_REALLOC(ptr, size) std::realloc(ptr, size) - #define PK_FREE(ptr) std::free(ptr) - #endif -#endif - // This is the maximum size of the value stack in py_TValue units // The actual size in bytes equals `sizeof(py_TValue) * PK_VM_STACK_SIZE` #ifndef PK_VM_STACK_SIZE // can be overridden by cmake @@ -90,3 +81,25 @@ #else #define PK_THREAD_LOCAL #endif + +// Memory allocation functions +#ifndef PK_MALLOC + #if PK_ENABLE_MIMALLOC + #include + #define PK_MALLOC(size) mi_malloc(size) + #define PK_REALLOC(ptr, size) mi_realloc(ptr, size) + #define PK_FREE(ptr) mi_free(ptr) + #else + #ifndef __cplusplus + #include + #define PK_MALLOC(size) malloc(size) + #define PK_REALLOC(ptr, size) realloc(ptr, size) + #define PK_FREE(ptr) free(ptr) + #else + #include + #define PK_MALLOC(size) std::malloc(size) + #define PK_REALLOC(ptr, size) std::realloc(ptr, size) + #define PK_FREE(ptr) std::free(ptr) + #endif + #endif +#endif diff --git a/src/common/algorithm.c b/src/common/algorithm.c index b02e9920..1b165868 100644 --- a/src/common/algorithm.c +++ b/src/common/algorithm.c @@ -1,7 +1,6 @@ #include "pocketpy/common/algorithm.h" #include "pocketpy/config.h" #include -#include static bool _stable_sort_merge(char* a, char* a_end, diff --git a/src/common/chunkedvector.c b/src/common/chunkedvector.c index e6eedc0e..3311fbeb 100644 --- a/src/common/chunkedvector.c +++ b/src/common/chunkedvector.c @@ -1,9 +1,6 @@ #include "pocketpy/common/chunkedvector.h" - -#include -#include -#include "pocketpy/common/utils.h" #include "pocketpy/config.h" +#include #if defined(_MSC_VER) #include diff --git a/src/common/memorypool.c b/src/common/memorypool.c index b0dd9bed..70b6960d 100644 --- a/src/common/memorypool.c +++ b/src/common/memorypool.c @@ -1,7 +1,6 @@ #include "pocketpy/common/memorypool.h" -#include "pocketpy/pocketpy.h" +#include "pocketpy/config.h" -#include #include void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount) { diff --git a/src/common/name.c b/src/common/name.c index 0eaa8211..46d0a863 100644 --- a/src/common/name.c +++ b/src/common/name.c @@ -4,6 +4,7 @@ #include "pocketpy/common/str.h" #include "pocketpy/pocketpy.h" #include "pocketpy/common/threads.h" +#include typedef struct NameBucket NameBucket; diff --git a/src/common/sourcedata.c b/src/common/sourcedata.c index fee47d74..5cda19bb 100644 --- a/src/common/sourcedata.c +++ b/src/common/sourcedata.c @@ -1,7 +1,6 @@ #include "pocketpy/objects/sourcedata.h" #include "pocketpy/common/sstream.h" #include -#include #include static void SourceData__ctor(struct SourceData* self, diff --git a/src/common/sstream.c b/src/common/sstream.c index 69b30876..b9cda467 100644 --- a/src/common/sstream.c +++ b/src/common/sstream.c @@ -4,8 +4,8 @@ #include "pocketpy/pocketpy.h" #include -#include #include +#include #include #include diff --git a/src/common/str.c b/src/common/str.c index 0982c455..ca2e9305 100644 --- a/src/common/str.c +++ b/src/common/str.c @@ -2,11 +2,9 @@ #include "pocketpy/common/sstream.h" #include "pocketpy/common/utils.h" -#include #include -#include -#include #include +#include c11_string* c11_string__new(const char* data) { return c11_string__new2(data, strlen(data)); } diff --git a/src/common/vector.c b/src/common/vector.c index af767262..b1a152f6 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -1,6 +1,5 @@ #include "pocketpy/common/vector.h" -#include #include #include "pocketpy/common/utils.h" #include "pocketpy/config.h" diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 673acab7..2dfa59e9 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -7,6 +7,7 @@ #include "pocketpy/pocketpy.h" #include "pocketpy/objects/error.h" #include +#include #include static bool stack_format_object(VM* self, c11_sv spec); diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index 2d5ee253..9ff16bf5 100644 --- a/src/interpreter/frame.c +++ b/src/interpreter/frame.c @@ -5,6 +5,7 @@ #include "pocketpy/objects/codeobject.h" #include "pocketpy/pocketpy.h" #include +#include void ValueStack__ctor(ValueStack* self) { self->sp = self->begin; diff --git a/src/interpreter/generator.c b/src/interpreter/generator.c index 95236845..1f7d4cda 100644 --- a/src/interpreter/generator.c +++ b/src/interpreter/generator.c @@ -4,6 +4,7 @@ #include "pocketpy/objects/base.h" #include "pocketpy/pocketpy.h" #include +#include void pk_newgenerator(py_Ref out, py_Frame* frame, py_TValue* begin, py_TValue* end) { Generator* ud = py_newobject(out, tp_generator, 1, sizeof(Generator)); diff --git a/src/interpreter/heap.c b/src/interpreter/heap.c index fda17b6d..2d061478 100644 --- a/src/interpreter/heap.c +++ b/src/interpreter/heap.c @@ -3,6 +3,7 @@ #include "pocketpy/interpreter/objectpool.h" #include "pocketpy/objects/base.h" #include "pocketpy/pocketpy.h" +#include void ManagedHeap__ctor(ManagedHeap* self) { MultiPool__ctor(&self->small_objects); diff --git a/src/interpreter/line_profier.c b/src/interpreter/line_profier.c index b9251356..a1ea48e9 100644 --- a/src/interpreter/line_profier.c +++ b/src/interpreter/line_profier.c @@ -1,4 +1,5 @@ #include "pocketpy/interpreter/line_profiler.h" +#include void LineProfiler__ctor(LineProfiler* self) { c11_smallmap_p2i__ctor(&self->records); diff --git a/src/interpreter/objectpool.c b/src/interpreter/objectpool.c index b8c5786e..08a18fae 100644 --- a/src/interpreter/objectpool.c +++ b/src/interpreter/objectpool.c @@ -1,12 +1,10 @@ #include "pocketpy/interpreter/objectpool.h" -#include "pocketpy/config.h" #include "pocketpy/objects/object.h" #include "pocketpy/common/sstream.h" #include #include -#include #include static PoolArena* PoolArena__new(int block_size) { diff --git a/src/interpreter/typeinfo.c b/src/interpreter/typeinfo.c index c3d2172a..9248ed4c 100644 --- a/src/interpreter/typeinfo.c +++ b/src/interpreter/typeinfo.c @@ -1,4 +1,5 @@ #include "pocketpy/interpreter/vm.h" +#include py_ItemRef pk_tpfindname(py_TypeInfo* ti, py_Name name) { assert(ti != NULL); diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index c75d501c..adba31d0 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -1,6 +1,5 @@ #include "pocketpy/interpreter/vm.h" #include "pocketpy/common/memorypool.h" -#include "pocketpy/common/sstream.h" #include "pocketpy/common/utils.h" #include "pocketpy/interpreter/generator.h" #include "pocketpy/interpreter/modules.h" @@ -10,6 +9,7 @@ #include "pocketpy/common/_generated.h" #include "pocketpy/pocketpy.h" #include +#include static char* pk_default_importfile(const char* path) { #if PK_ENABLE_OS diff --git a/src/interpreter/vmx.c b/src/interpreter/vmx.c index 4ebbcbee..abb00a2b 100644 --- a/src/interpreter/vmx.c +++ b/src/interpreter/vmx.c @@ -1,4 +1,5 @@ #include "pocketpy/interpreter/vm.h" +#include void pk_print_stack(VM* self, py_Frame* frame, Bytecode byte) { return; diff --git a/src/modules/conio.c b/src/modules/conio.c index 12ca2af4..78f543c0 100644 --- a/src/modules/conio.c +++ b/src/modules/conio.c @@ -1,5 +1,4 @@ #include "pocketpy/pocketpy.h" -#include #if PY_SYS_PLATFORM == 0 diff --git a/src/modules/lz4.c b/src/modules/lz4.c index 514fa5f8..2e01ac59 100644 --- a/src/modules/lz4.c +++ b/src/modules/lz4.c @@ -1,6 +1,5 @@ #ifdef PK_BUILD_MODULE_LZ4 -#include #include #include #include "pocketpy/pocketpy.h" diff --git a/src/modules/traceback.c b/src/modules/traceback.c index 42d97dcb..5fec7afb 100644 --- a/src/modules/traceback.c +++ b/src/modules/traceback.c @@ -1,6 +1,4 @@ -#include "pocketpy/objects/base.h" #include "pocketpy/pocketpy.h" -#include static bool traceback_format_exc(int argc, py_Ref argv) { PY_CHECK_ARGC(0); diff --git a/src/objects/codeobject.c b/src/objects/codeobject.c index 9e441527..b0fe51d5 100644 --- a/src/objects/codeobject.c +++ b/src/objects/codeobject.c @@ -2,6 +2,7 @@ #include "pocketpy/common/utils.h" #include "pocketpy/pocketpy.h" #include +#include void Bytecode__set_signed_arg(Bytecode* self, int arg) { self->arg = (int16_t)arg; diff --git a/src/objects/namedict.c b/src/objects/namedict.c index 7c21eb24..86f006f6 100644 --- a/src/objects/namedict.c +++ b/src/objects/namedict.c @@ -4,7 +4,6 @@ #include #include #include -#include #define HASH_PROBE_0(__k, ok, i) \ ok = false; \ diff --git a/src2/main.c b/src2/main.c index 109d15c6..52e1d2e1 100644 --- a/src2/main.c +++ b/src2/main.c @@ -1,6 +1,5 @@ #include #include -#include #include #include