| 1 |
/* Copyright (c) 1987, 1989, 2012 University of Maryland Department of |
| 2 |
Computer Science. |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
a copy of this software and associated documentation files (the |
| 6 |
"Software"), to deal in the Software without restriction, including |
| 7 |
without limitation, the rights to use, copy, modify, merge, publish, |
| 8 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
permit persons to whom the Software is furnished to do so, subject to |
| 10 |
the following conditions: The above copyright notice, this permission |
| 11 |
notice and the disclaimer statement shall be included in all copies |
| 12 |
or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* Key search routines (for a 32 bit key) |
| 25 |
* |
| 26 |
* SCreate initializes the search control area. |
| 27 |
* |
| 28 |
* SSearch returns the address of the data area (if found or created) |
| 29 |
* or a null pointer (if not). The last argument controls the disposition |
| 30 |
* in various cases, and is a ``value-result'' parameter. |
| 31 |
* |
| 32 |
* SEnumerate calls the given function on each data object within the |
| 33 |
* search table. Note that no ordering is specified (though currently |
| 34 |
* it runs in increasing-key-value sequence). |
| 35 |
*/ |
| 36 |
|
| 37 |
#include "types.h" |
| 38 |
#include "search.h" |
| 39 |
|
| 40 |
#if vax || mc68000 || ns32000 || pyr |
| 41 |
#define HARD_ALIGNMENT 4 |
| 42 |
#else |
| 43 |
#define HARD_ALIGNMENT 16 /* should suffice for most everyone */ |
| 44 |
#endif |
| 45 |
|
| 46 |
static int DOffset; /* part of alignment code */ |
| 47 |
|
| 48 |
#ifndef KPATHSEA |
| 49 |
char *malloc(), *realloc(); |
| 50 |
#endif |
| 51 |
|
| 52 |
struct search * |
| 53 |
SCreate(unsigned int dsize) |
| 54 |
{ |
| 55 |
register struct search *s; |
| 56 |
|
| 57 |
if ((s = (struct search *) malloc(sizeof *s)) == 0) |
| 58 |
return (0); |
| 59 |
|
| 60 |
if (DOffset == 0) { |
| 61 |
#ifndef HARD_ALIGNMENT |
| 62 |
DOffset = sizeof(i32); |
| 63 |
#else |
| 64 |
DOffset = (sizeof(i32) + HARD_ALIGNMENT - 1) & |
| 65 |
~(HARD_ALIGNMENT - 1); |
| 66 |
#endif |
| 67 |
} |
| 68 |
dsize += DOffset; /* tack on space for keys */ |
| 69 |
|
| 70 |
#ifdef HARD_ALIGNMENT |
| 71 |
/* |
| 72 |
* For machines with strict alignment constraints, it may be |
| 73 |
* necessary to align the data at a multiple of some positive power |
| 74 |
* of two. In general, it would suffice to make dsize a power of |
| 75 |
* two, but this could be very space-wasteful, so instead we align it |
| 76 |
* to HARD_ALIGNMENT. 64 bit machines might ``#define HARD_ALIGNMENT |
| 77 |
* 8'', for example. N.B.: we assume that HARD_ALIGNMENT is a power |
| 78 |
* of two. |
| 79 |
*/ |
| 80 |
|
| 81 |
dsize = (dsize + HARD_ALIGNMENT - 1) & ~(HARD_ALIGNMENT - 1); |
| 82 |
#endif |
| 83 |
|
| 84 |
s->s_dsize = dsize; /* save data object size */ |
| 85 |
s->s_space = 10; /* initially, room for 10 objects */ |
| 86 |
s->s_n = 0; /* and none in the table */ |
| 87 |
if ((s->s_data = malloc(s->s_space * dsize)) == 0) { |
| 88 |
free((char *)s); |
| 89 |
return (0); |
| 90 |
} |
| 91 |
return (s); |
| 92 |
} |
| 93 |
|
| 94 |
/* |
| 95 |
* We actually use a binary search right now - this may change. |
| 96 |
*/ |
| 97 |
char * |
| 98 |
SSearch(struct search *s, i32 key, int *disp) |
| 99 |
{ |
| 100 |
register char *keyaddr; |
| 101 |
int itemstomove; |
| 102 |
|
| 103 |
*disp &= S_CREATE | S_EXCL; /* clear return codes */ |
| 104 |
if (s->s_n) { /* look for the key */ |
| 105 |
register int h, l, m; |
| 106 |
|
| 107 |
h = s->s_n - 1; |
| 108 |
l = 0; |
| 109 |
while (l <= h) { |
| 110 |
m = (l + h) >> 1; |
| 111 |
keyaddr = s->s_data + m * s->s_dsize; |
| 112 |
if (*(i32 *) keyaddr > key) |
| 113 |
h = m - 1; |
| 114 |
else if (*(i32 *) keyaddr < key) |
| 115 |
l = m + 1; |
| 116 |
else { /* found it, now what? */ |
| 117 |
if (*disp & S_EXCL) { |
| 118 |
*disp |= S_COLL; |
| 119 |
return (0); /* collision */ |
| 120 |
} |
| 121 |
*disp |= S_FOUND; |
| 122 |
return (keyaddr + DOffset); |
| 123 |
} |
| 124 |
} |
| 125 |
keyaddr = s->s_data + l * s->s_dsize; |
| 126 |
} else |
| 127 |
keyaddr = s->s_data; |
| 128 |
|
| 129 |
/* keyaddr is now where the key should have been found, if anywhere */ |
| 130 |
if ((*disp & S_CREATE) == 0) |
| 131 |
return (0); /* not found */ |
| 132 |
|
| 133 |
/* avoid using realloc so as to retain old data if out of memory */ |
| 134 |
if (s->s_space <= 0) { /* must expand; double it */ |
| 135 |
register char *new; |
| 136 |
|
| 137 |
if ((new = malloc((s->s_n << 1) * s->s_dsize)) == 0) { |
| 138 |
*disp |= S_ERROR; /* no space */ |
| 139 |
return (0); |
| 140 |
} |
| 141 |
keyaddr = (keyaddr - s->s_data) + new; /* relocate */ |
| 142 |
memcpy(new, s->s_data, s->s_n * s->s_dsize); |
| 143 |
free(s->s_data); |
| 144 |
s->s_data = new; |
| 145 |
s->s_space = s->s_n; |
| 146 |
} |
| 147 |
/* now move any keyed data that is beyond keyaddr down */ |
| 148 |
itemstomove = s->s_n - (keyaddr - s->s_data) / s->s_dsize; |
| 149 |
if (itemstomove) { |
| 150 |
#ifndef BLOCK_COPY |
| 151 |
register char *from, *to; |
| 152 |
|
| 153 |
from = s->s_data + s->s_n * s->s_dsize; |
| 154 |
to = from + s->s_dsize; |
| 155 |
while (from > keyaddr) |
| 156 |
*--to = *--from; |
| 157 |
#else |
| 158 |
BLOCK_COPY(keyaddr, keyaddr + s->s_dsize, |
| 159 |
itemstomove * s->s_dsize); |
| 160 |
#endif |
| 161 |
} |
| 162 |
*disp |= S_NEW; |
| 163 |
s->s_n++; |
| 164 |
s->s_space--; |
| 165 |
*(i32 *) keyaddr = key; |
| 166 |
keyaddr += DOffset; /* now actually dataaddr */ |
| 167 |
/* the memset is just a frill... */ |
| 168 |
memset(keyaddr, 0, s->s_dsize - DOffset); |
| 169 |
return (keyaddr); |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Call function `f' for each element in the search table `s'. |
| 174 |
*/ |
| 175 |
void |
| 176 |
SEnumerate(struct search *s, void(*f)(char*,i32)) |
| 177 |
{ |
| 178 |
register int n; |
| 179 |
register char *p; |
| 180 |
|
| 181 |
n = s->s_n; |
| 182 |
p = s->s_data; |
| 183 |
while (--n >= 0) { |
| 184 |
(*f)(p + DOffset, *(i32 *)p); |
| 185 |
p += s->s_dsize; |
| 186 |
} |
| 187 |
} |