Skip to content
Open
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore


.DS_Store

## User settings
xcuserdata/

Expand Down
2 changes: 1 addition & 1 deletion Benchmarks/Benchmarks/Basic/BenchmarkRunner+Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let benchmarks: @Sendable () -> Void = {
}
}

let parameterization = (0...5).map { 1 << $0 } // 1, 2, 4, ...
let parameterization = (0...5).map { 1 << $0 } // 1, 2, 4, ...

Comment on lines 127 to 129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please merge in .swift-format from main and apply swift-format, then we can remove a lot of these diffs with extra spaces.

parameterization.forEach { count in
Benchmark("Parameterized", configuration: .init(tags: ["count": count.description])) { benchmark in
Expand Down
4 changes: 2 additions & 2 deletions Benchmarks/Benchmarks/Histogram/Histogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let benchmarks: @Sendable () -> Void = {

var histogram = Histogram<UInt64>(highestTrackableValue: maxValue, numberOfSignificantValueDigits: .three)

let numValues = 1_024 // so compiler can optimize modulo below
let numValues = 1_024 // so compiler can optimize modulo below
let values = [UInt64]((0..<numValues).map { _ in UInt64.random(in: 100...1_000) })

benchmark.startMeasurement()
Expand All @@ -51,7 +51,7 @@ let benchmarks: @Sendable () -> Void = {
benchmark.startMeasurement()
var histogram = Histogram<UInt64>(numberOfSignificantValueDigits: .three)

let numValues = 1_024 // so compiler can optimize modulo below
let numValues = 1_024 // so compiler can optimize modulo below
let values = [UInt64]((0..<numValues).map { _ in UInt64.random(in: 100...10_000) })

for i in benchmark.scaledIterations {
Expand Down
8 changes: 8 additions & 0 deletions LocalPackages/MallocInterposerC/.gitignore
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
24 changes: 24 additions & 0 deletions LocalPackages/MallocInterposerC/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// swift-tools-version: 6.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should move to "swift-tools-version: 5.10" everywhere, as we still support 5.10.

// 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dl library is Linux-specific and doesn't exist on macOS. This will cause build failures on macOS. The linker setting should be conditional:

linkerSettings: [
    .linkedLibrary("dl", .when(platforms: [.linux]))
]
Suggested change
linkerSettings: [
.linkedLibrary("dl")
])
linkerSettings: [
.linkedLibrary("dl", .when(platforms: [.linux]))
])

Spotted by Graphite Agent

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef INTERPOSER_H
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Loading
Loading