c: align hash structs with native types

This commit is contained in:
Adrien Beraud committed 2026-10-26 23:57:50 -04:00
1 parent 3f7b1bc3f5
commit 1db8abb7fc
4 files changed
+22 -4

No files matched your search

+4
View File
@@ -122,6 +122,10 @@ Enable `OPENDHT_C=ON` to build the C ABI. With `BUILD_TESTING=ON`, CTest runs
enabled. Rust regression and callback-lifetime tests run with Cargo; see
[the Rust binding guide](rust/README.md) for build and library-path settings.
`dht_infohash` and `dht_pkid` have `uint32_t` alignment, matching the native hash
types, while retaining their 20-byte and 32-byte sizes. Rebuild consumers against
the updated header; foreign-language declarations must match this alignment.
C handles returned by constructors, imports, and reference functions are owned
and require their matching delete/unref function. Data views, string getters,
query fields, and callback arguments are borrowed: copy them or take a reference
+5
View File
@@ -12,6 +12,11 @@ using PrivkeySp = std::shared_ptr<dht::crypto::PrivateKey>;
using PubkeySp = std::shared_ptr<const dht::crypto::PublicKey>;
using CertSp = std::shared_ptr<dht::crypto::Certificate>;
static_assert(sizeof(dht_infohash) == sizeof(dht::InfoHash));
static_assert(alignof(dht_infohash) == alignof(dht::InfoHash));
static_assert(sizeof(dht_pkid) == sizeof(dht::PkId));
static_assert(alignof(dht_pkid) == alignof(dht::PkId));
struct CompletionGuard
{
CompletionGuard(dht_done_cb callback, void* data)
+11 -2
View File
@@ -39,9 +39,17 @@ OPENDHT_C_PUBLIC dht_data_view dht_blob_get_data(const dht_blob* data);
OPENDHT_C_PUBLIC void dht_blob_delete(dht_blob* data);
// dht::InfoHash
#ifdef __cplusplus
#define OPENDHT_C_ALIGN_UINT32 alignas(uint32_t)
#elif defined(_MSC_VER)
#define OPENDHT_C_ALIGN_UINT32 __declspec(align(4))
#else
#define OPENDHT_C_ALIGN_UINT32 _Alignas(uint32_t)
#endif
struct OPENDHT_C_PUBLIC dht_infohash
{
uint8_t d[HASH_LEN];
OPENDHT_C_ALIGN_UINT32 uint8_t d[HASH_LEN];
};
typedef struct dht_infohash dht_infohash;
OPENDHT_C_PUBLIC void dht_infohash_zero(dht_infohash* h);
@@ -56,9 +64,10 @@ OPENDHT_C_PUBLIC bool dht_infohash_is_zero(const dht_infohash* h);
// dht::PkId
struct OPENDHT_C_PUBLIC dht_pkid
{
uint8_t d[32];
OPENDHT_C_ALIGN_UINT32 uint8_t d[32];
};
typedef struct dht_pkid dht_pkid;
#undef OPENDHT_C_ALIGN_UINT32
OPENDHT_C_PUBLIC const char* dht_pkid_print(const dht_pkid* h);
// dht::crypto::PublicKey
+2 -2
View File
@@ -26,13 +26,13 @@ pub struct Blob {
_thread_bound: std::marker::PhantomData<*mut ()>,
}
#[repr(C)]
#[repr(C, align(4))]
#[derive(PartialEq)]
pub struct InfoHash {
pub d: [u8; HASH_LEN],
}
#[repr(C)]
#[repr(C, align(4))]
pub struct PkId {
pub d: [u8; PKID_LEN],
}