#include <sys/isa_defs.h>
#include <stdlib.h>
#include <memory.h>
#include <thread.h>
#include <synch.h>
#include <mtlib.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <limits.h>
#ifdef DEBUG
#define ASSERT(p) ((void) ((p) || (abort(), 0)))
#define COUNT(n) ((void) n++)
static int nmalloc, nrealloc, nfree;
#else
#define ASSERT(p) ((void)0)
#define COUNT(n) ((void)0)
#endif
#define MEMCOPY(to, fr, n) ((void) memcpy(to, fr, n))
#ifndef NULL
#define NULL (0)
#endif
#define WORDSIZE (sizeof (WORD))
#define MINSIZE (sizeof (TREE) - sizeof (WORD))
#define ROUND(s) if (s % WORDSIZE) s += (WORDSIZE - (s % WORDSIZE))
#ifdef DEBUG32
#define ALIGN 8
typedef int WORD;
typedef struct _t_ {
size_t t_s;
struct _t_ *t_p;
struct _t_ *t_l;
struct _t_ *t_r;
struct _t_ *t_n;
struct _t_ *t_d;
} TREE;
#define SIZE(b) ((b)->t_s)
#define AFTER(b) ((b)->t_p)
#define PARENT(b) ((b)->t_p)
#define LEFT(b) ((b)->t_l)
#define RIGHT(b) ((b)->t_r)
#define LINKFOR(b) ((b)->t_n)
#define LINKBAK(b) ((b)->t_p)
#else
#ifdef _LP64
#define ALIGN 16
#else
#define ALIGN 8
#endif
typedef union _w_ {
size_t w_i;
struct _t_ *w_p;
char w_a[ALIGN];
} WORD;
typedef struct _t_ {
WORD t_s;
WORD t_p;
WORD t_l;
WORD t_r;
WORD t_n;
WORD t_d;
} TREE;
#define SIZE(b) (((b)->t_s).w_i)
#define PARENT(b) (((b)->t_p).w_p)
#define LEFT(b) (((b)->t_l).w_p)
#define RIGHT(b) (((b)->t_r).w_p)
#define AFTER(b) (((b)->t_p).w_p)
#define LINKFOR(b) (((b)->t_n).w_p)
#define LINKBAK(b) (((b)->t_p).w_p)
#endif
#define SETNOTREE(b) (LEFT(b) = (TREE *)(-1))
#define ISNOTREE(b) (LEFT(b) == (TREE *)(-1))
#define DATA(b) ((char *)(((uintptr_t)(b)) + WORDSIZE))
#define BLOCK(d) ((TREE *)(((uintptr_t)(d)) - WORDSIZE))
#define SELFP(b) ((TREE **)(((uintptr_t)(b)) + SIZE(b)))
#define LAST(b) (*((TREE **)(((uintptr_t)(b)) - WORDSIZE)))
#define NEXT(b) ((TREE *)(((uintptr_t)(b)) + SIZE(b) + WORDSIZE))
#define BOTTOM(b) ((DATA(b) + SIZE(b) + WORDSIZE) == Baddr)
#define BIT0 (01)
#define BIT1 (02)
#define BITS01 (03)
#define ISBIT0(w) ((w) & BIT0)
#define ISBIT1(w) ((w) & BIT1)
#define SETBIT0(w) ((w) |= BIT0)
#define SETBIT1(w) ((w) |= BIT1)
#define CLRBIT0(w) ((w) &= ~BIT0)
#define CLRBIT1(w) ((w) &= ~BIT1)
#define SETBITS01(w) ((w) |= BITS01)
#define CLRBITS01(w) ((w) &= ~BITS01)
#define SETOLD01(n, o) ((n) |= (BITS01 & (o)))
#define GETCORE sbrk
#define ERRCORE ((void *)(-1))
#define CORESIZE (1024*ALIGN)
#define MAX_GETCORE (size_t)(SSIZE_MAX & ~(ALIGN - 1))
#define MAX_MALLOC (size_t)(SIZE_MAX - CORESIZE - 3 * ALIGN)
#define MAX_ALIGN (1 + (size_t)SSIZE_MAX)
extern void *GETCORE(ssize_t);
extern void _free_unlocked(void *);
extern mutex_t libc_malloc_lock;