#define _MALLOC_INTERNAL
#ifdef HAVE_GTK_AND_PTHREAD
#define USE_PTHREAD
#endif
#ifndef _MALLOC_H
#define _MALLOC_H 1
#ifdef _MALLOC_INTERNAL
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if ((defined __cplusplus || (defined (__STDC__) && __STDC__) \
|| defined STDC_HEADERS || defined PROTOTYPES) \
&& ! defined (BROKEN_PROTOTYPES))
#undef PP
#define PP(args) args
#undef __ptr_t
#define __ptr_t void *
#else
#undef PP
#define PP(args) ()
#undef __ptr_t
#define __ptr_t char *
#endif
#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
#include <string.h>
#else
#ifndef memset
#define memset(s, zero, n) bzero ((s), (n))
#endif
#ifndef memcpy
#define memcpy(d, s, n) bcopy ((s), (d), (n))
#endif
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifndef CHAR_BIT
#define CHAR_BIT 8
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef USE_PTHREAD
#include <pthread.h>
#endif
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef STDC_HEADERS
#include <stddef.h>
#define __malloc_size_t size_t
#define __malloc_ptrdiff_t ptrdiff_t
#else
#ifdef __GNUC__
#include <stddef.h>
#ifdef __SIZE_TYPE__
#define __malloc_size_t __SIZE_TYPE__
#endif
#endif
#ifndef __malloc_size_t
#define __malloc_size_t unsigned int
#endif
#define __malloc_ptrdiff_t int
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef FREE_RETURN_TYPE
#define FREE_RETURN_TYPE void
#endif
extern __ptr_t malloc PP ((__malloc_size_t __size));
extern __ptr_t realloc PP ((__ptr_t __ptr, __malloc_size_t __size));
extern __ptr_t calloc PP ((__malloc_size_t __nmemb, __malloc_size_t __size));
extern FREE_RETURN_TYPE free PP ((__ptr_t __ptr));
#if ! (defined (_MALLOC_INTERNAL) && __DJGPP__ - 0 == 1)
extern __ptr_t memalign PP ((__malloc_size_t __alignment,
__malloc_size_t __size));
#endif
#if ! (defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC))
extern __ptr_t valloc PP ((__malloc_size_t __size));
#endif
#ifdef _MALLOC_INTERNAL
#define INT_BIT (CHAR_BIT * sizeof(int))
#define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
#define BLOCKSIZE (1 << BLOCKLOG)
#define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
#define HEAP (INT_BIT > 16 ? 4194304 : 65536)
#define FINAL_FREE_BLOCKS 8
typedef union
{
struct
{
int type;
union
{
struct
{
__malloc_size_t nfree;
__malloc_size_t first;
} frag;
__malloc_ptrdiff_t size;
} info;
} busy;
struct
{
__malloc_size_t size;
__malloc_size_t next;
__malloc_size_t prev;
} free;
} malloc_info;
extern char *_heapbase;
extern malloc_info *_heapinfo;
#define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
#define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
extern __malloc_size_t _heapindex;
extern __malloc_size_t _heaplimit;
struct list
{
struct list *next;
struct list *prev;
};
extern struct list _fraghead[];
struct alignlist
{
struct alignlist *next;
__ptr_t aligned;
__ptr_t exact;
};
extern struct alignlist *_aligned_blocks;
extern __malloc_size_t _chunks_used;
extern __malloc_size_t _bytes_used;
extern __malloc_size_t _chunks_free;
extern __malloc_size_t _bytes_free;
extern __ptr_t _malloc_internal PP ((__malloc_size_t __size));
extern __ptr_t _realloc_internal PP ((__ptr_t __ptr, __malloc_size_t __size));
extern void _free_internal PP ((__ptr_t __ptr));
#ifdef USE_PTHREAD
extern pthread_mutex_t _malloc_mutex;
#define LOCK() pthread_mutex_lock (&_malloc_mutex)
#define UNLOCK() pthread_mutex_unlock (&_malloc_mutex)
#else
#define LOCK()
#define UNLOCK()
#endif
#endif
extern __ptr_t malloc_find_object_address PP ((__ptr_t __ptr));
extern __ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size));
extern __ptr_t __default_morecore PP ((__malloc_ptrdiff_t __size));
extern void (*__after_morecore_hook) PP ((void));
extern __malloc_size_t __malloc_extra_blocks;
extern int __malloc_initialized;
extern int __malloc_initialize PP ((void));
extern void (*__malloc_initialize_hook) PP ((void));
extern void (*__free_hook) PP ((__ptr_t __ptr));
extern __ptr_t (*__malloc_hook) PP ((__malloc_size_t __size));
extern __ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
extern __ptr_t (*__memalign_hook) PP ((__malloc_size_t __size,
__malloc_size_t __alignment));
enum mcheck_status
{
MCHECK_DISABLED = -1,
MCHECK_OK,
MCHECK_FREE,
MCHECK_HEAD,
MCHECK_TAIL
};
extern int mcheck PP ((void (*__abortfunc) PP ((enum mcheck_status))));
extern enum mcheck_status mprobe PP ((__ptr_t __ptr));
extern void mtrace PP ((void));
extern void muntrace PP ((void));
struct mstats
{
__malloc_size_t bytes_total;
__malloc_size_t chunks_used;
__malloc_size_t bytes_used;
__malloc_size_t chunks_free;
__malloc_size_t bytes_free;
};
extern struct mstats mstats PP ((void));
extern void memory_warnings PP ((__ptr_t __start,
void (*__warnfun) PP ((const char *))));
extern __ptr_t r_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
extern void r_alloc_free PP ((__ptr_t *__handleptr));
extern __ptr_t r_re_alloc PP ((__ptr_t *__handleptr, __malloc_size_t __size));
#ifdef __cplusplus
}
#endif
#endif
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
#include <errno.h>
#if defined(CYGWIN)
extern __ptr_t bss_sbrk PP ((ptrdiff_t __size));
extern int bss_sbrk_did_unexec;
#endif
__ptr_t (*__morecore) PP ((ptrdiff_t __size)) = __default_morecore;
__ptr_t (*__malloc_hook) PP ((__malloc_size_t __size));
char *_heapbase;
malloc_info *_heapinfo;
static __malloc_size_t heapsize;
__malloc_size_t _heapindex;
__malloc_size_t _heaplimit;
struct list _fraghead[BLOCKLOG];
__malloc_size_t _chunks_used;
__malloc_size_t _bytes_used;
__malloc_size_t _chunks_free;
__malloc_size_t _bytes_free;
int __malloc_initialized;
__malloc_size_t __malloc_extra_blocks;
void (*__malloc_initialize_hook) PP ((void));
void (*__after_morecore_hook) PP ((void));
#if defined GC_MALLOC_CHECK && defined GC_PROTECT_MALLOC_STATE
#include <sys/types.h>
#include <sys/mman.h>
static int state_protected_p;
static __malloc_size_t last_state_size;
static malloc_info *last_heapinfo;
void
protect_malloc_state (protect_p)
int protect_p;
{
if (_heapinfo != last_heapinfo
&& last_heapinfo
&& state_protected_p)
mprotect (last_heapinfo, last_state_size, PROT_READ | PROT_WRITE);
last_state_size = _heaplimit * sizeof *_heapinfo;
last_heapinfo = _heapinfo;
if (protect_p != state_protected_p)
{
state_protected_p = protect_p;
if (mprotect (_heapinfo, last_state_size,
protect_p ? PROT_READ : PROT_READ | PROT_WRITE) != 0)
abort ();
}
}
#define PROTECT_MALLOC_STATE(PROT) protect_malloc_state(PROT)
#else
#define PROTECT_MALLOC_STATE(PROT)
#endif
static __ptr_t align PP ((__malloc_size_t));
static __ptr_t
align (size)
__malloc_size_t size;
{
__ptr_t result;
unsigned long int adj;
if ((__malloc_ptrdiff_t)size < 0)
result = 0;
else
result = (*__morecore) (size);
adj = (unsigned long int) ((unsigned long int) ((char *) result -
(char *) NULL)) % BLOCKSIZE;
if (adj != 0)
{
__ptr_t new;
adj = BLOCKSIZE - adj;
new = (*__morecore) (adj);
result = (char *) result + adj;
}
if (__after_morecore_hook)
(*__after_morecore_hook) ();
return result;
}
static __ptr_t get_contiguous_space PP ((__malloc_ptrdiff_t, __ptr_t));
static __ptr_t
get_contiguous_space (size, position)
__malloc_ptrdiff_t size;
__ptr_t position;
{
__ptr_t before;
__ptr_t after;
before = (*__morecore) (0);
if (before != position)
return 0;
after = (*__morecore) (size);
if (!after)
return 0;
if (after != position)
{
(*__morecore) (- size);
return 0;
}
return after;
}
static void register_heapinfo PP ((void));
#ifdef __GNUC__
__inline__
#endif
static void
register_heapinfo ()
{
__malloc_size_t block, blocks;
block = BLOCK (_heapinfo);
blocks = BLOCKIFY (heapsize * sizeof (malloc_info));
_bytes_used += blocks * BLOCKSIZE;
++_chunks_used;
_heapinfo[block].busy.type = 0;
_heapinfo[block].busy.info.size = blocks;
while (--blocks > 0)
_heapinfo[block + blocks].busy.info.size = -blocks;
}
#ifdef USE_PTHREAD
static pthread_once_t malloc_init_once_control = PTHREAD_ONCE_INIT;
pthread_mutex_t _malloc_mutex;
#endif
static void
malloc_initialize_1 ()
{
#ifdef GC_MCHECK
mcheck (NULL);
#endif
if (__malloc_initialize_hook)
(*__malloc_initialize_hook) ();
#ifdef USE_PTHREAD
{
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init (&_malloc_mutex, &attr);
pthread_mutexattr_destroy (&attr);
}
#endif
heapsize = HEAP / BLOCKSIZE;
_heapinfo = (malloc_info *) align (heapsize * sizeof (malloc_info));
if (_heapinfo == NULL)
return;
memset (_heapinfo, 0, heapsize * sizeof (malloc_info));
_heapinfo[0].free.size = 0;
_heapinfo[0].free.next = _heapinfo[0].free.prev = 0;
_heapindex = 0;
_heapbase = (char *) _heapinfo;
_heaplimit = BLOCK (_heapbase + heapsize * sizeof (malloc_info));
register_heapinfo ();
__malloc_initialized = 1;
PROTECT_MALLOC_STATE (1);
return;
}
int
__malloc_initialize ()
{
#ifdef USE_PTHREAD
pthread_once (&malloc_init_once_control, malloc_initialize_1);
#else
if (__malloc_initialized)
return 0;
malloc_initialize_1 ();
#endif
return __malloc_initialized;
}
static int morecore_recursing;
static __ptr_t morecore PP ((__malloc_size_t));
static __ptr_t
morecore (size)
__malloc_size_t size;
{
__ptr_t result;
malloc_info *newinfo, *oldinfo;
__malloc_size_t newsize;
if (morecore_recursing)
return NULL;
result = align (size);
if (result == NULL)
return NULL;
PROTECT_MALLOC_STATE (0);
if ((__malloc_size_t) BLOCK ((char *) result + size) > heapsize)
{
newsize = heapsize;
do
newsize *= 2;
while ((__malloc_size_t) BLOCK ((char *) result + size) > newsize);
if (_heaplimit != 0)
{
int save = errno;
morecore_recursing = 1;
newinfo = (malloc_info *) _realloc_internal
(_heapinfo, newsize * sizeof (malloc_info));
morecore_recursing = 0;
if (newinfo == NULL)
errno = save;
else
{
memset (&newinfo[heapsize], 0,
(newsize - heapsize) * sizeof (malloc_info));
_heapinfo = newinfo;
heapsize = newsize;
goto got_heap;
}
}
while (1)
{
newinfo = (malloc_info *) align (newsize * sizeof (malloc_info));
if (newinfo == NULL)
{
(*__morecore) (-size);
return NULL;
}
if ((__malloc_size_t) BLOCK ((char *) newinfo
+ newsize * sizeof (malloc_info))
< newsize)
break;
(*__morecore) (- newsize * sizeof (malloc_info));
newsize *= 2;
}
memcpy (newinfo, _heapinfo, heapsize * sizeof (malloc_info));
memset (&newinfo[heapsize], 0,
(newsize - heapsize) * sizeof (malloc_info));
oldinfo = _heapinfo;
_heapinfo = newinfo;
heapsize = newsize;
register_heapinfo ();
_heaplimit = 0;
_free_internal (oldinfo);
PROTECT_MALLOC_STATE (0);
_heaplimit = BLOCK ((char *) newinfo + heapsize * sizeof (malloc_info));
return result;
}
got_heap:
_heaplimit = BLOCK ((char *) result + size);
return result;
}
__ptr_t
_malloc_internal (size)
__malloc_size_t size;
{
__ptr_t result;
__malloc_size_t block, blocks, lastblocks, start;
register __malloc_size_t i;
struct list *next;
#if 0
if (size == 0)
return NULL;
#endif
LOCK ();
PROTECT_MALLOC_STATE (0);
if (size < sizeof (struct list))
size = sizeof (struct list);
#ifdef SUNOS_LOCALTIME_BUG
if (size < 16)
size = 16;
#endif
if (size <= BLOCKSIZE / 2)
{
register __malloc_size_t log = 1;
--size;
while ((size /= 2) != 0)
++log;
next = _fraghead[log].next;
if (next != NULL)
{
result = (__ptr_t) next;
next->prev->next = next->next;
if (next->next != NULL)
next->next->prev = next->prev;
block = BLOCK (result);
if (--_heapinfo[block].busy.info.frag.nfree != 0)
_heapinfo[block].busy.info.frag.first = (unsigned long int)
((unsigned long int) ((char *) next->next - (char *) NULL)
% BLOCKSIZE) >> log;
++_chunks_used;
_bytes_used += 1 << log;
--_chunks_free;
_bytes_free -= 1 << log;
}
else
{
#ifdef GC_MALLOC_CHECK
result = _malloc_internal (BLOCKSIZE);
PROTECT_MALLOC_STATE (0);
#else
result = malloc (BLOCKSIZE);
#endif
if (result == NULL)
{
PROTECT_MALLOC_STATE (1);
goto out;
}
next = (struct list *) ((char *) result + (1 << log));
next->next = NULL;
next->prev = &_fraghead[log];
_fraghead[log].next = next;
for (i = 2; i < (__malloc_size_t) (BLOCKSIZE >> log); ++i)
{
next = (struct list *) ((char *) result + (i << log));
next->next = _fraghead[log].next;
next->prev = &_fraghead[log];
next->prev->next = next;
next->next->prev = next;
}
block = BLOCK (result);
_heapinfo[block].busy.type = log;
_heapinfo[block].busy.info.frag.nfree = i - 1;
_heapinfo[block].busy.info.frag.first = i - 1;
_chunks_free += (BLOCKSIZE >> log) - 1;
_bytes_free += BLOCKSIZE - (1 << log);
_bytes_used -= BLOCKSIZE - (1 << log);
}
}
else
{
blocks = BLOCKIFY (size);
start = block = _heapindex;
while (_heapinfo[block].free.size < blocks)
{
block = _heapinfo[block].free.next;
if (block == start)
{
__malloc_size_t wantblocks = blocks + __malloc_extra_blocks;
block = _heapinfo[0].free.prev;
lastblocks = _heapinfo[block].free.size;
if (_heaplimit != 0 && block + lastblocks == _heaplimit &&
block + wantblocks <= heapsize &&
get_contiguous_space ((wantblocks - lastblocks) * BLOCKSIZE,
ADDRESS (block + lastblocks)))
{
block = _heapinfo[0].free.prev;
_heapinfo[block].free.size += (wantblocks - lastblocks);
_bytes_free += (wantblocks - lastblocks) * BLOCKSIZE;
_heaplimit += wantblocks - lastblocks;
continue;
}
result = morecore (wantblocks * BLOCKSIZE);
if (result == NULL)
goto out;
block = BLOCK (result);
_heapinfo[block].free.size = wantblocks;
_heapinfo[block].free.prev = _heapinfo[0].free.prev;
_heapinfo[block].free.next = 0;
_heapinfo[0].free.prev = block;
_heapinfo[_heapinfo[block].free.prev].free.next = block;
++_chunks_free;
}
}
result = ADDRESS (block);
if (_heapinfo[block].free.size > blocks)
{
_heapinfo[block + blocks].free.size
= _heapinfo[block].free.size - blocks;
_heapinfo[block + blocks].free.next
= _heapinfo[block].free.next;
_heapinfo[block + blocks].free.prev
= _heapinfo[block].free.prev;
_heapinfo[_heapinfo[block].free.prev].free.next
= _heapinfo[_heapinfo[block].free.next].free.prev
= _heapindex = block + blocks;
}
else
{
_heapinfo[_heapinfo[block].free.next].free.prev
= _heapinfo[block].free.prev;
_heapinfo[_heapinfo[block].free.prev].free.next
= _heapindex = _heapinfo[block].free.next;
--_chunks_free;
}
_heapinfo[block].busy.type = 0;
_heapinfo[block].busy.info.size = blocks;
++_chunks_used;
_bytes_used += blocks * BLOCKSIZE;
_bytes_free -= blocks * BLOCKSIZE;
while (--blocks > 0)
_heapinfo[block + blocks].busy.info.size = -blocks;
}
PROTECT_MALLOC_STATE (1);
out:
UNLOCK ();
return result;
}
__ptr_t
malloc (size)
__malloc_size_t size;
{
if (!__malloc_initialized && !__malloc_initialize ())
return NULL;
return (__malloc_hook != NULL ? *__malloc_hook : _malloc_internal) (size);
}
#ifndef _LIBC
__ptr_t
_malloc (size)
__malloc_size_t size;
{
return malloc (size);
}
void
_free (ptr)
__ptr_t ptr;
{
free (ptr);
}
__ptr_t
_realloc (ptr, size)
__ptr_t ptr;
__malloc_size_t size;
{
return realloc (ptr, size);
}
#endif
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
#ifndef memmove
#if (defined (MEMMOVE_MISSING) || \
!defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
#ifdef emacs
#undef __malloc_safe_bcopy
#define __malloc_safe_bcopy safe_bcopy
#endif
extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
#endif
#endif
void (*__free_hook) PP ((__ptr_t __ptr));
struct alignlist *_aligned_blocks = NULL;
void
_free_internal (ptr)
__ptr_t ptr;
{
int type;
__malloc_size_t block, blocks;
register __malloc_size_t i;
struct list *prev, *next;
__ptr_t curbrk;
const __malloc_size_t lesscore_threshold
= FINAL_FREE_BLOCKS + 2 * __malloc_extra_blocks;
register struct alignlist *l;
if (ptr == NULL)
return;
LOCK ();
PROTECT_MALLOC_STATE (0);
for (l = _aligned_blocks; l != NULL; l = l->next)
if (l->aligned == ptr)
{
l->aligned = NULL;
ptr = l->exact;
break;
}
block = BLOCK (ptr);
type = _heapinfo[block].busy.type;
switch (type)
{
case 0:
--_chunks_used;
_bytes_used -= _heapinfo[block].busy.info.size * BLOCKSIZE;
_bytes_free += _heapinfo[block].busy.info.size * BLOCKSIZE;
i = _heapindex;
if (i > block)
while (i > block)
i = _heapinfo[i].free.prev;
else
{
do
i = _heapinfo[i].free.next;
while (i > 0 && i < block);
i = _heapinfo[i].free.prev;
}
if (block == i + _heapinfo[i].free.size)
{
_heapinfo[i].free.size += _heapinfo[block].busy.info.size;
block = i;
}
else
{
_heapinfo[block].free.size = _heapinfo[block].busy.info.size;
_heapinfo[block].free.next = _heapinfo[i].free.next;
_heapinfo[block].free.prev = i;
_heapinfo[i].free.next = block;
_heapinfo[_heapinfo[block].free.next].free.prev = block;
++_chunks_free;
}
if (block + _heapinfo[block].free.size == _heapinfo[block].free.next)
{
_heapinfo[block].free.size
+= _heapinfo[_heapinfo[block].free.next].free.size;
_heapinfo[block].free.next
= _heapinfo[_heapinfo[block].free.next].free.next;
_heapinfo[_heapinfo[block].free.next].free.prev = block;
--_chunks_free;
}
blocks = _heapinfo[block].free.size;
curbrk = (*__morecore) (0);
if (_heaplimit != 0 && curbrk == ADDRESS (_heaplimit))
{
__malloc_size_t info_block = BLOCK (_heapinfo);
__malloc_size_t info_blocks = _heapinfo[info_block].busy.info.size;
__malloc_size_t prev_block = _heapinfo[block].free.prev;
__malloc_size_t prev_blocks = _heapinfo[prev_block].free.size;
__malloc_size_t next_block = _heapinfo[block].free.next;
__malloc_size_t next_blocks = _heapinfo[next_block].free.size;
if (
(block + blocks == _heaplimit &&
info_block + info_blocks == block &&
prev_block != 0 && prev_block + prev_blocks == info_block &&
blocks + prev_blocks >= lesscore_threshold) ||
(block + blocks == info_block &&
((info_block + info_blocks == _heaplimit &&
blocks >= lesscore_threshold) ||
(info_block + info_blocks == next_block &&
next_block + next_blocks == _heaplimit &&
blocks + next_blocks >= lesscore_threshold)))
)
{
malloc_info *newinfo;
__malloc_size_t oldlimit = _heaplimit;
_heaplimit = 0;
_free_internal (_heapinfo);
_heaplimit = oldlimit;
_heapindex = 0;
newinfo = (malloc_info *) _malloc_internal (info_blocks
* BLOCKSIZE);
PROTECT_MALLOC_STATE (0);
memmove (newinfo, _heapinfo, info_blocks * BLOCKSIZE);
_heapinfo = newinfo;
block = _heapinfo[0].free.prev;
blocks = _heapinfo[block].free.size;
}
if (block + blocks == _heaplimit && blocks >= lesscore_threshold)
{
register __malloc_size_t bytes = blocks * BLOCKSIZE;
_heaplimit -= blocks;
(*__morecore) (-bytes);
_heapinfo[_heapinfo[block].free.prev].free.next
= _heapinfo[block].free.next;
_heapinfo[_heapinfo[block].free.next].free.prev
= _heapinfo[block].free.prev;
block = _heapinfo[block].free.prev;
--_chunks_free;
_bytes_free -= bytes;
}
}
_heapindex = block;
break;
default:
--_chunks_used;
_bytes_used -= 1 << type;
++_chunks_free;
_bytes_free += 1 << type;
prev = (struct list *) ((char *) ADDRESS (block) +
(_heapinfo[block].busy.info.frag.first << type));
if (_heapinfo[block].busy.info.frag.nfree == (BLOCKSIZE >> type) - 1)
{
next = prev;
for (i = 1; i < (__malloc_size_t) (BLOCKSIZE >> type); ++i)
next = next->next;
prev->prev->next = next;
if (next != NULL)
next->prev = prev->prev;
_heapinfo[block].busy.type = 0;
_heapinfo[block].busy.info.size = 1;
++_chunks_used;
_bytes_used += BLOCKSIZE;
_chunks_free -= BLOCKSIZE >> type;
_bytes_free -= BLOCKSIZE;
#ifdef GC_MALLOC_CHECK
_free_internal (ADDRESS (block));
#else
free (ADDRESS (block));
#endif
}
else if (_heapinfo[block].busy.info.frag.nfree != 0)
{
next = (struct list *) ptr;
next->next = prev->next;
next->prev = prev;
prev->next = next;
if (next->next != NULL)
next->next->prev = next;
++_heapinfo[block].busy.info.frag.nfree;
}
else
{
prev = (struct list *) ptr;
_heapinfo[block].busy.info.frag.nfree = 1;
_heapinfo[block].busy.info.frag.first = (unsigned long int)
((unsigned long int) ((char *) ptr - (char *) NULL)
% BLOCKSIZE >> type);
prev->next = _fraghead[type].next;
prev->prev = &_fraghead[type];
prev->prev->next = prev;
if (prev->next != NULL)
prev->next->prev = prev;
}
break;
}
PROTECT_MALLOC_STATE (1);
UNLOCK ();
}
FREE_RETURN_TYPE
free (ptr)
__ptr_t ptr;
{
if (__free_hook != NULL)
(*__free_hook) (ptr);
else
_free_internal (ptr);
}
#ifdef weak_alias
weak_alias (free, cfree)
#else
void
cfree (ptr)
__ptr_t ptr;
{
free (ptr);
}
#endif
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
#if (defined (MEMMOVE_MISSING) || \
!defined(_LIBC) && !defined(STDC_HEADERS) && !defined(USG))
#ifdef emacs
#undef __malloc_safe_bcopy
#define __malloc_safe_bcopy safe_bcopy
#else
void
__malloc_safe_bcopy (afrom, ato, size)
__ptr_t afrom;
__ptr_t ato;
__malloc_size_t size;
{
char *from = afrom, *to = ato;
if (size <= 0 || from == to)
return;
if (to < from || from + size <= to)
bcopy (from, to, size);
else
{
register char *endf = from + size;
register char *endt = to + size;
if (to - from < 64)
{
do
*--endt = *--endf;
while (endf != from);
}
else
{
for (;;)
{
endt -= (to - from);
endf -= (to - from);
if (endt < to)
break;
bcopy (endf, endt, to - from);
}
bcopy (from, to, endt - from);
}
}
}
#endif
#ifndef memmove
extern void __malloc_safe_bcopy PP ((__ptr_t, __ptr_t, __malloc_size_t));
#define memmove(to, from, size) __malloc_safe_bcopy ((from), (to), (size))
#endif
#endif
#define min(A, B) ((A) < (B) ? (A) : (B))
__ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
__ptr_t
_realloc_internal (ptr, size)
__ptr_t ptr;
__malloc_size_t size;
{
__ptr_t result;
int type;
__malloc_size_t block, blocks, oldlimit;
if (size == 0)
{
_free_internal (ptr);
return _malloc_internal (0);
}
else if (ptr == NULL)
return _malloc_internal (size);
block = BLOCK (ptr);
LOCK ();
PROTECT_MALLOC_STATE (0);
type = _heapinfo[block].busy.type;
switch (type)
{
case 0:
if (size <= BLOCKSIZE / 2)
{
result = _malloc_internal (size);
if (result != NULL)
{
memcpy (result, ptr, size);
_free_internal (ptr);
goto out;
}
}
blocks = BLOCKIFY (size);
if (blocks < _heapinfo[block].busy.info.size)
{
_heapinfo[block + blocks].busy.type = 0;
_heapinfo[block + blocks].busy.info.size
= _heapinfo[block].busy.info.size - blocks;
_heapinfo[block].busy.info.size = blocks;
++_chunks_used;
_free_internal (ADDRESS (block + blocks));
result = ptr;
}
else if (blocks == _heapinfo[block].busy.info.size)
result = ptr;
else
{
blocks = _heapinfo[block].busy.info.size;
oldlimit = _heaplimit;
_heaplimit = 0;
_free_internal (ptr);
result = _malloc_internal (size);
PROTECT_MALLOC_STATE (0);
if (_heaplimit == 0)
_heaplimit = oldlimit;
if (result == NULL)
{
if (_heapindex == block)
(void) _malloc_internal (blocks * BLOCKSIZE);
else
{
__ptr_t previous
= _malloc_internal ((block - _heapindex) * BLOCKSIZE);
(void) _malloc_internal (blocks * BLOCKSIZE);
_free_internal (previous);
}
goto out;
}
if (ptr != result)
memmove (result, ptr, blocks * BLOCKSIZE);
}
break;
default:
if (size > (__malloc_size_t) (1 << (type - 1)) &&
size <= (__malloc_size_t) (1 << type))
result = ptr;
else
{
result = _malloc_internal (size);
if (result == NULL)
goto out;
memcpy (result, ptr, min (size, (__malloc_size_t) 1 << type));
_free_internal (ptr);
}
break;
}
PROTECT_MALLOC_STATE (1);
out:
UNLOCK ();
return result;
}
__ptr_t
realloc (ptr, size)
__ptr_t ptr;
__malloc_size_t size;
{
if (!__malloc_initialized && !__malloc_initialize ())
return NULL;
return (__realloc_hook != NULL ? *__realloc_hook : _realloc_internal)
(ptr, size);
}
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
__ptr_t
calloc (nmemb, size)
register __malloc_size_t nmemb;
register __malloc_size_t size;
{
register __ptr_t result = malloc (nmemb * size);
if (result != NULL)
(void) memset (result, 0, nmemb * size);
return result;
}
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
#ifndef __GNU_LIBRARY__
#define __sbrk sbrk
#endif
#ifdef __GNU_LIBRARY__
#include <stddef.h>
extern __ptr_t __sbrk PP ((ptrdiff_t increment));
#endif
#ifndef NULL
#define NULL 0
#endif
__ptr_t
__default_morecore (increment)
__malloc_ptrdiff_t increment;
{
__ptr_t result;
#if defined(CYGWIN)
if (!bss_sbrk_did_unexec)
{
return bss_sbrk (increment);
}
#endif
result = (__ptr_t) __sbrk (increment);
if (result == (__ptr_t) -1)
return NULL;
return result;
}
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
#if __DJGPP__ - 0 == 1
#else
__ptr_t (*__memalign_hook) PP ((__malloc_size_t __size,
__malloc_size_t __alignment));
__ptr_t
memalign (alignment, size)
__malloc_size_t alignment;
__malloc_size_t size;
{
__ptr_t result;
unsigned long int adj, lastadj;
if (__memalign_hook)
return (*__memalign_hook) (alignment, size);
result = malloc (size + alignment - 1);
if (result == NULL)
return NULL;
adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
do
{
free (result);
result = malloc (adj + size);
if (result == NULL)
return NULL;
lastadj = adj;
adj = (unsigned long int) ((char *) result - (char *) NULL) % alignment;
} while (adj > lastadj);
if (adj != 0)
{
struct alignlist *l;
for (l = _aligned_blocks; l != NULL; l = l->next)
if (l->aligned == NULL)
break;
if (l == NULL)
{
l = (struct alignlist *) malloc (sizeof (struct alignlist));
if (l == NULL)
{
free (result);
return NULL;
}
l->next = _aligned_blocks;
_aligned_blocks = l;
}
l->exact = result;
result = l->aligned = (char *) result + alignment - adj;
}
return result;
}
#endif
#if defined (_MALLOC_INTERNAL) && defined (GMALLOC_INHIBIT_VALLOC)
#define ELIDE_VALLOC
#endif
#ifndef ELIDE_VALLOC
#if defined (__GNU_LIBRARY__) || defined (_LIBC)
#include <stddef.h>
#include <sys/cdefs.h>
#if defined (__GLIBC__) && __GLIBC__ >= 2
#else
extern size_t __getpagesize PP ((void));
#endif
#else
#include "getpagesize.h"
#define __getpagesize() getpagesize()
#endif
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#endif
static __malloc_size_t pagesize;
__ptr_t
valloc (size)
__malloc_size_t size;
{
if (pagesize == 0)
pagesize = __getpagesize ();
return memalign (pagesize, size);
}
#endif
#ifdef GC_MCHECK
#ifdef emacs
#include <stdio.h>
#else
#ifndef _MALLOC_INTERNAL
#define _MALLOC_INTERNAL
#include <malloc.h>
#include <stdio.h>
#endif
#endif
static void (*old_free_hook) __P ((__ptr_t ptr));
static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size));
static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
static void (*abortfunc) __P ((enum mcheck_status));
#define MAGICWORD 0xfedabeeb
#define MAGICFREE 0xd8675309
#define MAGICBYTE ((char) 0xd7)
#define MALLOCFLOOD ((char) 0x93)
#define FREEFLOOD ((char) 0x95)
struct hdr
{
__malloc_size_t size;
unsigned long int magic;
};
#if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
#define flood memset
#else
static void flood __P ((__ptr_t, int, __malloc_size_t));
static void
flood (ptr, val, size)
__ptr_t ptr;
int val;
__malloc_size_t size;
{
char *cp = ptr;
while (size--)
*cp++ = val;
}
#endif
static enum mcheck_status checkhdr __P ((const struct hdr *));
static enum mcheck_status
checkhdr (hdr)
const struct hdr *hdr;
{
enum mcheck_status status;
switch (hdr->magic)
{
default:
status = MCHECK_HEAD;
break;
case MAGICFREE:
status = MCHECK_FREE;
break;
case MAGICWORD:
if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
status = MCHECK_TAIL;
else
status = MCHECK_OK;
break;
}
if (status != MCHECK_OK)
(*abortfunc) (status);
return status;
}
static void freehook __P ((__ptr_t));
static void
freehook (ptr)
__ptr_t ptr;
{
struct hdr *hdr;
if (ptr)
{
hdr = ((struct hdr *) ptr) - 1;
checkhdr (hdr);
hdr->magic = MAGICFREE;
flood (ptr, FREEFLOOD, hdr->size);
}
else
hdr = NULL;
__free_hook = old_free_hook;
free (hdr);
__free_hook = freehook;
}
static __ptr_t mallochook __P ((__malloc_size_t));
static __ptr_t
mallochook (size)
__malloc_size_t size;
{
struct hdr *hdr;
__malloc_hook = old_malloc_hook;
hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
__malloc_hook = mallochook;
if (hdr == NULL)
return NULL;
hdr->size = size;
hdr->magic = MAGICWORD;
((char *) &hdr[1])[size] = MAGICBYTE;
flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
return (__ptr_t) (hdr + 1);
}
static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t));
static __ptr_t
reallochook (ptr, size)
__ptr_t ptr;
__malloc_size_t size;
{
struct hdr *hdr = NULL;
__malloc_size_t osize = 0;
if (ptr)
{
hdr = ((struct hdr *) ptr) - 1;
osize = hdr->size;
checkhdr (hdr);
if (size < osize)
flood ((char *) ptr + size, FREEFLOOD, osize - size);
}
__free_hook = old_free_hook;
__malloc_hook = old_malloc_hook;
__realloc_hook = old_realloc_hook;
hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
__free_hook = freehook;
__malloc_hook = mallochook;
__realloc_hook = reallochook;
if (hdr == NULL)
return NULL;
hdr->size = size;
hdr->magic = MAGICWORD;
((char *) &hdr[1])[size] = MAGICBYTE;
if (size > osize)
flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
return (__ptr_t) (hdr + 1);
}
static void
mabort (status)
enum mcheck_status status;
{
const char *msg;
switch (status)
{
case MCHECK_OK:
msg = "memory is consistent, library is buggy";
break;
case MCHECK_HEAD:
msg = "memory clobbered before allocated block";
break;
case MCHECK_TAIL:
msg = "memory clobbered past end of allocated block";
break;
case MCHECK_FREE:
msg = "block freed twice";
break;
default:
msg = "bogus mcheck_status, library is buggy";
break;
}
#ifdef __GNU_LIBRARY__
__libc_fatal (msg);
#else
fprintf (stderr, "mcheck: %s\n", msg);
fflush (stderr);
abort ();
#endif
}
static int mcheck_used = 0;
int
mcheck (func)
void (*func) __P ((enum mcheck_status));
{
abortfunc = (func != NULL) ? func : &mabort;
if (!__malloc_initialized && !mcheck_used)
{
old_free_hook = __free_hook;
__free_hook = freehook;
old_malloc_hook = __malloc_hook;
__malloc_hook = mallochook;
old_realloc_hook = __realloc_hook;
__realloc_hook = reallochook;
mcheck_used = 1;
}
return mcheck_used ? 0 : -1;
}
enum mcheck_status
mprobe (__ptr_t ptr)
{
return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;
}
#endif