forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cpp
More file actions
866 lines (737 loc) · 20 KB
/
misc.cpp
File metadata and controls
866 lines (737 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
/*++
Module Name:
cruntime/misc.cpp
Abstract:
Implementation of C runtime functions that don't fit anywhere else.
--*/
#include "pal/thread.hpp"
#include "pal/threadsusp.hpp"
#include "pal/malloc.hpp"
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include "pal/misc.h"
#include <errno.h>
/* <stdarg.h> needs to be included after "palinternal.h" to avoid name
collision for va_start and va_end */
#include <stdarg.h>
#include <time.h>
#include <limits.h>
#if HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif // HAVE_CRT_EXTERNS_H
#if defined(_AMD64_) || defined(_x86_)
#include <xmmintrin.h>
#endif // defined(_AMD64_) || defined(_x86_)
SET_DEFAULT_DEBUG_CHANNEL(CRT);
char **palEnvironment = NULL;
CRITICAL_SECTION gcsEnvironment;
using namespace CorUnix;
/*++
Function:
_gcvt_s
See MSDN doc.
--*/
char *
__cdecl
_gcvt_s( char * buffer, int iSize, double value, int digits )
{
PERF_ENTRY(_gcvt);
ENTRY( "_gcvt( value:%f digits=%d, buffer=%p )\n", value, digits, buffer );
if ( !buffer )
{
ERROR( "buffer was an invalid pointer.\n" );
}
switch ( digits )
{
case 7 :
/* Fall through */
case 8 :
/* Fall through */
case 15 :
/* Fall through */
case 17 :
sprintf_s( buffer, iSize, "%.*g", digits, value );
break;
default :
ASSERT( "Only the digits 7, 8, 15, and 17 are valid.\n" );
*buffer = '\0';
}
LOGEXIT( "_gcvt returns %p (%s)\n", buffer , buffer );
PERF_EXIT(_gcvt);
return buffer;
}
/*++
Function :
__iscsym
See MSDN for more details.
--*/
int
__cdecl
__iscsym( int c )
{
PERF_ENTRY(__iscsym);
ENTRY( "__iscsym( c=%d )\n", c );
if ( isalnum( c ) || c == '_' )
{
LOGEXIT( "__iscsym returning 1\n" );
PERF_EXIT(__iscsym);
return 1;
}
LOGEXIT( "__iscsym returning 0\n" );
PERF_EXIT(__iscsym);
return 0;
}
/*++
Function :
PAL_errno
Returns the address of the errno.
--*/
int * __cdecl PAL_errno( int caller )
{
int *retval;
PERF_ENTRY(errno);
ENTRY( "PAL_errno( void )\n" );
retval = (INT*)(&errno);
LOGEXIT("PAL_errno returns %p\n",retval);
PERF_EXIT(errno);
return retval;
}
/*++
Function : _putenv.
See MSDN for more details.
Note: The BSD implementation can cause
memory leaks. See man pages for more details.
--*/
int
__cdecl
_putenv( const char * envstring )
{
int ret = -1;
PERF_ENTRY(_putenv);
ENTRY( "_putenv( %p (%s) )\n", envstring ? envstring : "NULL", envstring ? envstring : "NULL") ;
if (!envstring)
{
ERROR( "_putenv() called with NULL envstring!\n");
goto EXIT;
}
ret = MiscPutenv(envstring, TRUE) ? 0 : -1;
EXIT:
LOGEXIT( "_putenv returning %d\n", ret);
PERF_EXIT(_putenv);
return ret;
}
/*++
Function : PAL_getenv
See MSDN for more details.
--*/
char * __cdecl PAL_getenv(const char *varname)
{
char *retval;
PERF_ENTRY(getenv);
ENTRY("getenv (%p (%s))\n", varname ? varname : "NULL", varname ? varname : "NULL");
if (strcmp(varname, "") == 0)
{
ERROR("getenv called with a empty variable name\n");
LOGEXIT("getenv returning NULL\n");
PERF_EXIT(getenv);
return(NULL);
}
retval = MiscGetenv(varname);
LOGEXIT("getenv returning %p\n", retval);
PERF_EXIT(getenv);
return(retval);
}
/*++
Function:
rand
The difference between the FreeBSD and Windows implementations is the max
of the return value. in FreeBSD, RAND_MAX is 0x7fffffff and in Windows
it's 0x7fff.
See MSDN for more details.
--*/
int
__cdecl
PAL_rand(void)
{
int ret;
PERF_ENTRY(rand);
ENTRY("rand(void)\n");
ret = (rand() % (PAL_RAND_MAX + 1));
LOGEXIT("rand() returning %d\n", ret);
PERF_EXIT(rand);
return ret;
}
PALIMPORT
void __cdecl
PAL_qsort(void *base, size_t nmemb, size_t size,
int (__cdecl *compar )(const void *, const void *))
{
PERF_ENTRY(qsort);
ENTRY("qsort(base=%p, nmemb=%lu, size=%lu, compar=%p\n",
base,(unsigned long) nmemb,(unsigned long) size, compar);
/* reset ENTRY nesting level back to zero, qsort will invoke app-defined
callbacks and we want their entry traces... */
#if _ENABLE_DEBUG_MESSAGES_
{
int old_level;
old_level = DBG_change_entrylevel(0);
#endif /* _ENABLE_DEBUG_MESSAGES_ */
qsort(base,nmemb,size,compar);
/* ...and set nesting level back to what it was */
#if _ENABLE_DEBUG_MESSAGES_
DBG_change_entrylevel(old_level);
}
#endif /* _ENABLE_DEBUG_MESSAGES_ */
LOGEXIT("qsort returns\n");
PERF_EXIT(qsort);
}
typedef int (__cdecl * qsort_s_comparer)(void*, const void *, const void *);
struct CompareWrapperContext
{
qsort_s_comparer real_comparer;
void* real_context;
};
static int qsort_compare_wrapper(void* context, const void* e1, const void* e2)
{
CompareWrapperContext* wrapper = (CompareWrapperContext*) context;
return wrapper->real_comparer(wrapper->real_context, e1, e2);
}
union swap8
{
char chr[8]; // optimized for Js::Var
};
union swap4
{
char chr[4]; // optimized for TypedArray
};
union swap2
{
char chr[2]; // others
};
#define swap_big(T, a, b, nsize) \
{ \
for (size_t i = 0; i < nsize; i++) \
{ \
T c = (a)[i]; \
(a)[i] = (b)[i]; \
(b)[i] = c; \
} \
}
__attribute__((no_instrument_function))
inline static void
cc_swap(void *a, void *b, const size_t size) noexcept
{
if (a == b) return;
size_t tsize;
if (size >= 8)
{
tsize = 8;
size_t nsize = size / tsize;
swap_big(swap8, (swap8*) a, (swap8*) b, nsize)
}
else if (size >= 4)
{
tsize = 4;
size_t nsize = size / tsize;
swap_big(swap4, (swap4*) a, (swap4*) b, nsize)
}
else
{
tsize = 2;
size_t nsize = size / tsize;
swap_big(swap2, (swap2*) a, (swap2*) b, nsize)
}
swap_big(char, (char*) a, (char*) b, size % tsize);
}
// Sorting Networks BEGIN *****
#define SORT2(a, b) \
if (compar(thunk, \
base + (b * size), \
base + (a * size)) < 0) \
{ \
cc_swap(base + (a * size), \
base + (b * size), size); \
}
#define SORT3() \
SORT2(0, 1) \
SORT2(1, 2) \
SORT2(0, 1)
#define SORT4() \
SORT2(0, 1) \
SORT2(2, 3) \
SORT2(0, 2) \
SORT2(1, 3) \
SORT2(1, 2)
#define SORT5(a, b, c, d, e) \
SORT2(a, b) \
SORT2(d, e) \
SORT2(c, e) \
SORT2(c, d) \
SORT2(a, d) \
SORT2(a, c) \
SORT2(b, e) \
SORT2(b, d) \
SORT2(b, c)
#define SORT6() \
SORT2(1, 2) \
SORT2(0, 2) \
SORT2(0, 1) \
SORT2(4, 5) \
SORT2(3, 5) \
SORT2(3, 4) \
SORT2(0, 3) \
SORT2(1, 4) \
SORT2(2, 5) \
SORT2(2, 4) \
SORT2(1, 3) \
SORT2(2, 3)
#define SORT7() \
SORT2(1, 2) \
SORT2(0, 2) \
SORT2(0, 1) \
SORT2(3, 4) \
SORT2(5, 6) \
SORT2(3, 5) \
SORT2(4, 6) \
SORT2(4, 5) \
SORT2(0, 4) \
SORT2(0, 3) \
SORT2(1, 5) \
SORT2(2, 6) \
SORT2(2, 5) \
SORT2(1, 3) \
SORT2(2, 4) \
SORT2(2, 3)
#define SORT8() \
SORT2(0, 1) \
SORT2(2, 3) \
SORT2(0, 2) \
SORT2(1, 3) \
SORT2(1, 2) \
SORT2(4, 5) \
SORT2(6, 7) \
SORT2(4, 6) \
SORT2(5, 7) \
SORT2(5, 6) \
SORT2(0, 4) \
SORT2(1, 5) \
SORT2(1, 4) \
SORT2(2, 6) \
SORT2(3, 7) \
SORT2(3, 6) \
SORT2(2, 4) \
SORT2(3, 5) \
SORT2(3, 4)
#define SORT9() \
SORT2(0, 1) \
SORT2(3, 4) \
SORT2(6, 7) \
SORT2(1, 2) \
SORT2(4, 5) \
SORT2(7, 8) \
SORT2(0, 1) \
SORT2(3, 4) \
SORT2(6, 7) \
SORT2(0, 3) \
SORT2(3, 6) \
SORT2(0, 3) \
SORT2(1, 4) \
SORT2(4, 7) \
SORT2(1, 4) \
SORT2(2, 5) \
SORT2(5, 8) \
SORT2(2, 5) \
SORT2(1, 3) \
SORT2(5, 7) \
SORT2(2, 6) \
SORT2(4, 6) \
SORT2(2, 4) \
SORT2(2, 3) \
SORT2(5, 6)
// Sorting Networks END *****
__attribute__((no_instrument_function))
static void
cc_qsort_r(void *base_, size_t nmemb, size_t size,
int (__cdecl *compar )(void*, const void *, const void *), void *thunk)
{
char *base = (char *) base_;
if (nmemb <= 9) // use sorting networks for members <= 2^3
{
switch(nmemb)
{
case 9:
SORT9()
break;
case 8:
SORT8()
break;
case 7:
SORT7()
break;
case 6:
SORT6()
break;
case 5:
SORT5(0, 1, 2, 3, 4)
break;
case 4:
SORT4()
break;
case 3:
SORT3()
break;
case 2:
SORT2(0, 1)
break;
default:
break; // nothing to sort
}
return;
}
// sort find for median. `compare` is expectedly more expensive than `swap`
SORT5(0, (nmemb / 4), (nmemb / 2), ((3 * nmemb) / 4), (nmemb - 1));
// guess the median is in the middle now. (from the list above)
size_t pos = 0;
size_t pivot = (nmemb - 1) * size;
// make last element the median(pivot)
cc_swap(base + pivot, base + ((nmemb / 2) * size), size);
// standard qsort pt. below
for (size_t i = 0; i < pivot; i += size)
{
if (compar(thunk, base + i, base + pivot) <= 0)
{
cc_swap(base + i, base + (pos * size), size);
pos++;
}
}
// issue the last change
cc_swap(base + (pos * size), base + pivot, size);
if (pos >= nmemb - 1) {
return; // looks like it was either all sorted OR nothing to sort
}
cc_qsort_r(base, pos++, size, compar, thunk);
cc_qsort_r(base + (pos * size), nmemb - pos, size, compar, thunk);
}
PALIMPORT
void __cdecl
PAL_qsort_s(void *base, size_t nmemb, size_t size,
int (__cdecl *compar )(void*, const void *, const void *), void* context)
{
CompareWrapperContext qsort_s_context = {0};
qsort_s_context.real_comparer = compar;
qsort_s_context.real_context = context;
cc_qsort_r(
base,
nmemb,
size,
qsort_compare_wrapper,
&qsort_s_context
);
}
PALIMPORT
void * __cdecl
PAL_bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int (__cdecl *compar)(const void *, const void *))
{
void *retval;
PERF_ENTRY(bsearch);
ENTRY("bsearch(key=%p, base=%p, nmemb=%lu, size=%lu, compar=%p\n",
key, base, (unsigned long) nmemb, (unsigned long) size, compar);
/* reset ENTRY nesting level back to zero, bsearch will invoke app-defined
callbacks and we want their entry traces... */
#if _ENABLE_DEBUG_MESSAGES_
{
int old_level;
old_level = DBG_change_entrylevel(0);
#endif /* _ENABLE_DEBUG_MESSAGES_ */
retval = bsearch(key,base,nmemb,size,compar);
/* ...and set nesting level back to what it was */
#if _ENABLE_DEBUG_MESSAGES_
DBG_change_entrylevel(old_level);
}
#endif /* _ENABLE_DEBUG_MESSAGES_ */
LOGEXIT("bsearch returns %p\n",retval);
PERF_EXIT(bsearch);
return retval;
}
/*++
Function:
MiscGetEnvArray
Get a reference to the process's environ array into palEnvironment
NOTE: This function MUST be called while holding the gcsEnvironment
critical section (except if the caller is the initialization
routine)
--*/
void
MiscGetEnvArray(void)
{
#if HAVE__NSGETENVIRON
palEnvironment = *(_NSGetEnviron());
#else // HAVE__NSGETENVIRON
extern char **environ;
palEnvironment = environ;
#endif // HAVE__NSGETENVIRON
}
/*++
Function:
MiscSetEnvArray
Make sure the process's environ array is in sync with palEnvironment variable
NOTE: This function MUST be called while holding the gcsEnvironment
critical section (except if the caller is the initialization
routine)
--*/
void
MiscSetEnvArray(void)
{
#if HAVE__NSGETENVIRON
*(_NSGetEnviron()) = palEnvironment;
#else // HAVE__NSGETENVIRON
extern char **environ;
environ = palEnvironment;
#endif // HAVE__NSGETENVIRON
}
/*++
Function:
MiscInitialize
Initialization function called from PAL_Initialize.
Allocates the TLS Index. On systems that use extern variables for
time zone information, this also initializes those variables.
Note: This is called before debug channels are initialized, so it
cannot use debug tracing calls.
--*/
BOOL
MiscInitialize(void)
{
InternalInitializeCriticalSection(&gcsEnvironment);
MiscGetEnvArray();
return TRUE;
}
/*++
Function:
MiscCleanup
Termination function called from PAL_Terminate to delete the
TLS Keys created in MiscInitialize
--*/
void MiscCleanup(void)
{
TRACE("Cleaning Misc...\n");
InternalDeleteCriticalSection(&gcsEnvironment);
}
int compare(const char *left, const char *right, int *last_index)
{
int i;
int result = 0;
for(i = 0; left[i] != 0 && result == 0 && i < INT_MAX; i++)
{
result = (left[i] == right[i]) ? 0 : (left[i] < right[i] ? -1 : 1);
}
*last_index = i;
return result;
}
/*++
Function:
MiscGetenv
Gets an environment variable's value from environ. The returned buffer
must not be modified or freed.
--*/
char *MiscGetenv(const char *name)
{
int i, length;
char *equals;
char *pRet = NULL;
CPalThread * pthrCurrent = InternalGetCurrentThread();
InternalEnterCriticalSection(pthrCurrent, &gcsEnvironment);
if (palEnvironment)
{
for(i = 0; palEnvironment[i] != NULL; i++)
{
if (compare(palEnvironment[i], name, &length) == 0)
{
equals = palEnvironment[i] + length;
if (*equals == '\0')
{
pRet = (char *) "";
goto done;
}
else if (*equals == '=')
{
pRet = equals + 1;
goto done;
}
}
}
}
done:
InternalLeaveCriticalSection(pthrCurrent, &gcsEnvironment);
return pRet;
}
/*++
Function:
MiscPutenv
Sets an environment variable's value by directly modifying palEnvironment.
Returns TRUE if the variable was set, or FALSE if PAL_malloc or realloc
failed or if the given string is malformed.
--*/
BOOL MiscPutenv(const char *string, BOOL deleteIfEmpty)
{
const char *equals, *existingEquals;
char *copy = NULL;
int length;
int i, j;
bool fOwningCS = false;
BOOL result = FALSE;
CPalThread * pthrCurrent = InternalGetCurrentThread();
equals = strchr(string, '=');
if (equals == string || equals == NULL)
{
// "=foo" and "foo" have no meaning
goto done;
}
if (equals[1] == '\0' && deleteIfEmpty)
{
// "foo=" removes foo from the environment in _putenv() on Windows.
// The same string can result from a call to SetEnvironmentVariable()
// with the empty string as the value, but in that case we want to
// set the variable's value to "". deleteIfEmpty will be FALSE in
// that case.
length = strlen(string);
copy = (char *) InternalMalloc(length);
if (copy == NULL)
{
goto done;
}
memcpy(copy, string, length - 1);
copy[length - 1] = '\0'; // Change '=' to '\0'
MiscUnsetenv(copy);
result = TRUE;
}
else
{
// See if we are replacing an item or adding one.
// Make our copy up front, since we'll use it either way.
copy = InternalStrdup(string);
if (copy == NULL)
{
goto done;
}
length = equals - string;
InternalEnterCriticalSection(pthrCurrent, &gcsEnvironment);
fOwningCS = true;
for(i = 0; palEnvironment[i] != NULL; i++)
{
existingEquals = strchr(palEnvironment[i], '=');
if (existingEquals == NULL)
{
// The PAL screens out malformed strings, but
// environ comes from the system, so it might
// have strings without '='. We treat the entire
// string as a name in that case.
existingEquals = palEnvironment[i] + strlen(palEnvironment[i]);
}
if (existingEquals - palEnvironment[i] == length)
{
if (memcmp(string, palEnvironment[i], length) == 0)
{
// Replace this one. Don't free the original,
// though, because there may be outstanding
// references to it that were acquired via
// getenv. This is an unavoidable memory leak.
palEnvironment[i] = copy;
// Set 'copy' to NULL so it won't be freed
copy = NULL;
result = TRUE;
break;
}
}
}
if (palEnvironment[i] == NULL)
{
static BOOL sAllocatedEnviron = FALSE;
// Add a new environment variable.
// We'd like to realloc palEnvironment, but we can't do that the
// first time through.
char **newEnviron = NULL;
if (sAllocatedEnviron) {
if (NULL == (newEnviron =
(char **)InternalRealloc(palEnvironment, (i + 2) * sizeof(char *))))
{
goto done;
}
}
else
{
// Allocate palEnvironment ourselves so we can realloc it later.
newEnviron = (char **)InternalMalloc((i + 2) * sizeof(char *));
if (newEnviron == NULL)
{
goto done;
}
for(j = 0; palEnvironment[j] != NULL; j++)
{
newEnviron[j] = palEnvironment[j];
}
sAllocatedEnviron = TRUE;
}
palEnvironment = newEnviron;
MiscSetEnvArray();
palEnvironment[i] = copy;
palEnvironment[i + 1] = NULL;
// Set 'copy' to NULL so it won't be freed
copy = NULL;
result = TRUE;
}
}
done:
if (fOwningCS)
{
InternalLeaveCriticalSection(pthrCurrent, &gcsEnvironment);
}
if (NULL != copy)
{
InternalFree(copy);
}
return result;
}
/*++
Function:
MiscUnsetenv
Removes a variable from the environment. Does nothing if the variable
does not exist in the environment.
--*/
void MiscUnsetenv(const char *name)
{
const char *equals;
int length;
int i, j;
CPalThread * pthrCurrent = InternalGetCurrentThread();
length = strlen(name);
InternalEnterCriticalSection(pthrCurrent, &gcsEnvironment);
for(i = 0; palEnvironment[i] != NULL; i++)
{
equals = strchr(palEnvironment[i], '=');
if (equals == NULL)
{
equals = palEnvironment[i] + strlen(palEnvironment[i]);
}
if (equals - palEnvironment[i] == length)
{
if (memcmp(name, palEnvironment[i], length) == 0)
{
// Remove this one. Don't free it, though, since
// there might be oustanding references to it that
// were acquired via getenv. This is an
// unavoidable memory leak.
for(j = i + 1; palEnvironment[j] != NULL; j++) { }
// i is now the one we want to remove. j is the
// last index in palEnvironment, which is NULL.
// Shift palEnvironment down by the difference between i and j.
memmove(palEnvironment + i, palEnvironment + i + 1, (j - i) * sizeof(char *));
}
}
}
InternalLeaveCriticalSection(pthrCurrent, &gcsEnvironment);
}