diff --git a/tensorflow_lite_support/ios/task/audio/BUILD b/tensorflow_lite_support/ios/task/audio/BUILD index d7fb2b37d..6baee229e 100644 --- a/tensorflow_lite_support/ios/task/audio/BUILD +++ b/tensorflow_lite_support/ios/task/audio/BUILD @@ -15,7 +15,7 @@ objc_library( module_name = "TFLAudioClassifier", deps = [ "//tensorflow_lite_support/c/task/audio:audio_classifier", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils", "//tensorflow_lite_support/ios/task/audio/core/audio_tensor:TFLAudioTensor", "//tensorflow_lite_support/ios/task/audio/core/audio_tensor/utils:TFLAudioTensorUtils", "//tensorflow_lite_support/ios/task/core:TFLBaseOptions", diff --git a/tensorflow_lite_support/ios/task/audio/core/BUILD b/tensorflow_lite_support/ios/task/audio/core/BUILD index 6147d1f0a..3f349ac95 100644 --- a/tensorflow_lite_support/ios/task/audio/core/BUILD +++ b/tensorflow_lite_support/ios/task/audio/core/BUILD @@ -26,7 +26,7 @@ objc_library( deps = [ ":TFLFloatBuffer", "//tensorflow_lite_support/ios:TFLCommon", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", ], ) diff --git a/tensorflow_lite_support/ios/task/audio/core/audio_record/BUILD b/tensorflow_lite_support/ios/task/audio/core/audio_record/BUILD index cd8103395..2d062c0ce 100644 --- a/tensorflow_lite_support/ios/task/audio/core/audio_record/BUILD +++ b/tensorflow_lite_support/ios/task/audio/core/audio_record/BUILD @@ -14,7 +14,7 @@ objc_library( module_name = "TFLAudioRecord", sdk_frameworks = ["AVFoundation"], deps = [ - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", "//tensorflow_lite_support/ios/task/audio/core:TFLAudioFormat", "//tensorflow_lite_support/ios/task/audio/core:TFLFloatBuffer", "//tensorflow_lite_support/ios/task/audio/core:TFLRingBuffer", diff --git a/tensorflow_lite_support/ios/task/audio/core/audio_tensor/BUILD b/tensorflow_lite_support/ios/task/audio/core/audio_tensor/BUILD index 1b882ba30..f06e2ad66 100644 --- a/tensorflow_lite_support/ios/task/audio/core/audio_tensor/BUILD +++ b/tensorflow_lite_support/ios/task/audio/core/audio_tensor/BUILD @@ -15,7 +15,7 @@ objc_library( sdk_frameworks = ["AVFoundation"], deps = [ "//tensorflow_lite_support/ios:TFLCommon", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", "//tensorflow_lite_support/ios/task/audio/core:TFLFloatBuffer", "//tensorflow_lite_support/ios/task/audio/core:TFLRingBuffer", "//tensorflow_lite_support/ios/task/audio/core/audio_record:TFLAudioRecord", diff --git a/tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.m b/tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.m index 2e2e1f691..d7545f21c 100644 --- a/tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.m +++ b/tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.m @@ -14,7 +14,7 @@ ==============================================================================*/ #import "tensorflow_lite_support/ios/task/audio/sources/TFLAudioClassifier.h" #import "tensorflow_lite_support/ios/sources/TFLCommon.h" -#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h" +#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h" #import "tensorflow_lite_support/ios/task/audio/core/audio_tensor/utils/sources/TFLAudioTensor+Utils.h" #import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h" @@ -66,7 +66,7 @@ - (BOOL)populateRequiredBufferSizeWithError:(NSError **)error { TfLiteAudioClassifierGetRequiredInputBufferSize(_audioClassifier, &requiredBufferSizeError); // Populate iOS error if C Error is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:requiredBufferSizeError toError:error]) { + if (![TFLCommonCUtils checkCError:requiredBufferSizeError toError:error]) { TfLiteSupportErrorDelete(requiredBufferSizeError); } @@ -78,7 +78,7 @@ - (BOOL)populateRequiredAudioFormatWithError:(NSError **)error { _requiredCAudioFormat = TfLiteAudioClassifierGetRequiredAudioFormat(_audioClassifier, &getAudioFormatError); - if (![TFLCommonUtils checkCError:getAudioFormatError toError:error]) { + if (![TFLCommonCUtils checkCError:getAudioFormatError toError:error]) { TfLiteSupportErrorDelete(getAudioFormatError); } @@ -129,7 +129,7 @@ + (nullable instancetype)audioClassifierWithOptions:(TFLAudioClassifierOptions * deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)]; // Populate iOS error if TfliteSupportError is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:cCreateClassifierError toError:error]) { + if (![TFLCommonCUtils checkCError:cCreateClassifierError toError:error]) { TfLiteSupportErrorDelete(cCreateClassifierError); } @@ -159,7 +159,7 @@ - (nullable TFLClassificationResult *)classifyWithAudioTensor:(TFLAudioTensor *) TfLiteAudioClassifierClassify(_audioClassifier, &cAudioBuffer, &classifyError); // Populate iOS error if C Error is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:classifyError toError:error]) { + if (![TFLCommonCUtils checkCError:classifyError toError:error]) { TfLiteSupportErrorDelete(classifyError); } diff --git a/tensorflow_lite_support/ios/task/processor/BUILD b/tensorflow_lite_support/ios/task/processor/BUILD index 146c04923..fde3ea21d 100644 --- a/tensorflow_lite_support/ios/task/processor/BUILD +++ b/tensorflow_lite_support/ios/task/processor/BUILD @@ -63,7 +63,7 @@ objc_library( ], module_name = "TFLSegmentationResult", deps = [ - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", ], ) @@ -79,7 +79,7 @@ objc_library( deps = [ "//tensorflow_lite_support/c/task/processor:classification_options", "//tensorflow_lite_support/ios:TFLCommon", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", "//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions", ], ) diff --git a/tensorflow_lite_support/ios/task/vision/BUILD b/tensorflow_lite_support/ios/task/vision/BUILD index 0a756dc56..ed7091ff1 100644 --- a/tensorflow_lite_support/ios/task/vision/BUILD +++ b/tensorflow_lite_support/ios/task/vision/BUILD @@ -15,7 +15,7 @@ objc_library( module_name = "TFLImageClassifier", deps = [ "//tensorflow_lite_support/c/task/vision:image_classifier", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils", "//tensorflow_lite_support/ios/task/core:TFLBaseOptions", "//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers", "//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions", @@ -39,7 +39,7 @@ objc_library( module_name = "TFLObjectDetector", deps = [ "//tensorflow_lite_support/c/task/vision:object_detector", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils", "//tensorflow_lite_support/ios/task/core:TFLBaseOptions", "//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers", "//tensorflow_lite_support/ios/task/processor:TFLClassificationOptions", @@ -63,7 +63,7 @@ objc_library( module_name = "TFLImageSegmenter", deps = [ "//tensorflow_lite_support/c/task/vision:image_segmenter", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils/c:TFLCommonCUtils", "//tensorflow_lite_support/ios/task/core:TFLBaseOptions", "//tensorflow_lite_support/ios/task/core:TFLBaseOptionsHelpers", "//tensorflow_lite_support/ios/task/processor:TFLSegmentationResult", diff --git a/tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.m b/tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.m index f8c09527b..aec39a267 100644 --- a/tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.m +++ b/tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.m @@ -14,7 +14,7 @@ ==============================================================================*/ #import "tensorflow_lite_support/ios/task/vision/sources/TFLImageClassifier.h" #import "tensorflow_lite_support/ios/sources/TFLCommon.h" -#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h" +#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h" #import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationResult+Helpers.h" @@ -91,7 +91,7 @@ + (nullable instancetype)imageClassifierWithOptions:(TFLImageClassifierOptions * deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)]; // Populate iOS error if TfliteSupportError is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:cCreateClassifierError toError:error]) { + if (![TFLCommonCUtils checkCError:cCreateClassifierError toError:error]) { TfLiteSupportErrorDelete(cCreateClassifierError); } @@ -143,7 +143,7 @@ - (nullable TFLClassificationResult *)classifyWithGMLImage:(GMLImage *)image cFrameBuffer = NULL; // Populate iOS error if C Error is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:classifyError toError:error]) { + if (![TFLCommonCUtils checkCError:classifyError toError:error]) { TfLiteSupportErrorDelete(classifyError); } diff --git a/tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.m b/tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.m index 70068bfdd..0445face1 100644 --- a/tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.m +++ b/tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.m @@ -14,7 +14,7 @@ ==============================================================================*/ #import "tensorflow_lite_support/ios/task/vision/sources/TFLImageSegmenter.h" #import "tensorflow_lite_support/ios/sources/TFLCommon.h" -#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h" +#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h" #import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLSegmentationResult+Helpers.h" #import "tensorflow_lite_support/ios/task/vision/utils/sources/GMLImage+Utils.h" @@ -90,7 +90,7 @@ + (nullable instancetype)imageSegmenterWithOptions:(nonnull TFLImageSegmenterOpt // Freeing memory of allocated string. free(cOptions.display_names_locale); - if (![TFLCommonUtils checkCError:cCreateImageSegmenterError toError:error]) { + if (![TFLCommonCUtils checkCError:cCreateImageSegmenterError toError:error]) { TfLiteSupportErrorDelete(cCreateImageSegmenterError); } @@ -128,7 +128,7 @@ - (nullable TFLSegmentationResult *)segmentWithGMLImage:(GMLImage *)image cFrameBuffer = nil; // Populate iOS error if C Error is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:cSegmentError toError:error]) { + if (![TFLCommonCUtils checkCError:cSegmentError toError:error]) { TfLiteSupportErrorDelete(cSegmentError); } diff --git a/tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.m b/tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.m index 31cb241a2..2b1038d42 100644 --- a/tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.m +++ b/tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.m @@ -14,7 +14,7 @@ ==============================================================================*/ #import "tensorflow_lite_support/ios/task/vision/sources/TFLObjectDetector.h" #import "tensorflow_lite_support/ios/sources/TFLCommon.h" -#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h" +#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h" #import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLClassificationOptions+Helpers.h" #import "tensorflow_lite_support/ios/task/processor/sources/TFLDetectionResult+Helpers.h" @@ -91,7 +91,7 @@ + (nullable instancetype)objectDetectorWithOptions:(TFLObjectDetectorOptions *)o deleteAllocatedMemoryOfClassificationOptions:&(cOptions.classification_options)]; // Populate iOS error if TfliteSupportError is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:cCreateObjectDetectorError toError:error]) { + if (![TFLCommonCUtils checkCError:cCreateObjectDetectorError toError:error]) { TfLiteSupportErrorDelete(cCreateObjectDetectorError); } @@ -130,7 +130,7 @@ - (nullable TFLDetectionResult *)detectWithGMLImage:(GMLImage *)image cFrameBuffer = nil; // Populate iOS error if C Error is not null and afterwards delete it. - if (![TFLCommonUtils checkCError:cDetectError toError:error]) { + if (![TFLCommonCUtils checkCError:cDetectError toError:error]) { TfLiteSupportErrorDelete(cDetectError); } diff --git a/tensorflow_lite_support/ios/task/vision/utils/BUILD b/tensorflow_lite_support/ios/task/vision/utils/BUILD index 00e35c6c1..6d45e0b58 100644 --- a/tensorflow_lite_support/ios/task/vision/utils/BUILD +++ b/tensorflow_lite_support/ios/task/vision/utils/BUILD @@ -16,7 +16,7 @@ objc_library( sdk_frameworks = ["Accelerate"], deps = [ "//tensorflow_lite_support/c/task/vision/core:frame_buffer", - "//tensorflow_lite_support/ios:TFLCommonUtils", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", "//tensorflow_lite_support/odml/ios/image:MLImage", ], ) diff --git a/tensorflow_lite_support/ios/utils/BUILD b/tensorflow_lite_support/ios/utils/BUILD index 63f109154..7b0638c6d 100644 --- a/tensorflow_lite_support/ios/utils/BUILD +++ b/tensorflow_lite_support/ios/utils/BUILD @@ -6,10 +6,24 @@ package( objc_library( name = "TFLStringUtil", srcs = [ - "Sources/TFLStringUtil.mm", + "sources/TFLStringUtil.mm", ], hdrs = [ - "Sources/TFLStringUtil.h", + "sources/TFLStringUtil.h", ], module_name = "TFLStringUtil", ) + +objc_library( + name = "TFLCommonUtils", + srcs = [ + "sources/TFLCommonUtils.m", + ], + hdrs = [ + "sources/TFLCommonUtils.h", + ], + module_name = "TFLCommonUtils", + deps = [ + "//tensorflow_lite_support/ios:TFLCommon", + ], +) diff --git a/tensorflow_lite_support/ios/utils/c/BUILD b/tensorflow_lite_support/ios/utils/c/BUILD new file mode 100644 index 000000000..a3f8ad1ff --- /dev/null +++ b/tensorflow_lite_support/ios/utils/c/BUILD @@ -0,0 +1,19 @@ +package( + default_visibility = ["//tensorflow_lite_support:internal"], + licenses = ["notice"], # Apache 2.0 +) + +objc_library( + name = "TFLCommonCUtils", + srcs = [ + "sources/TFLCommonCUtils.m", + ], + hdrs = [ + "sources/TFLCommonCUtils.h", + ], + module_name = "TFLCommonCUtils", + deps = [ + "//tensorflow_lite_support/c:common", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", + ], +) diff --git a/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h b/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h new file mode 100644 index 000000000..e4b853819 --- /dev/null +++ b/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h @@ -0,0 +1,37 @@ +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#import +#import "tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h" + +#include "tensorflow_lite_support/c/common.h" + +NS_ASSUME_NONNULL_BEGIN + +/** Helper utility for the all tasks which encapsulates common functionality of the iOS task library + * backed by C APIs. */ +@interface TFLCommonCUtils : TFLCommonUtils + +/** + * Converts a C library error, TfLiteSupportError to an NSError. + * + * @param supportError C library error. + * @param error Pointer to the memory location where the created error should be saved. If `nil`, + * no error will be saved. + */ ++ (BOOL)checkCError:(TfLiteSupportError *)supportError toError:(NSError **)error; + +@end + +NS_ASSUME_NONNULL_END diff --git a/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.m b/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.m new file mode 100644 index 000000000..1bf017157 --- /dev/null +++ b/tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.m @@ -0,0 +1,29 @@ +// Copyright 2021 The TensorFlow Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "tensorflow_lite_support/ios/utils/c/sources/TFLCommonCUtils.h" + +@implementation TFLCommonCUtils + ++ (BOOL)checkCError:(TfLiteSupportError *)supportError toError:(NSError **)error { + if (!supportError) { + return YES; + } + NSString *description = [NSString stringWithCString:supportError->message + encoding:NSUTF8StringEncoding]; + [TFLCommonUtils createCustomError:error withCode:supportError->code description:description]; + return NO; +} + +@end diff --git a/tensorflow_lite_support/ios/utils/cpp/BUILD b/tensorflow_lite_support/ios/utils/cpp/BUILD new file mode 100644 index 000000000..2f1c65eaf --- /dev/null +++ b/tensorflow_lite_support/ios/utils/cpp/BUILD @@ -0,0 +1,17 @@ +package( + default_visibility = ["//tensorflow_lite_support:internal"], + licenses = ["notice"], # Apache 2.0 +) + +objc_library( + name = "TFLCommonCppUtils", + hdrs = [ + "sources/TFLCommonCppUtils.h", + ], + module_name = "TFLCommonCppUtils", + deps = [ + "//tensorflow_lite_support/cc:common", + "@com_google_absl//absl/status", + "//tensorflow_lite_support/ios/utils:TFLCommonUtils", + ], +) diff --git a/tensorflow_lite_support/ios/utils/cpp/sources/TFLCommonCppUtils.h b/tensorflow_lite_support/ios/utils/cpp/sources/TFLCommonCppUtils.h new file mode 100644 index 000000000..08fd2bfe6 --- /dev/null +++ b/tensorflow_lite_support/ios/utils/cpp/sources/TFLCommonCppUtils.h @@ -0,0 +1,37 @@ +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#import +#import "tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h" + +#include "absl/status/status.h" + +NS_ASSUME_NONNULL_BEGIN + +/** Helper utility for the all tasks which encapsulates common functionality of the iOS task library + * backed by C++ APIs. */ +@interface TFLCommonCppUtils : TFLCommonUtils + +/** + * Converts an absl status to an NSError. + * + * @param status absl status. + * @param error Pointer to the memory location where the created error should be saved. If `nil`, + * no error will be saved. + */ ++ (BOOL)checkCppError:(const absl::Status &)status toError:(NSError **)error; + +@end + +NS_ASSUME_NONNULL_END diff --git a/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h b/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h new file mode 100644 index 000000000..8f52fb7ad --- /dev/null +++ b/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h @@ -0,0 +1,68 @@ +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#import + +NS_ASSUME_NONNULL_BEGIN + +/** Error domain of TensorFlow Lite Support related errors. */ +static NSString *const TFLSupportTaskErrorDomain = @"org.tensorflow.lite.tasks"; + +/** Helper utility for the all tasks which encapsulates common functionality. */ +@interface TFLCommonUtils : NSObject + +/** + * Creates and saves an NSError in the Tensorflow Lite Task Library domain, with the given code and + * description. + * + * @param code Error code. + * @param description Error description. + * @param error Pointer to the memory location where the created error should be saved. If `nil`, + * no error will be saved. + */ ++ (void)createCustomError:(NSError **)error + withCode:(NSUInteger)code + description:(NSString *)description; + +/** + * Creates and saves an NSError with the given domain, code and description. + * + * @param error Pointer to the memory location where the created error should be saved. If `nil`, + * no error will be saved. + * @param domain Error domain. + * @param code Error code. + * @param description Error description. + */ ++ (void)createCustomError:(NSError **)error + withDomain:(NSString *)domain + code:(NSUInteger)code + description:(NSString *)description; + +/** + * Allocates a block of memory with the specified size and returns a pointer to it. If memory + * cannot be allocated because of an invalid memSize, it saves an error. In other cases, it + * terminates program execution. + * + * @param memSize size of memory to be allocated + * @param error Pointer to the memory location where errors if any should be saved. If `nil`, no + * error will be saved. + * + * @return Pointer to the allocated block of memory on successfull allocation. nil in case as + * error is encountered because of invalid memSize. If failure is due to any other reason, method + * terminates program execution. + */ ++ (void *)mallocWithSize:(size_t)memSize error:(NSError **)error; +@end + +NS_ASSUME_NONNULL_END diff --git a/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.m b/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.m new file mode 100644 index 000000000..1640b4341 --- /dev/null +++ b/tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.m @@ -0,0 +1,56 @@ +// Copyright 2021 The TensorFlow Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "tensorflow_lite_support/ios/utils/sources/TFLCommonUtils.h" +#import "tensorflow_lite_support/ios/sources/TFLCommon.h" + +@implementation TFLCommonUtils + ++ (void)createCustomError:(NSError **)error + withCode:(NSUInteger)code + description:(NSString *)description { + [TFLCommonUtils createCustomError:error + withDomain:TFLSupportTaskErrorDomain + code:code + description:description]; +} + ++ (void)createCustomError:(NSError **)error + withDomain:(NSString *)domain + code:(NSUInteger)code + description:(NSString *)description { + if (error) { + *error = [NSError errorWithDomain:domain + code:code + userInfo:@{NSLocalizedDescriptionKey : description}]; + } +} + ++ (void *)mallocWithSize:(size_t)memSize error:(NSError **)error { + if (!memSize) { + [TFLCommonUtils createCustomError:error + withCode:TFLSupportErrorCodeInvalidArgumentError + description:@"memSize cannot be zero."]; + return NULL; + } + + void *allocedMemory = malloc(memSize); + if (!allocedMemory) { + exit(-1); + } + + return allocedMemory; +} + +@end