Skip to content

Commit 7685a28

Browse files
authored
Add data part for OpenGraph (#109)
* Misc fix of C++ implementation * Add table implementation * Update vector implementation * Update ptr.hpp * Update zone, table and page * Update to dynamic framework * Update table implementation * Add openGraphSPITestTarget * Fix ptr issue * Fix Linux build issue
1 parent 277a6cf commit 7685a28

20 files changed

+1428
-424
lines changed

Diff for: Package.swift

+42-26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let includePath = SDKPath.appending("/usr/lib/swift")
2929

3030
var sharedCSettings: [CSetting] = [
3131
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
32+
.define("NDEBUG", .when(configuration: .release)),
3233
]
3334

3435
var sharedSwiftSettings: [SwiftSetting] = [
@@ -110,26 +111,54 @@ if warningsAsErrorsCondition {
110111

111112
// MARK: - Targets
112113

114+
let openGraphTarget = Target.target(
115+
name: "OpenGraph",
116+
dependencies: ["OpenGraph_SPI"],
117+
cSettings: sharedCSettings,
118+
swiftSettings: sharedSwiftSettings
119+
)
120+
// FIXME: Merge into one target
121+
// OpenGraph is a C++ & Swift mix target.
122+
// The SwiftPM support for such usage is still in progress.
123+
let openGraphSPITarget = Target.target(
124+
name: "OpenGraph_SPI",
125+
cSettings: sharedCSettings + [
126+
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
127+
]
128+
)
113129
let openGraphShimsTarget = Target.target(
114130
name: "OpenGraphShims",
115131
cSettings: sharedCSettings,
116132
swiftSettings: sharedSwiftSettings
117133
)
118134

119-
let openGraphShimsTestTarget = Target.testTarget(
120-
name: "OpenGraphShimsTests",
135+
// MARK: - Test Targets
136+
137+
let openGraphTestTarget = Target.testTarget(
138+
name: "OpenGraphTests",
121139
dependencies: [
122-
"OpenGraphShims",
140+
"OpenGraph",
123141
],
124142
exclude: ["README.md"],
125143
cSettings: sharedCSettings,
126144
swiftSettings: sharedSwiftSettings
127145
)
128-
129-
let openGraphTestTarget = Target.testTarget(
130-
name: "OpenGraphTests",
146+
let openGraphSPITestTarget = Target.testTarget(
147+
name: "OpenGraph_SPITests",
131148
dependencies: [
132-
"OpenGraph",
149+
"OpenGraph_SPI",
150+
],
151+
exclude: ["README.md"],
152+
cSettings: sharedCSettings + [
153+
.headerSearchPath("../../Sources/OpenGraph_SPI"),
154+
],
155+
swiftSettings: sharedSwiftSettings,
156+
linkerSettings: [.linkedFramework("XCTest")]
157+
)
158+
let openGraphShimsTestTarget = Target.testTarget(
159+
name: "OpenGraphShimsTests",
160+
dependencies: [
161+
"OpenGraphShims",
133162
],
134163
exclude: ["README.md"],
135164
cSettings: sharedCSettings,
@@ -159,37 +188,24 @@ let openGraphSPICompatibilityTestTarget = Target.testTarget(
159188
let package = Package(
160189
name: "OpenGraph",
161190
products: [
162-
.library(name: "OpenGraph_SPI", targets: ["OpenGraph_SPI"]),
163-
.library(name: "OpenGraph", targets: ["OpenGraph"]),
164-
.library(name: "OpenGraphShims", targets: ["OpenGraphShims"]),
191+
.library(name: "OpenGraph", type: .dynamic, targets: ["OpenGraph", "OpenGraph_SPI"]),
192+
.library(name: "OpenGraphShims", type: .dynamic, targets: ["OpenGraph", "OpenGraph_SPI", "OpenGraphShims"]),
165193
],
166194
dependencies: [
167195
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
168196
],
169197
targets: [
170-
// FIXME: Merge into one target
171-
// OpenGraph is a C++ & Swift mix target.
172-
// The SwiftPM support for such usage is still in progress.
173-
.target(
174-
name: "OpenGraph_SPI",
175-
cSettings: sharedCSettings + [
176-
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
177-
]
178-
),
179-
.target(
180-
name: "OpenGraph",
181-
dependencies: ["OpenGraph_SPI"],
182-
cSettings: sharedCSettings,
183-
swiftSettings: sharedSwiftSettings
184-
),
198+
openGraphTarget,
199+
openGraphSPITarget,
185200
openGraphShimsTarget,
186201

187202
openGraphTestTarget,
203+
openGraphSPITestTarget,
188204
openGraphShimsTestTarget,
189205
openGraphCompatibilityTestTarget,
190206
openGraphSPICompatibilityTestTarget,
191207
],
192-
cxxLanguageStandard: .cxx17
208+
cxxLanguageStandard: .cxx20
193209
)
194210

195211

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ The project is for the following purposes:
2525

2626
Currently, this project is in early development.
2727

28+
## Credits
29+
30+
OpenGraph_SPI's Data, Graph, Vector and more is modified based on [Compute](https://github.com/jcmosc/Compute)'s implementations.
31+
2832
## License
2933

3034
See LICENSE file - MIT

Diff for: Sources/OpenGraph_SPI/Data/page.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// page.hpp
3+
// OpenGraph_SPI
4+
5+
#ifndef page_hpp
6+
#define page_hpp
7+
8+
#include "OGBase.h"
9+
#include "ptr.hpp"
10+
11+
namespace OG {
12+
namespace data {
13+
14+
class zone;
15+
template <typename T> class ptr;
16+
17+
struct page {
18+
zone *zone;
19+
ptr<page> previous;
20+
uint32_t total;
21+
uint32_t in_use;
22+
}; /* page */
23+
24+
} /* data */
25+
} /* OG */
26+
27+
#endif /* page_hpp */

Diff for: Sources/OpenGraph_SPI/Data/page_const.hpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// page_const.hpp
3+
// OpenGraph_SPI
4+
5+
#ifndef page_const_hpp
6+
#define page_const_hpp
7+
8+
#include "OGBase.h"
9+
10+
namespace OG {
11+
namespace data {
12+
13+
constexpr const uint32_t page_mask_bits = 9;
14+
15+
/// 0x200
16+
constexpr const uint32_t page_size = 1 << page_mask_bits;
17+
18+
/// 0x1FF
19+
constexpr const uint32_t page_mask = page_size - 1;
20+
21+
/// 0xFFFF_FE00
22+
constexpr const uintptr_t page_alignment = ~page_mask;
23+
24+
} /* data */
25+
} /* OG */
26+
27+
28+
#endif /* page_const_hpp */

Diff for: Sources/OpenGraph_SPI/Data/ptr.hpp

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//
2+
// ptr.hpp
3+
// OpenGraph_SPI
4+
//
5+
// Status: Complete
6+
// Modified from https://github.com/jcmosc/Compute/blob/0a6b21a4cdeb9bbdd95e7e914c4e18bed37a2456/Sources/ComputeCxx/Data/Pointer.h [MIT License]
7+
8+
#ifndef ptr_hpp
9+
#define ptr_hpp
10+
11+
#include "OGBase.h"
12+
#include "table.hpp"
13+
#include <bitset>
14+
#include "page_const.hpp"
15+
16+
OG_ASSUME_NONNULL_BEGIN
17+
18+
namespace OG {
19+
namespace data {
20+
21+
struct page;
22+
23+
template <typename T> class ptr {
24+
public:
25+
using element_type = T;
26+
using difference_type = uint32_t;
27+
28+
private:
29+
difference_type _offset;
30+
31+
template <typename U> friend class ptr;
32+
33+
public:
34+
OG_INLINE OG_CONSTEXPR ptr(difference_type offset = 0) : _offset(offset){};
35+
OG_INLINE OG_CONSTEXPR ptr(std::nullptr_t){};
36+
37+
OG_INLINE
38+
void assert_valid(uint32_t capacity = shared_table().data_capacity()) const {
39+
if (capacity <= _offset) {
40+
precondition_failure("invalid data offset: %u", _offset);
41+
}
42+
}
43+
44+
OG_INLINE
45+
element_type *_Nonnull get(vm_address_t base = shared_table().data_base()) const OG_NOEXCEPT {
46+
assert(_offset != 0);
47+
return reinterpret_cast<element_type *>(base + _offset);
48+
}
49+
50+
OG_INLINE OG_CONSTEXPR
51+
ptr<page> page_ptr() const OG_NOEXCEPT { return ptr<page>(_offset & page_alignment); }
52+
53+
OG_INLINE OG_CONSTEXPR
54+
uint32_t page_index() const OG_NOEXCEPT { return (_offset >> page_mask_bits) - 1; }
55+
56+
OG_INLINE OG_CONSTEXPR
57+
difference_type page_relative_offset() const OG_NOEXCEPT { return _offset & page_mask; }
58+
59+
template <typename U> ptr<U> aligned(difference_type alignment_mask = sizeof(difference_type) - 1) const {
60+
return ptr<U>((_offset + alignment_mask) & ~alignment_mask);
61+
};
62+
63+
OG_INLINE OG_CONSTEXPR
64+
operator bool() const OG_NOEXCEPT { return _offset != 0; };
65+
66+
OG_INLINE OG_CONSTEXPR
67+
std::add_lvalue_reference_t<T> operator*() const OG_NOEXCEPT { return *get(); };
68+
69+
OG_INLINE OG_CONSTEXPR
70+
T *_Nonnull operator->() const OG_NOEXCEPT { return get(); };
71+
72+
OG_INLINE OG_CONSTEXPR
73+
bool operator==(std::nullptr_t) const OG_NOEXCEPT { return _offset == 0; };
74+
75+
OG_INLINE OG_CONSTEXPR
76+
bool operator!=(std::nullptr_t) const OG_NOEXCEPT { return _offset != 0; };
77+
78+
OG_INLINE OG_CONSTEXPR
79+
bool operator<(difference_type offset) const OG_NOEXCEPT { return _offset < offset; };
80+
81+
OG_INLINE OG_CONSTEXPR
82+
bool operator<=(difference_type offset) const OG_NOEXCEPT { return _offset <= offset; };
83+
84+
OG_INLINE OG_CONSTEXPR
85+
bool operator>(difference_type offset) const OG_NOEXCEPT { return _offset > offset; };
86+
87+
OG_INLINE OG_CONSTEXPR
88+
bool operator>=(difference_type offset) const OG_NOEXCEPT { return _offset >= offset; };
89+
90+
template <typename U>
91+
OG_INLINE OG_CONSTEXPR
92+
ptr<U> operator+(difference_type shift) const OG_NOEXCEPT { return ptr(_offset + shift); };
93+
94+
template <typename U>
95+
OG_INLINE OG_CONSTEXPR
96+
ptr<U> operator-(difference_type shift) const OG_NOEXCEPT { return ptr(_offset - shift); };
97+
98+
template <typename U>
99+
OG_INLINE OG_CONSTEXPR
100+
difference_type operator-(const ptr<U> &other) const OG_NOEXCEPT {
101+
return _offset - other._offset;
102+
};
103+
}; /* ptr */
104+
105+
} /* data */
106+
} /* OG */
107+
108+
OG_ASSUME_NONNULL_END
109+
110+
#endif /* ptr_hpp */

0 commit comments

Comments
 (0)