#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <memory.h>
#include <thread.h>
#include <pthread.h>
#include <synch.h>
#include <procfs.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.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
#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 _LP64
#define ALIGN 16
#else
#define ALIGN 8
#endif
typedef union _w_ {
size_t w_i;
struct _t_ *w_p[2];
} 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 RSIZE(b) (((b)->t_s).w_i & ~BITS01)
#define PARENT(b) (((b)->t_p).w_p[0])
#define LEFT(b) (((b)->t_l).w_p[0])
#define RIGHT(b) (((b)->t_r).w_p[0])
#define AFTER(b) (((b)->t_p).w_p[0])
#define LINKFOR(b) (((b)->t_n).w_p[0])
#define LINKBAK(b) (((b)->t_p).w_p[0])
#define SETNOTREE(b) (LEFT(b) = (TREE *)(-1))
#define ISNOTREE(b) (LEFT(b) == (TREE *)(-1))
#define DATA(b) (((char *)(b)) + WORDSIZE)
#define BLOCK(d) ((TREE *)(((char *)(d)) - WORDSIZE))
#define SELFP(b) (&(NEXT(b)->t_s.w_p[1]))
#define LAST(b) ((b)->t_s.w_p[1])
#define NEXT(b) ((TREE *)(((char *)(b)) + RSIZE(b) + WORDSIZE))
#define BOTTOM(b) ((DATA(b) + RSIZE(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 ((char *)(-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)