diff --git a/Sources/OpenGraphCxx/Attribute/OGAttribute.cpp b/Sources/OpenGraphCxx/Attribute/OGAttribute.cpp index ef8217e9..c7925f2a 100644 --- a/Sources/OpenGraphCxx/Attribute/OGAttribute.cpp +++ b/Sources/OpenGraphCxx/Attribute/OGAttribute.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include const OGAttribute OGAttributeNil = OGAttribute(OG::AttributeID::Kind::Null); diff --git a/Sources/OpenGraphCxx/Data/table.cpp b/Sources/OpenGraphCxx/Data/table.cpp index 9e280d70..5168b170 100644 --- a/Sources/OpenGraphCxx/Data/table.cpp +++ b/Sources/OpenGraphCxx/Data/table.cpp @@ -2,7 +2,7 @@ // table.cpp // OpenGraphCxx // -// Audited for 6.5.4 +// Audited for 6.5.1 // Status: WIP // Modified based Compute code @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #if OG_TARGET_OS_DARWIN @@ -29,7 +29,7 @@ namespace OG { namespace data { #if OG_TARGET_OS_DARWIN -malloc_zone_t *_Nullable _malloc_zone; +malloc_zone_t *table::_malloc_zone = nullptr; #endif table &table::ensure_shared() { @@ -242,9 +242,9 @@ void table::dealloc_page_locked(ptr page) OG_NOEXCEPT { } } -// TO BE AUDITED -uint64_t table::raw_page_seed(ptr page) OG_NOEXCEPT { - page.assert_valid(_data_capacity); +OG_CONSTEXPR +uint64_t table::raw_page_seed(ptr page) const OG_NOEXCEPT { + assert_valid(page); lock(); uint32_t page_index = page.page_index(); uint32_t map_index = page_index / pages_per_map; @@ -255,8 +255,8 @@ uint64_t table::raw_page_seed(ptr page) OG_NOEXCEPT { if (map_index < _page_metadata_maps.size() && _page_metadata_maps[map_index].test(page_index % page_size)) { auto info = page->zone->info(); // FIXME - w22 = info.to_raw_value() & 0xffffff00; - w21 = info.to_raw_value() & 0x000000ff; + w22 = info.value() & 0xffffff00; + w21 = info.value() & 0x000000ff; result = uint64_t(1) << 32; } unlock(); @@ -267,9 +267,9 @@ void table::print() const OG_NOEXCEPT { lock(); fprintf(stderr, "data::table %p:\n %.2fKB allocated, %.2fKB used, %.2fKB reusable.\n", this, - double(_region_capacity - page_size) / 1024.0, + double(region_capacity() - page_size) / 1024.0, double(this->used_pages_num()) / 1024.0, - double(_reusable_pages_num) / 1024.0); + double(reusable_pages_num()) / 1024.0); unlock(); } diff --git a/Sources/OpenGraphCxx/Data/zone.cpp b/Sources/OpenGraphCxx/Data/zone.cpp index eb072c3f..0a1e461c 100644 --- a/Sources/OpenGraphCxx/Data/zone.cpp +++ b/Sources/OpenGraphCxx/Data/zone.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #if OG_TARGET_OS_DARWIN #include #else @@ -15,7 +15,17 @@ namespace OG { namespace data { -void zone::clear() OG_NOEXCEPT { +//zone::zone() : _info(info(shared_table().make_zone_id())) {} + +//zone::~zone() { +// clear(); +// +//} + +void zone::clear() { +// for (auto &element : malloc_buffers()) { +// element.reset(); +// } shared_table().lock(); while (last_page()) { auto page = last_page(); @@ -121,7 +131,7 @@ void zone::print() const OG_NOEXCEPT { ); } -void zone::print_header() OG_NOEXCEPT { +void zone::print_header() const OG_NOEXCEPT { fprintf(stderr, "Zones\n%-16s %6s %8s %8s %6s %6s %6s %8s\n", "zone ptr", "pages", "total", "in-use", "free", "bytes", "malloc", "total"); } diff --git a/Sources/OpenGraphCxx/DebugServer/og-debug-server.mm b/Sources/OpenGraphCxx/DebugServer/og-debug-server.mm index 5255b59d..efefd8a8 100644 --- a/Sources/OpenGraphCxx/DebugServer/og-debug-server.mm +++ b/Sources/OpenGraphCxx/DebugServer/og-debug-server.mm @@ -8,8 +8,8 @@ #if OG_TARGET_OS_DARWIN #include -#include -#include +#include +#include #include #include diff --git a/Sources/OpenGraphCxx/Graph/GraphContext.cpp b/Sources/OpenGraphCxx/Graph/GraphContext.cpp index 50d70e33..6700fb4d 100644 --- a/Sources/OpenGraphCxx/Graph/GraphContext.cpp +++ b/Sources/OpenGraphCxx/Graph/GraphContext.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include OG::Graph::Context &OG::Graph::Context::from_cf(OGGraphRef storage) OG_NOEXCEPT { if (storage->context.isInvalid()) { diff --git a/Sources/OpenGraphCxx/Graph/GraphDescription.cpp b/Sources/OpenGraphCxx/Graph/GraphDescription.cpp index 0634613b..60c99606 100644 --- a/Sources/OpenGraphCxx/Graph/GraphDescription.cpp +++ b/Sources/OpenGraphCxx/Graph/GraphDescription.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include CFTypeRef OGGraphDescription(OGGraphRef graph, CFDictionaryRef options) { #if OG_OBJC_FOUNDATION diff --git a/Sources/OpenGraphCxx/Graph/GraphDescription.mm b/Sources/OpenGraphCxx/Graph/GraphDescription.mm index ae83cef5..ca73a2ea 100644 --- a/Sources/OpenGraphCxx/Graph/GraphDescription.mm +++ b/Sources/OpenGraphCxx/Graph/GraphDescription.mm @@ -5,7 +5,7 @@ #include #include #include -#include +#include #if OG_OBJC_FOUNDATION diff --git a/Sources/OpenGraphCxx/Graph/OGGraph.cpp b/Sources/OpenGraphCxx/Graph/OGGraph.cpp index 6aaa1a78..e3ed7f05 100644 --- a/Sources/OpenGraphCxx/Graph/OGGraph.cpp +++ b/Sources/OpenGraphCxx/Graph/OGGraph.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include #include #include diff --git a/Sources/OpenGraphCxx/Graph/OGSubgraph.cpp b/Sources/OpenGraphCxx/Graph/OGSubgraph.cpp index 530db225..e020f65a 100644 --- a/Sources/OpenGraphCxx/Graph/OGSubgraph.cpp +++ b/Sources/OpenGraphCxx/Graph/OGSubgraph.cpp @@ -6,8 +6,8 @@ #include #include #include -#include -#include +#include +#include #include #if !OG_TARGET_OS_WASI #include diff --git a/Sources/OpenGraphCxx/Util/assert.cpp b/Sources/OpenGraphCxx/Misc/assert.cpp similarity index 93% rename from Sources/OpenGraphCxx/Util/assert.cpp rename to Sources/OpenGraphCxx/Misc/assert.cpp index 7d85df66..3243e81b 100644 --- a/Sources/OpenGraphCxx/Util/assert.cpp +++ b/Sources/OpenGraphCxx/Misc/assert.cpp @@ -2,8 +2,8 @@ // assert.cpp // OpenGraphCxx -#include -#include +#include +#include #include #include diff --git a/Sources/OpenGraphCxx/Util/log.cpp b/Sources/OpenGraphCxx/Misc/log.cpp similarity index 91% rename from Sources/OpenGraphCxx/Util/log.cpp rename to Sources/OpenGraphCxx/Misc/log.cpp index b7c4a9f4..322971b0 100644 --- a/Sources/OpenGraphCxx/Util/log.cpp +++ b/Sources/OpenGraphCxx/Misc/log.cpp @@ -4,7 +4,7 @@ // // Audited for 2021 Release -#include +#include #if OG_TARGET_OS_DARWIN diff --git a/Sources/OpenGraphCxx/Runtime/OGTupleType.cpp b/Sources/OpenGraphCxx/Runtime/OGTupleType.cpp index 4422651f..e639cf90 100644 --- a/Sources/OpenGraphCxx/Runtime/OGTupleType.cpp +++ b/Sources/OpenGraphCxx/Runtime/OGTupleType.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include diff --git a/Sources/OpenGraphCxx/Runtime/OGTypeID.cpp b/Sources/OpenGraphCxx/Runtime/OGTypeID.cpp index 0974231a..ba13defd 100644 --- a/Sources/OpenGraphCxx/Runtime/OGTypeID.cpp +++ b/Sources/OpenGraphCxx/Runtime/OGTypeID.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #ifdef OPENGRAPH_SWIFT_TOOLCHAIN_SUPPORTED #include diff --git a/Sources/OpenGraphCxx/Util/realloc_vector.cpp b/Sources/OpenGraphCxx/Vector/realloc_vector.cpp similarity index 95% rename from Sources/OpenGraphCxx/Util/realloc_vector.cpp rename to Sources/OpenGraphCxx/Vector/realloc_vector.cpp index 7d073a78..e5ddcd88 100644 --- a/Sources/OpenGraphCxx/Util/realloc_vector.cpp +++ b/Sources/OpenGraphCxx/Vector/realloc_vector.cpp @@ -2,8 +2,8 @@ // realloc_vector.cpp // OpenGraphCxx -#include -#include +#include +#include #if OG_TARGET_OS_DARWIN #include diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Attribute/AttributeID.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Attribute/AttributeID.hpp index a67e6f0e..9e332433 100644 --- a/Sources/OpenGraphCxx/include/OpenGraphCxx/Attribute/AttributeID.hpp +++ b/Sources/OpenGraphCxx/include/OpenGraphCxx/Attribute/AttributeID.hpp @@ -7,7 +7,7 @@ #include #include -#include +#include namespace OG { class AttributeID final { diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/ptr.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/ptr.hpp index 0d97ff2c..dd6cf085 100644 --- a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/ptr.hpp +++ b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/ptr.hpp @@ -25,22 +25,11 @@ template class ptr { using element_type = T; using difference_type = uint32_t; -private: - difference_type _offset; - - template 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); - } - } - + // FIXME: this should be put into table API OG_INLINE element_type *_Nonnull get(vm_address_t base = shared_table().data_base()) const OG_NOEXCEPT { assert(_offset != 0); @@ -100,6 +89,11 @@ template class ptr { difference_type operator-(const ptr &other) const OG_NOEXCEPT { return _offset - other._offset; }; +private: + difference_type _offset; + + template friend class ptr; + friend class table; }; /* ptr */ } /* data */ diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/table.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/table.hpp index cb1c16aa..01088232 100644 --- a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/table.hpp +++ b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/table.hpp @@ -7,6 +7,7 @@ #include #include + #if OG_TARGET_OS_DARWIN #include #else @@ -19,6 +20,8 @@ typedef vm_offset_t vm_address_t; #include #endif +OG_ASSUME_NONNULL_BEGIN + namespace OG { namespace data { class zone; @@ -28,8 +31,17 @@ template class ptr; class table { public: class malloc_zone_deleter { + void operator()(void* ptr) const { + #if OG_TARGET_OS_DARWIN + malloc_zone_free(_malloc_zone, ptr); + #endif + } }; + #if OG_TARGET_OS_DARWIN + static malloc_zone_t *_malloc_zone; + #endif + public: static table &ensure_shared(); @@ -77,6 +89,14 @@ class table { return _zones_num; } + template + OG_INLINE + void assert_valid(const ptr& p) const { + if (data_capacity() <= p._offset) { + precondition_failure("invalid data offset: %u", p._offset); + } + } + table(); void grow_region() OG_NOEXCEPT; @@ -87,7 +107,8 @@ class table { void dealloc_page_locked(ptr page) OG_NOEXCEPT; - uint64_t raw_page_seed(ptr page) OG_NOEXCEPT; + OG_INLINE OG_CONSTEXPR + uint64_t raw_page_seed(ptr page) const OG_NOEXCEPT; void print() const OG_NOEXCEPT; private: @@ -133,4 +154,6 @@ static table &shared_table() { } /* data */ } /* OG */ +OG_ASSUME_NONNULL_END + #endif /* table_hpp */ diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp index a8b29ca4..3ca30182 100644 --- a/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp +++ b/Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp @@ -14,20 +14,47 @@ namespace data { class zone { public: class info { - private: - constexpr static uint32_t zone_id_mask = 0x7fffffff; - uint32_t _value; - constexpr info(uint32_t value) : _value(value){}; - public: - uint32_t zone_id() { return _value & zone_id_mask; }; - info with_zone_id(uint32_t zone_id) const { return info((_value & ~zone_id_mask) | (zone_id & zone_id_mask)); }; + OG_INLINE OG_CONSTEXPR + info() OG_NOEXCEPT : _value(0) {}; + + OG_INLINE OG_CONSTEXPR + info(uint32_t value) OG_NOEXCEPT : _value(value){}; + + OG_INLINE OG_CONSTEXPR + info(uint32_t zone_id, bool deleted) OG_NOEXCEPT : _value((zone_id & zone_id_mask) | (deleted ? 1 : 0)) {}; + + OG_INLINE OG_CONSTEXPR + uint32_t value() const OG_NOEXCEPT { return _value; }; + + OG_INLINE OG_CONSTEXPR + uint32_t zone_id() const OG_NOEXCEPT { return _value & zone_id_mask; }; + + OG_INLINE OG_CONSTEXPR + bool is_deleted() const OG_NOEXCEPT { return (_value & deleted) != 0; }; - uint32_t to_raw_value() { return _value; }; - static info from_raw_value(uint32_t value) { return info(value); }; + OG_INLINE OG_CONSTEXPR + info with_zone_id(uint32_t zone_id) const OG_NOEXCEPT { + return info((_value & ~zone_id_mask) | (zone_id & zone_id_mask), is_deleted()); + }; + + OG_INLINE OG_CONSTEXPR + info with_deleted(bool deleted) const OG_NOEXCEPT { + return info(zone_id(), deleted); + } + private: + enum { + zone_id_mask = 0x7fffffff, + deleted = 0x80000000, + }; + uint32_t _value; }; /* info */ public: - zone(); +// zone() OG_NOEXCEPT = default; +// ~zone() OG_NOEXCEPT; + + OG_INLINE OG_CONSTEXPR + auto& malloc_buffers() const OG_NOEXCEPT { return _malloc_buffers; }; OG_INLINE OG_CONSTEXPR ptr last_page() const OG_NOEXCEPT { return _last_page; }; @@ -35,7 +62,8 @@ class zone { OG_INLINE OG_CONSTEXPR info info() const OG_NOEXCEPT { return _info; }; - void clear() OG_NOEXCEPT; + OG_INLINE + void clear(); ptr alloc_slow(uint32_t size, uint32_t alignment_mask) OG_NOEXCEPT; @@ -49,15 +77,12 @@ class zone { // Printing void print() const OG_NOEXCEPT; - void print_header() OG_NOEXCEPT; - - ~zone(); + void print_header() const OG_NOEXCEPT; private: typedef struct _bytes_info { ptr next; uint32_t size; } bytes_info; - vector, 0, uint32_t> _malloc_buffers; ptr _last_page; ptr _free_bytes; diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Util/assert.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/assert.hpp similarity index 100% rename from Sources/OpenGraphCxx/include/OpenGraphCxx/Util/assert.hpp rename to Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/assert.hpp diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Util/env.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/env.hpp similarity index 100% rename from Sources/OpenGraphCxx/include/OpenGraphCxx/Util/env.hpp rename to Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/env.hpp diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Util/log.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/log.hpp similarity index 100% rename from Sources/OpenGraphCxx/include/OpenGraphCxx/Util/log.hpp rename to Sources/OpenGraphCxx/include/OpenGraphCxx/Misc/log.hpp diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Util/realloc_vector.hpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/realloc_vector.hpp similarity index 100% rename from Sources/OpenGraphCxx/include/OpenGraphCxx/Util/realloc_vector.hpp rename to Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/realloc_vector.hpp diff --git a/Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/vector.tpp b/Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/vector.tpp index b16b9a90..977c3dbb 100644 --- a/Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/vector.tpp +++ b/Sources/OpenGraphCxx/include/OpenGraphCxx/Vector/vector.tpp @@ -6,7 +6,7 @@ // Modified based Compute code #include -#include +#include #include #if OG_TARGET_OS_DARWIN diff --git a/Sources/OpenGraphCxx/include/module.private.modulemap b/Sources/OpenGraphCxx/include/module.private.modulemap index 006b447a..224fd911 100644 --- a/Sources/OpenGraphCxx/include/module.private.modulemap +++ b/Sources/OpenGraphCxx/include/module.private.modulemap @@ -8,14 +8,44 @@ module OpenGraphCxx_Private.Attribute { export * } +module OpenGraphCxx_Private.Comparison { + requires cplusplus + umbrella "OpenGraphCxx/Comparison" + export * +} + module OpenGraphCxx_Private.Data { requires cplusplus umbrella "OpenGraphCxx/Data" export * } -module OpenGraphCxx_Private.Comparison { +module OpenGraphCxx_Private.DebugServer { requires cplusplus - umbrella "OpenGraphCxx/Comparison" + umbrella "OpenGraphCxx/DebugServer" + export * +} + +module OpenGraphCxx_Private.Graph { + requires cplusplus + umbrella "OpenGraphCxx/Graph" + export * +} + +module OpenGraphCxx_Private.Misc { + requires cplusplus + umbrella "OpenGraphCxx/Misc" + export * +} + +module OpenGraphCxx_Private.Runtime { + requires cplusplus + umbrella "OpenGraphCxx/Runtime" + export * +} + +module OpenGraphCxx_Private.Vector { + requires cplusplus + umbrella "OpenGraphCxx/Vector" export * } diff --git a/Tests/OpenGraphCxxTests/Data/PageTests.swift b/Tests/OpenGraphCxxTests/Data/PageTests.swift new file mode 100644 index 00000000..5b128ab1 --- /dev/null +++ b/Tests/OpenGraphCxxTests/Data/PageTests.swift @@ -0,0 +1,12 @@ +// +// PageTests.swift +// OpenGraphCxxTests + +#if canImport(Darwin) +import OpenGraphCxx_Private.Data +import Testing + +struct PageTests { + +} +#endif diff --git a/Tests/OpenGraphCxxTests/Data/TableTests.swift b/Tests/OpenGraphCxxTests/Data/TableTests.swift new file mode 100644 index 00000000..4939a449 --- /dev/null +++ b/Tests/OpenGraphCxxTests/Data/TableTests.swift @@ -0,0 +1,16 @@ +// +// TableTests.swift +// OpenGraphCxxTests + +#if canImport(Darwin) // table() is not implemented on Linux yet. +import OpenGraphCxx_Private.Data +import Testing + +struct TableTests { + @Test + func table() { + let table = OG.data.table() + table.print() + } +} +#endif diff --git a/Tests/OpenGraphCxxTests/Data/ZoneTests.swift b/Tests/OpenGraphCxxTests/Data/ZoneTests.swift new file mode 100644 index 00000000..6d8dbe15 --- /dev/null +++ b/Tests/OpenGraphCxxTests/Data/ZoneTests.swift @@ -0,0 +1,17 @@ +// +// ZoneTests.swift +// OpenGraphCxxTests + +#if canImport(Darwin) +import OpenGraphCxx_Private.Data +import Testing + +struct ZoneTests { + @Test + func print() { +// let zone = OG.data.zone() +// zone.print_header() +// zone.print() + } +} +#endif diff --git a/Tests/OpenGraphCxxTests/OpenGraphCxxTests.swift b/Tests/OpenGraphCxxTests/OpenGraphCxxTests.swift deleted file mode 100644 index 91837d7f..00000000 --- a/Tests/OpenGraphCxxTests/OpenGraphCxxTests.swift +++ /dev/null @@ -1,15 +0,0 @@ -// -// OpenGraphCxxTests.swift -// OpenGraphCxxTests - -import OpenGraphCxx_Private -import Testing - -struct OpenGraphCxxTests { - #if canImport(Darwin) // table() is not implemented on Linux yet. - @Test - func table() { - OG.data.table.ensure_shared() - } - #endif -}