| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * @file wrapper.cpp |
| 20 | * @brief Replaces malloc with appropriate calls to TheCustomHeapType. |
| 21 | * @author Emery Berger <http://www.emeryberger.com> |
| 22 | */ |
| 23 | |
| 24 | #include "threads/cpuinfo.h" |
| 25 | |
| 26 | #include <string.h> // for memcpy and memset |
| 27 | #include <stdlib.h> // size_t |
| 28 | #include <stdint.h> |
| 29 | #include <new> |
| 30 | |
| 31 | |
| 32 | extern "C" { |
| 33 | |
| 34 | void * xxmalloc (size_t); |
| 35 | void xxfree (void *); |
| 36 | void * xxmemalign(size_t, size_t); |
| 37 | #if HL_USE_XXREALLOC |
| 38 | void * xxrealloc(void *, size_t); |
| 39 | #endif |
| 40 | // Takes a pointer and returns how much space it holds. |
| 41 | size_t xxmalloc_usable_size (void *); |
| 42 | |
| 43 | // Locks the heap(s), used prior to any invocation of fork(). |
| 44 | void xxmalloc_lock(); |
| 45 | |
| 46 | // Unlocks the heap(s), after fork(). |
| 47 | void xxmalloc_unlock(); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | #if defined(__APPLE__) |
| 52 | #include <malloc/malloc.h> |
| 53 | #elif defined(__FreeBSD__) || defined(__NetBSD__) |
| 54 | #include <stdlib.h> |
| 55 | #else |
| 56 | #include <malloc.h> // for memalign |
| 57 | #endif |
| 58 | |
| 59 | #ifdef _WIN32 |
| 60 | #define WIN32_LEAN_AND_MEAN |
| 61 | #include <windows.h> |
| 62 | // Disable warnings about long (> 255 chars) identifiers. |
| 63 | #pragma warning(disable:4786) |
| 64 | // Set inlining to the maximum possible depth. |
| 65 | #pragma inline_depth(255) |
| 66 | #pragma warning(disable: 4074) // initializers put in compiler reserved area |
| 67 | |
| 68 | #pragma comment(linker, "/disallowlib:libc.lib") |
| 69 | #pragma comment(linker, "/disallowlib:libcd.lib") |
| 70 | #pragma comment(linker, "/disallowlib:libcmt.lib") |
| 71 | #pragma comment(linker, "/disallowlib:libcmtd.lib") |
| 72 | #pragma comment(linker, "/disallowlib:msvcrtd.lib") |
| 73 | |
| 74 | #else |
| 75 | #include <errno.h> |
| 76 | #endif |
| 77 | |
| 78 | #ifndef CUSTOM_PREFIX |
| 79 | #define CUSTOM_PREFIX(x) x |
| 80 | #endif |
| 81 | |
| 82 | #define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) |
| 83 | #define CUSTOM_FREE(x) CUSTOM_PREFIX(free)(x) |
| 84 | #define CUSTOM_CFREE(x) CUSTOM_PREFIX(cfree)(x) |
| 85 | #define CUSTOM_REALLOC(x,y) CUSTOM_PREFIX(realloc)(x,y) |
| 86 | #define CUSTOM_REALLOCARRAY(x,y,z) CUSTOM_PREFIX(reallocarray)(x,y,z) |
| 87 | #define CUSTOM_CALLOC(x,y) CUSTOM_PREFIX(calloc)(x,y) |
| 88 | #define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y) |
| 89 | #define CUSTOM_POSIX_MEMALIGN(x,y,z) CUSTOM_PREFIX(posix_memalign)(x,y,z) |
| 90 | #define CUSTOM_ALIGNED_ALLOC(x,y) CUSTOM_PREFIX(aligned_alloc)(x,y) |
| 91 | #define CUSTOM_GETSIZE(x) CUSTOM_PREFIX(malloc_usable_size)(x) |
| 92 | #define CUSTOM_GOODSIZE(x) CUSTOM_PREFIX(malloc_good_size)(x) |
| 93 | #define CUSTOM_VALLOC(x) CUSTOM_PREFIX(valloc)(x) |
| 94 | #define CUSTOM_PVALLOC(x) CUSTOM_PREFIX(pvalloc)(x) |
| 95 | #define CUSTOM_RECALLOC(x,y,z) CUSTOM_PREFIX(recalloc)(x,y,z) |
| 96 | #define CUSTOM_STRNDUP(s,sz) CUSTOM_PREFIX(strndup)(s,sz) |
| 97 | #define CUSTOM_STRDUP(s) CUSTOM_PREFIX(strdup)(s) |
| 98 | #define CUSTOM_GETCWD(b,s) CUSTOM_PREFIX(getcwd)(b,s) |
| 99 | #define CUSTOM_GETENV(s) CUSTOM_PREFIX(getenv)(s) |
| 100 | |
| 101 | // GNU-related routines: |
| 102 | #define CUSTOM_MALLOPT(x,y) CUSTOM_PREFIX(mallopt)(x,y) |
| 103 | #define CUSTOM_MALLOC_TRIM(s) CUSTOM_PREFIX(malloc_trim)(s) |
| 104 | #define CUSTOM_MALLOC_STATS(a) CUSTOM_PREFIX(malloc_stats)(a) |
| 105 | #define CUSTOM_MALLOC_GET_STATE(p) CUSTOM_PREFIX(malloc_get_state)(p) |
| 106 | #define CUSTOM_MALLOC_SET_STATE(p) CUSTOM_PREFIX(malloc_set_state)(p) |
| 107 | #define CUSTOM_MALLINFO(a) CUSTOM_PREFIX(mallinfo)(a) |
| 108 | |
| 109 | #if defined(_WIN32) |
| 110 | #define MYCDECL __cdecl |
| 111 | #if !defined(NO_INLINE) |
| 112 | #define NO_INLINE __declspec(noinline) |
| 113 | #endif // !defined(NO_INLINE) |
| 114 | #pragma inline_depth(255) |
| 115 | |
| 116 | #if !defined(NDEBUG) |
| 117 | #define __forceinline inline |
| 118 | #endif // !defined(NDEBUG) |
| 119 | |
| 120 | #else |
| 121 | #define MYCDECL |
| 122 | #endif // defined(_WIN32) |
| 123 | |
| 124 | // #if !defined(__GNUG__) && (defined(__linux__) || defined(__APPLE__)) |
| 125 | #if defined(NDEBUG) && ((defined(__linux__) || defined(__APPLE__))) |
| 126 | #define HEAP_LAYERS_INLINE __attribute__((always_inline)) |
| 127 | #else |
| 128 | #define HEAP_LAYERS_INLINE |
| 129 | #endif // defined(__linux__) || defined(__APPLE__) |
| 130 | |
| 131 | /***** generic malloc functions *****/ |
| 132 | |
| 133 | #include <stdio.h> |
| 134 | |
| 135 | #ifndef FLATTEN |
| 136 | #if 1 |
| 137 | #define FLATTEN __attribute__((flatten)) |
| 138 | #else |
| 139 | #define FLATTEN |
| 140 | #endif |
| 141 | #endif |
| 142 | |
| 143 | #if !defined(_WIN32) |
| 144 | #include <dlfcn.h> |
| 145 | #include <limits.h> |
| 146 | |
| 147 | #if !defined(RTLD_NEXT) |
| 148 | #define RTLD_NEXT ((void *) -1) |
| 149 | #endif |
| 150 | |
| 151 | extern "C" void MYCDECL CUSTOM_FREE(void *); |
| 152 | extern "C" void * MYCDECL CUSTOM_MALLOC(size_t) __attribute__((alloc_size(1))) __attribute((malloc)); |
| 153 | extern "C" void * MYCDECL CUSTOM_CALLOC(size_t nelem, size_t elsize) __attribute__((alloc_size(1,2))) __attribute((malloc)); |
| 154 | |
| 155 | extern "C" FLATTEN void MYCDECL CUSTOM_FREE (void * ptr) |
| 156 | { |
| 157 | xxfree (ptr); |
| 158 | } |
| 159 | |
| 160 | extern "C" FLATTEN void * MYCDECL CUSTOM_MALLOC(size_t sz) |
| 161 | { |
| 162 | void * ptr = xxmalloc(sz); |
| 163 | return ptr; |
| 164 | } |
| 165 | |
| 166 | #include <atomic> |
| 167 | |
| 168 | static __thread int in_dlsym { 0 }; |
| 169 | |
| 170 | // Wrapper around dlsym that we use in calloc, below. |
| 171 | extern "C" void * my_dlsym(void * handle, const char * symbol) { |
| 172 | ++in_dlsym; |
| 173 | auto ptr = dlsym(handle, symbol); |
| 174 | --in_dlsym; |
| 175 | return ptr; |
| 176 | } |
| 177 | |
| 178 | extern "C" FLATTEN void * MYCDECL CUSTOM_CALLOC(size_t nelem, size_t elsize) |
| 179 | { |
| 180 | // Reject calls from dlsym so it uses its own internal buffer. |
| 181 | if (in_dlsym) { |
| 182 | return nullptr; |
| 183 | } |
| 184 | |
| 185 | size_t n = nelem * elsize; |
| 186 | |
| 187 | if (elsize && (nelem != n / elsize)) { |
| 188 | return nullptr; |
| 189 | } |
| 190 | |
| 191 | void * ptr = xxmalloc(n); |
| 192 | |
| 193 | // Zero out the malloc'd block. |
| 194 | if (ptr) { |
| 195 | memset (ptr, 0, n); |
| 196 | } |
| 197 | return ptr; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | #if !defined(_WIN32) |
| 202 | extern "C" void * MYCDECL CUSTOM_MEMALIGN (size_t alignment, size_t size) |
| 203 | #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__SVR4) |
| 204 | throw() |
| 205 | #endif |
| 206 | __attribute__((alloc_size(2))) __attribute__((alloc_align(1))) |
| 207 | ; |
| 208 | |
| 209 | extern "C" FLATTEN int CUSTOM_POSIX_MEMALIGN (void **memptr, size_t alignment, size_t size) |
| 210 | #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__SVR4) |
| 211 | throw() |
| 212 | #endif |
| 213 | { |
| 214 | // Check for non power-of-two alignment. |
| 215 | if ((alignment == 0) || |
| 216 | (alignment & (alignment - 1))) |
| 217 | { |
| 218 | return EINVAL; |
| 219 | } |
| 220 | void * ptr = CUSTOM_MEMALIGN (alignment, size); |
| 221 | if (!ptr) { |
| 222 | return ENOMEM; |
| 223 | } else { |
| 224 | *memptr = ptr; |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | |
| 231 | extern "C" FLATTEN void * MYCDECL CUSTOM_MEMALIGN (size_t alignment, size_t size) |
| 232 | #if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__SVR4) |
| 233 | throw() |
| 234 | #endif |
| 235 | { |
| 236 | return xxmemalign(alignment, size); |
| 237 | } |
| 238 | |
| 239 | extern "C" FLATTEN void * MYCDECL CUSTOM_ALIGNED_ALLOC(size_t alignment, size_t size) |
| 240 | #if !defined(__FreeBSD__) && !defined(__NetBSD__) |
| 241 | throw() |
| 242 | #endif |
| 243 | { |
| 244 | // Per the man page: "The function aligned_alloc() is the same as |
| 245 | // memalign(), except for the added restriction that size should be |
| 246 | // a multiple of alignment." Rather than check and potentially fail, |
| 247 | // we just enforce this by rounding up the size, if necessary. |
| 248 | size = size + alignment - (size % alignment); |
| 249 | return CUSTOM_MEMALIGN(alignment, size); |
| 250 | } |
| 251 | |
| 252 | extern "C" FLATTEN size_t MYCDECL CUSTOM_GETSIZE (void * ptr) |
| 253 | { |
| 254 | return xxmalloc_usable_size (ptr); |
| 255 | } |
| 256 | |
| 257 | extern "C" void MYCDECL CUSTOM_CFREE (void * ptr) |
| 258 | { |
| 259 | xxfree (ptr); |
| 260 | } |
| 261 | |
| 262 | extern "C" size_t MYCDECL CUSTOM_GOODSIZE (size_t sz) { |
| 263 | void * ptr = xxmalloc(sz); |
| 264 | size_t objSize = CUSTOM_GETSIZE(ptr); |
| 265 | CUSTOM_FREE(ptr); |
| 266 | return objSize; |
| 267 | } |
| 268 | |
| 269 | extern "C" void * MYCDECL CUSTOM_REALLOC (void * ptr, size_t sz) |
| 270 | { |
| 271 | #if HL_USE_XXREALLOC |
| 272 | void* buf = xxrealloc(ptr, sz); |
| 273 | return buf; |
| 274 | #else |
| 275 | if (!ptr) { |
| 276 | ptr = xxmalloc (sz); |
| 277 | return ptr; |
| 278 | } |
| 279 | if (sz == 0) { |
| 280 | CUSTOM_FREE (ptr); |
| 281 | #if defined(__APPLE__) |
| 282 | // 0 size = free. We return a small object. This behavior is |
| 283 | // apparently required under Mac OS X and optional under POSIX. |
| 284 | return xxmalloc(1); |
| 285 | #else |
| 286 | // For POSIX, don't return anything. |
| 287 | return nullptr; |
| 288 | #endif |
| 289 | } |
| 290 | |
| 291 | size_t objSize = CUSTOM_GETSIZE (ptr); |
| 292 | |
| 293 | void * buf = xxmalloc(sz); |
| 294 | |
| 295 | if (buf) { |
| 296 | if (objSize == CUSTOM_GETSIZE(buf)) { |
| 297 | // The objects are the same actual size. |
| 298 | // Free the new object and return the original. |
| 299 | CUSTOM_FREE (buf); |
| 300 | return ptr; |
| 301 | } |
| 302 | // Copy the contents of the original object |
| 303 | // up to the size of the new block. |
| 304 | size_t minSize = (objSize < sz) ? objSize : sz; |
| 305 | memcpy (buf, ptr, minSize); |
| 306 | } |
| 307 | |
| 308 | // Free the old block. |
| 309 | CUSTOM_FREE (ptr); |
| 310 | |
| 311 | // Return a pointer to the new one. |
| 312 | return buf; |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | extern "C" void * MYCDECL CUSTOM_REALLOCARRAY (void * ptr, size_t sz1, size_t sz2) |
| 317 | { |
| 318 | if ((sz1 * sz2 < sz1) || (sz1 * sz2 < sz2)) { // overflow |
| 319 | errno = ENOMEM; |
| 320 | return nullptr; |
| 321 | } |
| 322 | return CUSTOM_REALLOC(ptr, sz1 * sz2); |
| 323 | } |
| 324 | |
| 325 | #if defined(__linux) |
| 326 | |
| 327 | extern "C" char * MYCDECL CUSTOM_STRNDUP(const char * s, size_t sz) |
| 328 | { |
| 329 | char * newString = nullptr; |
| 330 | if (s) { |
| 331 | size_t cappedLength = strnlen (s, sz); |
| 332 | if ((newString = (char *) xxmalloc(cappedLength + 1))) { |
| 333 | strncpy(newString, s, cappedLength); |
| 334 | newString[cappedLength] = '\0'; |
| 335 | } |
| 336 | } |
| 337 | return newString; |
| 338 | } |
| 339 | #endif |
| 340 | |
| 341 | extern "C" char * MYCDECL CUSTOM_STRDUP(const char * s) |
| 342 | { |
| 343 | char * newString = nullptr; |
| 344 | if (s) { |
| 345 | if ((newString = (char *) xxmalloc(strlen(s) + 1))) { |
| 346 | strcpy(newString, s); |
| 347 | } |
| 348 | } |
| 349 | return newString; |
| 350 | } |
| 351 | |
| 352 | typedef char * getcwdFunction (char *, size_t); |
| 353 | |
| 354 | |
| 355 | extern "C" char * MYCDECL CUSTOM_GETCWD(char * buf, size_t size) |
| 356 | { |
| 357 | static getcwdFunction * real_getcwd |
| 358 | = reinterpret_cast<getcwdFunction *> |
| 359 | (reinterpret_cast<uintptr_t>(dlsym (RTLD_NEXT, "getcwd"))); |
| 360 | |
| 361 | if (!buf) { |
| 362 | if (size == 0) { |
| 363 | size = PATH_MAX; |
| 364 | } |
| 365 | buf = (char *) xxmalloc(size); |
| 366 | } |
| 367 | return (real_getcwd)(buf, size); |
| 368 | } |
| 369 | |
| 370 | #endif |
| 371 | |
| 372 | |
| 373 | extern "C" int CUSTOM_MALLOPT (int /* param */, int /* value */) { |
| 374 | // NOP. |
| 375 | return 1; // success. |
| 376 | } |
| 377 | |
| 378 | extern "C" int xxmalloc_TRIM(size_t /* pad */) { |
| 379 | // NOP. |
| 380 | return 0; // no memory returned to OS. |
| 381 | } |
| 382 | |
| 383 | extern "C" void xxmalloc_STATS() { |
| 384 | // NOP. |
| 385 | } |
| 386 | |
| 387 | extern "C" void * xxmalloc_GET_STATE() { |
| 388 | return nullptr; // always returns "error". |
| 389 | } |
| 390 | |
| 391 | extern "C" int xxmalloc_SET_STATE(void * /* ptr */) { |
| 392 | return 0; // success. |
| 393 | } |
| 394 | |
| 395 | // NOTE-- MUSL defines GNUC for some reason but does not have mallinfo |
| 396 | #if defined(__GNUC__) && defined(__GLIBC__) && !defined(__FreeBSD__) && !defined(__NetBSD__) |
| 397 | extern "C" struct mallinfo CUSTOM_MALLINFO() { |
| 398 | // For now, we return useless stats. |
| 399 | struct mallinfo m; |
| 400 | m.arena = 0; |
| 401 | m.ordblks = 0; |
| 402 | m.smblks = 0; |
| 403 | m.hblks = 0; |
| 404 | m.hblkhd = 0; |
| 405 | m.usmblks = 0; |
| 406 | m.fsmblks = 0; |
| 407 | m.uordblks = 0; |
| 408 | m.fordblks = 0; |
| 409 | m.keepcost = 0; |
| 410 | return m; |
| 411 | } |
| 412 | #endif |
| 413 | |
| 414 | #if defined(__SVR4) |
| 415 | // Apparently we no longer need to replace new and friends for Solaris. |
| 416 | #define NEW_INCLUDED |
| 417 | #endif |
| 418 | |
| 419 | |
| 420 | #ifndef NEW_INCLUDED |
| 421 | #define NEW_INCLUDED |
| 422 | |
| 423 | void * FLATTEN operator new (size_t sz) |
| 424 | #if defined(_GLIBCXX_THROW) |
| 425 | _GLIBCXX_THROW (std::bad_alloc) |
| 426 | #endif |
| 427 | { |
| 428 | void * ptr = xxmalloc (sz); |
| 429 | if (ptr) { |
| 430 | return ptr; |
| 431 | } |
| 432 | throw std::bad_alloc(); |
| 433 | } |
| 434 | |
| 435 | void FLATTEN operator delete (void * ptr) |
| 436 | #if !defined(linux_) |
| 437 | throw () |
| 438 | #endif |
| 439 | { |
| 440 | CUSTOM_FREE (ptr); |
| 441 | } |
| 442 | |
| 443 | #if !defined(__SUNPRO_CC) || __SUNPRO_CC > 0x420 |
| 444 | void * FLATTEN operator new (size_t sz, const std::nothrow_t&) throw() { |
| 445 | return xxmalloc(sz); |
| 446 | } |
| 447 | |
| 448 | void * FLATTEN operator new[] (size_t size) |
| 449 | #if defined(_GLIBCXX_THROW) |
| 450 | _GLIBCXX_THROW (std::bad_alloc) |
| 451 | #endif |
| 452 | { |
| 453 | void * ptr = xxmalloc(size); |
| 454 | if (ptr) { |
| 455 | return ptr; |
| 456 | } |
| 457 | throw std::bad_alloc(); |
| 458 | } |
| 459 | |
| 460 | void * FLATTEN operator new[] (size_t sz, const std::nothrow_t&) |
| 461 | throw() |
| 462 | { |
| 463 | return xxmalloc(sz); |
| 464 | } |
| 465 | |
| 466 | void FLATTEN operator delete[] (void * ptr) |
| 467 | #if defined(_GLIBCXX_USE_NOEXCEPT) |
| 468 | _GLIBCXX_USE_NOEXCEPT |
| 469 | #else |
| 470 | #if defined(__GNUC__) |
| 471 | // clang + libcxx on linux |
| 472 | _NOEXCEPT |
| 473 | #endif |
| 474 | #endif |
| 475 | { |
| 476 | CUSTOM_FREE (ptr); |
| 477 | } |
| 478 | |
| 479 | #if defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309 |
| 480 | |
| 481 | void FLATTEN operator delete(void * ptr, size_t) |
| 482 | #if !defined(linux_) |
| 483 | throw () |
| 484 | #endif |
| 485 | { |
| 486 | CUSTOM_FREE (ptr); |
| 487 | } |
| 488 | |
| 489 | void FLATTEN operator delete[](void * ptr, size_t) |
| 490 | #if defined(__GNUC__) |
| 491 | _GLIBCXX_USE_NOEXCEPT |
| 492 | #endif |
| 493 | { |
| 494 | CUSTOM_FREE (ptr); |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | #endif |
| 499 | #endif |
| 500 | |
| 501 | /***** replacement functions for GNU libc extensions to malloc *****/ |
| 502 | |
| 503 | extern "C" void * MYCDECL CUSTOM_VALLOC (size_t sz) |
| 504 | { |
| 505 | return CUSTOM_MEMALIGN (HL::CPUInfo::PageSize, sz); // Default page size on most architectures. |
| 506 | } |
| 507 | |
| 508 | |
| 509 | extern "C" void * MYCDECL CUSTOM_PVALLOC (size_t sz) |
| 510 | { |
| 511 | size_t pgsize_minus1 = HL::CPUInfo::PageSize - 1; |
| 512 | // Rounds up to the next pagesize and then calls valloc. Hoard |
| 513 | // doesn't support aligned memory requests. |
| 514 | return CUSTOM_VALLOC ((sz + pgsize_minus1) & ~pgsize_minus1); |
| 515 | } |
| 516 | |
| 517 | // The wacky recalloc function, for Windows. |
| 518 | extern "C" void * MYCDECL CUSTOM_RECALLOC (void * p, size_t num, size_t sz) |
| 519 | { |
| 520 | auto n = num * sz; |
| 521 | void * ptr = CUSTOM_REALLOC (p, n); |
| 522 | if (ptr) { |
| 523 | // Clear out the memory. |
| 524 | memset (ptr, 0, n); |
| 525 | } |
| 526 | return ptr; |
| 527 | } |
| 528 | |
| 529 | #if defined(_WIN32) |
| 530 | |
| 531 | /////// Other replacement functions that call malloc. |
| 532 | |
| 533 | // from http://msdn2.microsoft.com/en-us/library/6ewkz86d(VS.80).aspx |
| 534 | // fgetc, _fgetchar, fgets, fprintf, fputc, _fputchar, fputs, fread, fscanf, fseek, fsetpos |
| 535 | // _fullpath, fwrite, getc, getchar, _getcwd, _getdcwd, gets, _getw, _popen, printf, putc |
| 536 | // putchar, _putenv, puts, _putw, scanf, _searchenv, setvbuf, _strdup, system, _tempnam, |
| 537 | // ungetc, vfprintf, vprintf |
| 538 | |
| 539 | |
| 540 | char * CUSTOM_GETENV(const char * str) { |
| 541 | char buf[32767]; |
| 542 | int len = GetEnvironmentVariable (str, buf, 32767); |
| 543 | if (len == 0) { |
| 544 | return nullptr; |
| 545 | } else { |
| 546 | char * str = new char[len + 1]; |
| 547 | strncpy (str, buf, len + 1); |
| 548 | return str; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | int CUSTOM_PUTENV(char * str) { |
| 553 | char * eqpos = strchr (str, '='); |
| 554 | if (eqpos) { |
| 555 | char first[32767], second[32767]; |
| 556 | int namelen = (size_t) eqpos - (size_t) str; |
| 557 | strncpy (first, str, namelen); |
| 558 | first[namelen] = '\0'; |
| 559 | int valuelen = strlen (eqpos + 1); |
| 560 | strncpy (second, eqpos + 1, valuelen); |
| 561 | second[valuelen] = '\0'; |
| 562 | char buf[255]; |
| 563 | sprintf (buf, "setting %s to %s\n", first, second); |
| 564 | printf (buf); |
| 565 | SetEnvironmentVariable (first, second); |
| 566 | return 0; |
| 567 | } |
| 568 | return -1; |
| 569 | } |
| 570 | |
| 571 | #endif |