/*
    Copyright 2000 (c) by David Schweikert,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Net/NetSyscall.c,v $
    Author(s):             
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
 
 
    $Log: NetSyscall.c,v $
    Revision 1.5  2001/01/29 22:07:47  cjeker
    added a lot of debug printfs in the memory stuff
 
    Revision 1.4  2001/01/23 10:05:29  cjeker
    moved to Net/old
 
    Revision 1.3  2001/01/23 09:10:00  cjeker
    clean up
 
    Revision 1.2  2000/11/15 10:12:14  ruf
    Repository Reorganisation
 
    Revision 1.1  2000/03/31 17:50:35  gfa
    Merged with /Net from several term projects
 
*/
#include <Syscall.h>
#include <NetDebug.h>
#include <NetSyscall.h>

/* NetSyscall.c:
   This is mainly a wrapper to let all memory request come from the same
   Thread. THis is needed because only the owner can free a buffer */

extern ThreadId netMemoryThread;

/* we use the genericSyscall function used in Syscall.c */
SYSCALL_ERROR genericSyscall(ThreadId to, Message* message, Message* reply);

/* Please don't call these directly, but use the netbufAlloc and netbufFree
   instead! */
SYSCALL_ERROR netAlloc(Address *addressPtr, unsigned long int size)
{
  Message message, reply;

  message.id = VM_ALLOC;
  message.msg.vmAlloc.size = size;
  reply.id = VM_ALLOCREPLY;

  if (genericSyscall(netMemoryThread, &message, &reply) == TM_MSGSENDFAILED)
    return VM_ALLOCFAILED;

  *addressPtr = reply.msg.vmAllocReply.address;

  return reply.msg.vmAllocReply.errorCode;
}

SYSCALL_ERROR netFree(Address address)
{
  Message message, reply;

  message.id = VM_FREE;
  message.msg.vmFree.address = address;
  reply.id = VM_FREEREPLY;

  if (genericSyscall(netMemoryThread, &message, &reply) == TM_MSGSENDFAILED)
    return VM_FREEFAILED;

  return reply.msg.vmFreeReply.errorCode;
}
