Macros | Functions | Variables
ndbm.cc File Reference
#include <kernel/mod2.h>
#include <reporter/si_signals.h>
#include <strings.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <Singular/links/ndbm.h>

Go to the source code of this file.

Macros

#define bcopy(a, b, c)   memmove(b,a,c)
 
#define BYTESIZ   8
 

Functions

static void dbm_access (register DBM *db, long hash)
 
static int getbit (register DBM *db)
 
static void setbit (register DBM *db)
 
static datum makdatum (char buf[PBLKSIZ], int n)
 
static int finddatum (char buf[PBLKSIZ], datum item)
 
static long dcalchash (datum item)
 
static int delitem (char buf[PBLKSIZ], int n)
 
static int additem (char buf[PBLKSIZ], datum item, datum item1)
 
int singular_fstat (int fd, struct stat *buf)
 
DBMdbm_open (char *file, int flags, int mode)
 
void dbm_close (DBM *db)
 
long dbm_forder (register DBM *db, datum key)
 
datum dbm_fetch (register DBM *db, datum key)
 
int dbm_delete (register DBM *db, datum key)
 
int dbm_store (register DBM *db, datum key, datum dat, int replace)
 
datum dbm_firstkey (DBM *db)
 
datum dbm_nextkey (register DBM *db)
 

Variables

static int hitab [16]
 
static long hltab [64]
 

Macro Definition Documentation

#define bcopy (   a,
  b,
 
)    memmove(b,a,c)

Definition at line 54 of file ndbm.cc.

#define BYTESIZ   8

Definition at line 58 of file ndbm.cc.

Function Documentation

static int additem ( char  buf[PBLKSIZ],
datum  item,
datum  item1 
)
static

Definition at line 510 of file ndbm.cc.

511 {
512  register short *sp;
513  register int i1, i2, tmp;
514 
515  sp = (short *)buf;
516  i1 = PBLKSIZ;
517  i2 = sp[0];
518  if (i2 > 0)
519  i1 = sp[i2];
520  i1 -= item.dsize + item1.dsize;
521  tmp = (i2+3) * sizeof(short);
522  if (i1 <= tmp) return (0);
523  sp[0] += 2;
524  sp[++i2] = i1 + item1.dsize;
525  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
526  sp[++i2] = i1;
527  bcopy(item1.dptr, &buf[i1], item1.dsize);
528  return (1);
529 }
char * dptr
Definition: ndbm.h:84
int status int void * buf
Definition: si_signals.h:59
int dsize
Definition: ndbm.h:85
#define bcopy(a, b, c)
Definition: ndbm.cc:54
#define PBLKSIZ
Definition: ndbm.h:50
static void dbm_access ( register DBM db,
long  hash 
)
static

Definition at line 315 of file ndbm.cc.

316 {
317  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
318  {
319  db->dbm_blkno = hash & db->dbm_hmask;
320  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
321  if (getbit(db) == 0)
322  break;
323  }
324  if (db->dbm_blkno != db->dbm_pagbno)
325  {
326  db->dbm_pagbno = db->dbm_blkno;
327  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
328  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
329  memset(db->dbm_pagbuf, 0, PBLKSIZ);
330 #ifdef DEBUG
331  else if (chkblk(db->dbm_pagbuf) < 0)
332  db->dbm_flags |= _DBM_IOERR;
333 #endif
334  }
335 }
static int getbit(register DBM *db)
Definition: ndbm.cc:337
#define _DBM_IOERR
Definition: ndbm.h:71
#define PBLKSIZ
Definition: ndbm.h:50
void dbm_close ( DBM db)

Definition at line 110 of file ndbm.cc.

111 {
112  (void) si_close(db->dbm_dirf);
113  (void) si_close(db->dbm_pagf);
114  free((char *)db);
115 }
int dbm_pagf
Definition: ndbm.h:56
#define free
Definition: omAllocFunc.c:12
int dbm_dirf
Definition: ndbm.h:55
int dbm_delete ( register DBM db,
datum  key 
)

Definition at line 152 of file ndbm.cc.

153 {
154  register int i;
155  // datum item;
156 
157  if (dbm_error(db))
158  return (-1);
159  if (dbm_rdonly(db))
160  {
161  errno = EPERM;
162  return (-1);
163  }
164  dbm_access(db, dcalchash(key));
165  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
166  return (-1);
167  if (!delitem(db->dbm_pagbuf, i))
168  goto err;
169  db->dbm_pagbno = db->dbm_blkno;
170  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
171  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
172  {
173  err:
174  db->dbm_flags |= _DBM_IOERR;
175  return (-1);
176  }
177  return (0);
178 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
#define dbm_rdonly(db)
Definition: ndbm.h:73
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:478
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int i
Definition: cfEzgcd.cc:123
#define _DBM_IOERR
Definition: ndbm.h:71
#define PBLKSIZ
Definition: ndbm.h:50
#define dbm_error(db)
Definition: ndbm.h:75
datum dbm_fetch ( register DBM db,
datum  key 
)

Definition at line 132 of file ndbm.cc.

133 {
134  register int i;
135  datum item;
136 
137  if (dbm_error(db))
138  goto err;
139  dbm_access(db, dcalchash(key));
140  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
141  {
142  item = makdatum(db->dbm_pagbuf, i+1);
143  if (item.dptr != NULL)
144  return (item);
145  }
146 err:
147  item.dptr = NULL;
148  item.dsize = 0;
149  return (item);
150 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
char * dptr
Definition: ndbm.h:84
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int dsize
Definition: ndbm.h:85
int i
Definition: cfEzgcd.cc:123
#define NULL
Definition: omList.c:10
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75
datum dbm_firstkey ( DBM db)

Definition at line 265 of file ndbm.cc.

266 {
267 
268  db->dbm_blkptr = 0L;
269  db->dbm_keyptr = 0;
270  return (dbm_nextkey(db));
271 }
int dbm_keyptr
Definition: ndbm.h:62
datum dbm_nextkey(register DBM *db)
Definition: ndbm.cc:273
long dbm_blkptr
Definition: ndbm.h:61
long dbm_forder ( register DBM db,
datum  key 
)

Definition at line 117 of file ndbm.cc.

118 {
119  long hash;
120 
121  hash = dcalchash(key);
122  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
123  {
124  db->dbm_blkno = hash & db->dbm_hmask;
125  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
126  if (getbit(db) == 0)
127  break;
128  }
129  return (db->dbm_blkno);
130 }
static long dcalchash(datum item)
Definition: ndbm.cc:453
static int getbit(register DBM *db)
Definition: ndbm.cc:337
datum dbm_nextkey ( register DBM db)

Definition at line 273 of file ndbm.cc.

274 {
275  struct stat statb;
276  datum item;
277 
278  if (dbm_error(db)
279  || singular_fstat(db->dbm_pagf, &statb) < 0
280  )
281  goto err;
282  statb.st_size /= PBLKSIZ;
283  for (;;)
284  {
285  if (db->dbm_blkptr != db->dbm_pagbno)
286  {
287  db->dbm_pagbno = db->dbm_blkptr;
288  (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, L_SET);
289  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
290  memset(db->dbm_pagbuf, 0, PBLKSIZ);
291 #ifdef DEBUG
292  else if (chkblk(db->dbm_pagbuf) < 0)
293  db->dbm_flags |= _DBM_IOERR;
294 #endif
295  }
296  if (((short *)db->dbm_pagbuf)[0] != 0)
297  {
298  item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
299  if (item.dptr != NULL)
300  {
301  db->dbm_keyptr += 2;
302  return (item);
303  }
304  db->dbm_keyptr = 0;
305  }
306  if (++db->dbm_blkptr >= statb.st_size)
307  break;
308  }
309 err:
310  item.dptr = NULL;
311  item.dsize = 0;
312  return (item);
313 }
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1060
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
#define _DBM_IOERR
Definition: ndbm.h:71
#define NULL
Definition: omList.c:10
#define PBLKSIZ
Definition: ndbm.h:50
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75
DBM* dbm_open ( char *  file,
int  flags,
int  mode 
)

Definition at line 72 of file ndbm.cc.

73 {
74  struct stat statb;
75  register DBM *db;
76 
77  if ((db = (DBM *)malloc(sizeof *db)) == 0)
78  {
79  errno = ENOMEM;
80  return ((DBM *)0);
81  }
82 #ifdef MSDOS
83  // default mode of open is ascii, we need binary mode.
84  flags |= O_BINARY;
85 #endif
86  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
87  if ((flags & 03) == O_WRONLY)
88  flags = (flags & ~03) | O_RDWR;
89  strcpy(db->dbm_pagbuf, file);
90  strcat(db->dbm_pagbuf, ".pag");
91  db->dbm_pagf = si_open(db->dbm_pagbuf, flags, mode);
92  if (db->dbm_pagf < 0)
93  goto bad;
94  strcpy(db->dbm_pagbuf, file);
95  strcat(db->dbm_pagbuf, ".dir");
96  db->dbm_dirf = si_open(db->dbm_pagbuf, flags, mode);
97  if (db->dbm_dirf < 0)
98  goto bad1;
99  singular_fstat(db->dbm_dirf, &statb);
100  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
101  db->dbm_pagbno = db->dbm_dirbno = -1;
102  return (db);
103 bad1:
104  (void) si_close(db->dbm_pagf);
105 bad:
106  free((char *)db);
107  return ((DBM *)0);
108 }
#define si_open(...)
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1060
#define _DBM_RDONLY
Definition: ndbm.h:70
bool bad
Definition: facFactorize.cc:65
Definition: ndbm.h:54
void * malloc(size_t size)
Definition: omalloc.c:92
#define BYTESIZ
Definition: ndbm.cc:58
#define free
Definition: omAllocFunc.c:12
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:73
int dbm_store ( register DBM db,
datum  key,
datum  dat,
int  replace 
)

Definition at line 180 of file ndbm.cc.

181 {
182  register int i;
183  int ret;
184  datum item, item1;
185  char ovfbuf[PBLKSIZ];
186 
187  if (dbm_error(db))
188  return (-1);
189  if (dbm_rdonly(db))
190  {
191  errno = EPERM;
192  return (-1);
193  }
194 
195 _loop:
196  dbm_access(db, dcalchash(key));
197  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
198  {
199  if (!replace)
200  return (1);
201  if (!delitem(db->dbm_pagbuf, i))
202  {
203  db->dbm_flags |= _DBM_IOERR;
204  return (-1);
205  }
206  }
207  if (!additem(db->dbm_pagbuf, key, dat))
208  goto split;
209  db->dbm_pagbno = db->dbm_blkno;
210  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
211  if ( (ret=si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ)
212  {
213  db->dbm_flags |= _DBM_IOERR;
214  return (-1);
215  }
216  return (0);
217 
218 split:
219  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ)
220  {
221  db->dbm_flags |= _DBM_IOERR;
222  errno = ENOSPC;
223  return (-1);
224  }
225  memset(ovfbuf, 0, PBLKSIZ);
226  for (i=0;;) {
227  item = makdatum(db->dbm_pagbuf, i);
228  if (item.dptr == NULL)
229  break;
230  if (dcalchash(item) & (db->dbm_hmask+1))
231  {
232  item1 = makdatum(db->dbm_pagbuf, i+1);
233  if (item1.dptr == NULL) {
234  fprintf(stderr, "ndbm: split not paired\n");
235  db->dbm_flags |= _DBM_IOERR;
236  break;
237  }
238  if (!additem(ovfbuf, item, item1) ||
239  !delitem(db->dbm_pagbuf, i))
240  {
241  db->dbm_flags |= _DBM_IOERR;
242  return (-1);
243  }
244  continue;
245  }
246  i += 2;
247  }
248  db->dbm_pagbno = db->dbm_blkno;
249  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
250  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
251  {
252  db->dbm_flags |= _DBM_IOERR;
253  return (-1);
254  }
255  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, L_SET);
256  if (si_write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ)
257  {
258  db->dbm_flags |= _DBM_IOERR;
259  return (-1);
260  }
261  setbit(db);
262  goto _loop;
263 }
static void setbit(register DBM *db)
Definition: ndbm.cc:359
static long dcalchash(datum item)
Definition: ndbm.cc:453
static int additem(char buf[PBLKSIZ], datum item, datum item1)
Definition: ndbm.cc:510
static void dbm_access(register DBM *db, long hash)
Definition: ndbm.cc:315
char * dptr
Definition: ndbm.h:84
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:384
#define dbm_rdonly(db)
Definition: ndbm.h:73
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:478
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:405
int dsize
Definition: ndbm.h:85
int i
Definition: cfEzgcd.cc:123
static CFList split(const CanonicalForm &F, const int m, const Variable &x)
Definition: facMul.cc:3336
#define _DBM_IOERR
Definition: ndbm.h:71
#define NULL
Definition: omList.c:10
#define PBLKSIZ
Definition: ndbm.h:50
Definition: ndbm.h:83
#define dbm_error(db)
Definition: ndbm.h:75
static long dcalchash ( datum  item)
static

Definition at line 453 of file ndbm.cc.

454 {
455  register int s, c, j;
456  register char *cp;
457  register unsigned long hashl;
458  register unsigned int hashi;
459 
460  hashl = 0;
461  hashi = 0;
462  for (cp = item.dptr, s=item.dsize; --s >= 0; )
463  {
464  c = *cp++;
465  for (j=0; j<BYTESIZ; j+=4)
466  {
467  hashi += hitab[c&017];
468  hashl += hltab[hashi&63];
469  c >>= 4;
470  }
471  }
472  return (long)(hashl);
473 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
static int hitab[16]
Definition: ndbm.cc:430
char * dptr
Definition: ndbm.h:84
int j
Definition: myNF.cc:70
#define BYTESIZ
Definition: ndbm.cc:58
int dsize
Definition: ndbm.h:85
static long hltab[64]
Definition: ndbm.cc:434
static int delitem ( char  buf[PBLKSIZ],
int  n 
)
static

Definition at line 478 of file ndbm.cc.

479 {
480  register short *sp, *sp1;
481  register int i1, i2;
482 
483  sp = (short *)buf;
484  i2 = sp[0];
485  if ((unsigned)n >= (unsigned)i2 || (n & 1))
486  return (0);
487  if (n == i2-2)
488  {
489  sp[0] -= 2;
490  return (1);
491  }
492  i1 = PBLKSIZ;
493  if (n > 0)
494  i1 = sp[n];
495  i1 -= sp[n+2];
496  if (i1 > 0)
497  {
498  i2 = sp[i2];
499  bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
500  }
501  sp[0] -= 2;
502  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
503  sp[0] = sp[2] + i1;
504  return (1);
505 }
int status int void * buf
Definition: si_signals.h:59
#define bcopy(a, b, c)
Definition: ndbm.cc:54
#define PBLKSIZ
Definition: ndbm.h:50
static int finddatum ( char  buf[PBLKSIZ],
datum  item 
)
static

Definition at line 405 of file ndbm.cc.

406 {
407  register short *sp;
408  register int i, n, j;
409 
410  sp = (short *)buf;
411  n = PBLKSIZ;
412  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i])
413  {
414  n -= sp[i+1];
415  if (n != item.dsize)
416  continue;
417  if (n == 0 || memcmp(&buf[sp[i+1]], item.dptr, n) == 0)
418  return (i);
419  }
420  return (-1);
421 }
char * dptr
Definition: ndbm.h:84
int j
Definition: myNF.cc:70
int status int void * buf
Definition: si_signals.h:59
int dsize
Definition: ndbm.h:85
int i
Definition: cfEzgcd.cc:123
#define PBLKSIZ
Definition: ndbm.h:50
static int getbit ( register DBM db)
static

Definition at line 337 of file ndbm.cc.

338 {
339  long bn;
340  register int b, i, n;
341 
342 
343  if (db->dbm_bitno > db->dbm_maxbno)
344  return (0);
345  n = db->dbm_bitno % BYTESIZ;
346  bn = db->dbm_bitno / BYTESIZ;
347  i = bn % DBLKSIZ;
348  b = bn / DBLKSIZ;
349  if (b != db->dbm_dirbno)
350  {
351  db->dbm_dirbno = b;
352  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
353  if (si_read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
354  memset(db->dbm_dirbuf, 0, DBLKSIZ);
355  }
356  return (db->dbm_dirbuf[i] & (1<<n));
357 }
#define BYTESIZ
Definition: ndbm.cc:58
int i
Definition: cfEzgcd.cc:123
const poly b
Definition: syzextra.cc:213
#define DBLKSIZ
Definition: ndbm.h:52
static datum makdatum ( char  buf[PBLKSIZ],
int  n 
)
static

Definition at line 384 of file ndbm.cc.

385 {
386  register short *sp;
387  register int t;
388  datum item;
389 
390  sp = (short *)buf;
391  if ((unsigned)n >= (unsigned)sp[0])
392  {
393  item.dptr = NULL;
394  item.dsize = 0;
395  return (item);
396  }
397  t = PBLKSIZ;
398  if (n > 0)
399  t = sp[n];
400  item.dptr = buf+sp[n+1];
401  item.dsize = t - sp[n+1];
402  return (item);
403 }
char * dptr
Definition: ndbm.h:84
int status int void * buf
Definition: si_signals.h:59
int dsize
Definition: ndbm.h:85
#define NULL
Definition: omList.c:10
#define PBLKSIZ
Definition: ndbm.h:50
Definition: ndbm.h:83
static void setbit ( register DBM db)
static

Definition at line 359 of file ndbm.cc.

360 {
361  long bn;
362  register int i, n, b;
363 
364  if (db->dbm_bitno > db->dbm_maxbno)
365  db->dbm_maxbno = db->dbm_bitno;
366  n = db->dbm_bitno % BYTESIZ;
367  bn = db->dbm_bitno / BYTESIZ;
368  i = bn % DBLKSIZ;
369  b = bn / DBLKSIZ;
370  if (b != db->dbm_dirbno)
371  {
372  db->dbm_dirbno = b;
373  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
374  if (si_read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
375  memset(db->dbm_dirbuf, 0, DBLKSIZ);
376  }
377  db->dbm_dirbuf[i] |= 1<<n;
378  db->dbm_dirbno = b;
379  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
380  if (si_write(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
381  db->dbm_flags |= _DBM_IOERR;
382 }
#define BYTESIZ
Definition: ndbm.cc:58
int i
Definition: cfEzgcd.cc:123
#define _DBM_IOERR
Definition: ndbm.h:71
const poly b
Definition: syzextra.cc:213
#define DBLKSIZ
Definition: ndbm.h:52
int singular_fstat ( int  fd,
struct stat *  buf 
)

Definition at line 1060 of file misc_ip.cc.

1061 {
1062  return si_fstat(fd,buf);
1063 }
int status int fd
Definition: si_signals.h:59
int status int void * buf
Definition: si_signals.h:59

Variable Documentation

int hitab[16]
static
Initial value:
= { 61, 57, 53, 49, 45, 41, 37, 33,
29, 25, 21, 17, 13, 9, 5, 1,
}

Definition at line 430 of file ndbm.cc.

long hltab[64]
static
Initial value:
= {
06100151277L,06106161736L,06452611562L,05001724107L,
02614772546L,04120731531L,04665262210L,07347467531L,
06735253126L,06042345173L,03072226605L,01464164730L,
03247435524L,07652510057L,01546775256L,05714532133L,
06173260402L,07517101630L,02431460343L,01743245566L,
00261675137L,02433103631L,03421772437L,04447707466L,
04435620103L,03757017115L,03641531772L,06767633246L,
02673230344L,00260612216L,04133454451L,00615531516L,
06137717526L,02574116560L,02304023373L,07061702261L,
05153031405L,05322056705L,07401116734L,06552375715L,
06165233473L,05311063631L,01212221723L,01052267235L,
06000615237L,01075222665L,06330216006L,04402355630L,
01451177262L,02000133436L,06025467062L,07121076461L,
03123433522L,01010635225L,01716177066L,05161746527L,
01736635071L,06243505026L,03637211610L,01756474365L,
04723077174L,03642763134L,05750130273L,03655541561L,
}

Definition at line 434 of file ndbm.cc.