Skip to content

Add data part for OpenGraph #109

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

Merged
merged 10 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 42 additions & 26 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let includePath = SDKPath.appending("/usr/lib/swift")

var sharedCSettings: [CSetting] = [
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
.define("NDEBUG", .when(configuration: .release)),
]

var sharedSwiftSettings: [SwiftSetting] = [
Expand Down Expand Up @@ -110,26 +111,54 @@ if warningsAsErrorsCondition {

// MARK: - Targets

let openGraphTarget = Target.target(
name: "OpenGraph",
dependencies: ["OpenGraph_SPI"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)
// FIXME: Merge into one target
// OpenGraph is a C++ & Swift mix target.
// The SwiftPM support for such usage is still in progress.
let openGraphSPITarget = Target.target(
name: "OpenGraph_SPI",
cSettings: sharedCSettings + [
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
]
)
let openGraphShimsTarget = Target.target(
name: "OpenGraphShims",
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

let openGraphShimsTestTarget = Target.testTarget(
name: "OpenGraphShimsTests",
// MARK: - Test Targets

let openGraphTestTarget = Target.testTarget(
name: "OpenGraphTests",
dependencies: [
"OpenGraphShims",
"OpenGraph",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
)

let openGraphTestTarget = Target.testTarget(
name: "OpenGraphTests",
let openGraphSPITestTarget = Target.testTarget(
name: "OpenGraph_SPITests",
dependencies: [
"OpenGraph",
"OpenGraph_SPI",
],
exclude: ["README.md"],
cSettings: sharedCSettings + [
.headerSearchPath("../../Sources/OpenGraph_SPI"),
],
swiftSettings: sharedSwiftSettings,
linkerSettings: [.linkedFramework("XCTest")]
)
let openGraphShimsTestTarget = Target.testTarget(
name: "OpenGraphShimsTests",
dependencies: [
"OpenGraphShims",
],
exclude: ["README.md"],
cSettings: sharedCSettings,
Expand Down Expand Up @@ -159,37 +188,24 @@ let openGraphSPICompatibilityTestTarget = Target.testTarget(
let package = Package(
name: "OpenGraph",
products: [
.library(name: "OpenGraph_SPI", targets: ["OpenGraph_SPI"]),
.library(name: "OpenGraph", targets: ["OpenGraph"]),
.library(name: "OpenGraphShims", targets: ["OpenGraphShims"]),
.library(name: "OpenGraph", type: .dynamic, targets: ["OpenGraph", "OpenGraph_SPI"]),
.library(name: "OpenGraphShims", type: .dynamic, targets: ["OpenGraph", "OpenGraph_SPI", "OpenGraphShims"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-numerics", from: "1.0.2"),
],
targets: [
// FIXME: Merge into one target
// OpenGraph is a C++ & Swift mix target.
// The SwiftPM support for such usage is still in progress.
.target(
name: "OpenGraph_SPI",
cSettings: sharedCSettings + [
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
]
),
.target(
name: "OpenGraph",
dependencies: ["OpenGraph_SPI"],
cSettings: sharedCSettings,
swiftSettings: sharedSwiftSettings
),
openGraphTarget,
openGraphSPITarget,
openGraphShimsTarget,

openGraphTestTarget,
openGraphSPITestTarget,
openGraphShimsTestTarget,
openGraphCompatibilityTestTarget,
openGraphSPICompatibilityTestTarget,
],
cxxLanguageStandard: .cxx17
cxxLanguageStandard: .cxx20
)


Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ The project is for the following purposes:

Currently, this project is in early development.

## Credits

OpenGraph_SPI's Data, Graph, Vector and more is modified based on [Compute](https://github.com/jcmosc/Compute)'s implementations.

## License

See LICENSE file - MIT
27 changes: 27 additions & 0 deletions Sources/OpenGraph_SPI/Data/page.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// page.hpp
// OpenGraph_SPI

#ifndef page_hpp
#define page_hpp

#include "OGBase.h"
#include "ptr.hpp"

namespace OG {
namespace data {

class zone;
template <typename T> class ptr;

struct page {
zone *zone;
ptr<page> previous;
uint32_t total;
uint32_t in_use;
}; /* page */

} /* data */
} /* OG */

#endif /* page_hpp */
28 changes: 28 additions & 0 deletions Sources/OpenGraph_SPI/Data/page_const.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// page_const.hpp
// OpenGraph_SPI

#ifndef page_const_hpp
#define page_const_hpp

#include "OGBase.h"

namespace OG {
namespace data {

constexpr const uint32_t page_mask_bits = 9;

/// 0x200
constexpr const uint32_t page_size = 1 << page_mask_bits;

/// 0x1FF
constexpr const uint32_t page_mask = page_size - 1;

/// 0xFFFF_FE00
constexpr const uintptr_t page_alignment = ~page_mask;

} /* data */
} /* OG */


#endif /* page_const_hpp */
110 changes: 110 additions & 0 deletions Sources/OpenGraph_SPI/Data/ptr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// ptr.hpp
// OpenGraph_SPI
//
// Status: Complete
// Modified from https://github.com/jcmosc/Compute/blob/0a6b21a4cdeb9bbdd95e7e914c4e18bed37a2456/Sources/ComputeCxx/Data/Pointer.h [MIT License]

#ifndef ptr_hpp
#define ptr_hpp

#include "OGBase.h"
#include "table.hpp"
#include <bitset>
#include "page_const.hpp"

OG_ASSUME_NONNULL_BEGIN

namespace OG {
namespace data {

struct page;

template <typename T> class ptr {
public:
using element_type = T;
using difference_type = uint32_t;

private:
difference_type _offset;

template <typename U> friend class ptr;

public:
OG_INLINE OG_CONSTEXPR ptr(difference_type offset = 0) : _offset(offset){};
OG_INLINE OG_CONSTEXPR ptr(std::nullptr_t){};

OG_INLINE
void assert_valid(uint32_t capacity = shared_table().data_capacity()) const {
if (capacity <= _offset) {
precondition_failure("invalid data offset: %u", _offset);
}
}

OG_INLINE
element_type *_Nonnull get(vm_address_t base = shared_table().data_base()) const OG_NOEXCEPT {
assert(_offset != 0);
return reinterpret_cast<element_type *>(base + _offset);
}

OG_INLINE OG_CONSTEXPR
ptr<page> page_ptr() const OG_NOEXCEPT { return ptr<page>(_offset & page_alignment); }

OG_INLINE OG_CONSTEXPR
uint32_t page_index() const OG_NOEXCEPT { return (_offset >> page_mask_bits) - 1; }

OG_INLINE OG_CONSTEXPR
difference_type page_relative_offset() const OG_NOEXCEPT { return _offset & page_mask; }

template <typename U> ptr<U> aligned(difference_type alignment_mask = sizeof(difference_type) - 1) const {
return ptr<U>((_offset + alignment_mask) & ~alignment_mask);
};

OG_INLINE OG_CONSTEXPR
operator bool() const OG_NOEXCEPT { return _offset != 0; };

OG_INLINE OG_CONSTEXPR
std::add_lvalue_reference_t<T> operator*() const OG_NOEXCEPT { return *get(); };

OG_INLINE OG_CONSTEXPR
T *_Nonnull operator->() const OG_NOEXCEPT { return get(); };

OG_INLINE OG_CONSTEXPR
bool operator==(std::nullptr_t) const OG_NOEXCEPT { return _offset == 0; };

OG_INLINE OG_CONSTEXPR
bool operator!=(std::nullptr_t) const OG_NOEXCEPT { return _offset != 0; };

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

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

OG_INLINE OG_CONSTEXPR
bool operator>(difference_type offset) const OG_NOEXCEPT { return _offset > offset; };

OG_INLINE OG_CONSTEXPR
bool operator>=(difference_type offset) const OG_NOEXCEPT { return _offset >= offset; };

template <typename U>
OG_INLINE OG_CONSTEXPR
ptr<U> operator+(difference_type shift) const OG_NOEXCEPT { return ptr(_offset + shift); };

template <typename U>
OG_INLINE OG_CONSTEXPR
ptr<U> operator-(difference_type shift) const OG_NOEXCEPT { return ptr(_offset - shift); };

template <typename U>
OG_INLINE OG_CONSTEXPR
difference_type operator-(const ptr<U> &other) const OG_NOEXCEPT {
return _offset - other._offset;
};
}; /* ptr */

} /* data */
} /* OG */

OG_ASSUME_NONNULL_END

#endif /* ptr_hpp */
Loading