/* argsEnv - set up command line argument array and * environment dir. */ #include "common.h" #include "errabort.h" #include "memalloc.h" #include "dystring.h" #include "portable.h" #include "pthreadWrap.h" #include "../compiler/pfPreamble.h" #include "runType.h" #include "object.h" #include "pfString.h" #include "initVar.h" static char **cl_env; static struct memHandler *parentMem; static pthread_mutex_t memMutex; static void *threadSafeAlloc(size_t size) /* Wrap mutex lock around allocation. */ { void *result; pthreadMutexLock(&memMutex); result = parentMem->alloc(size); pthreadMutexUnlock(&memMutex); return result; } static void threadSafeFree(void *vpt) /* Wrap mutex lock around free. */ { pthreadMutexLock(&memMutex); parentMem->free(vpt); pthreadMutexUnlock(&memMutex); } static void *threadSafeRealloc(void *vpt, size_t size) /* Wrap mutex lock around realloc. */ { void *result; pthreadMutexLock(&memMutex); result = parentMem->realloc(vpt, size); pthreadMutexUnlock(&memMutex); return result; } static struct memHandler threadSafeMemHandler = /* Default memory handler. */ { NULL, threadSafeAlloc, threadSafeFree, threadSafeRealloc, }; void _pf_init_mem() /* Initialize thread safe memory handler. */ { #ifdef UNNEEDED pthreadMutexInit(&memMutex); parentMem = pushMemHandler(&threadSafeMemHandler); #endif /* UNNEEDED */ } void *_pf_need_mem(int size) /* Allocate memory, which is initialized to zero. */ { return needMem(size); } void _pf_free_mem(void *pt) /* Free memory. */ { return freeMem(pt); } void _pf_clear_mem(void *pt, int size) /* Clear memory. */ { memset(pt, 0, size); } void _pf_init_args(int argc, char **argv, _pf_String *retProg, _pf_Array *retArgs, char *environ[]) /* Set up command line arguments. */ { _pf_Array args; int stringTypeId = _pf_find_string_type_id(); int i; *retProg = _pf_string_from_const(argv[0]); struct _pf_string **a; args = _pf_dim_array(argc-1, stringTypeId); a = (struct _pf_string **)(args->elements); for (i=1; ielements); for (i=0; istring, dy->bufSize); string->size = dy->stringSize; dyStringCannibalize(&dy); stack[0].String = string; } } void pf_milliTicks(_pf_Stack *stack) /* Return a long value that counts up pretty * quickly. Units are milliseconds. It may * either be starting since program startup or * relative to some other absolute number depending * on the system it is running on. */ { stack[0].Long = clock1000(); }