Skip to content

Commit 9b791c9

Browse files
Fix cross-platform compatibility for environment variable handling
- Add portable setenv/unsetenv macros for Windows (_putenv_s) - Replace all setenv/unsetenv calls with portable versions - Fix trailing whitespace issues for editorconfig compliance This fixes Windows and macOS build failures caused by POSIX-only functions. Co-Authored-By: Alex Peng <[email protected]>
1 parent db2a903 commit 9b791c9

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

ggml/src/ggml-alloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static bool ggml_alloc_should_fail(size_t size) {
8484
return true;
8585
}
8686
}
87-
87+
8888
const char * fail_count = getenv("GGML_ALLOC_FAIL_COUNT");
8989
if (fail_count) {
9090
static int alloc_count = 0;
@@ -93,7 +93,7 @@ static bool ggml_alloc_should_fail(size_t size) {
9393
return true;
9494
}
9595
}
96-
96+
9797
return false;
9898
}
9999

tests/test-invalid-inputs.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
#include <string>
99
#include <vector>
1010

11+
#ifdef _WIN32
12+
#define setenv_portable(name, value) _putenv_s(name, value)
13+
#define unsetenv_portable(name) _putenv_s(name, "")
14+
#else
15+
#define setenv_portable(name, value) setenv(name, value, 1)
16+
#define unsetenv_portable(name) unsetenv(name)
17+
#endif
18+
1119
enum invalid_input_scenario {
1220
INVALID_TENSOR_SHAPE_NEGATIVE = 1,
1321
INVALID_TENSOR_SHAPE_ZERO,
@@ -64,7 +72,7 @@ static bool test_invalid_input_scenario(enum invalid_input_scenario scenario) {
6472
case INVALID_TENSOR_SHAPE_MISMATCH: {
6573
ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 20);
6674
ggml_tensor * b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 15, 25);
67-
75+
6876
if (a && b) {
6977
bool shapes_different = (a->ne[0] != b->ne[0]) || (a->ne[1] != b->ne[1]);
7078
if (shapes_different) {
@@ -80,7 +88,7 @@ static bool test_invalid_input_scenario(enum invalid_input_scenario scenario) {
8088
case INVALID_TENSOR_TYPE_MISMATCH: {
8189
ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 100);
8290
ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_F16, 100);
83-
91+
8492
if (a && b && a->type != b->type) {
8593
printf(" - \033[1;32mOK\033[0m: type mismatch detected\n");
8694
test_passed = true;
@@ -105,7 +113,7 @@ static bool test_invalid_input_scenario(enum invalid_input_scenario scenario) {
105113
case INVALID_OPERATION_INCOMPATIBLE: {
106114
ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 100);
107115
ggml_tensor * b = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 10, 20);
108-
116+
109117
if (a && b) {
110118
bool incompatible = (a->ne[1] != b->ne[1]) || (a->ne[0] != 100 && b->ne[0] != 10);
111119
if (incompatible) {

tests/test-memory-exhaustion.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
#include <string>
99
#include <vector>
1010

11+
#ifdef _WIN32
12+
#define setenv_portable(name, value) _putenv_s(name, value)
13+
#define unsetenv_portable(name) _putenv_s(name, "")
14+
#else
15+
#define setenv_portable(name, value) setenv(name, value, 1)
16+
#define unsetenv_portable(name) unsetenv(name)
17+
#endif
18+
1119
enum memory_exhaustion_scenario {
1220
MEM_EXHAUST_SMALL_ALLOC = 1,
1321
MEM_EXHAUST_MEDIUM_ALLOC,
@@ -40,23 +48,23 @@ static bool test_memory_exhaustion_scenario(ggml_backend_t backend, enum memory_
4048

4149
switch (scenario) {
4250
case MEM_EXHAUST_SMALL_ALLOC:
43-
setenv("GGML_ALLOC_FAIL_THRESHOLD", "1024", 1);
51+
setenv_portable("GGML_ALLOC_FAIL_THRESHOLD", "1024");
4452
break;
4553
case MEM_EXHAUST_MEDIUM_ALLOC:
46-
setenv("GGML_ALLOC_FAIL_THRESHOLD", "1048576", 1);
54+
setenv_portable("GGML_ALLOC_FAIL_THRESHOLD", "1048576");
4755
break;
4856
case MEM_EXHAUST_LARGE_ALLOC:
49-
setenv("GGML_ALLOC_FAIL_THRESHOLD", "10485760", 1);
57+
setenv_portable("GGML_ALLOC_FAIL_THRESHOLD", "10485760");
5058
break;
5159
case MEM_EXHAUST_MANY_ALLOCS:
52-
setenv("GGML_ALLOC_FAIL_COUNT", "10", 1);
60+
setenv_portable("GGML_ALLOC_FAIL_COUNT", "10");
5361
break;
5462
case MEM_EXHAUST_BUFFER_OVERFLOW:
55-
setenv("GGML_ALLOC_FAIL_THRESHOLD", "100", 1);
63+
setenv_portable("GGML_ALLOC_FAIL_THRESHOLD", "100");
5664
break;
5765
default:
58-
unsetenv("GGML_ALLOC_FAIL_THRESHOLD");
59-
unsetenv("GGML_ALLOC_FAIL_COUNT");
66+
unsetenv_portable("GGML_ALLOC_FAIL_THRESHOLD");
67+
unsetenv_portable("GGML_ALLOC_FAIL_COUNT");
6068
break;
6169
}
6270

@@ -72,7 +80,7 @@ static bool test_memory_exhaustion_scenario(ggml_backend_t backend, enum memory_
7280
}
7381

7482
ggml_tensor * a = nullptr;
75-
83+
7684
switch (scenario) {
7785
case MEM_EXHAUST_SMALL_ALLOC:
7886
a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 256);
@@ -124,9 +132,9 @@ static bool test_memory_exhaustion_scenario(ggml_backend_t backend, enum memory_
124132
}
125133

126134
ggml_free(ctx);
127-
128-
unsetenv("GGML_ALLOC_FAIL_THRESHOLD");
129-
unsetenv("GGML_ALLOC_FAIL_COUNT");
135+
136+
unsetenv_portable("GGML_ALLOC_FAIL_THRESHOLD");
137+
unsetenv_portable("GGML_ALLOC_FAIL_COUNT");
130138

131139
return test_passed;
132140
}

0 commit comments

Comments
 (0)