runtime: don't call __go_alloc/__go_free in environment functions

Reviewed-on: https://go-review.googlesource.com/33363

From-SVN: r242594
This commit is contained in:
Ian Lance Taylor 2016-11-18 16:03:13 +00:00
parent d519aeda8b
commit d6255159e8
3 changed files with 22 additions and 34 deletions

View File

@ -1,4 +1,4 @@
2ab785788691ad289f838a0b3a6bc9013d0fc337 fc4ca600b2fc6de81fd3c4014542d6a50593db1a
The first line of this file holds the git revision number of the last The first line of this file holds the git revision number of the last
merge done from the gofrontend repository. merge done from the gofrontend repository.

View File

@ -9,10 +9,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include "go-alloc.h"
#include "runtime.h" #include "runtime.h"
#include "arch.h"
#include "malloc.h"
/* Set the C environment from Go. This is called by syscall.Setenv. */ /* Set the C environment from Go. This is called by syscall.Setenv. */
@ -25,7 +22,6 @@ setenv_c (String k, String v)
unsigned char *kn; unsigned char *kn;
const byte *vs; const byte *vs;
unsigned char *vn; unsigned char *vn;
intgo len;
ks = k.str; ks = k.str;
if (ks == NULL) if (ks == NULL)
@ -39,25 +35,23 @@ setenv_c (String k, String v)
#ifdef HAVE_SETENV #ifdef HAVE_SETENV
if (ks != NULL && ks[k.len] != 0) if (ks[k.len] != 0)
{ {
// Objects that are explicitly freed must be at least 16 bytes in size, kn = malloc (k.len + 1);
// so that they are not allocated using tiny alloc. if (kn == NULL)
len = k.len + 1; runtime_throw ("out of malloc memory");
if (len < TinySize)
len = TinySize;
kn = __go_alloc (len);
__builtin_memcpy (kn, ks, k.len); __builtin_memcpy (kn, ks, k.len);
kn[k.len] = '\0';
ks = kn; ks = kn;
} }
if (vs != NULL && vs[v.len] != 0) if (vs[v.len] != 0)
{ {
len = v.len + 1; vn = malloc (v.len + 1);
if (len < TinySize) if (vn == NULL)
len = TinySize; runtime_throw ("out of malloc memory");
vn = __go_alloc (len);
__builtin_memcpy (vn, vs, v.len); __builtin_memcpy (vn, vs, v.len);
vn[v.len] = '\0';
vs = vn; vs = vn;
} }
@ -66,19 +60,20 @@ setenv_c (String k, String v)
#else /* !defined(HAVE_SETENV) */ #else /* !defined(HAVE_SETENV) */
len = k.len + v.len + 2; len = k.len + v.len + 2;
if (len < TinySize) kn = malloc (len);
len = TinySize; if (kn == NULL)
kn = __go_alloc (len); runtime_throw ("out of malloc memory");
__builtin_memcpy (kn, ks, k.len); __builtin_memcpy (kn, ks, k.len);
kn[k.len] = '='; kn[k.len] = '=';
__builtin_memcpy (kn + k.len + 1, vs, v.len); __builtin_memcpy (kn + k.len + 1, vs, v.len);
kn[k.len + v.len + 1] = '\0'; kn[k.len + v.len + 1] = '\0';
putenv ((char *) kn); putenv ((char *) kn);
kn = NULL; /* putenv takes ownership of the string. */
#endif /* !defined(HAVE_SETENV) */ #endif /* !defined(HAVE_SETENV) */
if (kn != NULL) if (kn != NULL)
__go_free (kn); free (kn);
if (vn != NULL) if (vn != NULL)
__go_free (vn); free (vn);
} }

View File

@ -9,10 +9,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include "go-alloc.h"
#include "runtime.h" #include "runtime.h"
#include "arch.h"
#include "malloc.h"
/* Unset an environment variable from Go. This is called by /* Unset an environment variable from Go. This is called by
syscall.Unsetenv. */ syscall.Unsetenv. */
@ -24,7 +21,6 @@ unsetenv_c (String k)
{ {
const byte *ks; const byte *ks;
unsigned char *kn; unsigned char *kn;
intgo len;
ks = k.str; ks = k.str;
if (ks == NULL) if (ks == NULL)
@ -33,14 +29,11 @@ unsetenv_c (String k)
#ifdef HAVE_UNSETENV #ifdef HAVE_UNSETENV
if (ks != NULL && ks[k.len] != 0) if (ks[k.len] != 0)
{ {
// Objects that are explicitly freed must be at least 16 bytes in size, kn = malloc (k.len + 1);
// so that they are not allocated using tiny alloc. if (kn == NULL)
len = k.len + 1; runtime_throw ("out of malloc memory");
if (len < TinySize)
len = TinySize;
kn = __go_alloc (len);
__builtin_memcpy (kn, ks, k.len); __builtin_memcpy (kn, ks, k.len);
ks = kn; ks = kn;
} }
@ -50,5 +43,5 @@ unsetenv_c (String k)
#endif /* !defined(HAVE_UNSETENV) */ #endif /* !defined(HAVE_UNSETENV) */
if (kn != NULL) if (kn != NULL)
__go_free (kn); free (kn);
} }