/* Copyright (c) 1997-2004			 -*- C -*-
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   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.
*/

#ifndef _POLYMAKE_XS_UTILS_H
#define _POLYMAKE_XS_UTILS_H "$Project: polymake $$Id: Ext.h,v 1.4 2004/11/24 18:01:09 gawrilow Exp $"

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif

#ifndef TOPm1s
#define not_PERL_5_8
#define TOPm1s (*(sp-1))
#endif

static inline
I32 min (I32 a, I32 b) { return a<b ? a : b; }

static inline
I32 max (I32 a, I32 b) { return a>b ? a : b; }

static inline
void write_protect_on(SV *x)
{
   if (x != &PL_sv_undef) SvREADONLY_on(x);
}

static inline
void write_protect_off(SV *x)
{
   if (x != &PL_sv_undef) SvREADONLY_off(x);
}

void dump_me(SV *x);

static inline
SV **get_cx_curpad(pTHX_ PERL_CONTEXT *cx, PERL_CONTEXT *cx_bottom)
{
   CV *cv;
   I32 d;
   while (--cx>=cx_bottom) {
      if (CxTYPE(cx) == CXt_SUB) {
	 cv=cx->blk_sub.cv;
	 d=cx->blk_sub.olddepth;
	 goto FOUND;
      }
      if (CxREALEVAL(cx)) {
	 cv=PL_compcv;
	 d=0;
	 goto FOUND;
      }
   }
   cv=PL_main_cv;
   d=0;
 FOUND:
   return (SV**)AvARRAY(((AV**)AvARRAY(CvPADLIST(cv)))[d+1]);
}

#endif // _POLYMAKE_XS_UTILS_H
