Skip to content

Commit 45db359

Browse files
committed
Fix Linux build issue
1 parent 5132260 commit 45db359

File tree

9 files changed

+77
-11
lines changed

9 files changed

+77
-11
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ let openGraphSPITestTarget = Target.testTarget(
152152
cSettings: sharedCSettings + [
153153
.headerSearchPath("../../Sources/OpenGraph_SPI"),
154154
],
155-
swiftSettings: sharedSwiftSettings
155+
swiftSettings: sharedSwiftSettings,
156+
linkerSettings: [.linkedFramework("XCTest")]
156157
)
157158
let openGraphShimsTestTarget = Target.testTarget(
158159
name: "OpenGraphShimsTests",

Sources/OpenGraph_SPI/Data/ptr.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
#include "OGBase.h"
1212
#include "table.hpp"
13-
#include <mach/vm_types.h>
1413
#include <bitset>
15-
#include <os/lock.h>
1614
#include "page_const.hpp"
1715

1816
OG_ASSUME_NONNULL_BEGIN
@@ -34,7 +32,7 @@ template <typename T> class ptr {
3432

3533
public:
3634
OG_INLINE OG_CONSTEXPR ptr(difference_type offset = 0) : _offset(offset){};
37-
OG_INLINE OG_CONSTEXPR ptr(nullptr_t){};
35+
OG_INLINE OG_CONSTEXPR ptr(std::nullptr_t){};
3836

3937
OG_INLINE
4038
void assert_valid(uint32_t capacity = shared_table().data_capacity()) const {
@@ -72,10 +70,10 @@ template <typename T> class ptr {
7270
T *_Nonnull operator->() const OG_NOEXCEPT { return get(); };
7371

7472
OG_INLINE OG_CONSTEXPR
75-
bool operator==(nullptr_t) const OG_NOEXCEPT { return _offset == 0; };
73+
bool operator==(std::nullptr_t) const OG_NOEXCEPT { return _offset == 0; };
7674

7775
OG_INLINE OG_CONSTEXPR
78-
bool operator!=(nullptr_t) const OG_NOEXCEPT { return _offset != 0; };
76+
bool operator!=(std::nullptr_t) const OG_NOEXCEPT { return _offset != 0; };
7977

8078
OG_INLINE OG_CONSTEXPR
8179
bool operator<(difference_type offset) const OG_NOEXCEPT { return _offset < offset; };

Sources/OpenGraph_SPI/Data/table.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,25 @@
1212
#include "zone.hpp"
1313
#include "../Util/assert.hpp"
1414
#include <sys/mman.h>
15+
#include <dispatch/dispatch.h>
16+
#if OG_TARGET_OS_DARWIN
1517
#include <malloc/malloc.h>
18+
#else
19+
#include <malloc.h>
20+
#endif
21+
22+
#if OG_TARGET_OS_DARWIN
1623
#include <mach/mach.h>
24+
#endif
1725

1826
void *OGGraphVMRegionBaseAddress;
1927

2028
namespace OG {
2129
namespace data {
2230

31+
#if OG_TARGET_OS_DARWIN
2332
malloc_zone_t *_Nullable _malloc_zone;
33+
#endif
2434

2535
table &table::ensure_shared() {
2636
static dispatch_once_t once;
@@ -41,14 +51,19 @@ table::table() {
4151
}
4252
_data_base = reinterpret_cast<vm_address_t>(region) - page_size;
4353
_data_capacity = initial_size + page_size;
54+
#if OG_TARGET_OS_DARWIN
4455
if (_malloc_zone == nullptr) {
4556
malloc_zone_t *zone = malloc_create_zone(0, 0);
4657
_malloc_zone = zone;
4758
malloc_set_zone_name(zone, "OpenGraph graph data");
4859
}
60+
#else
61+
precondition_failure("not implemented");
62+
#endif
4963
}
5064

5165
void table::grow_region() OG_NOEXCEPT {
66+
#if OG_TARGET_OS_DARWIN
5267
uint32_t new_size = 4 * _region_capacity;
5368
// Check size does not exceed 32 bits
5469
if (new_size <= _region_capacity) {
@@ -81,9 +96,13 @@ void table::grow_region() OG_NOEXCEPT {
8196
_region_base = reinterpret_cast<vm_address_t>(new_region);
8297
_region_capacity = new_size;
8398
_data_capacity = new_size + page_size;
99+
#else
100+
precondition_failure("not implemented");
101+
#endif
84102
}
85103

86104
void table::make_pages_reusable(uint32_t page_index, bool reusable) OG_NOEXCEPT {
105+
#if OG_TARGET_OS_DARWIN
87106
static constexpr uint32_t mapped_pages_size = page_size * pages_per_map; // 64 * 512 = 0x8000
88107
void *mapped_pages_address = reinterpret_cast<void *>(region_base() + ((page_index * page_size) & ~(mapped_pages_size - 1)));
89108
int advice = reusable ? MADV_FREE_REUSABLE : MADV_FREE_REUSE;
@@ -100,6 +119,9 @@ void table::make_pages_reusable(uint32_t page_index, bool reusable) OG_NOEXCEPT
100119
mprotect(mapped_pages_address, mapped_pages_size, protection);
101120
}
102121
_reusable_pages_num += reusable ? mapped_pages_size : -mapped_pages_size;
122+
#else
123+
precondition_failure("not implemented");
124+
#endif
103125
}
104126

105127
// TO BE AUDITED
@@ -242,13 +264,13 @@ uint64_t table::raw_page_seed(ptr<page> page) OG_NOEXCEPT {
242264
}
243265

244266
void table::print() const OG_NOEXCEPT {
245-
os_unfair_lock_lock(&_lock);
267+
lock();
246268
fprintf(stderr, "data::table %p:\n %.2fKB allocated, %.2fKB used, %.2fKB reusable.\n",
247269
this,
248270
double(_region_capacity - page_size) / 1024.0,
249271
double(this->used_pages_num()) / 1024.0,
250272
double(_reusable_pages_num) / 1024.0);
251-
os_unfair_lock_unlock(&_lock);
273+
unlock();
252274
}
253275

254276
} /* data */

Sources/OpenGraph_SPI/Data/table.hpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
#include "OGBase.h"
99
#include "../Vector/vector.hpp"
10+
#if OG_TARGET_OS_DARWIN
1011
#include <mach/vm_types.h>
12+
#else
13+
typedef uintptr_t vm_size_t;
14+
typedef uintptr_t vm_offset_t;
15+
typedef vm_offset_t vm_address_t;
16+
#endif
1117
#include <bitset>
18+
#if OG_TARGET_OS_DARWIN
1219
#include <os/lock.h>
20+
#endif
1321

1422
namespace OG {
1523
namespace data {
@@ -32,10 +40,18 @@ class table {
3240
vm_address_t region_base() const OG_NOEXCEPT { return _region_base; };
3341

3442
OG_INLINE OG_CONSTEXPR
35-
void lock() const OG_NOEXCEPT { return os_unfair_lock_lock(&_lock); };
43+
void lock() const OG_NOEXCEPT {
44+
#if OG_TARGET_OS_DARWIN
45+
return os_unfair_lock_lock(&_lock);
46+
#endif
47+
};
3648

3749
OG_INLINE OG_CONSTEXPR
38-
void unlock() const OG_NOEXCEPT { return os_unfair_lock_unlock(&_lock); };
50+
void unlock() const OG_NOEXCEPT {
51+
#if OG_TARGET_OS_DARWIN
52+
return os_unfair_lock_unlock(&_lock);
53+
#endif
54+
};
3955

4056
OG_INLINE OG_CONSTEXPR
4157
uint32_t region_capacity() const OG_NOEXCEPT { return _region_capacity; };
@@ -80,7 +96,9 @@ class table {
8096

8197
vm_address_t _region_base;
8298

99+
#if OG_TARGET_OS_DARWIN
83100
mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
101+
#endif
84102

85103
uint32_t _region_capacity;
86104

Sources/OpenGraph_SPI/Data/zone.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include "table.hpp"
77
#include "page.hpp"
88
#include "../Util/assert.hpp"
9+
#if OG_TARGET_OS_DARWIN
10+
#include <malloc/malloc.h>
11+
#else
12+
#include <malloc.h>
13+
#endif
914

1015
namespace OG {
1116
namespace data {
@@ -96,7 +101,11 @@ void zone::print() const OG_NOEXCEPT {
96101
unsigned long num_persistent_buffers = _malloc_buffers.size();
97102
size_t malloc_total_size = 0;
98103
for (auto &element : _malloc_buffers) {
104+
#if OG_TARGET_OS_DARWIN
99105
malloc_total_size += malloc_size(element.get());
106+
#else
107+
malloc_total_size += malloc_usable_size(element.get());
108+
#endif
100109
}
101110
double malloc_total_size_kb = malloc_total_size / 1024.0;
102111

Sources/OpenGraph_SPI/Util/realloc_vector.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define realloc_vector_hpp
1010

1111
#include "OGBase.h"
12-
#include <stdio.h>
1312

1413
OG_ASSUME_NONNULL_BEGIN
1514

Sources/OpenGraph_SPI/Vector/vector.tpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#include "../Util/assert.hpp"
1010

1111
#include <algorithm>
12+
#if OG_TARGET_OS_DARWIN
1213
#include <malloc/malloc.h>
14+
#else
15+
#include <malloc.h>
16+
#endif /* OG_TARGET_OS_DARWIN */
1317
#include <memory>
1418
#include <cassert>
1519

@@ -34,7 +38,11 @@ void *realloc_vector(void *buffer, void *stack_buffer, size_type stack_size, siz
3438
return nullptr;
3539
}
3640

41+
#if OG_TARGET_OS_DARWIN
3742
size_t new_size_bytes = malloc_good_size(preferred_new_size * element_size_bytes);
43+
#else
44+
size_t new_size_bytes = malloc_good_size(preferred_new_size * element_size_bytes);
45+
#endif
3846
size_type new_size = new_size_bytes / element_size_bytes;
3947
if (new_size == *size) {
4048
// nothing to do
@@ -172,7 +180,11 @@ void *realloc_vector(void *buffer, size_type *size, size_type preferred_new_size
172180
return nullptr;
173181
}
174182

183+
#if OG_TARGET_OS_DARWIN
175184
size_t new_size_bytes = malloc_good_size(preferred_new_size * element_size);
185+
#else
186+
size_t new_size_bytes = preferred_new_size * element_size;
187+
#endif
176188
size_type new_size = (size_type)(new_size_bytes / element_size);
177189
if (new_size == *size) {
178190
// nothing to do

Tests/OpenGraph_SPITests/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## OpenGraph_SPITests
2+
3+
Test C++ API of OpenGraph_SPI

Tests/OpenGraph_SPITests/table_test_case.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// table_test_case.mm
33
// OpenGraph_SPITests
44

5+
#include "OGBase.h"
6+
7+
#if OG_TARGET_OS_DARWIN
58
#include <XCTest/XCTest.h>
69
#include "Data/table.hpp"
710

@@ -18,3 +21,4 @@ - (void)setUp {
1821
}
1922

2023
@end
24+
#endif

0 commit comments

Comments
 (0)