Skip to content

Commit 7178286

Browse files
committed
Update Data part
1 parent 7d9109b commit 7178286

File tree

9 files changed

+134
-54
lines changed

9 files changed

+134
-54
lines changed

Sources/OpenGraphCxx/Data/table.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// table.cpp
33
// OpenGraphCxx
44
//
5-
// Audited for 6.5.4
5+
// Audited for 6.5.1
66
// Status: WIP
77
// Modified based Compute code
88

@@ -29,7 +29,7 @@ namespace OG {
2929
namespace data {
3030

3131
#if OG_TARGET_OS_DARWIN
32-
malloc_zone_t *_Nullable _malloc_zone;
32+
malloc_zone_t *table::_malloc_zone = nullptr;
3333
#endif
3434

3535
table &table::ensure_shared() {
@@ -242,9 +242,9 @@ void table::dealloc_page_locked(ptr<page> page) OG_NOEXCEPT {
242242
}
243243
}
244244

245-
// TO BE AUDITED
246-
uint64_t table::raw_page_seed(ptr<page> page) OG_NOEXCEPT {
247-
page.assert_valid(_data_capacity);
245+
OG_CONSTEXPR
246+
uint64_t table::raw_page_seed(ptr<page> page) const OG_NOEXCEPT {
247+
assert_valid(page);
248248
lock();
249249
uint32_t page_index = page.page_index();
250250
uint32_t map_index = page_index / pages_per_map;
@@ -255,8 +255,8 @@ uint64_t table::raw_page_seed(ptr<page> page) OG_NOEXCEPT {
255255
if (map_index < _page_metadata_maps.size() && _page_metadata_maps[map_index].test(page_index % page_size)) {
256256
auto info = page->zone->info();
257257
// FIXME
258-
w22 = info.to_raw_value() & 0xffffff00;
259-
w21 = info.to_raw_value() & 0x000000ff;
258+
w22 = info.value() & 0xffffff00;
259+
w21 = info.value() & 0x000000ff;
260260
result = uint64_t(1) << 32;
261261
}
262262
unlock();
@@ -267,9 +267,9 @@ void table::print() const OG_NOEXCEPT {
267267
lock();
268268
fprintf(stderr, "data::table %p:\n %.2fKB allocated, %.2fKB used, %.2fKB reusable.\n",
269269
this,
270-
double(_region_capacity - page_size) / 1024.0,
270+
double(region_capacity() - page_size) / 1024.0,
271271
double(this->used_pages_num()) / 1024.0,
272-
double(_reusable_pages_num) / 1024.0);
272+
double(reusable_pages_num()) / 1024.0);
273273
unlock();
274274
}
275275

Sources/OpenGraphCxx/Data/zone.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@
1515
namespace OG {
1616
namespace data {
1717

18-
void zone::clear() OG_NOEXCEPT {
18+
//zone::zone() : _info(info(shared_table().make_zone_id())) {}
19+
20+
//zone::~zone() {
21+
// clear();
22+
//
23+
//}
24+
25+
void zone::clear() {
26+
// for (auto &element : malloc_buffers()) {
27+
// element.reset();
28+
// }
1929
shared_table().lock();
2030
while (last_page()) {
2131
auto page = last_page();
@@ -121,7 +131,7 @@ void zone::print() const OG_NOEXCEPT {
121131
);
122132
}
123133

124-
void zone::print_header() OG_NOEXCEPT {
134+
void zone::print_header() const OG_NOEXCEPT {
125135
fprintf(stderr, "Zones\n%-16s %6s %8s %8s %6s %6s %6s %8s\n",
126136
"zone ptr", "pages", "total", "in-use", "free", "bytes", "malloc", "total");
127137
}

Sources/OpenGraphCxx/include/OpenGraphCxx/Data/ptr.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,11 @@ template <typename T> class ptr {
2525
using element_type = T;
2626
using difference_type = uint32_t;
2727

28-
private:
29-
difference_type _offset;
30-
31-
template <typename U> friend class ptr;
32-
3328
public:
3429
OG_INLINE OG_CONSTEXPR ptr(difference_type offset = 0) : _offset(offset){};
3530
OG_INLINE OG_CONSTEXPR ptr(std::nullptr_t){};
3631

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-
32+
// FIXME: this should be put into table API
4433
OG_INLINE
4534
element_type *_Nonnull get(vm_address_t base = shared_table().data_base()) const OG_NOEXCEPT {
4635
assert(_offset != 0);
@@ -100,6 +89,11 @@ template <typename T> class ptr {
10089
difference_type operator-(const ptr<U> &other) const OG_NOEXCEPT {
10190
return _offset - other._offset;
10291
};
92+
private:
93+
difference_type _offset;
94+
95+
template <typename U> friend class ptr;
96+
friend class table;
10397
}; /* ptr */
10498

10599
} /* data */

Sources/OpenGraphCxx/include/OpenGraphCxx/Data/table.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <OpenGraph/OGBase.h>
99
#include <OpenGraphCxx/Vector/vector.hpp>
10+
1011
#if OG_TARGET_OS_DARWIN
1112
#include <mach/vm_types.h>
1213
#else
@@ -19,6 +20,8 @@ typedef vm_offset_t vm_address_t;
1920
#include <os/lock.h>
2021
#endif
2122

23+
OG_ASSUME_NONNULL_BEGIN
24+
2225
namespace OG {
2326
namespace data {
2427
class zone;
@@ -28,8 +31,15 @@ template <typename T> class ptr;
2831
class table {
2932
public:
3033
class malloc_zone_deleter {
34+
void operator()(void* ptr) const {
35+
malloc_zone_free(_malloc_zone, ptr);
36+
}
3137
};
3238

39+
#if OG_TARGET_OS_DARWIN
40+
static malloc_zone_t *_malloc_zone;
41+
#endif
42+
3343
public:
3444
static table &ensure_shared();
3545

@@ -77,6 +87,14 @@ class table {
7787
return _zones_num;
7888
}
7989

90+
template <typename T>
91+
OG_INLINE
92+
void assert_valid(const ptr<T>& p) const {
93+
if (data_capacity() <= p._offset) {
94+
precondition_failure("invalid data offset: %u", p._offset);
95+
}
96+
}
97+
8098
table();
8199

82100
void grow_region() OG_NOEXCEPT;
@@ -87,7 +105,8 @@ class table {
87105

88106
void dealloc_page_locked(ptr<page> page) OG_NOEXCEPT;
89107

90-
uint64_t raw_page_seed(ptr<page> page) OG_NOEXCEPT;
108+
OG_INLINE OG_CONSTEXPR
109+
uint64_t raw_page_seed(ptr<page> page) const OG_NOEXCEPT;
91110

92111
void print() const OG_NOEXCEPT;
93112
private:
@@ -133,4 +152,6 @@ static table &shared_table() {
133152
} /* data */
134153
} /* OG */
135154

155+
OG_ASSUME_NONNULL_END
156+
136157
#endif /* table_hpp */

Sources/OpenGraphCxx/include/OpenGraphCxx/Data/zone.hpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,56 @@ namespace data {
1414
class zone {
1515
public:
1616
class info {
17-
private:
18-
constexpr static uint32_t zone_id_mask = 0x7fffffff;
19-
uint32_t _value;
20-
constexpr info(uint32_t value) : _value(value){};
21-
2217
public:
23-
uint32_t zone_id() { return _value & zone_id_mask; };
24-
info with_zone_id(uint32_t zone_id) const { return info((_value & ~zone_id_mask) | (zone_id & zone_id_mask)); };
18+
OG_INLINE OG_CONSTEXPR
19+
info() OG_NOEXCEPT : _value(0) {};
20+
21+
OG_INLINE OG_CONSTEXPR
22+
info(uint32_t value) OG_NOEXCEPT : _value(value){};
23+
24+
OG_INLINE OG_CONSTEXPR
25+
info(uint32_t zone_id, bool deleted) OG_NOEXCEPT : _value((zone_id & zone_id_mask) | (deleted ? 1 : 0)) {};
26+
27+
OG_INLINE OG_CONSTEXPR
28+
uint32_t value() const OG_NOEXCEPT { return _value; };
29+
30+
OG_INLINE OG_CONSTEXPR
31+
uint32_t zone_id() const OG_NOEXCEPT { return _value & zone_id_mask; };
32+
33+
OG_INLINE OG_CONSTEXPR
34+
bool is_deleted() const OG_NOEXCEPT { return (_value & deleted) != 0; };
2535

26-
uint32_t to_raw_value() { return _value; };
27-
static info from_raw_value(uint32_t value) { return info(value); };
36+
OG_INLINE OG_CONSTEXPR
37+
info with_zone_id(uint32_t zone_id) const OG_NOEXCEPT {
38+
return info((_value & ~zone_id_mask) | (zone_id & zone_id_mask), is_deleted());
39+
};
40+
41+
OG_INLINE OG_CONSTEXPR
42+
info with_deleted(bool deleted) const OG_NOEXCEPT {
43+
return info(zone_id(), deleted);
44+
}
45+
private:
46+
enum {
47+
zone_id_mask = 0x7fffffff,
48+
deleted = 0x80000000,
49+
};
50+
uint32_t _value;
2851
}; /* info */
2952
public:
30-
zone();
53+
// zone() OG_NOEXCEPT = default;
54+
// ~zone() OG_NOEXCEPT;
55+
56+
OG_INLINE OG_CONSTEXPR
57+
auto& malloc_buffers() const OG_NOEXCEPT { return _malloc_buffers; };
3158

3259
OG_INLINE OG_CONSTEXPR
3360
ptr<page> last_page() const OG_NOEXCEPT { return _last_page; };
3461

3562
OG_INLINE OG_CONSTEXPR
3663
info info() const OG_NOEXCEPT { return _info; };
3764

38-
void clear() OG_NOEXCEPT;
65+
OG_INLINE
66+
void clear();
3967

4068
ptr<void> alloc_slow(uint32_t size, uint32_t alignment_mask) OG_NOEXCEPT;
4169

@@ -49,15 +77,12 @@ class zone {
4977
// Printing
5078
void print() const OG_NOEXCEPT;
5179

52-
void print_header() OG_NOEXCEPT;
53-
54-
~zone();
80+
void print_header() const OG_NOEXCEPT;
5581
private:
5682
typedef struct _bytes_info {
5783
ptr<struct _bytes_info> next;
5884
uint32_t size;
5985
} bytes_info;
60-
6186
vector<std::unique_ptr<void, table::malloc_zone_deleter>, 0, uint32_t> _malloc_buffers;
6287
ptr<page> _last_page;
6388
ptr<bytes_info> _free_bytes;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// PageTests.swift
3+
// OpenGraphCxxTests
4+
5+
import OpenGraphCxx_Private
6+
import Testing
7+
8+
#if canImport(Darwin) // table() is not implemented on Linux yet.
9+
struct PageTests {
10+
11+
}
12+
#endif
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// TableTests.swift
3+
// OpenGraphCxxTests
4+
5+
#if canImport(Darwin) // table() is not implemented on Linux yet.
6+
import OpenGraphCxx_Private
7+
import Testing
8+
9+
struct TableTests {
10+
@Test
11+
func table() {
12+
let table = OG.data.table()
13+
table.print()
14+
}
15+
}
16+
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// ZoneTests.swift
3+
// OpenGraphCxxTests
4+
5+
#if canImport(Darwin)
6+
import OpenGraphCxx_Private
7+
import Testing
8+
9+
struct ZoneTests {
10+
@Test
11+
func print() {
12+
// let zone = OG.data.zone()
13+
// zone.print_header()
14+
// zone.print()
15+
}
16+
}
17+
#endif

Tests/OpenGraphCxxTests/OpenGraphCxxTests.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)