diff --git a/cscore/src/main/native/cpp/HttpCameraImpl.cpp b/cscore/src/main/native/cpp/HttpCameraImpl.cpp index 4ffc7f15dd..a16c672f29 100644 --- a/cscore/src/main/native/cpp/HttpCameraImpl.cpp +++ b/cscore/src/main/native/cpp/HttpCameraImpl.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include "Handle.h" @@ -574,7 +575,8 @@ void CS_SetHttpCameraUrls(CS_Source source, const char** urls, int count, char** CS_GetHttpCameraUrls(CS_Source source, int* count, CS_Status* status) { auto urls = cs::GetHttpCameraUrls(source, status); - char** out = static_cast(std::malloc(urls.size() * sizeof(char*))); + char** out = + static_cast(wpi::CheckedMalloc(urls.size() * sizeof(char*))); *count = urls.size(); for (size_t i = 0; i < urls.size(); ++i) out[i] = cs::ConvertToC(urls[i]); return out; diff --git a/cscore/src/main/native/cpp/UsbCameraImpl.cpp b/cscore/src/main/native/cpp/UsbCameraImpl.cpp index d53f2560c6..ae46b06013 100644 --- a/cscore/src/main/native/cpp/UsbCameraImpl.cpp +++ b/cscore/src/main/native/cpp/UsbCameraImpl.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include "Handle.h" @@ -1315,7 +1316,7 @@ char* CS_GetUsbCameraPath(CS_Source source, CS_Status* status) { CS_UsbCameraInfo* CS_EnumerateUsbCameras(int* count, CS_Status* status) { auto cameras = cs::EnumerateUsbCameras(status); CS_UsbCameraInfo* out = static_cast( - std::malloc(cameras.size() * sizeof(CS_UsbCameraInfo))); + wpi::CheckedMalloc(cameras.size() * sizeof(CS_UsbCameraInfo))); *count = cameras.size(); for (size_t i = 0; i < cameras.size(); ++i) { out[i].dev = cameras[i].dev; diff --git a/cscore/src/main/native/cpp/c_util.h b/cscore/src/main/native/cpp/c_util.h index 8e2b0349bc..6264e1fc71 100644 --- a/cscore/src/main/native/cpp/c_util.h +++ b/cscore/src/main/native/cpp/c_util.h @@ -12,11 +12,12 @@ #include #include +#include namespace cs { inline char* ConvertToC(wpi::StringRef in) { - char* out = static_cast(std::malloc(in.size() + 1)); + char* out = static_cast(wpi::CheckedMalloc(in.size() + 1)); std::memmove(out, in.data(), in.size()); out[in.size()] = '\0'; return out; diff --git a/cscore/src/main/native/cpp/cscore_c.cpp b/cscore/src/main/native/cpp/cscore_c.cpp index 335e7b142e..467fa3c0fe 100644 --- a/cscore/src/main/native/cpp/cscore_c.cpp +++ b/cscore/src/main/native/cpp/cscore_c.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "c_util.h" #include "cscore_cpp.h" @@ -68,7 +69,8 @@ void CS_SetStringProperty(CS_Property property, const char* value, char** CS_GetEnumPropertyChoices(CS_Property property, int* count, CS_Status* status) { auto choices = cs::GetEnumPropertyChoices(property, status); - char** out = static_cast(std::malloc(choices.size() * sizeof(char*))); + char** out = + static_cast(wpi::CheckedMalloc(choices.size() * sizeof(char*))); *count = choices.size(); for (size_t i = 0; i < choices.size(); ++i) out[i] = cs::ConvertToC(choices[i]); @@ -110,8 +112,8 @@ CS_Property* CS_EnumerateSourceProperties(CS_Source source, int* count, CS_Status* status) { wpi::SmallVector buf; auto vec = cs::EnumerateSourceProperties(source, buf, status); - CS_Property* out = - static_cast(std::malloc(vec.size() * sizeof(CS_Property))); + CS_Property* out = static_cast( + wpi::CheckedMalloc(vec.size() * sizeof(CS_Property))); *count = vec.size(); std::copy(vec.begin(), vec.end(), out); return out; @@ -162,7 +164,7 @@ CS_VideoMode* CS_EnumerateSourceVideoModes(CS_Source source, int* count, CS_Status* status) { auto vec = cs::EnumerateSourceVideoModes(source, status); CS_VideoMode* out = static_cast( - std::malloc(vec.size() * sizeof(CS_VideoMode))); + wpi::CheckedMalloc(vec.size() * sizeof(CS_VideoMode))); *count = vec.size(); std::copy(vec.begin(), vec.end(), out); return out; @@ -172,8 +174,8 @@ CS_Sink* CS_EnumerateSourceSinks(CS_Source source, int* count, CS_Status* status) { wpi::SmallVector buf; auto handles = cs::EnumerateSourceSinks(source, buf, status); - CS_Sink* sinks = - static_cast(std::malloc(handles.size() * sizeof(CS_Sink))); + CS_Sink* sinks = static_cast( + wpi::CheckedMalloc(handles.size() * sizeof(CS_Sink))); *count = handles.size(); std::copy(handles.begin(), handles.end(), sinks); return sinks; @@ -323,8 +325,8 @@ void CS_SetDefaultLogger(unsigned int min_level) { CS_Source* CS_EnumerateSources(int* count, CS_Status* status) { wpi::SmallVector buf; auto handles = cs::EnumerateSourceHandles(buf, status); - CS_Source* sources = - static_cast(std::malloc(handles.size() * sizeof(CS_Source))); + CS_Source* sources = static_cast( + wpi::CheckedMalloc(handles.size() * sizeof(CS_Source))); *count = handles.size(); std::copy(handles.begin(), handles.end(), sources); return sources; @@ -342,8 +344,8 @@ void CS_ReleaseEnumeratedSources(CS_Source* sources, int count) { CS_Sink* CS_EnumerateSinks(int* count, CS_Status* status) { wpi::SmallVector buf; auto handles = cs::EnumerateSinkHandles(buf, status); - CS_Sink* sinks = - static_cast(std::malloc(handles.size() * sizeof(CS_Sink))); + CS_Sink* sinks = static_cast( + wpi::CheckedMalloc(handles.size() * sizeof(CS_Sink))); *count = handles.size(); std::copy(handles.begin(), handles.end(), sinks); return sinks; @@ -378,8 +380,8 @@ char* CS_GetHostname() { return cs::ConvertToC(cs::GetHostname()); } char** CS_GetNetworkInterfaces(int* count) { auto interfaces = cs::GetNetworkInterfaces(); - char** out = - static_cast(std::malloc(interfaces.size() * sizeof(char*))); + char** out = static_cast( + wpi::CheckedMalloc(interfaces.size() * sizeof(char*))); *count = interfaces.size(); for (size_t i = 0; i < interfaces.size(); ++i) out[i] = cs::ConvertToC(interfaces[i]); diff --git a/ntcore/src/main/native/cpp/Value.cpp b/ntcore/src/main/native/cpp/Value.cpp index 0c906b1852..6cd60cdb40 100644 --- a/ntcore/src/main/native/cpp/Value.cpp +++ b/ntcore/src/main/native/cpp/Value.cpp @@ -7,6 +7,7 @@ #include +#include #include #include "Value_internal.h" @@ -113,7 +114,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) { case NT_BOOLEAN_ARRAY: { auto v = in.GetBooleanArray(); out->data.arr_boolean.arr = - static_cast(std::malloc(v.size() * sizeof(int))); + static_cast(wpi::CheckedMalloc(v.size() * sizeof(int))); out->data.arr_boolean.size = v.size(); std::copy(v.begin(), v.end(), out->data.arr_boolean.arr); break; @@ -121,15 +122,15 @@ void nt::ConvertToC(const Value& in, NT_Value* out) { case NT_DOUBLE_ARRAY: { auto v = in.GetDoubleArray(); out->data.arr_double.arr = - static_cast(std::malloc(v.size() * sizeof(double))); + static_cast(wpi::CheckedMalloc(v.size() * sizeof(double))); out->data.arr_double.size = v.size(); std::copy(v.begin(), v.end(), out->data.arr_double.arr); break; } case NT_STRING_ARRAY: { auto v = in.GetStringArray(); - out->data.arr_string.arr = - static_cast(std::malloc(v.size() * sizeof(NT_String))); + out->data.arr_string.arr = static_cast( + wpi::CheckedMalloc(v.size() * sizeof(NT_String))); for (size_t i = 0; i < v.size(); ++i) ConvertToC(v[i], &out->data.arr_string.arr[i]); out->data.arr_string.size = v.size(); @@ -144,7 +145,7 @@ void nt::ConvertToC(const Value& in, NT_Value* out) { void nt::ConvertToC(wpi::StringRef in, NT_String* out) { out->len = in.size(); - out->str = static_cast(std::malloc(in.size() + 1)); + out->str = static_cast(wpi::CheckedMalloc(in.size() + 1)); std::memcpy(out->str, in.data(), in.size()); out->str[in.size()] = '\0'; } diff --git a/ntcore/src/main/native/cpp/WireDecoder.cpp b/ntcore/src/main/native/cpp/WireDecoder.cpp index b86472bfe6..347f13ee71 100644 --- a/ntcore/src/main/native/cpp/WireDecoder.cpp +++ b/ntcore/src/main/native/cpp/WireDecoder.cpp @@ -15,6 +15,7 @@ #include #include +#include using namespace nt; @@ -52,7 +53,7 @@ WireDecoder::WireDecoder(wpi::raw_istream& is, unsigned int proto_rev, // Start with a 1K temporary buffer. Use malloc instead of new so we can // realloc. m_allocated = 1024; - m_buf = static_cast(std::malloc(m_allocated)); + m_buf = static_cast(wpi::CheckedMalloc(m_allocated)); m_proto_rev = proto_rev; m_error = nullptr; } @@ -71,7 +72,7 @@ void WireDecoder::Realloc(size_t len) { if (m_allocated >= len) return; size_t newlen = m_allocated * 2; while (newlen < len) newlen *= 2; - m_buf = static_cast(std::realloc(m_buf, newlen)); + m_buf = static_cast(wpi::CheckedRealloc(m_buf, newlen)); m_allocated = newlen; } diff --git a/ntcore/src/main/native/cpp/ntcore_c.cpp b/ntcore/src/main/native/cpp/ntcore_c.cpp index 09b457a713..16e83d19dc 100644 --- a/ntcore/src/main/native/cpp/ntcore_c.cpp +++ b/ntcore/src/main/native/cpp/ntcore_c.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include "Value_internal.h" @@ -20,7 +21,7 @@ using namespace nt; // Conversion helpers static void ConvertToC(wpi::StringRef in, char** out) { - *out = static_cast(std::malloc(in.size() + 1)); + *out = static_cast(wpi::CheckedMalloc(in.size() + 1)); std::memmove(*out, in.data(), in.size()); (*out)[in.size()] = '\0'; } @@ -57,13 +58,13 @@ static void ConvertToC(const RpcDefinition& in, NT_RpcDefinition* out) { out->num_params = in.params.size(); out->params = static_cast( - std::malloc(in.params.size() * sizeof(NT_RpcParamDef))); + wpi::CheckedMalloc(in.params.size() * sizeof(NT_RpcParamDef))); for (size_t i = 0; i < in.params.size(); ++i) ConvertToC(in.params[i], &out->params[i]); out->num_results = in.results.size(); out->results = static_cast( - std::malloc(in.results.size() * sizeof(NT_RpcResultDef))); + wpi::CheckedMalloc(in.results.size() * sizeof(NT_RpcResultDef))); for (size_t i = 0; i < in.results.size(); ++i) ConvertToC(in.results[i], &out->results[i]); } @@ -107,9 +108,9 @@ static void ConvertToC(const std::vector& in, O** out, size_t* out_len) { *out = nullptr; return; } - *out = static_cast(std::malloc(sizeof(O*) * in.size())); + *out = static_cast(wpi::CheckedMalloc(sizeof(O*) * in.size())); for (size_t i = 0; i < in.size(); ++i) { - out[i] = static_cast(std::malloc(sizeof(O))); + out[i] = static_cast(wpi::CheckedMalloc(sizeof(O))); ConvertToC(in[i], out[i]); } } @@ -191,8 +192,8 @@ NT_Entry* NT_GetEntries(NT_Inst inst, const char* prefix, size_t prefix_len, if (info_v.size() == 0) return nullptr; // create array and copy into it - NT_Entry* info = - static_cast(std::malloc(info_v.size() * sizeof(NT_Entry))); + NT_Entry* info = static_cast( + wpi::CheckedMalloc(info_v.size() * sizeof(NT_Entry))); std::memcpy(info, info_v.data(), info_v.size() * sizeof(NT_Entry)); return info; } @@ -251,7 +252,7 @@ struct NT_EntryInfo* NT_GetEntryInfo(NT_Inst inst, const char* prefix, // create array and copy into it NT_EntryInfo* info = static_cast( - std::malloc(info_v.size() * sizeof(NT_EntryInfo))); + wpi::CheckedMalloc(info_v.size() * sizeof(NT_EntryInfo))); for (size_t i = 0; i < info_v.size(); ++i) ConvertToC(info_v[i], &info[i]); return info; } @@ -541,10 +542,10 @@ NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len, if (values_v.size() == 0) return nullptr; // create array and copy into it - NT_Value** values = - static_cast(std::malloc(values_v.size() * sizeof(NT_Value*))); + NT_Value** values = static_cast( + wpi::CheckedMalloc(values_v.size() * sizeof(NT_Value*))); for (size_t i = 0; i < values_v.size(); ++i) { - values[i] = static_cast(std::malloc(sizeof(NT_Value))); + values[i] = static_cast(wpi::CheckedMalloc(sizeof(NT_Value))); ConvertToC(*values_v[i], values[i]); } return values; @@ -628,7 +629,7 @@ struct NT_ConnectionInfo* NT_GetConnections(NT_Inst inst, size_t* count) { // create array and copy into it NT_ConnectionInfo* conn = static_cast( - std::malloc(conn_v.size() * sizeof(NT_ConnectionInfo))); + wpi::CheckedMalloc(conn_v.size() * sizeof(NT_ConnectionInfo))); for (size_t i = 0; i < conn_v.size(); ++i) ConvertToC(conn_v[i], &conn[i]); return conn; } @@ -836,26 +837,27 @@ void NT_DisposeRpcAnswer(NT_RpcAnswer* call_info) { /* Allocates a char array of the specified size.*/ char* NT_AllocateCharArray(size_t size) { - char* retVal = static_cast(std::malloc(size * sizeof(char))); + char* retVal = static_cast(wpi::CheckedMalloc(size * sizeof(char))); return retVal; } /* Allocates an integer or boolean array of the specified size. */ int* NT_AllocateBooleanArray(size_t size) { - int* retVal = static_cast(std::malloc(size * sizeof(int))); + int* retVal = static_cast(wpi::CheckedMalloc(size * sizeof(int))); return retVal; } /* Allocates a double array of the specified size. */ double* NT_AllocateDoubleArray(size_t size) { - double* retVal = static_cast(std::malloc(size * sizeof(double))); + double* retVal = + static_cast(wpi::CheckedMalloc(size * sizeof(double))); return retVal; } /* Allocates an NT_String array of the specified size. */ struct NT_String* NT_AllocateStringArray(size_t size) { NT_String* retVal = - static_cast(std::malloc(size * sizeof(NT_String))); + static_cast(wpi::CheckedMalloc(size * sizeof(NT_String))); return retVal; } @@ -976,7 +978,8 @@ char* NT_GetValueString(const struct NT_Value* value, uint64_t* last_change, if (!value || value->type != NT_Type::NT_STRING) return nullptr; *last_change = value->last_change; *str_len = value->data.v_string.len; - char* str = static_cast(std::malloc(value->data.v_string.len + 1)); + char* str = + static_cast(wpi::CheckedMalloc(value->data.v_string.len + 1)); std::memcpy(str, value->data.v_string.str, value->data.v_string.len + 1); return str; } @@ -986,7 +989,8 @@ char* NT_GetValueRaw(const struct NT_Value* value, uint64_t* last_change, if (!value || value->type != NT_Type::NT_RAW) return nullptr; *last_change = value->last_change; *raw_len = value->data.v_string.len; - char* raw = static_cast(std::malloc(value->data.v_string.len + 1)); + char* raw = + static_cast(wpi::CheckedMalloc(value->data.v_string.len + 1)); std::memcpy(raw, value->data.v_string.str, value->data.v_string.len + 1); return raw; } @@ -997,7 +1001,7 @@ NT_Bool* NT_GetValueBooleanArray(const struct NT_Value* value, *last_change = value->last_change; *arr_size = value->data.arr_boolean.size; NT_Bool* arr = static_cast( - std::malloc(value->data.arr_boolean.size * sizeof(NT_Bool))); + wpi::CheckedMalloc(value->data.arr_boolean.size * sizeof(NT_Bool))); std::memcpy(arr, value->data.arr_boolean.arr, value->data.arr_boolean.size * sizeof(NT_Bool)); return arr; @@ -1009,7 +1013,7 @@ double* NT_GetValueDoubleArray(const struct NT_Value* value, *last_change = value->last_change; *arr_size = value->data.arr_double.size; double* arr = static_cast( - std::malloc(value->data.arr_double.size * sizeof(double))); + wpi::CheckedMalloc(value->data.arr_double.size * sizeof(double))); std::memcpy(arr, value->data.arr_double.arr, value->data.arr_double.size * sizeof(double)); return arr; @@ -1021,11 +1025,11 @@ NT_String* NT_GetValueStringArray(const struct NT_Value* value, *last_change = value->last_change; *arr_size = value->data.arr_string.size; NT_String* arr = static_cast( - std::malloc(value->data.arr_string.size * sizeof(NT_String))); + wpi::CheckedMalloc(value->data.arr_string.size * sizeof(NT_String))); for (size_t i = 0; i < value->data.arr_string.size; ++i) { size_t len = value->data.arr_string.arr[i].len; arr[i].len = len; - arr[i].str = static_cast(std::malloc(len + 1)); + arr[i].str = static_cast(wpi::CheckedMalloc(len + 1)); std::memcpy(arr[i].str, value->data.arr_string.arr[i].str, len + 1); } return arr; @@ -1129,7 +1133,8 @@ NT_Bool* NT_GetEntryBooleanArray(NT_Entry entry, uint64_t* last_change, if (!v || !v->IsBooleanArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetBooleanArray(); - NT_Bool* arr = static_cast(std::malloc(vArr.size() * sizeof(NT_Bool))); + NT_Bool* arr = + static_cast(wpi::CheckedMalloc(vArr.size() * sizeof(NT_Bool))); *arr_size = vArr.size(); std::copy(vArr.begin(), vArr.end(), arr); return arr; @@ -1141,7 +1146,8 @@ double* NT_GetEntryDoubleArray(NT_Entry entry, uint64_t* last_change, if (!v || !v->IsDoubleArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetDoubleArray(); - double* arr = static_cast(std::malloc(vArr.size() * sizeof(double))); + double* arr = + static_cast(wpi::CheckedMalloc(vArr.size() * sizeof(double))); *arr_size = vArr.size(); std::copy(vArr.begin(), vArr.end(), arr); return arr; @@ -1153,8 +1159,8 @@ NT_String* NT_GetEntryStringArray(NT_Entry entry, uint64_t* last_change, if (!v || !v->IsStringArray()) return nullptr; *last_change = v->last_change(); auto vArr = v->GetStringArray(); - NT_String* arr = - static_cast(std::malloc(vArr.size() * sizeof(NT_String))); + NT_String* arr = static_cast( + wpi::CheckedMalloc(vArr.size() * sizeof(NT_String))); for (size_t i = 0; i < vArr.size(); ++i) { ConvertToC(vArr[i], &arr[i]); } diff --git a/ntcore/src/main/native/cpp/ntcore_test.cpp b/ntcore/src/main/native/cpp/ntcore_test.cpp index 26ec755161..982b9f9d80 100644 --- a/ntcore/src/main/native/cpp/ntcore_test.cpp +++ b/ntcore/src/main/native/cpp/ntcore_test.cpp @@ -7,12 +7,14 @@ #include "ntcore_test.h" +#include + #include "Value_internal.h" extern "C" { struct NT_String* NT_GetStringForTesting(const char* string, int* struct_size) { struct NT_String* str = - static_cast(std::calloc(1, sizeof(NT_String))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_String))); nt::ConvertToC(wpi::StringRef(string), str); *struct_size = sizeof(NT_String); return str; @@ -24,7 +26,7 @@ struct NT_EntryInfo* NT_GetEntryInfoForTesting(const char* name, uint64_t last_change, int* struct_size) { struct NT_EntryInfo* entry_info = - static_cast(std::calloc(1, sizeof(NT_EntryInfo))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_EntryInfo))); nt::ConvertToC(wpi::StringRef(name), &entry_info->name); entry_info->type = type; entry_info->flags = flags; @@ -42,7 +44,7 @@ struct NT_ConnectionInfo* NT_GetConnectionInfoForTesting( const char* remote_id, const char* remote_ip, unsigned int remote_port, uint64_t last_update, unsigned int protocol_version, int* struct_size) { struct NT_ConnectionInfo* conn_info = static_cast( - std::calloc(1, sizeof(NT_ConnectionInfo))); + wpi::CheckedCalloc(1, sizeof(NT_ConnectionInfo))); nt::ConvertToC(wpi::StringRef(remote_id), &conn_info->remote_id); nt::ConvertToC(wpi::StringRef(remote_ip), &conn_info->remote_ip); conn_info->remote_port = remote_port; @@ -61,7 +63,7 @@ void NT_FreeConnectionInfoForTesting(struct NT_ConnectionInfo* info) { struct NT_Value* NT_GetValueBooleanForTesting(uint64_t last_change, int val, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.v_boolean = val; @@ -72,7 +74,7 @@ struct NT_Value* NT_GetValueBooleanForTesting(uint64_t last_change, int val, struct NT_Value* NT_GetValueDoubleForTesting(uint64_t last_change, double val, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_DOUBLE; value->last_change = last_change; value->data.v_double = val; @@ -84,7 +86,7 @@ struct NT_Value* NT_GetValueStringForTesting(uint64_t last_change, const char* str, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_STRING; value->last_change = last_change; nt::ConvertToC(wpi::StringRef(str), &value->data.v_string); @@ -95,7 +97,7 @@ struct NT_Value* NT_GetValueStringForTesting(uint64_t last_change, struct NT_Value* NT_GetValueRawForTesting(uint64_t last_change, const char* raw, int raw_len, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_RAW; value->last_change = last_change; nt::ConvertToC(wpi::StringRef(raw, raw_len), &value->data.v_string); @@ -108,7 +110,7 @@ struct NT_Value* NT_GetValueBooleanArrayForTesting(uint64_t last_change, size_t array_len, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN_ARRAY; value->last_change = last_change; value->data.arr_boolean.arr = NT_AllocateBooleanArray(array_len); @@ -124,7 +126,7 @@ struct NT_Value* NT_GetValueDoubleArrayForTesting(uint64_t last_change, size_t array_len, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.arr_double.arr = NT_AllocateDoubleArray(array_len); @@ -140,7 +142,7 @@ struct NT_Value* NT_GetValueStringArrayForTesting(uint64_t last_change, size_t array_len, int* struct_size) { struct NT_Value* value = - static_cast(std::calloc(1, sizeof(NT_Value))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_Value))); value->type = NT_BOOLEAN; value->last_change = last_change; value->data.arr_string.arr = NT_AllocateStringArray(array_len); @@ -149,7 +151,7 @@ struct NT_Value* NT_GetValueStringArrayForTesting(uint64_t last_change, size_t len = arr[i].len; value->data.arr_string.arr[i].len = len; value->data.arr_string.arr[i].str = - static_cast(std::malloc(len + 1)); + static_cast(wpi::CheckedMalloc(len + 1)); std::memcpy(value->data.arr_string.arr[i].str, arr[i].str, len + 1); } *struct_size = sizeof(NT_Value); @@ -171,8 +173,8 @@ static void CopyNtString(const struct NT_String* copy_from, struct NT_RpcParamDef* NT_GetRpcParamDefForTesting(const char* name, const struct NT_Value* val, int* struct_size) { - struct NT_RpcParamDef* def = - static_cast(std::calloc(1, sizeof(NT_RpcParamDef))); + struct NT_RpcParamDef* def = static_cast( + wpi::CheckedCalloc(1, sizeof(NT_RpcParamDef))); nt::ConvertToC(wpi::StringRef(name), &def->name); CopyNtValue(val, &def->def_value); *struct_size = sizeof(NT_RpcParamDef); @@ -188,8 +190,8 @@ void NT_FreeRpcParamDefForTesting(struct NT_RpcParamDef* def) { struct NT_RpcResultDef* NT_GetRpcResultsDefForTesting(const char* name, enum NT_Type type, int* struct_size) { - struct NT_RpcResultDef* def = - static_cast(std::calloc(1, sizeof(NT_RpcResultDef))); + struct NT_RpcResultDef* def = static_cast( + wpi::CheckedCalloc(1, sizeof(NT_RpcResultDef))); nt::ConvertToC(wpi::StringRef(name), &def->name); def->type = type; *struct_size = sizeof(NT_RpcResultDef); @@ -205,20 +207,20 @@ struct NT_RpcDefinition* NT_GetRpcDefinitionForTesting( unsigned int version, const char* name, size_t num_params, const struct NT_RpcParamDef* params, size_t num_results, const struct NT_RpcResultDef* results, int* struct_size) { - struct NT_RpcDefinition* def = - static_cast(std::calloc(1, sizeof(NT_RpcDefinition))); + struct NT_RpcDefinition* def = static_cast( + wpi::CheckedCalloc(1, sizeof(NT_RpcDefinition))); def->version = version; nt::ConvertToC(wpi::StringRef(name), &def->name); def->num_params = num_params; def->params = static_cast( - std::malloc(num_params * sizeof(NT_RpcParamDef))); + wpi::CheckedMalloc(num_params * sizeof(NT_RpcParamDef))); for (size_t i = 0; i < num_params; ++i) { CopyNtString(¶ms[i].name, &def->params[i].name); CopyNtValue(¶ms[i].def_value, &def->params[i].def_value); } def->num_results = num_results; def->results = static_cast( - std::malloc(num_results * sizeof(NT_RpcResultDef))); + wpi::CheckedMalloc(num_results * sizeof(NT_RpcResultDef))); for (size_t i = 0; i < num_results; ++i) { CopyNtString(&results[i].name, &def->results[i].name); def->results[i].type = results[i].type; @@ -232,7 +234,7 @@ struct NT_RpcAnswer* NT_GetRpcAnswerForTesting( unsigned int rpc_id, unsigned int call_uid, const char* name, const char* params, size_t params_len, int* struct_size) { struct NT_RpcAnswer* info = - static_cast(std::calloc(1, sizeof(NT_RpcAnswer))); + static_cast(wpi::CheckedCalloc(1, sizeof(NT_RpcAnswer))); info->entry = rpc_id; info->call = call_uid; nt::ConvertToC(wpi::StringRef(name), &info->name); diff --git a/wpiutil/src/main/native/include/wpi/memory.h b/wpiutil/src/main/native/include/wpi/memory.h new file mode 100644 index 0000000000..de7e933dcf --- /dev/null +++ b/wpiutil/src/main/native/include/wpi/memory.h @@ -0,0 +1,64 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2018 FIRST. All Rights Reserved. */ +/* Open Source Software - may be modified and shared by FRC teams. The code */ +/* must be accompanied by the FIRST BSD license file in the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#ifndef WPIUTIL_SUPPORT_MEMORY_H_ +#define WPIUTIL_SUPPORT_MEMORY_H_ + +#include +#include + +#include "wpi/raw_ostream.h" + +namespace wpi { + +/** + * Wrapper around std::calloc that calls std::terminate on out of memory. + * @param num number of objects to allocate + * @param size number of bytes per object to allocate + * @return Pointer to beginning of newly allocated memory. + */ +inline void* CheckedCalloc(size_t num, size_t size) { + void* p = std::calloc(num, size); + if (!p) { + errs() << "FATAL: failed to allocate " << (num * size) << " bytes\n"; + std::terminate(); + } + return p; +} + +/** + * Wrapper around std::malloc that calls std::terminate on out of memory. + * @param size number of bytes to allocate + * @return Pointer to beginning of newly allocated memory. + */ +inline void* CheckedMalloc(size_t size) { + void* p = std::malloc(size == 0 ? 1 : size); + if (!p) { + errs() << "FATAL: failed to allocate " << size << " bytes\n"; + std::terminate(); + } + return p; +} + +/** + * Wrapper around std::realloc that calls std::terminate on out of memory. + * @param ptr memory previously allocated + * @param size number of bytes to allocate + * @return Pointer to beginning of newly allocated memory. + */ +inline void* CheckedRealloc(void* ptr, size_t size) { + void* p = std::realloc(ptr, size == 0 ? 1 : size); + if (!p) { + errs() << "FATAL: failed to allocate " << size << " bytes\n"; + std::terminate(); + } + return p; +} + +} // namespace wpi + +#endif // WPIUTIL_SUPPORT_MEMORY_H_