-
Notifications
You must be signed in to change notification settings - Fork 196
Add a few C APIs #2047
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a few C APIs #2047
Conversation
@cdleary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good -- just the only thing I think should change is that the API should offer an xls_bytes_free to do the delete[] of the xls_bits_to_bytes() result under the hood.
With that in place all looks good!
I'm not sure why the CLA scan didn't happen automatically but I think to land the change I imagine you'll need to do https://cla.developers.google.com/about/google-individual
xls/public/c_api.cc
Outdated
const auto* cpp_bits = reinterpret_cast<const xls::Bits*>(bits); | ||
auto bytes = cpp_bits->ToBytes(); | ||
*byte_count_out = bytes.size(); | ||
*bytes_out = (uint8_t*)malloc(bytes.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need a xls_bytes_free()
for this (because the libxls allocator can be different from the surrounding process so you can't assume the surrounding process can use the same free()
), and generally I think we've been doing new[] inside the impl e.g. https://sourcegraph.com/github.com/google/xls@ef07f4bc7db8df70dc32e52a04d268653b58847a/-/blob/xls/public/c_api_impl_helpers.cc?L72
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, can you take another look?
19089f8
to
03d519c
Compare
I am a Googler thus have signed CLA. |
xls/public/c_api.h
Outdated
@@ -199,6 +213,21 @@ bool xls_bits_eq(const struct xls_bits* a, const struct xls_bits* b); | |||
// returns 1, `xls_bits_get_bit(bits, 1)` returns 0. | |||
bool xls_bits_get_bit(const struct xls_bits* bits, int64_t index); | |||
|
|||
// Returns the bytes in the given bits value. The caller must | |||
// free the returned `bytes_out` pointer via `free`, The caller |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be freed via xls_bytes_free
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
xls/public/c_api.h
Outdated
// Converts the given bits value to a signed or unsigned integer 64-bit integer | ||
// value. if the conversion is not possible, false is returned and `error_out` | ||
// contains the error message and the caller must free the `value_out` pointer | ||
// itself via `free`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the success case, no memory is returned, so there is nothing to free, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc in comment updated.
e205c20
to
2eadc0f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Disclaimer: I am not familiar with the coding conventions of this project. Some of the code does not respect the Google style guide (like the use of auto
) but similar patterns are used in the rest of the file so I guess it's fine.
xls/public/c_api.h
Outdated
uint8_t** bytes_out, size_t* byte_count_out); | ||
|
||
// Converts the given bits value to a signed or unsigned integer 64-bit integer | ||
// value. if the conversion is not possible, false is returned, `error_out` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ultra-nit:
// value. if the conversion is not possible, false is returned, `error_out` | |
// value. If the conversion is not possible, false is returned, `error_out` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
It would be nice to have a lint tool for style conformance. |
@cdleary can you take another look and if it is OK, can we proceed with the merge? |
Add xls_value_get_kind to allow inspecting the type of a Value object. Signed-off-by: Hui Peng <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! You may want to check that this has nothing flagged under bazel test -c opt --config=asan
or it may hit a snag when folks attempt to import it to the repo.
xls/public/c_api.cc
Outdated
CHECK(error_out != nullptr); | ||
CHECK(byte_count_out != nullptr); | ||
const auto* cpp_bits = reinterpret_cast<const xls::Bits*>(bits); | ||
auto bytes = cpp_bits->ToBytes(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the type is not obvious from the right hand side expression this auto should probably be elided. (That's the general C++ style guide guideline.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the type is not obvious from the right hand side expression this auto should probably be elided. (That's the general C++ style guide guideline.)
Fixed.
Yeah this is a good point, an Thanks for helping build out this API! cc @meheff @richmckeever and team to tag for import. |
Can we add a check in the CI? |
Fixed |
I got the following odr violation, is it expected?================================================================= These globals were registered at these points: [2]: ==3602574==HINT: if you don't care about these errors you may set ASAN_OPTIONS=detect_odr_violation=0 |
Yes I get that as well, use the env var to disable it as it suggests: |
#1447 is the issue that tracks this but I'm not sure there's active movement on it |
I ran the following command and all tests passed
|
Awesome! I expect the google XLS team build gardener should be able to merge successfully then. @benquike Just out of curiosity are you building out the C APIs for use in the Rust crate? (https://github.com/xlsynth/xlsynth-crate) Or a different use case? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG overall. Just the comment on auto
from cdleary@ to address and then can merge.
- xls_bits_to_bytes - xls_bits_to_uint64 - xls_bits_to_int64 Signed-off-by: Hui Peng <[email protected]>
No, We are using these APIs to create python bindings. |
Nah it's ok was just curious, I can add them there no problem, look forward to the Python bindings if you'll be doing them in open source! |
No description provided.