Skip to content

Commit c254ddd

Browse files
fix: use static buffer for printing while we correct a leak
1 parent 52a746c commit c254ddd

File tree

2 files changed

+15
-62
lines changed

2 files changed

+15
-62
lines changed

src/debug.c

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <php.h>
22
#include "Zend/zend_alloc.h"
3+
#include "Zend/zend_API.h"
34
#include "debug.h"
45
#include "../config.h"
56
#include "ndarray.h"
@@ -44,28 +45,6 @@ NDArray_Dump(NDArray* array) {
4445
printf("\n=================================================\n");
4546
}
4647

47-
char*
48-
expand_str(char* str, unsigned int additional_size) {
49-
if (str == NULL) {
50-
return (char*)emalloc(additional_size * sizeof(char));
51-
}
52-
//reallocate memory for the string to accommodate extra characters
53-
char *new_str_pointer = (char*)erealloc(str, (strlen(str) + additional_size) * sizeof(char));
54-
return new_str_pointer;
55-
}
56-
57-
int string_size_of_float(float number) {
58-
char buf[32];
59-
int size = snprintf(buf, sizeof(buf), "%f", number);
60-
int actualSize = 0;
61-
while (actualSize < size && buf[actualSize] != '\0') {
62-
++actualSize;
63-
}
64-
return actualSize;
65-
}
66-
67-
#pragma clang diagnostic push
68-
#pragma ide diagnostic ignored "misc-no-recursion"
6948
/**
7049
* @param buffer
7150
* @param ndims
@@ -77,24 +56,23 @@ int string_size_of_float(float number) {
7756
*/
7857
char*
7958
print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_dim, int* index, int num_elements, int* padded) {
80-
char* str = NULL;
59+
char* str;
8160
int i, j, t;
8261
int reverse_run = 0;
83-
84-
if (ndims == 0 && buffer != NULL) {
85-
str = expand_str(str, string_size_of_float(buffer[0]));
86-
sprintf(str, "%g", buffer[0]);
87-
return str;
62+
// Allocate memory for the string
63+
str = (char*)emalloc(10000000 * sizeof(char));
64+
if (str == NULL) {
65+
fprintf(stderr, "Error: Failed to allocate memory for string.\n");
66+
exit(1);
8867
}
8968

90-
if (index == NULL) {
91-
fprintf(stderr, "Error: print_array_float called with NULL index.\n");
92-
exit(1);
69+
if (ndims == 0) {
70+
sprintf(str, "%g\n", buffer[0]);
71+
return str;
9372
}
9473

95-
if (cur_dim == ndims - 1 && buffer != NULL) {
74+
if (cur_dim == ndims - 1) {
9675
// Print the opening bracket for this dimension
97-
str = expand_str(str, 2);
9876
sprintf(str, "[");
9977
// Print the elements of the array
10078
for (i = 0; i < shape[cur_dim]; i++) {
@@ -107,60 +85,43 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
10785
offset += index[k] * strides[k];
10886
}
10987
// Print the element
110-
str = expand_str(str, string_size_of_float(buffer[offset / sizeof(float)]) + 1);
11188
sprintf(str + strlen(str), "%g", buffer[offset / sizeof(float)]);
11289

11390
// Print a comma if this is not the last element in the dimension
11491
if (i < shape[cur_dim] - 1) {
115-
str = expand_str(str, strlen(", ") + 1);
11692
sprintf(str + strlen(str), ", ");
11793
}
11894

11995
if ((i + 1) % 10 == 0 && i < shape[cur_dim] - 1) {
120-
str = expand_str(str, 1);
12196
sprintf(str + strlen(str), "\n");
12297
for (t = 0; t < ndims; t++) {
123-
str = expand_str(str, strlen(" ") + 1);
12498
sprintf(str + strlen(str), " ");
12599
}
126100
}
127101

128-
if (shape[cur_dim] > 20 ) {
102+
if (shape[cur_dim] > 20) {
129103
if (i > 1 && reverse_run == 0) {
130104
i = shape[cur_dim] - 4;
131105
reverse_run = 1;
132-
str = expand_str(str, strlen("... ") + 1);
133106
sprintf(str + strlen(str), "... ");
134107
}
135108
}
136109
}
137110

138111
// Print the closing bracket for this dimension
139-
str = expand_str(str, 2);
140112
sprintf(str + strlen(str), "]");
141-
142-
if (ndims >= 2) {
143-
if (index[cur_dim - 1] < shape[ndims - 2] - 1) {
144-
str = expand_str(str, strlen("\n ") + 1);
145-
sprintf(str + strlen(str), "\n ");
146-
}
147-
} else {
148-
if (index[cur_dim] < shape[ndims - 1] - 1) {
149-
str = expand_str(str, strlen("\n ") + 1);
150-
sprintf(str + strlen(str), "\n ");
151-
}
113+
if (index[cur_dim-1] < shape[ndims - 2] - 1) {
114+
sprintf(str + strlen(str), "\n ");
152115
}
153116
} else {
154117
if (cur_dim != 0) {
155118
if (cur_dim == index[cur_dim - 1]) {
156119
for (t = cur_dim; t < ndims; t++) {
157-
str = expand_str(str, 2);
158120
sprintf(str + strlen(str), " ");
159121
}
160122
}
161123
}
162124
// Print the opening bracket for this dimension
163-
str = expand_str(str, strlen("[") + 1);
164125
sprintf(str, "[");
165126

166127
// Recursively print each element in the dimension
@@ -171,7 +132,6 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
171132
char* child_str = print_array_float(buffer, ndims, shape, strides, cur_dim + 1, index, num_elements, padded);
172133

173134
// Add the child string to the parent string
174-
str = expand_str(str, strlen(child_str) + 1);
175135
sprintf(str + strlen(str), "%s", child_str);
176136

177137
// Free the child string
@@ -180,7 +140,6 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
180140
// Print a comma and newline if this is not the last element in the dimension
181141
if (i < shape[cur_dim] - 1) {
182142
for (j = 0; j < cur_dim; j++) {
183-
str = expand_str(str, strlen(" ") + 1);
184143
sprintf(str + strlen(str), " ");
185144
}
186145
}
@@ -190,7 +149,6 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
190149
if(i >= 2 && reverse_run == 0) {
191150
i = shape[cur_dim] - 4;
192151
reverse_run = 1;
193-
str = expand_str(str, strlen("...\n") + 1);
194152
sprintf(str + strlen(str), "...\n");
195153
if (i < shape[cur_dim] - 1) {
196154
for (j = 1; j < ndims; j++) {
@@ -203,24 +161,20 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
203161
}
204162
}
205163
// Print the closing bracket for this dimension
206-
str = expand_str(str, strlen("]") + 1);
207164
sprintf(str + strlen(str), "]");
208165

209166
if (cur_dim != 0 && index[cur_dim-1] < shape[cur_dim-1] - 1) {
210-
str = expand_str(str, strlen("\n") + 1);
211167
sprintf(str + strlen(str), "\n");
212168
}
213169
}
214170

215171
// Add a newline if this is the outermost dimension
216172
if (cur_dim == 0) {
217-
str = expand_str(str, strlen("\n") + 1);
218173
sprintf(str + strlen(str), "\n");
219174
}
220175

221176
return str;
222177
}
223-
#pragma clang diagnostic pop
224178

225179
/**
226180
* Print matrix to
@@ -232,7 +186,7 @@ print_array_float(float* buffer, int ndims, int* shape, int* strides, int cur_di
232186
*/
233187
char*
234188
print_matrix_float(float* buffer, int ndims, int* shape, int* strides, int num_elements, int device) {
235-
float *tmp_buffer = NULL;
189+
float *tmp_buffer;
236190
int *index = emalloc(ndims * sizeof(int));
237191
if (device == NDARRAY_DEVICE_GPU) {
238192
#ifdef HAVE_CUBLAS

src/ndarray.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ NDArray_Print(NDArray *array, int do_return) {
607607
}
608608
if (do_return == 0) {
609609
printf("%s", str);
610-
efree(str);
611610
return NULL;
612611
}
613612
return str;

0 commit comments

Comments
 (0)