Skip to content

Commit 2e39556

Browse files
authored
add abi stable way to query the size of index_t (#1064)
* add abi stable way to query the size of index_t
1 parent 57114a3 commit 2e39556

10 files changed

Lines changed: 120 additions & 3 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s
66

77
## Unreleased
88

9+
### Added
10+
#### General
11+
- Added C++ `int DataType::sizeof_index_t()` and C `int conduit_datatype_sizeof_index_t()` methods to provide a stable ABI to determine configured size (number of bytes) of Conduit's index_t type.
12+
913
### Fixed
1014

1115
#### General

src/libs/conduit/c/conduit_datatype.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extern "C" {
3232
struct conduit_datatype_impl;
3333
typedef struct conduit_datatype_impl conduit_datatype;
3434

35+
CONDUIT_API int conduit_datatype_sizeof_index_t();
36+
3537
CONDUIT_API conduit_index_t conduit_datatype_id(const conduit_datatype *cdatatype);
3638
CONDUIT_API char* conduit_datatype_name(const conduit_datatype *cdatatype);
3739
CONDUIT_API void conduit_datatype_name_destroy(char *name);

src/libs/conduit/c/conduit_datatype_c.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ extern "C" {
2929

3030
using namespace conduit;
3131

32+
int conduit_datatype_sizeof_index_t()
33+
{
34+
return DataType::sizeof_index_t();
35+
}
36+
3237
conduit_index_t conduit_datatype_id(const conduit_datatype *cdatatype)
3338
{
3439
return cpp_datatype_ref(cdatatype).id();

src/libs/conduit/conduit_bitwidth_style_types.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ typedef conduit_int64 conduit_index64_t;
338338
/// use a 64-bit index, unless CONDUIT_INDEX_32 is defined.
339339
#ifdef CONDUIT_INDEX_32
340340
typedef conduit_index32_t conduit_index_t;
341+
#define CONDUIT_SIZEOF_INDEX_T 4
341342
#else
342343
typedef conduit_index64_t conduit_index_t;
344+
#define CONDUIT_SIZEOF_INDEX_T 8
343345
#endif
344346

345347
//-----------------------------------------------------------------------------

src/libs/conduit/conduit_core.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,18 @@ about(Node &n)
8585
n["system"] = CONDUIT_SYSTEM_TYPE;
8686
n["install_prefix"] = CONDUIT_INSTALL_PREFIX;
8787
n["license"] = CONDUIT_LICENSE_TEXT;
88-
88+
89+
Node &nt = n["index_t_typemap"];
90+
91+
// index_t
92+
#ifdef CONDUIT_INDEX_32
93+
nt["index_t"] = "int32";
94+
nt["sizeof_index_t"] = 4;
95+
#else
96+
nt["index_t"] = "int64";
97+
nt["sizeof_index_t"] = 8;
98+
#endif
99+
89100
// Type Info Map
90101
Node &nn = n["native_typemap"];
91102

@@ -152,6 +163,14 @@ about(Node &n)
152163
nn["float64"] = "<unmapped>";
153164
#endif
154165

166+
// index_t
167+
#ifdef CONDUIT_INDEX_32
168+
nn["index_t"] = CONDUIT_INT32_NATIVE_NAME;
169+
#else
170+
nn["index_t"] = CONDUIT_INT64_NATIVE_NAME;
171+
#endif
172+
173+
155174
}
156175

157176

src/libs/conduit/conduit_data_type.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,38 @@ namespace conduit
6161
{
6262

6363

64+
//-----------------------------------------------------------------------------
6465
DataType
6566
DataType::empty()
6667
{
6768
return DataType(EMPTY_ID);
6869
}
6970

71+
//-----------------------------------------------------------------------------
7072
DataType
7173
DataType::object()
7274
{
7375
return DataType(OBJECT_ID);
7476
}
7577

78+
//-----------------------------------------------------------------------------
7679
DataType
7780
DataType::list()
7881
{
7982
return DataType(LIST_ID);
8083
}
8184

85+
//-----------------------------------------------------------------------------
86+
int
87+
DataType::sizeof_index_t()
88+
{
89+
#ifdef CONDUIT_INDEX_32
90+
return 4;
91+
#else
92+
return 8;
93+
#endif
94+
}
95+
8296

8397

8498
//-----------------------------------------------------------------------------

src/libs/conduit/conduit_data_type.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ class CONDUIT_API DataType
7171
CHAR8_STR_ID = CONDUIT_CHAR8_STR_ID // char8 string (incore c-string)
7272
} TypeID;
7373

74+
//-----------------------------------------------------------------------------
75+
/// DataType::sizeof_index_t() provides an ABI stable way to probe the size
76+
/// of index_t.
77+
///
78+
/// index_t is fundamental to sizes in Conduit's interface,
79+
/// it is usually 64-bits but there is still a 32-bit option.
80+
//-----------------------------------------------------------------------------
81+
static int sizeof_index_t();
82+
7483
//-----------------------------------------------------------------------------
7584
// -- begin conduit::DataType Objects Constructor Helpers --
7685
//-----------------------------------------------------------------------------
@@ -86,7 +95,7 @@ class CONDUIT_API DataType
8695
static DataType empty();
8796
static DataType object();
8897
static DataType list();
89-
98+
9099
//-----------------------------------------------------------------------------
91100
// -- end conduit::DataType Objects Constructor Helpers --
92101
//-----------------------------------------------------------------------------

src/tests/conduit/c/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
set(C_TESTS
1010
t_c_conduit_smoke
1111
t_c_conduit_node
12-
t_c_conduit_node_set)
12+
t_c_conduit_node_set
13+
t_c_conduit_datatype)
1314

1415
################################
1516
# Add our tests
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Lawrence Livermore National Security, LLC and other Conduit
2+
// Project developers. See top-level LICENSE AND COPYRIGHT files for dates and
3+
// other details. No copyright assignment is required to contribute to Conduit.
4+
5+
//-----------------------------------------------------------------------------
6+
///
7+
/// file: t_c_conduit_datatype.cpp
8+
///
9+
//-----------------------------------------------------------------------------
10+
11+
#include "conduit.h"
12+
13+
#include <stdio.h>
14+
#include "gtest/gtest.h"
15+
16+
//-----------------------------------------------------------------------------
17+
TEST(c_conduit_datatype, sizeof_index_t)
18+
{
19+
int sz_it = conduit_datatype_sizeof_index_t();
20+
21+
#ifdef CONDUIT_INDEX_32
22+
EXPECT_EQ(sz_it,4);
23+
#else
24+
EXPECT_EQ(sz_it,8);
25+
#endif
26+
27+
}
28+

src/tests/conduit/t_conduit_datatype_tests.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,36 @@ TEST(dtype_tests,dtype_comparison_checks)
737737
}
738738
}
739739
}
740+
741+
742+
//-----------------------------------------------------------------------------
743+
TEST(dtype_tests,sizeof_index_t)
744+
{
745+
int sz_it = DataType::sizeof_index_t();
746+
747+
#ifdef CONDUIT_INDEX_32
748+
EXPECT_EQ(sz_it,4);
749+
#else
750+
EXPECT_EQ(sz_it,8);
751+
#endif
752+
753+
EXPECT_EQ(sz_it,CONDUIT_SIZEOF_INDEX_T);
754+
755+
// also check about entries
756+
Node n_about;
757+
conduit::about(n_about);
758+
759+
760+
Node &n_it_info = n_about["index_t_typemap"];
761+
EXPECT_EQ(sz_it,n_it_info["sizeof_index_t"].to_int());
762+
763+
if(sz_it == 4)
764+
{
765+
EXPECT_EQ(n_it_info["index_t"].as_string(),"int32");
766+
}
767+
else // if(sz_it == 8)
768+
{
769+
EXPECT_EQ(n_it_info["index_t"].as_string(),"int64");
770+
}
771+
772+
}

0 commit comments

Comments
 (0)