Skip to content

Commit

Permalink
add abi stable way to query the size of index_t (#1064)
Browse files Browse the repository at this point in the history
* add abi stable way to query the size of index_t
  • Loading branch information
cyrush committed Jan 9, 2023
1 parent 57114a3 commit 2e39556
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project aspires to adhere to [Semantic Versioning](https://semver.org/s

## Unreleased

### Added
#### General
- 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.

### Fixed

#### General
Expand Down
2 changes: 2 additions & 0 deletions src/libs/conduit/c/conduit_datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extern "C" {
struct conduit_datatype_impl;
typedef struct conduit_datatype_impl conduit_datatype;

CONDUIT_API int conduit_datatype_sizeof_index_t();

CONDUIT_API conduit_index_t conduit_datatype_id(const conduit_datatype *cdatatype);
CONDUIT_API char* conduit_datatype_name(const conduit_datatype *cdatatype);
CONDUIT_API void conduit_datatype_name_destroy(char *name);
Expand Down
5 changes: 5 additions & 0 deletions src/libs/conduit/c/conduit_datatype_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ extern "C" {

using namespace conduit;

int conduit_datatype_sizeof_index_t()
{
return DataType::sizeof_index_t();
}

conduit_index_t conduit_datatype_id(const conduit_datatype *cdatatype)
{
return cpp_datatype_ref(cdatatype).id();
Expand Down
2 changes: 2 additions & 0 deletions src/libs/conduit/conduit_bitwidth_style_types.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,10 @@ typedef conduit_int64 conduit_index64_t;
/// use a 64-bit index, unless CONDUIT_INDEX_32 is defined.
#ifdef CONDUIT_INDEX_32
typedef conduit_index32_t conduit_index_t;
#define CONDUIT_SIZEOF_INDEX_T 4
#else
typedef conduit_index64_t conduit_index_t;
#define CONDUIT_SIZEOF_INDEX_T 8
#endif

//-----------------------------------------------------------------------------
Expand Down
21 changes: 20 additions & 1 deletion src/libs/conduit/conduit_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ about(Node &n)
n["system"] = CONDUIT_SYSTEM_TYPE;
n["install_prefix"] = CONDUIT_INSTALL_PREFIX;
n["license"] = CONDUIT_LICENSE_TEXT;


Node &nt = n["index_t_typemap"];

// index_t
#ifdef CONDUIT_INDEX_32
nt["index_t"] = "int32";
nt["sizeof_index_t"] = 4;
#else
nt["index_t"] = "int64";
nt["sizeof_index_t"] = 8;
#endif

// Type Info Map
Node &nn = n["native_typemap"];

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

// index_t
#ifdef CONDUIT_INDEX_32
nn["index_t"] = CONDUIT_INT32_NATIVE_NAME;
#else
nn["index_t"] = CONDUIT_INT64_NATIVE_NAME;
#endif


}


Expand Down
14 changes: 14 additions & 0 deletions src/libs/conduit/conduit_data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,38 @@ namespace conduit
{


//-----------------------------------------------------------------------------
DataType
DataType::empty()
{
return DataType(EMPTY_ID);
}

//-----------------------------------------------------------------------------
DataType
DataType::object()
{
return DataType(OBJECT_ID);
}

//-----------------------------------------------------------------------------
DataType
DataType::list()
{
return DataType(LIST_ID);
}

//-----------------------------------------------------------------------------
int
DataType::sizeof_index_t()
{
#ifdef CONDUIT_INDEX_32
return 4;
#else
return 8;
#endif
}



//-----------------------------------------------------------------------------
Expand Down
11 changes: 10 additions & 1 deletion src/libs/conduit/conduit_data_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ class CONDUIT_API DataType
CHAR8_STR_ID = CONDUIT_CHAR8_STR_ID // char8 string (incore c-string)
} TypeID;

//-----------------------------------------------------------------------------
/// DataType::sizeof_index_t() provides an ABI stable way to probe the size
/// of index_t.
///
/// index_t is fundamental to sizes in Conduit's interface,
/// it is usually 64-bits but there is still a 32-bit option.
//-----------------------------------------------------------------------------
static int sizeof_index_t();

//-----------------------------------------------------------------------------
// -- begin conduit::DataType Objects Constructor Helpers --
//-----------------------------------------------------------------------------
Expand All @@ -86,7 +95,7 @@ class CONDUIT_API DataType
static DataType empty();
static DataType object();
static DataType list();

//-----------------------------------------------------------------------------
// -- end conduit::DataType Objects Constructor Helpers --
//-----------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/tests/conduit/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
set(C_TESTS
t_c_conduit_smoke
t_c_conduit_node
t_c_conduit_node_set)
t_c_conduit_node_set
t_c_conduit_datatype)

################################
# Add our tests
Expand Down
28 changes: 28 additions & 0 deletions src/tests/conduit/c/t_c_conduit_datatype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Lawrence Livermore National Security, LLC and other Conduit
// Project developers. See top-level LICENSE AND COPYRIGHT files for dates and
// other details. No copyright assignment is required to contribute to Conduit.

//-----------------------------------------------------------------------------
///
/// file: t_c_conduit_datatype.cpp
///
//-----------------------------------------------------------------------------

#include "conduit.h"

#include <stdio.h>
#include "gtest/gtest.h"

//-----------------------------------------------------------------------------
TEST(c_conduit_datatype, sizeof_index_t)
{
int sz_it = conduit_datatype_sizeof_index_t();

#ifdef CONDUIT_INDEX_32
EXPECT_EQ(sz_it,4);
#else
EXPECT_EQ(sz_it,8);
#endif

}

33 changes: 33 additions & 0 deletions src/tests/conduit/t_conduit_datatype_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,36 @@ TEST(dtype_tests,dtype_comparison_checks)
}
}
}


//-----------------------------------------------------------------------------
TEST(dtype_tests,sizeof_index_t)
{
int sz_it = DataType::sizeof_index_t();

#ifdef CONDUIT_INDEX_32
EXPECT_EQ(sz_it,4);
#else
EXPECT_EQ(sz_it,8);
#endif

EXPECT_EQ(sz_it,CONDUIT_SIZEOF_INDEX_T);

// also check about entries
Node n_about;
conduit::about(n_about);


Node &n_it_info = n_about["index_t_typemap"];
EXPECT_EQ(sz_it,n_it_info["sizeof_index_t"].to_int());

if(sz_it == 4)
{
EXPECT_EQ(n_it_info["index_t"].as_string(),"int32");
}
else // if(sz_it == 8)
{
EXPECT_EQ(n_it_info["index_t"].as_string(),"int64");
}

}

0 comments on commit 2e39556

Please sign in to comment.