# include "pantasks.h"

# define MAX_N_JOBS 10000
static char *JobIDList;
static int   JobIDPtr;

void InitJobIDs () {

  JobIDPtr = 0;
  ALLOCATE (JobIDList, char, MAX_N_JOBS);
  bzero (JobIDList, MAX_N_JOBS*sizeof(char));
}  

void FreeJobIDs () {
  free (JobIDList);
}

/* return next unique ID, recycle every MAX_N_JOBS */
int NextJobID () {

  int Ntry;

  JobIDPtr ++;
  if (JobIDPtr >= MAX_N_JOBS) JobIDPtr = 0;

  Ntry = 0;
  while (JobIDList[JobIDPtr]) {
    Ntry ++;
    JobIDPtr ++;
    if (JobIDPtr >= MAX_N_JOBS) JobIDPtr = 0;
    if (Ntry == MAX_N_JOBS) return (-1);
  }
  JobIDList[JobIDPtr] = TRUE;
  return (JobIDPtr);
}

int FreeJobID (int JobID) {

  if (JobID < 0) return (FALSE);
  if (JobID >= MAX_N_JOBS) return (FALSE);

  JobIDList[JobID] = FALSE;
  return (TRUE);
}
