/* vim: set filetype=c:
 *
 * memory.h86  -  Memory management interface functions for target library
 *
 * Copyright (C) 2003-2007 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: memory.h86,v 1.8 2007/01/06 18:30:54 gkminix Exp $
 */


#ifndef I386_MEMORY_H86
#define I386_MEMORY_H86



/*
 **************************************************************************
 *
 * This file can not be included into assembler source
 */
#ifndef _ASSEMBLY_

#include <common.h86>



/*
 **************************************************************************
 *
 * Memory handling
 */

/* Get amount of conventional memory */
__nb_inline__ unsigned long convmem(void)
{
  register __u32 ret;

  __asm__ __volatile__(
	"call getmem"
	: "=&a" (ret)
	: /* no input */
	: "cc");
  return(ret);
}


/* Get amount of total memory (conventional + extended) */
__nb_inline__ unsigned long extmem(void)
{
  register __u32 ret;

  __asm__ __volatile__(
	"call getext"
	: "=&a" (ret)
	: /* no input */
	: "cc");
  return(ret);
}


/* Set heap pointer */
__nb_inline__ short brk(void *newptr)
{
  register __u16 ret;

  __asm__ __volatile__(
	"call brk\n\t"
	"setc %b0\n\t"
	"cbtw"
	: "=&a" (ret)
	: "0" ((__u16)((int)newptr & 0xFFFF))
	: "memory", "cc");
  return((short)ret);
}


/* Add a value to the heap pointer */
__nb_inline__ void *sbrk(short val)
{
  register __u16 ret;

  __asm__ __volatile__(
	"call sbrk"
	: "=&a" (ret)
	: "0" ((__u16)val)
	: "memory", "cc");
  return((void *)((int)ret));
}


/* Allocate heap memory */
extern void *malloc(size_t nb) __attribute__((malloc));


/* Free any previously allocated memory */
extern void free(void *ptr);


/* Re-allocate heap memory */
extern void *realloc(void *ptr, size_t nb) __attribute__((malloc));


/* Allocate and zero-out heap memory */
extern void *calloc(size_t n, size_t elem_size) __attribute__((malloc));

#endif /* _ASSEMBLY_ */

#endif

