Skip to content
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

Implement string allocator #6

Closed
Rantanen opened this issue Dec 5, 2017 · 2 comments
Closed

Implement string allocator #6

Rantanen opened this issue Dec 5, 2017 · 2 comments
Assignees

Comments

@Rantanen
Copy link
Owner

Rantanen commented Dec 5, 2017

Currently our BSTR implementation uses SysAllocString, etc. methods from the Windows API. This is the de facto way to allocate/deallocate BSTRs on Windows.

For cross platform purposes we need an alternative way. One option would be to statically implement a IStringAllocator interface with a known GUID. This allows the COM client to acquire a IStringAllocator interface that provides FreeString(BSTR) method. This method can then allocate/deallocate the BSTR properly.

The only important consideration with the implementation is that the allocation and deallocation is done in a compatible way with each other. Thus if we allocate the BSTR from Rust heap, the deallocate must free the BSTR into Rust heap. If we use SysAllocString then the freeing must be done with SysFreeString.


Tasks:

  • Define the interface as intercom::IStringAllocator or similar trait. The implementation can be static intercom::StringAllocator as well. We might not even need the trait but instead just use the implicit impl instead.
  • Modify the #[com_library] code gen to include the StringAllocator in the ClassFactory constructor.
  • Implement string allocation with SysAllocString in Windows and something else (such as Rust heap/Box) on other platforms.
@Fluxie
Copy link
Contributor

Fluxie commented Feb 9, 2018

I would like to have the ability to pass raw strings as well. Going through Bstr requires additional effort especially on Linux since the native character type is char. And when the machine's locale is set to UTF-8 we could omit all conversions and just verify that the string we receive from C++ is a valid UTF-8 string. This topic was discussed on IRC quite a bit already.

This is my current understanding and a summary of the earlier discussion:

  • We will generate two interfaces for traits.
    • The default version shall pass Strings and &str's as native character type by default.
    • The other, fast but unsafe, version shall pass strings as U8Str structs with the promise that they contain a valid UTF-8 string which we just cast directly into &str in Rust. This enables a fast and somewhat safe way for C++ applications to pass strings to Rust code.
  • The default character type for Strings and &strs will be the native character type of the platform. On Windows this will be "wchar_t" and on others it will be "char"
    • We will probably need an attribute which allows the developer specify the exact type.
  • The Bstr type will function identically on all platforms. We will need to implement it manually on non-Windows platforms.
    • Typedef of the BSTR must be char16_t on non-Windows platforms. The size of "wchar_t" may be 32-bits.

@Rantanen Rantanen self-assigned this Jul 24, 2018
@Rantanen
Copy link
Owner Author

We have an allocator for BStrings and raw memory. Once we support U8Strings (or whatever we end up calling them) we can add that to the IAllocator interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants