environment: set-os-envs leaks memory on unix systems. provide set-os-envs-pointer in case this is unacceptable.

db4
Doug Coleman 2012-08-25 18:37:50 -07:00
parent 6c4043bb25
commit e6dc36b073
3 changed files with 20 additions and 4 deletions

View File

@ -54,10 +54,18 @@ HELP: os-envs
HELP: set-os-envs HELP: set-os-envs
{ $values { "assoc" "an association mapping strings to strings" } } { $values { "assoc" "an association mapping strings to strings" } }
{ $description "Replaces the current set of environment variables." } { $description "Replaces the current set of environment variables." }
{ $warning "Leaks memory on Unix. If your program calls this function repeatedly, call " { $link set-os-envs-pointer } " with a malloced pointer and manage your memory instead." }
{ $notes { $notes
"Names and values of environment variables are operating system-specific. Windows NT allows values up to 32766 characters in length." "Names and values of environment variables are operating system-specific. Windows NT allows values up to 32766 characters in length."
} ; } ;
HELP: set-os-envs-pointer
{ $values { "malloc" "a pointer to memory from the heap obtained through " { $link malloc } " or similar" } }
{ $description "Set then " { $link environ } " pointer. Factor must retain a pointer to this memory until exiting the program." }
{ $notes
"Names and values of environment variables are operating system-specific."
} ;
HELP: set-os-env HELP: set-os-env
{ $values { "value" string } { "key" string } } { $values { "value" string } { "key" string } }
{ $description "Set an environment variable." } { $description "Set an environment variable." }
@ -72,7 +80,7 @@ HELP: unset-os-env
"Names and values of environment variables are operating system-specific." "Names and values of environment variables are operating system-specific."
} ; } ;
{ os-env os-envs set-os-env unset-os-env set-os-envs change-os-env } related-words { os-env os-envs set-os-env unset-os-env set-os-envs set-os-envs-pointer change-os-env } related-words
ARTICLE: "environment" "Environment variables" ARTICLE: "environment" "Environment variables"
@ -88,6 +96,10 @@ ARTICLE: "environment" "Environment variables"
unset-os-env unset-os-env
set-os-envs set-os-envs
change-os-env change-os-env
}
"Leak-free setting of all environment variables on Unix:"
{ $subsections
set-os-envs-pointer
} ; } ;
ABOUT: "environment" ABOUT: "environment"

View File

@ -1,7 +1,7 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: assocs combinators kernel sequences splitting system USING: assocs combinators init kernel sequences splitting
vocabs init vocabs.loader ; system vocabs vocabs.loader ;
IN: environment IN: environment
HOOK: os-env os ( key -- value ) HOOK: os-env os ( key -- value )
@ -14,6 +14,8 @@ HOOK: (os-envs) os ( -- seq )
HOOK: (set-os-envs) os ( seq -- ) HOOK: (set-os-envs) os ( seq -- )
HOOK: set-os-envs-pointer os ( malloc -- )
: change-os-env ( key quot -- ) : change-os-env ( key quot -- )
[ [ os-env ] keep ] dip dip set-os-env ; inline [ [ os-env ] keep ] dip dip set-os-env ; inline

View File

@ -21,8 +21,10 @@ M: unix (os-envs) ( -- seq )
: set-void* ( value alien -- ) 0 set-alien-cell ; : set-void* ( value alien -- ) 0 set-alien-cell ;
M: unix set-os-envs-pointer ( malloc -- ) environ set-void* ;
M: unix (set-os-envs) ( seq -- ) M: unix (set-os-envs) ( seq -- )
utf8 strings>alien malloc-byte-array environ set-void* ; utf8 strings>alien malloc-byte-array set-os-envs-pointer ;
os { os {
{ macosx [ "environment.unix.macosx" require ] } { macosx [ "environment.unix.macosx" require ] }