malloc.c revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/***********************************************************************
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* This software is part of the ast package *
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz* Copyright (c) 1985-2009 AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* and is licensed under the *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Common Public License, Version 1.0 *
7c2fbfb345896881c631598ee3852ce9ce33fb07April Chin* by AT&T Intellectual Property *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* A copy of the License is available at *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Information and Software Systems Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* AT&T Research *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Florham Park NJ *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Glenn Fowler <gsf@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* David Korn <dgk@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin* Phong Vo <kpv@research.att.com> *
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin***********************************************************************/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern int atexit(void(*)(void));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern char* getenv(const char*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * define _AST_std_malloc=1 to force the standard malloc
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * if _map_malloc is also defined then _ast_malloc etc.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * will simply call malloc etc.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* malloc compatibility functions.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** These are aware of debugging/profiling and driven by the environment variables:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** VMETHOD: select an allocation method by name.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** VMPROFILE: if is a file name, write profile data to it.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** VMTRACE: if is a file name, write trace data to it.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** The pattern %p in a file name will be replaced by the process ID.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** VMDEBUG:
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** a: abort on any warning.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** w[decimal]: file descriptor for warnings.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** [decimal]: period to check arena.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** 0x[hexadecimal]: address to watch.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin** Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern int _sigblock(void);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern void _sigunblock(int);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin register unsigned long v = ((unsigned long)p)>>16;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic char*
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinlcl_getenv(const char* s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (!(n = GetEnvironmentVariable(s, buf, sizeof(buf))) || n > sizeof(buf))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _UWIN */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _WINIX */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define VMRECORD(p) (p)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define extern extern __EXPORT__
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin char* s = *sp;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { for(s += 2; *s; ++s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { for(; *s; ++s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int _Vmflinit = 0;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if((_Vmdbtime += 1) < _Vmdbstart) _Vmdbtime = _Vmdbstart; \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if(_Vmdbtime >= _Vmdbstart && (_Vmdbtime % _Vmdbcheck) == 0 && \
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(char*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(s == begs)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(char*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin while(s < ends)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin *begs++ = *s++;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin case 'p' :
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return -1;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void pfprint(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic void pfprint()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int vmflinit(void)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinstatic int vmflinit()
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* this must be done now to avoid any inadvertent recursion (more below) */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* if getenv() calls malloc(), the eventual region may not see this */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(strcmp(env,"Vmdebug") == 0 || strcmp(env,"vmdebug") == 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(strcmp(env,"Vmprofile") == 0 || strcmp(env,"vmprofile") == 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(strcmp(env,"Vmlast") == 0 || strcmp(env,"vmlast") == 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(strcmp(env,"Vmbest") == 0 || strcmp(env,"vmbest") == 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(env[0] == '0' && (env[1] == 'x' || env[1] == 'X') )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* slip in the new region now so that malloc() will work fine */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* turn on tracing if requested */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((env = getenv("VMTRACE")) && env[0] && (fd = createfile(env)) >= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* make sure that profile data is output upon exiting */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* this may wind up calling malloc(), but region is ok now */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else if(_Vmpffd >= 0)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin /* reset file and line number to correct values for the call */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* calloc(reg size_t n_obj, reg size_t s_obj)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return VMRECORD((*Vmregion->meth.resizef)(Vmregion,NIL(Void_t*),n_obj*s_obj,VM_RSZERO));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return VMRECORD((*Vmregion->meth.allocf)(Vmregion,size));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* realloc(reg Void_t* data, reg size_t size)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if((newdata = (*Vmregion->meth.allocf)(Vmregion,size)) )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin if (newdata = (*Vmregion->meth.resizef)(Vmregion,data,size,VM_RSCOPY|VM_RSMOVE))
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return VMRECORD((*Vmregion->meth.resizef)(Vmregion,data,size,VM_RSCOPY|VM_RSMOVE));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin extern void free();
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* memalign(reg size_t align, reg size_t size)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin addr = VMRECORD((*Vmregion->meth.alignf)(Vmregion,size,align));
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainzextern int posix_memalign(reg Void_t **memptr, reg size_t align, reg size_t size)
34f9b3eef6fdadbda0a846aa4d68691ac40eace5Roland Mainz if(align == 0 || (align%sizeof(Void_t*)) != 0 || ((align-1)&align) != 0 )
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return VMRECORD((*Vmregion->meth.alignf)(Vmregion,size,_Vmpagesize));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return VMRECORD((*Vmregion->meth.alignf)(Vmregion,ROUND(size,_Vmpagesize),_Vmpagesize));
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinchar* strdup(const char* s)
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin return NIL(char*);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _PACKAGE_ast */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin { if(( _stk_down && &array[0] > Frame->head.head.addr) ||
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin else break;
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin f = (Alloca_t*)(*Vmregion->meth.allocf)(Vmregion,size+sizeof(Alloca_t)-1);
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*!_lib_alloca || _mal_alloca*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* not sure of all the implications -- 0 is conservative for now */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define USE_NATIVE 0 /* native free/realloc on non-vmalloc ptrs */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin/* intercept _* __* __libc_* variants */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_calloc, size_t,n, size_t,m) { return calloc(n, m); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_malloc, size_t,n) { return malloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_memalign, size_t,a, size_t,n) { return memalign(a, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_pvalloc, size_t,n) { return pvalloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_realloc, Void_t*,p, size_t,n) { return realloc(p, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_valloc, size_t,n) { return valloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__calloc, size_t,n, size_t,m) { return calloc(n, m); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__malloc, size_t,n) { return malloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__memalign, size_t,a, size_t,n) { return memalign(a, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__pvalloc, size_t,n) { return pvalloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__realloc, Void_t*,p, size_t,n) { return realloc(p, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__valloc, size_t,n) { return valloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__libc_calloc, size_t,n, size_t,m) { return calloc(n, m); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__libc_malloc, size_t,n) { return malloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__libc_memalign, size_t,a, size_t,n) { return memalign(a, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__libc_pvalloc, size_t,n) { return pvalloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(__libc_realloc, Void_t*,p, size_t,n) { return realloc(p, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(__libc_valloc, size_t,n) { return valloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _map_malloc */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#if _hdr_malloc /* need the mallint interface for statistics, etc. */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define extern __EXPORT__
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*_lib_mallopt*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /* _lib_mallinfo */
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*_lib_mstats*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif/*_hdr_malloc*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * even though there is no malloc override, still provide
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin * _ast_* counterparts for object compatibility
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define extern __EXPORT__
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_ast_calloc, size_t,n, size_t,m) { return calloc(n, m); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_ast_malloc, size_t,n) { return malloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_ast_memalign, size_t,a, size_t,n) { return memalign(a, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_ast_pvalloc, size_t,n) { return pvalloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F2(_ast_realloc, Void_t*,p, size_t,n) { return realloc(p, n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Void_t* F1(_ast_valloc, size_t,n) { return valloc(n); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#define extern __EXPORT__
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern int F2(_ast_mallopt, int,cmd, int,value) { return mallopt(cmd, value); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Mallinfo_t F0(_ast_mallinfo, void) { return mallinfo(); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chinextern Mstats_t F0(_ast_mstats, void) { return mstats(); }
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*_hdr_malloc*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*!_std_malloc*/
da2e3ebdc1edfbc5028edf1354e7dd2fa69a7968chin#endif /*_UWIN*/