/****** amiga.lib/CreatePort ************************************/
/*
  Copyright (c) 1988 Commodore-Amiga, Inc.
 
  Executables based on this information may be used in software
  for Commodore Amiga computers.  All other rights reserved.
 
  This information is provided "as is"; no warranties are made.
  All use is at your own risk, and no liability or responsibility is assumed.
*/


#include "exec/types.h"
#include "exec/ports.h"
#include "exec/memory.h"
/*
#include "proto/exec.h"
#include "functions.h"
*/


/* Example only, please use the amiga.lib version where possible */

struct MsgPort *CreatePort(name, pri)
char *name;
LONG  pri;
{
int sigBit;
struct MsgPort *port;

    if ((sigBit = AllocSignal(-1L)) == -1)
	return(NULL);

    port = (struct MsgPort *)
      AllocMem((ULONG)sizeof(struct MsgPort),(ULONG)MEMF_CLEAR|MEMF_PUBLIC);

    if (!port)
	{
	FreeSignal(sigBit);
	return(NULL);
	}

    port-> mp_Node.ln_Name = name;
    port-> mp_Node.ln_Pri  = pri;
    port-> mp_Node.ln_Type = NT_MSGPORT;

    port-> mp_Flags   = PA_SIGNAL;
    port-> mp_SigBit  = sigBit;
    port-> mp_SigTask = (struct Task *)FindTask(0L); /* find THIS task */

    if (name)
	AddPort(port);
    else
	NewList(&(port-> mp_MsgList)); /* init message list */

    return(port);
}


/****** amiga.lib/DeletePort ************************************/

void DeletePort(port)
struct MsgPort *port;
{
    if ( port-> mp_Node.ln_Name ) /* if it was public... */
	RemPort(port);

    /* Make it difficult to re-use the port */
    port-> mp_SigTask	      = (struct Task *) -1;
    port-> mp_MsgList.lh_Head = (struct Node *) -1;

    FreeSignal( port-> mp_SigBit );

    FreeMem( port, (ULONG)sizeof(struct MsgPort) );
}
