source: git/Singular/ndbm.cc @ ca7a56

fieker-DuValspielwiese
Last change on this file since ca7a56 was 954622, checked in by Hans Schönemann <hannes@…>, 26 years ago
* hannes: fixes for "TEST_MAC_ORDER" and some optimizations for binomials (binom.cc binom.h claptmpl.cc ipid.h kstd1.cc kstd2.cc kutil.cc polys-impl.h polys.cc polys.h polys0.cc polys1.cc ring.h spolys.cc spolys0.h) git-svn-id: file:///usr/local/Singular/svn/trunk@1005 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.1 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5//**************************************************************************/
6//
7// $Id: ndbm.cc,v 1.7 1998-01-05 16:39:24 Singular Exp $
8//
9//**************************************************************************/
10// 'ndbm.cc' containes all low-level functions to manipulate dbm-files
11// for the original Copyright of this file and 'ndbm.h' see below .
12//
13// some minor change where needed to compile and run under MacOS/MPW
14//
15//**************************************************************************/
16
17#include "mod2.h"
18#ifdef HAVE_DBM
19/*
20 * Copyright (c) 1983 Regents of the University of California.
21 * All rights reserved.  The Berkeley software License Agreement
22 * specifies the terms and conditions for redistribution.
23 */
24
25#if defined(LIBC_SCCS) && !defined(lint)
26static char sccsid[] = "@(#)ndbm.c        5.3 (Berkeley) 3/9/86";
27#endif LIBC_SCCS and not lint
28
29//**************************************************************************/
30
31#include <stdio.h>
32#ifdef macintosh
33#   include <stdlib.h>
34#   include <errno.h>
35#   include "fcntl.h"
36#   include <string.h>
37#   include <stat.h>
38#   define L_SET SEEK_SET
39#   define EPERM 1
40#   define ENOSPC 28
41#else /* not macintosh */
42#   include <sys/types.h>
43#   include <sys/stat.h>
44#   include <sys/file.h>
45#   include <errno.h>
46#   include <stdlib.h>
47#   include <string.h>
48#   include <unistd.h>
49#   include <fcntl.h>
50#endif /* macintosh */
51#ifndef HAVE_BCOPY
52#   define bcopy(a,b,c) memmove(b,a,c)
53#endif /* not HAVE_BCOPY */
54#include "ndbm.h"
55
56#define BYTESIZ 8
57#undef setbit
58
59static  void dbm_access(register DBM *db, long hash);
60static  int getbit(register DBM *db);
61static  void setbit(register DBM *db);
62static  datum makdatum(char buf[PBLKSIZ], int n);
63static  int finddatum(char buf[PBLKSIZ], datum item);
64static  long hashinc(register DBM *db, long hash);
65static  long dcalchash(datum item);
66static  int delitem(char buf[PBLKSIZ], int n);
67static  int additem(char buf[PBLKSIZ], datum item, datum item1);
68extern  int errno;
69
70
71DBM *
72dbm_open(char *file, int flags, int mode)
73{
74  struct stat statb;
75  register DBM *db;
76
77  if ((db = (DBM *)malloc(sizeof *db)) == 0) {
78    errno = ENOMEM;
79    return ((DBM *)0);
80  }
81#ifdef macintosh
82  // It seems that the compile has some problems to commit the flags properly
83  // O_RDWR | O_CREAT = 0x102 change to 0x200. We don't know why.
84  // setting flags to O_RDWR | O_CREAT solved our problem. :-(
85  flags = O_RDWR | O_CREAT;
86#endif /* macintosh */
87#ifdef MSDOS
88  // default mode of open is ascii, we need binary mode.
89  flags |= O_BINARY;
90#endif
91  db->dbm_flags = (flags & 03) == O_RDONLY ? _DBM_RDONLY : 0;
92  if ((flags & 03) == O_WRONLY)
93    flags = (flags & ~03) | O_RDWR;
94  strcpy(db->dbm_pagbuf, file);
95  strcat(db->dbm_pagbuf, ".pag");
96#ifdef macintosh
97  db->dbm_pagf = open(db->dbm_pagbuf, flags);
98#else /* not macintosh */
99  db->dbm_pagf = open(db->dbm_pagbuf, flags, mode);
100#endif /* macintosh */
101  if (db->dbm_pagf < 0)
102    goto bad;
103  strcpy(db->dbm_pagbuf, file);
104  strcat(db->dbm_pagbuf, ".dir");
105#ifdef macintosh
106  db->dbm_dirf = open(db->dbm_pagbuf, flags);
107#else /* not macintosh */
108  db->dbm_dirf = open(db->dbm_pagbuf, flags, mode);
109#endif /* macintosh */
110  if (db->dbm_dirf < 0)
111    goto bad1;
112  fstat(db->dbm_dirf, &statb);
113  db->dbm_maxbno = statb.st_size*BYTESIZ-1;
114  db->dbm_pagbno = db->dbm_dirbno = -1;
115  return (db);
116bad1:
117  (void) close(db->dbm_pagf);
118bad:
119  free((char *)db);
120  return ((DBM *)0);
121}
122
123void
124dbm_close(DBM *db)
125{
126  (void) close(db->dbm_dirf);
127  (void) close(db->dbm_pagf);
128  free((char *)db);
129}
130
131long
132dbm_forder(register DBM *db, datum key)
133{
134  long hash;
135 
136  hash = dcalchash(key);
137  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
138    db->dbm_blkno = hash & db->dbm_hmask;
139    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
140    if (getbit(db) == 0)
141      break;
142  }
143  return (db->dbm_blkno);
144}
145
146datum
147dbm_fetch(register DBM *db, datum key)
148{
149  register i;
150  datum item;
151
152  if (dbm_error(db))
153    goto err;
154  dbm_access(db, dcalchash(key));
155  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
156    item = makdatum(db->dbm_pagbuf, i+1);
157    if (item.dptr != NULL)
158      return (item);
159  }
160err:
161  item.dptr = NULL;
162  item.dsize = 0;
163  return (item);
164}
165
166int dbm_delete(register DBM *db, datum key)
167{
168  register i;
169  datum item;
170
171  if (dbm_error(db))
172    return (-1);
173  if (dbm_rdonly(db)) {
174    errno = EPERM;
175    return (-1);
176  }
177  dbm_access(db, dcalchash(key));
178  if ((i = finddatum(db->dbm_pagbuf, key)) < 0)
179    return (-1);
180  if (!delitem(db->dbm_pagbuf, i))
181    goto err;
182  db->dbm_pagbno = db->dbm_blkno;
183  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
184  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
185  err:
186    db->dbm_flags |= _DBM_IOERR;
187    return (-1);
188  }
189  return (0);
190}
191
192int dbm_store(register DBM *db, datum key, datum dat, int replace)
193{
194  register i;
195  int ret;
196  datum item, item1;
197  char ovfbuf[PBLKSIZ];
198
199  if (dbm_error(db))
200    return (-1);
201  if (dbm_rdonly(db)) {
202    errno = EPERM;
203    return (-1);
204  }
205loop:
206  dbm_access(db, dcalchash(key));
207  if ((i = finddatum(db->dbm_pagbuf, key)) >= 0) {
208    if (!replace)
209      return (1);
210    if (!delitem(db->dbm_pagbuf, i)) {
211      db->dbm_flags |= _DBM_IOERR;
212      return (-1);
213    }
214  }
215  if (!additem(db->dbm_pagbuf, key, dat))
216    goto split;
217  db->dbm_pagbno = db->dbm_blkno;
218  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
219  if ( (ret=write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ)) != PBLKSIZ) {
220    db->dbm_flags |= _DBM_IOERR;
221    return (-1);
222  }
223  return (0);
224 
225split:
226  if (key.dsize+dat.dsize+3*sizeof(short) >= PBLKSIZ) {
227    db->dbm_flags |= _DBM_IOERR;
228    errno = ENOSPC;
229    return (-1);
230  }
231  memset(ovfbuf, 0, PBLKSIZ);
232  for (i=0;;) {
233    item = makdatum(db->dbm_pagbuf, i);
234    if (item.dptr == NULL)
235      break;
236    if (dcalchash(item) & (db->dbm_hmask+1)) {
237      item1 = makdatum(db->dbm_pagbuf, i+1);
238      if (item1.dptr == NULL) {
239        fprintf(stderr, "ndbm: split not paired\n");
240        db->dbm_flags |= _DBM_IOERR;
241        break;
242      }
243      if (!additem(ovfbuf, item, item1) ||
244          !delitem(db->dbm_pagbuf, i)) {
245        db->dbm_flags |= _DBM_IOERR;
246        return (-1);
247      }
248      continue;
249    }
250    i += 2;
251  }
252  db->dbm_pagbno = db->dbm_blkno;
253  (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
254  if (write(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ) {
255    db->dbm_flags |= _DBM_IOERR;
256    return (-1);
257  }
258  (void) lseek(db->dbm_pagf, (db->dbm_blkno+db->dbm_hmask+1)*PBLKSIZ, L_SET);
259  if (write(db->dbm_pagf, ovfbuf, PBLKSIZ) != PBLKSIZ) {
260    db->dbm_flags |= _DBM_IOERR;
261    return (-1);
262  }
263  setbit(db);
264  goto loop;
265}
266
267datum
268dbm_firstkey(DBM *db)
269{
270 
271  db->dbm_blkptr = 0L;
272  db->dbm_keyptr = 0;
273  return (dbm_nextkey(db));
274}
275
276datum
277dbm_nextkey(register DBM *db)
278{
279  struct stat statb;
280  datum item;
281 
282  if (dbm_error(db) || fstat(db->dbm_pagf, &statb) < 0)
283                goto err;
284  statb.st_size /= PBLKSIZ;
285  for (;;) {
286    if (db->dbm_blkptr != db->dbm_pagbno) {
287      db->dbm_pagbno = db->dbm_blkptr;
288      (void) lseek(db->dbm_pagf, db->dbm_blkptr*PBLKSIZ, L_SET);
289      if (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      item = makdatum(db->dbm_pagbuf, db->dbm_keyptr);
298      if (item.dptr != NULL) {
299        db->dbm_keyptr += 2;
300        return (item);
301      }
302      db->dbm_keyptr = 0;
303    }
304    if (++db->dbm_blkptr >= statb.st_size)
305      break;
306  }
307err:
308  item.dptr = NULL;
309  item.dsize = 0;
310  return (item);
311}
312
313static void
314dbm_access(register DBM *db, long hash)
315{
316  for (db->dbm_hmask=0;; db->dbm_hmask=(db->dbm_hmask<<1)+1) {
317    db->dbm_blkno = hash & db->dbm_hmask;
318    db->dbm_bitno = db->dbm_blkno + db->dbm_hmask;
319    if (getbit(db) == 0)
320      break;
321  }
322  if (db->dbm_blkno != db->dbm_pagbno) {
323    db->dbm_pagbno = db->dbm_blkno;
324    (void) lseek(db->dbm_pagf, db->dbm_blkno*PBLKSIZ, L_SET);
325    if (read(db->dbm_pagf, db->dbm_pagbuf, PBLKSIZ) != PBLKSIZ)
326      memset(db->dbm_pagbuf, 0, PBLKSIZ);
327#ifdef DEBUG
328    else if (chkblk(db->dbm_pagbuf) < 0)
329      db->dbm_flags |= _DBM_IOERR;
330#endif
331  }
332}
333
334static
335int getbit(register DBM *db)
336{
337  long bn;
338  register b, i, n;
339       
340
341  if (db->dbm_bitno > db->dbm_maxbno)
342    return (0);
343  n = db->dbm_bitno % BYTESIZ;
344  bn = db->dbm_bitno / BYTESIZ;
345  i = bn % DBLKSIZ;
346  b = bn / DBLKSIZ;
347  if (b != db->dbm_dirbno) {
348    db->dbm_dirbno = b;
349    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
350    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
351      memset(db->dbm_dirbuf, 0, DBLKSIZ);
352  }
353  return (db->dbm_dirbuf[i] & (1<<n));
354}
355
356static void
357setbit(register DBM *db)
358{
359  long bn;
360  register i, n, b;
361
362  if (db->dbm_bitno > db->dbm_maxbno)
363    db->dbm_maxbno = db->dbm_bitno;
364  n = db->dbm_bitno % BYTESIZ;
365  bn = db->dbm_bitno / BYTESIZ;
366  i = bn % DBLKSIZ;
367  b = bn / DBLKSIZ;
368  if (b != db->dbm_dirbno) {
369    db->dbm_dirbno = b;
370    (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
371    if (read(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
372      memset(db->dbm_dirbuf, 0, DBLKSIZ);
373  }
374  db->dbm_dirbuf[i] |= 1<<n;
375  db->dbm_dirbno = b;
376  (void) lseek(db->dbm_dirf, (long)b*DBLKSIZ, L_SET);
377  if (write(db->dbm_dirf, db->dbm_dirbuf, DBLKSIZ) != DBLKSIZ)
378    db->dbm_flags |= _DBM_IOERR;
379}
380
381static datum
382makdatum(char buf[PBLKSIZ], int n)
383{
384  register short *sp;
385  register t;
386  datum item;
387
388  sp = (short *)buf;
389  if ((unsigned)n >= sp[0]) {
390    item.dptr = NULL;
391    item.dsize = 0;
392    return (item);
393  }
394  t = PBLKSIZ;
395  if (n > 0)
396    t = sp[n];
397  item.dptr = buf+sp[n+1];
398  item.dsize = t - sp[n+1];
399  return (item);
400}
401
402static
403int finddatum(char buf[PBLKSIZ], datum item)
404{
405  register short *sp;
406  register int i, n, j;
407
408  sp = (short *)buf;
409  n = PBLKSIZ;
410  for (i=0, j=sp[0]; i<j; i+=2, n = sp[i]) {
411    n -= sp[i+1];
412    if (n != item.dsize)
413      continue;
414    if (n == 0 || memcmp(&buf[sp[i+1]], item.dptr, n) == 0)
415      return (i);
416  }
417  return (-1);
418}
419
420static  int hitab[16]
421/* ken's
422{
423        055,043,036,054,063,014,004,005,
424        010,064,077,000,035,027,025,071,
425};
426*/
427 = {    61, 57, 53, 49, 45, 41, 37, 33,
428        29, 25, 21, 17, 13,  9,  5,  1,
429};
430static  long hltab[64]
431 = {
432        06100151277L,06106161736L,06452611562L,05001724107L,
433        02614772546L,04120731531L,04665262210L,07347467531L,
434        06735253126L,06042345173L,03072226605L,01464164730L,
435        03247435524L,07652510057L,01546775256L,05714532133L,
436        06173260402L,07517101630L,02431460343L,01743245566L,
437        00261675137L,02433103631L,03421772437L,04447707466L,
438        04435620103L,03757017115L,03641531772L,06767633246L,
439        02673230344L,00260612216L,04133454451L,00615531516L,
440        06137717526L,02574116560L,02304023373L,07061702261L,
441        05153031405L,05322056705L,07401116734L,06552375715L,
442        06165233473L,05311063631L,01212221723L,01052267235L,
443        06000615237L,01075222665L,06330216006L,04402355630L,
444        01451177262L,02000133436L,06025467062L,07121076461L,
445        03123433522L,01010635225L,01716177066L,05161746527L,
446        01736635071L,06243505026L,03637211610L,01756474365L,
447        04723077174L,03642763134L,05750130273L,03655541561L,
448};
449
450static long
451hashinc(register DBM *db, long hash)
452{
453  long bit;
454
455  hash &= db->dbm_hmask;
456  bit = db->dbm_hmask+1;
457  for (;;) {
458    bit >>= 1;
459    if (bit == 0)
460      return (0L);
461    if ((hash & bit) == 0)
462      return (hash | bit);
463    hash &= ~bit;
464  }
465}
466
467static long
468dcalchash(datum item)
469{
470  register int s, c, j;
471  register char *cp;
472  register long hashl;
473  register int hashi;
474 
475  hashl = 0;
476  hashi = 0;
477  for (cp = item.dptr, s=item.dsize; --s >= 0; ) {
478    c = *cp++;
479    for (j=0; j<BYTESIZ; j+=4) {
480      hashi += hitab[c&017];
481      hashl += hltab[hashi&63];
482      c >>= 4;
483    }
484  }
485  return (hashl);
486}
487
488/*
489 * Delete pairs of items (n & n+1).
490 */
491static
492int delitem(char buf[PBLKSIZ], int n)
493{
494  register short *sp, *sp1;
495  register i1, i2;
496
497  sp = (short *)buf;
498  i2 = sp[0];
499  if ((unsigned)n >= i2 || (n & 1))
500    return (0);
501  if (n == i2-2) {
502    sp[0] -= 2;
503    return (1);
504  }
505  i1 = PBLKSIZ;
506  if (n > 0)
507    i1 = sp[n];
508  i1 -= sp[n+2];
509  if (i1 > 0) {
510    i2 = sp[i2];
511    bcopy(&buf[i2], &buf[i2 + i1], sp[n+2] - i2);
512  }
513  sp[0] -= 2;
514  for (sp1 = sp + sp[0], sp += n+1; sp <= sp1; sp++)
515    sp[0] = sp[2] + i1;
516  return (1);
517}
518
519/*
520 * Add pairs of items (item & item1).
521 */
522static
523int additem(char buf[PBLKSIZ], datum item, datum item1)
524{
525  register short *sp;
526  register i1, i2, tmp;
527
528  sp = (short *)buf;
529  i1 = PBLKSIZ;
530  i2 = sp[0];
531  if (i2 > 0)
532    i1 = sp[i2];
533  i1 -= item.dsize + item1.dsize;
534  tmp = (i2+3) * sizeof(short);
535  if (i1 <= tmp) return (0);
536  sp[0] += 2;
537  sp[++i2] = i1 + item1.dsize;
538  bcopy(item.dptr, &buf[i1 + item1.dsize], item.dsize);
539  sp[++i2] = i1;
540  bcopy(item1.dptr, &buf[i1], item1.dsize);
541  return (1);
542}
543
544#ifdef DEBUG
545static
546chkblk(char buf[PBLKSIZ])
547{
548  register short *sp;
549  register t, i;
550
551  sp = (short *)buf;
552  t = PBLKSIZ;
553  for (i=0; i<sp[0]; i++) {
554    if (sp[i+1] > t)
555      return (-1);
556    t = sp[i+1];
557  }
558  if (t < (sp[0]+1)*sizeof(short))
559    return (-1);
560  return (0);
561}
562#endif
563
564#endif /* HAVE_DBM */
Note: See TracBrowser for help on using the repository browser.