Integrate mimalloc (#88)

This commit is contained in:
Luke Street 2025-08-15 10:03:13 -06:00 committed by GitHub
parent 78f4d534df
commit c80b7cb3d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 13 deletions

View File

@ -5,14 +5,27 @@ if(NOT CMAKE_BUILD_TYPE)
"Build type options: Debug Release RelWithDebInfo MinSizeRel" FORCE) "Build type options: Debug Release RelWithDebInfo MinSizeRel" FORCE)
endif() endif()
set(CMAKE_C_FLAGS_INIT "-m32")
set(CMAKE_CXX_FLAGS_INIT "-m32")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-m32")
set(CMAKE_SHARED_LINKER_FLAGS_INIT "-m32")
project(wibo LANGUAGES CXX) project(wibo LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-pie -no-pie -D_LARGEFILE64_SOURCE")
find_package(Filesystem REQUIRED) find_package(Filesystem REQUIRED)
set(CMAKE_CXX_STANDARD 17) include(FetchContent)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -Wall -fno-pie -no-pie -D_LARGEFILE64_SOURCE") FetchContent_Declare(
mimalloc
GIT_REPOSITORY https://github.com/microsoft/mimalloc.git
GIT_TAG dfa50c37d951128b1e77167dd9291081aa88eea4 # v3.1.5
)
FetchContent_MakeAvailable(mimalloc)
include_directories(.) include_directories(.)
add_executable(wibo add_executable(wibo
@ -35,5 +48,5 @@ add_executable(wibo
processes.cpp processes.cpp
strutil.cpp strutil.cpp
) )
target_link_libraries(wibo PRIVATE std::filesystem) target_link_libraries(wibo PRIVATE std::filesystem mimalloc-static)
install(TARGETS wibo DESTINATION bin) install(TARGETS wibo DESTINATION bin)

View File

@ -2,7 +2,7 @@
FROM --platform=linux/i386 alpine:latest AS build FROM --platform=linux/i386 alpine:latest AS build
# Install dependencies # Install dependencies
RUN apk add --no-cache cmake ninja g++ linux-headers binutils RUN apk add --no-cache cmake ninja g++ linux-headers binutils git
# Copy source files # Copy source files
COPY . /wibo COPY . /wibo

View File

@ -10,7 +10,7 @@
#include <fnmatch.h> #include <fnmatch.h>
#include <string> #include <string>
#include "strutil.h" #include "strutil.h"
#include <malloc.h> #include <mimalloc.h>
#include <random> #include <random>
#include <stdarg.h> #include <stdarg.h>
#include <system_error> #include <system_error>
@ -46,9 +46,9 @@ namespace kernel32 {
static void *doAlloc(unsigned int dwBytes, bool zero) { static void *doAlloc(unsigned int dwBytes, bool zero) {
if (dwBytes == 0) if (dwBytes == 0)
dwBytes = 1; dwBytes = 1;
void *ret = malloc(dwBytes); void *ret = mi_malloc_aligned(dwBytes, 8);
if (ret && zero) { if (ret && zero) {
memset(ret, 0, malloc_usable_size(ret)); memset(ret, 0, mi_usable_size(ret));
} }
return ret; return ret;
} }
@ -56,9 +56,9 @@ namespace kernel32 {
static void *doRealloc(void *mem, unsigned int dwBytes, bool zero) { static void *doRealloc(void *mem, unsigned int dwBytes, bool zero) {
if (dwBytes == 0) if (dwBytes == 0)
dwBytes = 1; dwBytes = 1;
size_t oldSize = malloc_usable_size(mem); size_t oldSize = mi_usable_size(mem);
void *ret = realloc(mem, dwBytes); void *ret = mi_realloc_aligned(mem, dwBytes, 8);
size_t newSize = malloc_usable_size(ret); size_t newSize = mi_usable_size(ret);
if (ret && zero && newSize > oldSize) { if (ret && zero && newSize > oldSize) {
memset((char*)ret + oldSize, 0, newSize - oldSize); memset((char*)ret + oldSize, 0, newSize - oldSize);
} }
@ -353,9 +353,17 @@ namespace kernel32 {
int WIN_FUNC InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending, LPVOID* lpContext) { int WIN_FUNC InitOnceBeginInitialize(LPINIT_ONCE lpInitOnce, DWORD dwFlags, PBOOL fPending, LPVOID* lpContext) {
DEBUG_LOG("STUB: InitOnceBeginInitialize\n"); DEBUG_LOG("STUB: InitOnceBeginInitialize\n");
if (fPending != nullptr) {
*fPending = TRUE;
}
return 1; return 1;
} }
BOOL WIN_FUNC InitOnceComplete(LPINIT_ONCE lpInitOnce, DWORD dwFlags, LPVOID lpContext) {
DEBUG_LOG("STUB: InitOnceComplete\n");
return TRUE;
}
void WIN_FUNC AcquireSRWLockShared(void *SRWLock) { DEBUG_LOG("STUB: AcquireSRWLockShared(%p)\n", SRWLock); } void WIN_FUNC AcquireSRWLockShared(void *SRWLock) { DEBUG_LOG("STUB: AcquireSRWLockShared(%p)\n", SRWLock); }
void WIN_FUNC ReleaseSRWLockShared(void *SRWLock) { DEBUG_LOG("STUB: ReleaseSRWLockShared(%p)\n", SRWLock); } void WIN_FUNC ReleaseSRWLockShared(void *SRWLock) { DEBUG_LOG("STUB: ReleaseSRWLockShared(%p)\n", SRWLock); }
@ -485,7 +493,7 @@ namespace kernel32 {
bufSize++; bufSize++;
// Step 2, actually build that buffer // Step 2, actually build that buffer
char *buffer = (char *) malloc(bufSize); char *buffer = (char *) mi_malloc(bufSize);
char *ptr = buffer; char *ptr = buffer;
work = environ; work = environ;
@ -515,7 +523,7 @@ namespace kernel32 {
bufSizeW++; bufSizeW++;
// Step 2, actually build that buffer // Step 2, actually build that buffer
uint16_t *buffer = (uint16_t *) malloc(bufSizeW * 2); uint16_t *buffer = (uint16_t *) mi_malloc(bufSizeW * 2);
uint16_t *ptr = buffer; uint16_t *ptr = buffer;
work = environ; work = environ;
@ -1896,7 +1904,7 @@ namespace kernel32 {
unsigned int WIN_FUNC HeapSize(void *hHeap, unsigned int dwFlags, void *lpMem) { unsigned int WIN_FUNC HeapSize(void *hHeap, unsigned int dwFlags, void *lpMem) {
DEBUG_LOG("HeapSize(heap=%p, flags=%x, mem=%p)\n", hHeap, dwFlags, lpMem); DEBUG_LOG("HeapSize(heap=%p, flags=%x, mem=%p)\n", hHeap, dwFlags, lpMem);
return malloc_usable_size(lpMem); return mi_usable_size(lpMem);
} }
void *WIN_FUNC GetProcessHeap() { void *WIN_FUNC GetProcessHeap() {
@ -2348,6 +2356,7 @@ static void *resolveByName(const char *name) {
if (strcmp(name, "EnterCriticalSection") == 0) return (void *) kernel32::EnterCriticalSection; if (strcmp(name, "EnterCriticalSection") == 0) return (void *) kernel32::EnterCriticalSection;
if (strcmp(name, "LeaveCriticalSection") == 0) return (void *) kernel32::LeaveCriticalSection; if (strcmp(name, "LeaveCriticalSection") == 0) return (void *) kernel32::LeaveCriticalSection;
if (strcmp(name, "InitOnceBeginInitialize") == 0) return (void *) kernel32::InitOnceBeginInitialize; if (strcmp(name, "InitOnceBeginInitialize") == 0) return (void *) kernel32::InitOnceBeginInitialize;
if (strcmp(name, "InitOnceComplete") == 0) return (void *) kernel32::InitOnceComplete;
if (strcmp(name, "AcquireSRWLockShared") == 0) return (void *) kernel32::AcquireSRWLockShared; if (strcmp(name, "AcquireSRWLockShared") == 0) return (void *) kernel32::AcquireSRWLockShared;
if (strcmp(name, "ReleaseSRWLockShared") == 0) return (void *) kernel32::ReleaseSRWLockShared; if (strcmp(name, "ReleaseSRWLockShared") == 0) return (void *) kernel32::ReleaseSRWLockShared;
if (strcmp(name, "AcquireSRWLockExclusive") == 0) return (void *) kernel32::AcquireSRWLockExclusive; if (strcmp(name, "AcquireSRWLockExclusive") == 0) return (void *) kernel32::AcquireSRWLockExclusive;