My Project
Macros | Functions | Variables
ndbm.cc File Reference
#include "kernel/mod2.h"
#include "reporter/si_signals.h"
#include "Singular/links/ndbm.h"
#include <strings.h>
#include <errno.h>
#include <stdlib.h>
#include <string.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 (DBM *db, long hash)
 
static int getbit (DBM *db)
 
static void setbit (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 (DBM *db, datum key)
 
datum dbm_fetch (DBM *db, datum key)
 
int dbm_delete (DBM *db, datum key)
 
int dbm_store (DBM *db, datum key, datum dat, int replace)
 
datum dbm_firstkey (DBM *db)
 
datum dbm_nextkey (DBM *db)
 

Variables

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

Macro Definition Documentation

◆ bcopy

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

Definition at line 43 of file ndbm.cc.

◆ BYTESIZ

#define BYTESIZ   8

Definition at line 46 of file ndbm.cc.

Function Documentation

◆ additem()

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

Definition at line 499 of file ndbm.cc.

500 {
501  short *sp;
502  int i1, i2, tmp;
503 
504  sp = (short *)buf;
505  i1 = PBLKSIZ;
506  i2 = sp[0];
507  if (i2 > 0)
508  i1 = sp[i2];
509  i1 -= item.dsize + item1.dsize;
510  tmp = (i2+3) * sizeof(short);
511  if (i1 <= tmp) return (0);
512  sp[0] += 2;
513  sp[++i2] = i1 + item1.dsize;
514  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
515  sp[++i2] = i1;
516  bcopy(item1.dptr, &buf[i1], item1.dsize);
517  return (1);
518 }
#define bcopy(a, b, c)
Definition: ndbm.cc:43
char * dptr
Definition: ndbm.h:84
int dsize
Definition: ndbm.h:85
#define PBLKSIZ
Definition: ndbm.h:50
int status int void * buf
Definition: si_signals.h:59

◆ dbm_access()

static void dbm_access ( DBM db,
long  hash 
)
static

Definition at line 304 of file ndbm.cc.

305 {
306  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
307  {
308  db->dbm_blkno = hash & db->dbm_hmask;
309  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
310  if (getbit(db) == 0)
311  break;
312  }
313  if (db->dbm_blkno != db->dbm_pagbno)
314  {
315  db->dbm_pagbno = db->dbm_blkno;
316  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
317  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
318  memset(db->dbm_pagbuf, 0, PBLKSIZ);
319 #ifdef DEBUG
320  else if (chkblk(db->dbm_pagbuf) < 0)
321  db->dbm_flags |= _DBM_IOERR;
322 #endif
323  }
324 }
#define SEEK_SET
Definition: mod2.h:115
static int getbit(DBM *db)
Definition: ndbm.cc:326
#define _DBM_IOERR
Definition: ndbm.h:71
long dbm_pagbno
Definition: ndbm.h:64
long dbm_hmask
Definition: ndbm.h:60
int dbm_pagf
Definition: ndbm.h:56
int dbm_flags
Definition: ndbm.h:57
long dbm_blkno
Definition: ndbm.h:63
char dbm_pagbuf[PBLKSIZ]
Definition: ndbm.h:65
long dbm_bitno
Definition: ndbm.h:59

◆ dbm_close()

void dbm_close ( DBM db)

Definition at line 97 of file ndbm.cc.

98 {
99  (void) si_close(db->dbm_dirf);
100  (void) si_close(db->dbm_pagf);
101  free((char *)db);
102 }
int dbm_dirf
Definition: ndbm.h:55
#define free
Definition: omAllocFunc.c:14

◆ dbm_delete()

int dbm_delete ( DBM db,
datum  key 
)

Definition at line 139 of file ndbm.cc.

140 {
141  int i;
142  // datum item;
143 
144  if (dbm_error(db))
145  return (-1);
146  if (dbm_rdonly(db))
147  {
148  errno = EPERM;
149  return (-1);
150  }
151  dbm_access(db, dcalchash(key));
152  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
153  return (-1);
154  if (!delitem(db->dbm_pagbuf, i))
155  goto err;
156  db->dbm_pagbno = db->dbm_blkno;
157  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
158  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
159  {
160  err:
161  db->dbm_flags |= _DBM_IOERR;
162  return (-1);
163  }
164  return (0);
165 }
int i
Definition: cfEzgcd.cc:132
static void dbm_access(DBM *db, long hash)
Definition: ndbm.cc:304
static int delitem(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:467
static int finddatum(char buf[PBLKSIZ], datum item)
Definition: ndbm.cc:394
static long dcalchash(datum item)
Definition: ndbm.cc:442
#define dbm_rdonly(db)
Definition: ndbm.h:73
#define dbm_error(db)
Definition: ndbm.h:75

◆ dbm_fetch()

datum dbm_fetch ( DBM db,
datum  key 
)

Definition at line 119 of file ndbm.cc.

120 {
121  int i;
122  datum item;
123 
124  if (dbm_error(db))
125  goto err;
126  dbm_access(db, dcalchash(key));
127  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
128  {
129  item = makdatum(db->dbm_pagbuf, i+1);
130  if (item.dptr != NULL)
131  return (item);
132  }
133 err:
134  item.dptr = NULL;
135  item.dsize = 0;
136  return (item);
137 }
static datum makdatum(char buf[PBLKSIZ], int n)
Definition: ndbm.cc:373
Definition: ndbm.h:83
#define NULL
Definition: omList.c:12

◆ dbm_firstkey()

datum dbm_firstkey ( DBM db)

Definition at line 252 of file ndbm.cc.

253 {
254 
255  db->dbm_blkptr = 0L;
256  db->dbm_keyptr = 0;
257  return (dbm_nextkey(db));
258 }
datum dbm_nextkey(DBM *db)
Definition: ndbm.cc:260
int dbm_keyptr
Definition: ndbm.h:62
long dbm_blkptr
Definition: ndbm.h:61

◆ dbm_forder()

long dbm_forder ( DBM db,
datum  key 
)

Definition at line 104 of file ndbm.cc.

105 {
106  long hash;
107 
108  hash = dcalchash(key);
109  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1)
110  {
111  db->dbm_blkno = hash & db->dbm_hmask;
112  db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
113  if (getbit(db) == 0)
114  break;
115  }
116  return (db->dbm_blkno);
117 }

◆ dbm_nextkey()

datum dbm_nextkey ( DBM db)

Definition at line 260 of file ndbm.cc.

261 {
262  struct stat statb;
263  datum item;
264 
265  if (dbm_error(db)
266  || singular_fstat(db->dbm_pagf, &statb) < 0
267  )
268  goto err;
269  statb.st_size /= PBLKSIZ;
270  for (;;)
271  {
272  if (db->dbm_blkptr != db->dbm_pagbno)
273  {
274  db->dbm_pagbno = db->dbm_blkptr;
275  (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, SEEK_SET);
276  if (si_read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
277  memset(db->dbm_pagbuf, 0, PBLKSIZ);
278 #ifdef DEBUG
279  else if (chkblk(db->dbm_pagbuf) < 0)
280  db->dbm_flags |= _DBM_IOERR;
281 #endif
282  }
283  short tmp;
284  memcpy(&tmp, db->dbm_pagbuf, sizeof(tmp));
285  if (tmp != 0)
286  {
287  item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
288  if (item.dptr != NULL)
289  {
290  db->dbm_keyptr += 2;
291  return (item);
292  }
293  db->dbm_keyptr = 0;
294  }
295  if (++db->dbm_blkptr >= statb.st_size)
296  break;
297  }
298 err:
299  item.dptr = NULL;
300  item.dsize = 0;
301  return (item);
302 }
int singular_fstat(int fd, struct stat *buf)
Definition: misc_ip.cc:1082

◆ dbm_open()

DBM* dbm_open ( char *  file,
int  flags,
int  mode 
)

Definition at line 59 of file ndbm.cc.

60 {
61  struct stat statb;
62  DBM *db;
63 
64  if ((db = (DBM *)malloc(sizeof *db)) == 0)
65  {
66  errno = ENOMEM;
67  return ((DBM *)0);
68  }
69 #ifdef MSDOS
70  // default mode of open is ascii, we need binary mode.
71  flags |= O_BINARY;
72 #endif
73  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
74  if ((flags & 03) == O_WRONLY)
75  flags = (flags & ~03) | O_RDWR;
76  strcpy(db->dbm_pagbuf, file);
77  strcat(db->dbm_pagbuf, ".pag");
78  db->dbm_pagf = si_open(db->dbm_pagbuf, flags, mode);
79  if (db->dbm_pagf < 0)
80  goto bad;
81  strcpy(db->dbm_pagbuf, file);
82  strcat(db->dbm_pagbuf, ".dir");
83  db->dbm_dirf = si_open(db->dbm_pagbuf, flags, mode);
84  if (db->dbm_dirf < 0)
85  goto bad1;
86  singular_fstat(db->dbm_dirf, &statb);
87  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
88  db->dbm_pagbno = db->dbm_dirbno = -1;
89  return (db);
90 bad1:
91  (void) si_close(db->dbm_pagf);
92 bad:
93  free((char *)db);
94  return ((DBM *)0);
95 }
bool bad
Definition: facFactorize.cc:64
#define BYTESIZ
Definition: ndbm.cc:46
#define _DBM_RDONLY
Definition: ndbm.h:70
Definition: ndbm.h:54
void * malloc(size_t size)
Definition: omalloc.c:85
#define si_open(...)
int status int void size_t count int const void size_t count const char int flags
Definition: si_signals.h:73

◆ dbm_store()

int dbm_store ( DBM db,
datum  key,
datum  dat,
int  replace 
)

Definition at line 167 of file ndbm.cc.

168 {
169  int i;
170  int ret;
171  datum item, item1;
172  char ovfbuf[PBLKSIZ];
173 
174  if (dbm_error(db))
175  return (-1);
176  if (dbm_rdonly(db))
177  {
178  errno = EPERM;
179  return (-1);
180  }
181 
182 _loop:
183  dbm_access(db, dcalchash(key));
184  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0)
185  {
186  if (!replace)
187  return (1);
188  if (!delitem(db->dbm_pagbuf, i))
189  {
190  db->dbm_flags |= _DBM_IOERR;
191  return (-1);
192  }
193  }
194  if (!additem(db->dbm_pagbuf, key, dat))
195  goto split;
196  db->dbm_pagbno = db->dbm_blkno;
197  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
198  if ( (ret=si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ)
199  {
200  db->dbm_flags |= _DBM_IOERR;
201  return (-1);
202  }
203  return (0);
204 
205 split:
206  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ)
207  {
208  db->dbm_flags |= _DBM_IOERR;
209  errno = ENOSPC;
210  return (-1);
211  }
212  memset(ovfbuf, 0, PBLKSIZ);
213  for (i=0;;) {
214  item = makdatum(db->dbm_pagbuf, i);
215  if (item.dptr == NULL)
216  break;
217  if (dcalchash(item) & (db->dbm_hmask+1))
218  {
219  item1 = makdatum(db->dbm_pagbuf, i+1);
220  if (item1.dptr == NULL) {
221  fprintf(stderr, "ndbm: split not paired\n");
222  db->dbm_flags |= _DBM_IOERR;
223  break;
224  }
225  if (!additem(ovfbuf, item, item1) ||
226  !delitem(db->dbm_pagbuf, i))
227  {
228  db->dbm_flags |= _DBM_IOERR;
229  return (-1);
230  }
231  continue;
232  }
233  i += 2;
234  }
235  db->dbm_pagbno = db->dbm_blkno;
236  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, SEEK_SET);
237  if (si_write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
238  {
239  db->dbm_flags |= _DBM_IOERR;
240  return (-1);
241  }
242  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, SEEK_SET);
243  if (si_write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ)
244  {
245  db->dbm_flags |= _DBM_IOERR;
246  return (-1);
247  }
248  setbit(db);
249  goto _loop;
250 }
static CFList split(const CanonicalForm &F, const int m, const Variable &x)
Definition: facMul.cc:3469
static int additem(char buf[PBLKSIZ], datum item, datum item1)
Definition: ndbm.cc:499
static void setbit(DBM *db)
Definition: ndbm.cc:348

◆ dcalchash()

static long dcalchash ( datum  item)
static

Definition at line 442 of file ndbm.cc.

443 {
444  int s, c, j;
445  char *cp;
446  unsigned long hashl;
447  unsigned int hashi;
448 
449  hashl = 0;
450  hashi = 0;
451  for (cp = item.dptr, s=item.dsize; --s >= 0; )
452  {
453  c = *cp++;
454  for (j=0; j<BYTESIZ; j+=4)
455  {
456  hashi += hitab[c&017];
457  hashl += hltab[hashi&63];
458  c >>= 4;
459  }
460  }
461  return (long)(hashl);
462 }
const CanonicalForm int s
Definition: facAbsFact.cc:51
int j
Definition: facHensel.cc:110
STATIC_VAR long hltab[64]
Definition: ndbm.cc:423
STATIC_VAR int hitab[16]
Definition: ndbm.cc:419

◆ delitem()

static int delitem ( char  buf[PBLKSIZ],
int  n 
)
static

Definition at line 467 of file ndbm.cc.

468 {
469  short *sp, *sp1;
470  int i1, i2;
471 
472  sp = (short *)buf;
473  i2 = sp[0];
474  if ((unsigned)n >= (unsigned)i2 || (n & 1))
475  return (0);
476  if (n == i2-2)
477  {
478  sp[0] -= 2;
479  return (1);
480  }
481  i1 = PBLKSIZ;
482  if (n > 0)
483  i1 = sp[n];
484  i1 -= sp[n+2];
485  if (i1 > 0)
486  {
487  i2 = sp[i2];
488  bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
489  }
490  sp[0] -= 2;
491  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
492  sp[0] = sp[2] + i1;
493  return (1);
494 }

◆ finddatum()

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

Definition at line 394 of file ndbm.cc.

395 {
396  short *sp;
397  int i, n, j;
398 
399  sp = (short *)buf;
400  n = PBLKSIZ;
401  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i])
402  {
403  n -= sp[i+1];
404  if (n != item.dsize)
405  continue;
406  if (n == 0 || memcmp(&buf[sp[i+1]], item.dptr, n) == 0)
407  return (i);
408  }
409  return (-1);
410 }

◆ getbit()

static int getbit ( DBM db)
static

Definition at line 326 of file ndbm.cc.

327 {
328  long bn;
329  int b, i, n;
330 
331 
332  if (db->dbm_bitno > db->dbm_maxbno)
333  return (0);
334  n = db->dbm_bitno % BYTESIZ;
335  bn = db->dbm_bitno / BYTESIZ;
336  i = bn % DBLKSIZ;
337  b = bn / DBLKSIZ;
338  if (b != db->dbm_dirbno)
339  {
340  db->dbm_dirbno = b;
341  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, SEEK_SET);
342  if (si_read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
343  memset(db->dbm_dirbuf, 0, DBLKSIZ);
344  }
345  return (db->dbm_dirbuf[i] & (1<<n));
346 }
CanonicalForm b
Definition: cfModGcd.cc:4103
char dbm_dirbuf[DBLKSIZ]
Definition: ndbm.h:67
long dbm_maxbno
Definition: ndbm.h:58
long dbm_dirbno
Definition: ndbm.h:66
#define DBLKSIZ
Definition: ndbm.h:52

◆ makdatum()

static datum makdatum ( char  buf[PBLKSIZ],
int  n 
)
static

Definition at line 373 of file ndbm.cc.

374 {
375  short *sp;
376  int t;
377  datum item;
378 
379  sp = (short *)buf;
380  if ((unsigned)n >= (unsigned)sp[0])
381  {
382  item.dptr = NULL;
383  item.dsize = 0;
384  return (item);
385  }
386  t = PBLKSIZ;
387  if (n > 0)
388  t = sp[n];
389  item.dptr = buf+sp[n+1];
390  item.dsize = t - sp[n+1];
391  return (item);
392 }

◆ setbit()

static void setbit ( DBM db)
static

Definition at line 348 of file ndbm.cc.

349 {
350  long bn;
351  int i, n, b;
352 
353  if (db->dbm_bitno > db->dbm_maxbno)
354  db->dbm_maxbno = db->dbm_bitno;
355  n = db->dbm_bitno % BYTESIZ;
356  bn = db->dbm_bitno / BYTESIZ;
357  i = bn % DBLKSIZ;
358  b = bn / DBLKSIZ;
359  if (b != db->dbm_dirbno)
360  {
361  db->dbm_dirbno = b;
362  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, SEEK_SET);
363  if (si_read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
364  memset(db->dbm_dirbuf, 0, DBLKSIZ);
365  }
366  db->dbm_dirbuf[i] |= 1<<n;
367  db->dbm_dirbno = b;
368  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, SEEK_SET);
369  if (si_write(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
370  db->dbm_flags |= _DBM_IOERR;
371 }

◆ singular_fstat()

int singular_fstat ( int  fd,
struct stat *  buf 
)

Definition at line 1082 of file misc_ip.cc.

1083 {
1084  return si_fstat(fd,buf);
1085 }
int status int fd
Definition: si_signals.h:59

Variable Documentation

◆ hitab

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

Definition at line 412 of file ndbm.cc.

◆ hltab

STATIC_VAR long hltab[64]
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 422 of file ndbm.cc.