malloc.c revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2009 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
void _STUB_malloc(){}
#else
#if _UWIN
#define calloc ______calloc
#define _ast_free ______free
#define malloc ______malloc
#define mallinfo ______mallinfo
#define mallopt ______mallopt
#define mstats ______mstats
#define realloc ______realloc
#define _STDLIB_H_ 1
extern int atexit(void(*)(void));
extern char* getenv(const char*);
#endif
#include "vmhdr.h"
#include <errno.h>
#if _UWIN
#include <malloc.h>
#define _map_malloc 1
#define _mal_alloca 1
#define calloc _ast_calloc
#define malloc _ast_malloc
typedef struct ______mallinfo Mallinfo_t;
typedef struct ______mstats Mstats_t;
#define realloc _ast_realloc
#endif
#if __STD_C
#else
#endif
/*
* define _AST_std_malloc=1 to force the standard malloc
* if _map_malloc is also defined then _ast_malloc etc.
* will simply call malloc etc.
*/
#if !defined(_AST_std_malloc) && __CYGWIN__
#define _AST_std_malloc 1
#endif
/* malloc compatibility functions.
** VMETHOD: select an allocation method by name.
**
** VMPROFILE: if is a file name, write profile data to it.
** VMTRACE: if is a file name, write trace data to it.
** The pattern %p in a file name will be replaced by the process ID.
**
** VMDEBUG:
** a: abort on any warning.
** w[decimal]: file descriptor for warnings.
** [decimal]: period to check arena.
** 0x[hexadecimal]: address to watch.
**
** Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
*/
#if _sys_stat
#endif
#include <fcntl.h>
#ifdef S_IRUSR
#else
#define CREAT_MODE 0644
#endif
#if !_map_malloc
#endif
#if _WINIX
#include <ast_windows.h>
#if _UWIN
extern int _sigblock(void);
extern void _sigunblock(int);
extern unsigned long _record[2048];
{
register unsigned long v = ((unsigned long)p)>>16;
return p;
}
#else
#define getenv(s) lcl_getenv(s)
static char*
lcl_getenv(const char* s)
{
int n;
static char buf[512];
return 0;
return buf;
}
#endif /* _UWIN */
#endif /* _WINIX */
#ifndef VMRECORD
#define VMRECORD(p) (p)
#define VMBLOCK
#define VMUNBLOCK
#endif
#if defined(__EXPORT__)
#define extern extern __EXPORT__
#endif
#if __STD_C
#else
char** sp;
#endif
{
char* s = *sp;
Vmulong_t v = 0;
if(s[0] == '0' && (s[1] == 'x' || s[1] == 'X') )
{ for(s += 2; *s; ++s)
{ if(*s >= '0' && *s <= '9')
v = (v << 4) + (*s - '0');
else if(*s >= 'a' && *s <= 'f')
v = (v << 4) + (*s - 'a') + 10;
else if(*s >= 'A' && *s <= 'F')
v = (v << 4) + (*s - 'A') + 10;
else break;
}
}
else
{ for(; *s; ++s)
{ if(*s >= '0' && *s <= '9')
v = v*10 + (*s - '0');
else break;
}
}
*sp = s;
return v;
}
static int _Vmflinit = 0;
static Vmulong_t _Vmdbstart = 0;
static Vmulong_t _Vmdbcheck = 0;
static int _Vmpffd = -1;
#define VMFLINIT() \
if(_Vmdbcheck) \
} \
}
#if __STD_C
#else
char* begs;
char* ends;
#endif
{ int pid;
char* s;
return NIL(char*);
s = ends;
do
{ if(s == begs)
return NIL(char*);
} while((pid /= 10) > 0);
while(s < ends)
*begs++ = *s++;
return begs;
}
#if __STD_C
static int createfile(char* file)
#else
static int createfile(file)
char* file;
#endif
{
char buf[1024];
while(*file)
{ if(*file == '%')
{ switch(file[1])
{
case 'p' :
return -1;
file += 2;
break;
default :
goto copy;
}
}
else
{ copy:
}
return -1;
}
*next = '\0';
#if _PACKAGE_ast
{ int fd;
#ifdef FD_CLOEXEC
if (fd >= 0)
#endif
return fd;
}
#else
#endif
}
#if __STD_C
static void pfprint(void)
#else
static void pfprint()
#endif
{
}
#if __STD_C
static int vmflinit(void)
#else
static int vmflinit()
#endif
{
char* env;
int fd;
char* file;
int line;
/* this must be done now to avoid any inadvertent recursion (more below) */
_Vmflinit = 1;
/* if getenv() calls malloc(), the eventual region may not see this */
}
while(*env)
{ if(*env == 'a')
env += 1;
}
else if(*env =='w')
{ env += 1;
}
env += 1;
}
else
setcheck = 1;
if(*env == ',')
{ env += 1;
}
}
}
if(!setcheck)
_Vmdbcheck = 1;
}
}
if(!vm)
}
/* slip in the new region now so that malloc() will work fine */
if(vm)
/* turn on tracing if requested */
}
/* make sure that profile data is output upon exiting */
{ if(_Vmpffd < 0)
_Vmpffd = 2;
/* this may wind up calling malloc(), but region is ok now */
}
else if(_Vmpffd >= 0)
_Vmpffd = -1;
}
/* reset file and line number to correct values for the call */
return 0;
}
#if __STD_C
#else
#endif
{
VMFLINIT();
}
#if __STD_C
#else
#endif
{
VMFLINIT();
}
#if __STD_C
#else
#endif
{
#if USE_NATIVE
#if __STD_C
#else
#endif
#endif
VMFLINIT();
#if _PACKAGE_ast
#if !USE_NATIVE
#endif
{
#if USE_NATIVE
#else
#endif
}
#endif
#if USE_NATIVE
return newdata;
}
#else
#endif
}
#if __STD_C
#else
#endif
{
#if USE_NATIVE
#if __STD_C
#else
extern void free();
#endif
#endif
VMFLINIT();
#if _PACKAGE_ast
#if !USE_NATIVE
#endif
{
#if USE_NATIVE
#endif
return;
}
#endif
#if USE_NATIVE
#else
#endif
}
#if __STD_C
#else
#endif
{
}
#if __STD_C
#else
#endif
{
VMFLINIT();
return addr;
}
#if __STD_C
#else
#endif
{
return EINVAL;
return ENOMEM;
return 0;
}
#if __STD_C
#else
#endif
{
VMFLINIT();
}
#if __STD_C
#else
#endif
{
VMFLINIT();
}
#if !_PACKAGE_ast
#if __STD_C
char* strdup(const char* s)
#else
char* strdup(s)
char* s;
#endif
{
char *ns;
size_t n;
if(!s)
return NIL(char*);
else
{ n = strlen(s);
return ns;
}
}
#endif /* _PACKAGE_ast */
#if !_lib_alloca || _mal_alloca
#ifndef _stk_down
#define _stk_down 0
#endif
union _alloca_u
{ struct
{ char* addr;
} head;
};
struct _alloca_s
};
#if __STD_C
#else
#endif
char* file;
int line;
VMFLINIT();
while(Frame)
{ f = Frame;
}
else break;
}
Frame = f;
}
#endif /*!_lib_alloca || _mal_alloca*/
#if _map_malloc
/* not sure of all the implications -- 0 is conservative for now */
#define USE_NATIVE 0 /* native free/realloc on non-vmalloc ptrs */
#else
/* intercept _* __* __libc_* variants */
#if __lib__malloc
#if _lib_memalign
#endif
#if _lib_pvalloc
#endif
#if _lib_valloc
#endif
#endif
#if _lib___malloc
#if _lib_memalign
#endif
#if _lib_pvalloc
#endif
#if _lib_valloc
#endif
#endif
#if _lib_memalign
#endif
#if _lib_pvalloc
#endif
#if _lib_valloc
#endif
#endif
#endif /* _map_malloc */
#undef extern
#if _hdr_malloc /* need the mallint interface for statistics, etc. */
#define calloc ______calloc
#define cfree ______cfree
#define free ______free
#define malloc ______malloc
#define pvalloc ______pvalloc
#define realloc ______realloc
#define valloc ______valloc
#if !_UWIN
#include <malloc.h>
typedef struct mallinfo Mallinfo_t;
#endif
#if defined(__EXPORT__)
#define extern __EXPORT__
#endif
#if _lib_mallopt
#if __STD_C
#else
int cmd;
int value;
#endif
{
VMFLINIT();
return 0;
}
#endif /*_lib_mallopt*/
#if _lib_mallinfo && _mem_arena_mallinfo
#if __STD_C
extern Mallinfo_t mallinfo(void)
#else
extern Mallinfo_t mallinfo()
#endif
{
VMFLINIT();
}
return mi;
}
#endif /* _lib_mallinfo */
#if __STD_C
#else
#endif
{
VMFLINIT();
}
return ms;
}
#endif /*_lib_mstats*/
#undef extern
#endif/*_hdr_malloc*/
#else
/*
* even though there is no malloc override, still provide
* _ast_* counterparts for object compatibility
*/
#if _lib_memalign
#endif
#if _lib_pvalloc
#endif
#if _lib_valloc
#endif
#if defined(__EXPORT__)
#define extern __EXPORT__
#endif
#if _lib_memalign
#endif
#if _lib_pvalloc
#endif
#if _lib_valloc
#endif
#undef extern
#if _hdr_malloc
#define calloc ______calloc
#define cfree ______cfree
#define free ______free
#define malloc ______malloc
#define pvalloc ______pvalloc
#define realloc ______realloc
#define valloc ______valloc
#if !_UWIN
#include <malloc.h>
typedef struct mallinfo Mallinfo_t;
#endif
#if defined(__EXPORT__)
#define extern __EXPORT__
#endif
#if _lib_mallopt
#endif
#if _lib_mallinfo && _mem_arena_mallinfo
#endif
#endif
#undef extern
#endif /*_hdr_malloc*/
#endif /*!_std_malloc*/
#endif /*_UWIN*/