1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/contrib/lite/context.h"
17 #include <stdio.h>
18 #include <string.h>
19
TfLiteIntArrayCreate(int size)20 TfLiteIntArray* TfLiteIntArrayCreate(int size) {
21 TfLiteIntArray* ret =
22 (TfLiteIntArray*)malloc(sizeof(*ret) + sizeof(ret->data[0]) * size);
23 ret->size = size;
24 return ret;
25 }
26
TfLiteIntArrayPrint(const char * s,TfLiteIntArray * a)27 void TfLiteIntArrayPrint(const char* s, TfLiteIntArray* a) {
28 printf("%s: length=%d [", s, a->size);
29 if (a->size) printf("%d", a->data[0]);
30 int i = 1;
31 for (; i < a->size; i++) {
32 printf(" %d", a->data[i]);
33 }
34 printf("]\n");
35 }
36
TfLiteIntArrayEqual(TfLiteIntArray * a,TfLiteIntArray * b)37 int TfLiteIntArrayEqual(TfLiteIntArray* a, TfLiteIntArray* b) {
38 if (a == b) return 1;
39 if (a == NULL || b == NULL) return 0;
40 if (a->size != b->size) return 0;
41 int i = 0;
42 for (; i < a->size; i++)
43 if (a->data[i] != b->data[i]) return 0;
44 return 1;
45 }
46
TfLiteIntArrayCopy(TfLiteIntArray * src)47 TfLiteIntArray* TfLiteIntArrayCopy(TfLiteIntArray* src) {
48 if (!src) return NULL;
49 TfLiteIntArray* ret = TfLiteIntArrayCreate(src->size);
50 if (ret) {
51 memcpy(ret->data, src->data, src->size * sizeof(int));
52 }
53 return ret;
54 }
55
TfLiteIntArrayFree(TfLiteIntArray * a)56 void TfLiteIntArrayFree(TfLiteIntArray* a) { free(a); }
57
TfLiteTensorFree(TfLiteTensor * t)58 void TfLiteTensorFree(TfLiteTensor* t) {
59 if (t->allocation_type == kTfLiteDynamic && t->data.raw) {
60 free(t->data.raw);
61 }
62 if (t->dims) TfLiteIntArrayFree(t->dims);
63 t->data.raw = NULL;
64 t->dims = NULL;
65 }
66
TfLiteTensorReset(TfLiteType type,const char * name,TfLiteIntArray * dims,TfLiteQuantizationParams quantization,char * buffer,size_t size,TfLiteAllocationType allocation_type,const void * allocation,TfLiteTensor * tensor)67 void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims,
68 TfLiteQuantizationParams quantization, char* buffer,
69 size_t size, TfLiteAllocationType allocation_type,
70 const void* allocation, TfLiteTensor* tensor) {
71 TfLiteTensorFree(tensor);
72 tensor->type = type;
73 tensor->name = name;
74 tensor->dims = dims;
75 tensor->params = quantization;
76 tensor->data.raw = buffer;
77 tensor->bytes = size;
78 tensor->allocation_type = allocation_type;
79 tensor->allocation = allocation;
80 }
81
TfLiteTensorRealloc(size_t num_bytes,TfLiteTensor * tensor)82 void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
83 if (tensor->allocation_type != kTfLiteDynamic) {
84 return;
85 }
86 if (!tensor->data.raw) {
87 tensor->data.raw = malloc(num_bytes);
88 } else if (num_bytes > tensor->bytes) {
89 tensor->data.raw = realloc(tensor->data.raw, num_bytes);
90 }
91 tensor->bytes = num_bytes;
92 }
93