Change C APIs to a unified string implementation (#6299)

Currently in the entire C API of WPILib we have ~8 different ways of handling strings. The C API actually isn't built for pure C callers (We don't actually have any of those). Instead, they're built for interop between languages like LabVIEW and C# which can talk to C API's directly.

For output parameters, the choice was fairly obvious. An output struct containing a const string pointer and a length makes the most sense. Its easy to use these from most other languages, and doesn't require special null termination handling. Freeing these is also easy, as if you ever receive one of these string structures, theres just a single function call to free it.

Input parameters are a bit more complex. To be used from pure C, and from LabVIEW, a null terminated string is the best in most cases. However, null terminated strings in general have a lot of downsides. Additionally, from LabVIEW there are other considerations around encoding that having a wrapper struct helps make a bit easier. From a language like C#, a wrapper struct is by far the easiest, as custom marshalling can make it trivial to marshal both UTF8 and UTF16 strings down.

The final consideration is its nice to have an identical concept for both input and output. It makes the rules fairly easy to understand.

WPILib will not have any APIs that manipulate a string allocated externally. This means WPI_String can be const, as across the boundary it is always const.
If a WPILib API takes a const WPI_String*, WPILib will not manipulate or attempt to free that string, and that string is treated as an input. It is up to the caller to handle that memory, WPILib will never hold onto that memory longer than the call.
If a WPILib API takes a WPI_String*, that string is an output. WPILib will allocate that API with WPI_AllocateString(), fill in the string, and return to the caller. When the caller is done with the string, they must free it with WPI_FreeString().
If an output struct contains a WPI_String member, that member is considered read only, and should not be explicitly freed. The caller should call the free function for that struct.
If an array of WPI_Strings are returned, each individual string is considered read only, and should not be explicitly freed. The free function for that array should be called by the caller.
If an input struct containing a WPI_String, or an input array of WPI_Strings is passed to WPILib, the individual strings will not be manipulated or freed by WPILib, and the caller owns and should free that memory.
Callbacks also follow these rules. The most common is a callback either getting passed a const WPI_String* or a struct containing a WPI_String. In both of these cases, the callback target should consider these strings read only, and not attempt to free them or manipulate them.
This commit is contained in:
Thad House
2024-05-13 05:35:14 -07:00
committed by GitHub
parent 178fe99f12
commit 4ce8f3f935
60 changed files with 990 additions and 914 deletions

View File

@@ -339,17 +339,14 @@ void DriverStationData::SetJoystickType(int32_t stick, int32_t type) {
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetJoystickName(int32_t stick, const char* name,
size_t size) {
void DriverStationData::SetJoystickName(int32_t stick, std::string_view name) {
if (stick < 0 || stick >= kNumJoysticks) {
return;
}
std::scoped_lock lock(m_joystickDataMutex);
if (size > sizeof(m_joystickData[stick].descriptor.name) - 1) {
size = sizeof(m_joystickData[stick].descriptor.name) - 1;
}
std::strncpy(m_joystickData[stick].descriptor.name, name, size);
m_joystickData[stick].descriptor.name[size] = '\0';
auto copied = name.copy(m_joystickData[stick].descriptor.name,
sizeof(m_joystickData[stick].descriptor.name) - 1);
m_joystickData[stick].descriptor.name[copied] = '\0';
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
@@ -366,27 +363,20 @@ void DriverStationData::SetJoystickAxisType(int32_t stick, int32_t axis,
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
void DriverStationData::SetGameSpecificMessage(const char* message,
size_t size) {
void DriverStationData::SetGameSpecificMessage(std::string_view message) {
std::scoped_lock lock(m_matchInfoMutex);
if (size > sizeof(m_matchInfo.gameSpecificMessage) - 1) {
size = sizeof(m_matchInfo.gameSpecificMessage) - 1;
}
std::strncpy(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage),
message, size);
m_matchInfo.gameSpecificMessage[size] = '\0';
m_matchInfo.gameSpecificMessageSize =
std::strlen(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage));
auto copied =
message.copy(reinterpret_cast<char*>(m_matchInfo.gameSpecificMessage),
sizeof(m_matchInfo.gameSpecificMessage));
m_matchInfo.gameSpecificMessageSize = copied;
m_matchInfoCallbacks(&m_matchInfo);
}
void DriverStationData::SetEventName(const char* name, size_t size) {
void DriverStationData::SetEventName(std::string_view name) {
std::scoped_lock lock(m_matchInfoMutex);
if (size > sizeof(m_matchInfo.eventName) - 1) {
size = sizeof(m_matchInfo.eventName) - 1;
}
std::strncpy(m_matchInfo.eventName, name, size);
m_matchInfo.eventName[size] = '\0';
auto copied =
name.copy(m_matchInfo.eventName, sizeof(m_matchInfo.eventName) - 1);
m_matchInfo.eventName[copied] = '\0';
m_matchInfoCallbacks(&m_matchInfo);
}
@@ -551,20 +541,20 @@ void HALSIM_SetJoystickType(int32_t stick, int32_t type) {
SimDriverStationData->SetJoystickType(stick, type);
}
void HALSIM_SetJoystickName(int32_t stick, const char* name, size_t size) {
SimDriverStationData->SetJoystickName(stick, name, size);
void HALSIM_SetJoystickName(int32_t stick, const WPI_String* name) {
SimDriverStationData->SetJoystickName(stick, wpi::to_string_view(name));
}
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type) {
SimDriverStationData->SetJoystickAxisType(stick, axis, type);
}
void HALSIM_SetGameSpecificMessage(const char* message, size_t size) {
SimDriverStationData->SetGameSpecificMessage(message, size);
void HALSIM_SetGameSpecificMessage(const WPI_String* message) {
SimDriverStationData->SetGameSpecificMessage(wpi::to_string_view(message));
}
void HALSIM_SetEventName(const char* name, size_t size) {
SimDriverStationData->SetEventName(name, size);
void HALSIM_SetEventName(const WPI_String* name) {
SimDriverStationData->SetEventName(wpi::to_string_view(name));
}
void HALSIM_SetMatchType(HAL_MatchType type) {

View File

@@ -107,11 +107,11 @@ class DriverStationData {
void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void SetJoystickType(int32_t stick, int32_t type);
void SetJoystickName(int32_t stick, const char* name, size_t size);
void SetJoystickName(int32_t stick, std::string_view message);
void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);
void SetGameSpecificMessage(const char* message, size_t size);
void SetEventName(const char* name, size_t size);
void SetGameSpecificMessage(std::string_view message);
void SetEventName(std::string_view name);
void SetMatchType(HAL_MatchType type);
void SetMatchNumber(int32_t matchNumber);
void SetReplayNumber(int32_t replayNumber);

View File

@@ -53,25 +53,20 @@ void RoboRioData::CancelSerialNumberCallback(int32_t uid) {
m_serialNumberCallbacks.Cancel(uid);
}
size_t RoboRioData::GetSerialNumber(char* buffer, size_t size) {
void RoboRioData::GetSerialNumber(struct WPI_String* serialNumber) {
std::scoped_lock lock(m_serialNumberMutex);
size_t copied = m_serialNumber.copy(buffer, size);
// Null terminate
if (copied == size) {
copied -= 1;
}
buffer[copied] = '\0';
return copied;
auto write = WPI_AllocateString(serialNumber, m_serialNumber.size());
m_serialNumber.copy(write, m_serialNumber.size());
}
void RoboRioData::SetSerialNumber(const char* serialNumber, size_t size) {
void RoboRioData::SetSerialNumber(std::string_view serialNumber) {
// Limit serial number to 8 characters internally- serialnum environment
// variable is always 8 characters
if (size > 8) {
size = 8;
if (serialNumber.size() > 8) {
serialNumber = serialNumber.substr(0, 8);
}
std::scoped_lock lock(m_serialNumberMutex);
m_serialNumber = std::string(serialNumber, size);
m_serialNumber = std::string(serialNumber);
m_serialNumberCallbacks(m_serialNumber.c_str(), m_serialNumber.size());
}
@@ -90,22 +85,18 @@ void RoboRioData::CancelCommentsCallback(int32_t uid) {
m_commentsCallbacks.Cancel(uid);
}
size_t RoboRioData::GetComments(char* buffer, size_t size) {
void RoboRioData::GetComments(struct WPI_String* comments) {
std::scoped_lock lock(m_commentsMutex);
size_t copied = m_comments.copy(buffer, size);
// Null terminate if there is room
if (copied < size) {
buffer[copied] = '\0';
}
return copied;
auto write = WPI_AllocateString(comments, m_comments.size());
m_comments.copy(write, m_comments.size());
}
void RoboRioData::SetComments(const char* comments, size_t size) {
if (size > 64) {
size = 64;
void RoboRioData::SetComments(std::string_view comments) {
if (comments.size() > 64) {
comments = comments.substr(0, 64);
}
std::scoped_lock lock(m_commentsMutex);
m_comments = std::string(comments, size);
m_comments = std::string(comments);
m_commentsCallbacks(m_comments.c_str(), m_comments.size());
}
@@ -146,11 +137,11 @@ int32_t HALSIM_RegisterRoboRioSerialNumberCallback(
void HALSIM_CancelRoboRioSerialNumberCallback(int32_t uid) {
return SimRoboRioData->CancelSerialNumberCallback(uid);
}
size_t HALSIM_GetRoboRioSerialNumber(char* buffer, size_t size) {
return SimRoboRioData->GetSerialNumber(buffer, size);
void HALSIM_GetRoboRioSerialNumber(struct WPI_String* serialNumber) {
SimRoboRioData->GetSerialNumber(serialNumber);
}
void HALSIM_SetRoboRioSerialNumber(const char* serialNumber, size_t size) {
SimRoboRioData->SetSerialNumber(serialNumber, size);
void HALSIM_SetRoboRioSerialNumber(const struct WPI_String* serialNumber) {
SimRoboRioData->SetSerialNumber(wpi::to_string_view(serialNumber));
}
int32_t HALSIM_RegisterRoboRioCommentsCallback(
@@ -161,11 +152,11 @@ int32_t HALSIM_RegisterRoboRioCommentsCallback(
void HALSIM_CancelRoboRioCommentsCallback(int32_t uid) {
SimRoboRioData->CancelCommentsCallback(uid);
}
size_t HALSIM_GetRoboRioComments(char* buffer, size_t size) {
return SimRoboRioData->GetComments(buffer, size);
void HALSIM_GetRoboRioComments(struct WPI_String* comments) {
SimRoboRioData->GetComments(comments);
}
void HALSIM_SetRoboRioComments(const char* comments, size_t size) {
SimRoboRioData->SetComments(comments, size);
void HALSIM_SetRoboRioComments(const struct WPI_String* comments) {
SimRoboRioData->SetComments(wpi::to_string_view(comments));
}
void HALSIM_RegisterRoboRioAllCallbacks(HAL_NotifyCallback callback,

View File

@@ -72,14 +72,14 @@ class RoboRioData {
int32_t RegisterSerialNumberCallback(HAL_RoboRioStringCallback callback,
void* param, HAL_Bool initialNotify);
void CancelSerialNumberCallback(int32_t uid);
size_t GetSerialNumber(char* buffer, size_t size);
void SetSerialNumber(const char* serialNumber, size_t size);
void GetSerialNumber(struct WPI_String* serialNumber);
void SetSerialNumber(std::string_view serialNumber);
int32_t RegisterCommentsCallback(HAL_RoboRioStringCallback callback,
void* param, HAL_Bool initialNotify);
void CancelCommentsCallback(int32_t uid);
size_t GetComments(char* buffer, size_t size);
void SetComments(const char* comments, size_t size);
void GetComments(struct WPI_String* comments);
void SetComments(std::string_view comments);
virtual void ResetData();