C API: Add rocksdb_memory_allocator_create_custom#14420
C API: Add rocksdb_memory_allocator_create_custom#14420edmanlog123 wants to merge 1 commit intofacebook:mainfrom
Conversation
|
Hi @edmanlog123! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
This PR adds rocksdb_memory_allocator_create_custom to the C API. This allows language bindings (Rust, Go, Python, etc.) to implement and plug in their own custom memory allocators. Currently, the C API only supports creating a jemalloc based allocator.
Motivation
This is a critical addition for improving RocksDB performance on Windows. The default Windows CRT allocator does not decommit memory to the OS, leading to unbounded memory growth in long-running processes (see #4112). By exposing this C-bridge, users can now plug in alternative allocators like mimalloc or tcmalloc via their respective language bindings to resolve these memory issues.
Changes
include/rocksdb/c.h: Added the rocksdb_memory_allocator_create_custom function signature and necessary callback typedefs.
db/c.cc: Implemented the CustomMemoryAllocator proxy class that inherits from rocksdb::MemoryAllocator and dispatches calls to the user-provided C function pointers.
db/c_test.c: Added a new test case CheckCustomAllocator to verify that the custom allocator is correctly called and that the DB opens/operates successfully with it.
Test Plan
Environment: macOS M2 (Apple Silicon).
Build: Verified with a static build using make c_test LIB_MODE=static.
Execution: Ran ./c_test and confirmed the CheckCustomAllocator test passed, ensuring the C-to-C++ bridge and lifecycle management (via shared_ptr) work as intended.