#include <stdio.h>
#include <string.h>
#include "db_headers.h"
#include "db_vers.h"
#include "nisdb_mt.h"
const long unsigned MAXLOW = 32768*32768;
vers::vers(vers* other)
{
INITRW(vers);
assign(other);
}
void
vers::assign(vers* other)
{
WRITELOCKV(this, "w vers::assign");
if (other == NULL) {
syslog(LOG_ERR, "vers::vers: making copy of null vers?");
vers_high = vers_low = time_sec = time_usec = 0;
} else {
time_sec = other->time_sec;
time_usec = other->time_usec;
vers_low = other->vers_low;
vers_high = other->vers_high;
}
WRITEUNLOCKV(this, "wu vers::assign");
}
vers*
vers::nextminor()
{
READLOCK(this, NULL, "r vers::nextminor");
vers * newvers = new vers;
if (newvers == NULL) {
READUNLOCK(this, NULL, "ru vers::nextminor DB_MEMORY_LIMIT");
FATAL3("vers::nextminor: cannot allocation space",
DB_MEMORY_LIMIT, NULL);
}
struct timeval mt;
gettimeofday(&mt, NULL);
newvers->time_sec = (unsigned int) mt.tv_sec;
newvers->time_usec = (unsigned int) mt.tv_usec;
newvers->vers_low = (this->vers_low + 1);
newvers->vers_high = (this->vers_high);
if (newvers->vers_low >= MAXLOW){
newvers->vers_high++;
newvers->vers_low = 0;
}
READUNLOCK(this, newvers, "ru vers::nextminor");
return (newvers);
}
vers*
vers::nextmajor()
{
READLOCK(this, NULL, "r vers::nextmajor");
vers * newvers = new vers;
if (newvers == NULL) {
READUNLOCK(this, NULL, "ru vers::nextmajor DB_MEMORY_LIMIT");
FATAL3("vers::nextminor: cannot allocation space",
DB_MEMORY_LIMIT, NULL);
}
struct timeval mt;
gettimeofday(&mt, NULL);
newvers->time_sec = (unsigned int) mt.tv_sec;
newvers->time_usec = (unsigned int) mt.tv_usec;
newvers->vers_low = 0;
newvers->vers_high = (this->vers_high+1);
READUNLOCK(this, newvers, "ru vers::nextmajor");
return (newvers);
}
bool_t
vers::earlier_than(vers *other)
{
int ret, lret;
if (other == NULL) {
syslog(LOG_ERR,
"vers::earlier_than: comparing against null vers");
return (FALSE);
}
READLOCK(this, FALSE, "r vers::earlier_than");
READLOCKNR(other, lret, "r other vers::earlier_than");
if (lret != 0) {
READUNLOCK(this, FALSE, "ru + r other vers::earlier_than");
return (FALSE);
}
if (other->vers_high > vers_high) ret = TRUE;
else if (other->vers_high < vers_high) ret = FALSE;
else if (other->vers_low > vers_low) ret = TRUE;
else ret = FALSE;
READUNLOCKNR(other, lret, "ru other vers::earlier_than");
READUNLOCK(this, ret, ((lret != 0) ?
"ru + ru other vers::earlier_than" :
"ru vers::earlier_than"));
return (ret);
}
void
vers::print(FILE* file)
{
char *thetime;
thetime = ctime((long *) (&(time_sec)));
thetime[strlen(thetime)-1] = 0;
READLOCKV(this, "r vers::print");
fprintf(file, "version=%u.%u %s:%u",
vers_high,
vers_low,
thetime,
time_usec);
READUNLOCKV(this, "ru vers::print");
}
void
vers::zero() {
WRITELOCKV(this, "r vers::zero");
vers_high = vers_low = time_sec = time_usec = 0;
WRITEUNLOCKV(this, "ru vers::zero");
}
bool_t
vers::equal( vers *other) {
READLOCK(this, FALSE, "r vers::equal");
bool_t ret = other != NULL &&
vers_high == other->vers_high &&
vers_low == other->vers_low &&
time_sec == other->time_sec &&
time_usec == other->time_usec;
READUNLOCK(this, ret, "ru vers::equal");
return (ret);
};