-
Notifications
You must be signed in to change notification settings - Fork 29
feat(major): [sc-23696] replace jemalloc dependency with custom malloc interposer #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
6e95843
2ee5b64
5d45061
d17af85
b1912c4
ba18ccd
3a3e0f2
8372094
95c613c
1dc55b4
7aa8368
675f682
6858ed0
2bbc2ad
87e6ec1
59deb29
35924ef
c884d7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| .DS_Store | ||
| /.build | ||
| /Packages | ||
| xcuserdata/ | ||
| DerivedData/ | ||
| .swiftpm/configuration/registries.json | ||
| .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
| .netrc |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,24 @@ | ||||||||||||||
| // swift-tools-version: 6.1 | ||||||||||||||
|
||||||||||||||
| // The swift-tools-version declares the minimum version of Swift required to build this package. | ||||||||||||||
|
|
||||||||||||||
| import PackageDescription | ||||||||||||||
|
|
||||||||||||||
| let package = Package( | ||||||||||||||
| name: "MallocInterposer", | ||||||||||||||
| products: [ | ||||||||||||||
| // Products define the executables and libraries a package produces, making them visible to other packages. | ||||||||||||||
| .library( | ||||||||||||||
| name: "MallocInterposerC", | ||||||||||||||
| type: .dynamic, | ||||||||||||||
| targets: ["MallocInterposerC"]) | ||||||||||||||
| ], | ||||||||||||||
| targets: [ | ||||||||||||||
| // Targets are the basic building blocks of a package, defining a module or a test suite. | ||||||||||||||
| // Targets can depend on other targets in this package and products from dependencies. | ||||||||||||||
| .target( | ||||||||||||||
| name: "MallocInterposerC", | ||||||||||||||
| linkerSettings: [ | ||||||||||||||
| .linkedLibrary("dl") | ||||||||||||||
| ]) | ||||||||||||||
|
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The linkerSettings: [
.linkedLibrary("dl", .when(platforms: [.linux]))
]
Suggested change
Spotted by Graphite Agent |
||||||||||||||
| ] | ||||||||||||||
| ) | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #ifndef INTERPOSER_H | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general for all new source files, add Apache license header: // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// |
||
| #define INTERPOSER_H | ||
|
|
||
| #include <stdlib.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/types.h> | ||
| #if __APPLE__ | ||
| # include <malloc/malloc.h> | ||
| #endif | ||
|
|
||
| // Hook function types | ||
| typedef void (*malloc_hook_t)(size_t size); | ||
| typedef void (*free_hook_t)(void* ptr); | ||
| typedef void (*calloc_hook_t)(size_t nmemb, size_t size); | ||
| typedef void (*realloc_hook_t)(void* ptr, size_t size); | ||
| typedef void (*valloc_hook_t)(size_t size); | ||
| typedef void (*posix_memalign_hook_t)(void **memptr, size_t alignment, size_t size); | ||
|
|
||
| #if __APPLE__ | ||
| typedef void (*malloc_zone_hook_t)(malloc_zone_t *zone, size_t size); | ||
| typedef void (*malloc_zone_calloc_hook_t)(malloc_zone_t *zone, size_t num_items, size_t size); | ||
| typedef void (*malloc_zone_realloc_hook_t)(malloc_zone_t *zone, void *ptr, size_t size); | ||
| typedef void (*malloc_zone_memalign_hook_t)(malloc_zone_t *zone, size_t alignment, size_t size); | ||
| typedef void (*malloc_zone_valloc_hook_t)(malloc_zone_t *zone, size_t size); | ||
| typedef void (*malloc_zone_free_hook_t)(malloc_zone_t *zone, void *ptr); | ||
| #endif | ||
|
|
||
| // Hook management functions | ||
| void set_malloc_hook(malloc_hook_t hook); | ||
| void set_free_hook(free_hook_t hook); | ||
| void set_calloc_hook(calloc_hook_t hook); | ||
| void set_realloc_hook(realloc_hook_t hook); | ||
|
|
||
| #if __APPLE__ | ||
| void set_malloc_zone_hook(malloc_zone_hook_t hook); | ||
| void set_malloc_zone_calloc_hook(malloc_zone_calloc_hook_t hook); | ||
| void set_malloc_zone_realloc_hook(malloc_zone_realloc_hook_t hook); | ||
| void set_malloc_zone_memalign_hook(malloc_zone_memalign_hook_t hook); | ||
| void set_malloc_zone_valloc_hook(malloc_zone_valloc_hook_t hook); | ||
| void set_malloc_zone_free_hook(malloc_zone_free_hook_t hook); | ||
| #endif | ||
|
|
||
| void clear_malloc_hook(void); | ||
| void clear_free_hook(void); | ||
| void clear_calloc_hook(void); | ||
| void clear_realloc_hook(void); | ||
|
|
||
| #if __APPLE__ | ||
| void clear_malloc_zone_hook(void); | ||
| void clear_malloc_zone_calloc_hook(void); | ||
| void clear_malloc_zone_realloc_hook(void); | ||
| void clear_malloc_zone_memalign_hook(void); | ||
| void clear_malloc_zone_valloc_hook(void); | ||
| void clear_malloc_zone_free_hook(void); | ||
| #endif | ||
|
|
||
| // Replacement functions | ||
| void *replacement_malloc(size_t size); | ||
| void replacement_free(void *ptr); | ||
| void *replacement_calloc(size_t nmemb, size_t size); | ||
| void *replacement_realloc(void *ptr, size_t size); | ||
| void *replacement_reallocf(void *ptr, size_t size); | ||
| void *replacement_valloc(size_t size); | ||
| int replacement_posix_memalign(void **memptr, size_t alignment, size_t size); | ||
|
|
||
| // On Linux we use LD_PRELOAD to interpose the standard malloc functions | ||
| // and we have to declare them ourselves | ||
| #if !__APPLE__ | ||
| void free(void *ptr); | ||
| void *malloc(size_t size); | ||
| void *calloc(size_t nmemb, size_t size); | ||
| void *realloc(void *ptr, size_t size); | ||
| void *reallocf(void *ptr, size_t size); | ||
| void *valloc(size_t size); | ||
| int posix_memalign(void **memptr, size_t alignment, size_t size); | ||
| #endif | ||
|
|
||
|
|
||
| #if __APPLE__ | ||
| void *replacement_malloc_zone_malloc(malloc_zone_t *zone, size_t size); | ||
| void *replacement_malloc_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size); | ||
| void *replacement_malloc_zone_valloc(malloc_zone_t *zone, size_t size); | ||
| void *replacement_malloc_zone_realloc(malloc_zone_t *zone, void *ptr, size_t size); | ||
| void *replacement_malloc_zone_memalign(malloc_zone_t *zone, size_t alignment, size_t size); | ||
| void replacement_malloc_zone_free(malloc_zone_t *zone, void *ptr); | ||
| #endif | ||
|
|
||
| #endif | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please merge in
.swift-formatfrom main and applyswift-format, then we can remove a lot of these diffs with extra spaces.