2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 //%% (c) Copyright 1993, 1994 Hewlett-Packard Company
24 //%% (c) Copyright 1993, 1994 International Business Machines Corp.
25 //%% (c) Copyright 1993, 1994 Sun Microsystems, Inc.
26 //%% (c) Copyright 1993, 1994 Novell, Inc.
27 //%% $XConsortium: dm_access_cache.C /main/3 1995/10/20 16:40:39 rswiston $
29 * Tool Talk Database Manager - dm_access_cache.h
31 * Copyright (c) 1989 Sun Microsystems, Inc.
33 * This file contains class implementation for the oid access info cache.
37 #include "dm_access_cache.h"
42 implement_ptr_to(_Tt_oid_access)
43 implement_ptr_to(_Tt_oid_access_elem)
44 implement_ptr_to(_Tt_oid_access_queue)
45 implement_ptr_to(_Tt_link_access)
46 implement_ptr_to(_Tt_link_access_elem)
47 implement_ptr_to(_Tt_link_access_queue)
49 static char _tt_access_record[ISMAXRECLEN];
52 * class _Tt_oid_access
55 _Tt_oid_access::_Tt_oid_access(char *ku)
57 memcpy(_key, ku, OID_KEY_LENGTH);
58 memcpy((char *)&_user, ku + OID_KEY_LENGTH, sizeof(uid_t));
59 memcpy((char *)&_group, ku + OID_KEY_LENGTH + sizeof(uid_t),
61 memcpy((char *)&_mode,
62 ku + OID_KEY_LENGTH + sizeof(uid_t) + sizeof(gid_t),
66 _Tt_oid_access::_Tt_oid_access(const char *key, uid_t user, gid_t group,
69 memcpy(_key, key, OID_KEY_LENGTH);
75 _Tt_oid_access::~_Tt_oid_access()
82 memcpy(_tt_access_record, _key, OID_KEY_LENGTH);
83 memcpy(_tt_access_record + OID_KEY_LENGTH, (char *)&_user,
85 memcpy(_tt_access_record + OID_KEY_LENGTH + sizeof(uid_t),
86 (char *)&_group, sizeof(gid_t));
87 memcpy(_tt_access_record + OID_KEY_LENGTH + sizeof(uid_t) + sizeof(gid_t),
88 (char *)&_mode, sizeof(mode_t));
89 return _tt_access_record;
93 _Tt_oid_access::print(FILE *fs) const
95 fprintf(fs, "oid-access entry: ");
96 fprintf(fs, "key - <%d, %d, %d, %d>, user = %d\n",
97 *((short *) ((char *)_key)),
98 *((int *) ((char *)_key + 4)), *((int *) ((char *)_key + 8)),
99 *((int *) ((char *)_key + 12)), _user);
103 * class _Tt_oid_access_elem
106 _Tt_oid_access_elem::_Tt_oid_access_elem(_Tt_oid_access_ptr oa,
107 _Tt_oid_access_elem_ptr next)
114 _Tt_oid_access_elem::print(FILE *fs) const
120 * class _Tt_oid_access_queue
123 _Tt_oid_access_queue::_Tt_oid_access_queue()
127 for (int i = 0; i < DM_OID_ACCESS_BUCKETS; i++) {
132 _Tt_oid_access_queue::~_Tt_oid_access_queue()
137 _Tt_oid_access_queue::enqueue(_Tt_oid_access_ptr oa)
139 if (_len == DM_MAX_ACCESS_ELEMS) {
142 /* put the new element on the LRU list */
143 _Tt_oid_access_elem_ptr oae = new _Tt_oid_access_elem(oa, _head);
144 if (_head.is_null()) {
148 /* put the new element in the lookup table */
149 int bucket = hash(oa->key());
150 _table[bucket] = new _Tt_oid_access_elem(oa, _table[bucket]);
155 _Tt_oid_access_queue::lookup(const char *key)
157 if (!key) { /* erroneous condition, read by record number needs key */
160 int bucket_no = hash(key);
161 _Tt_oid_access_elem_ptr e = _table[bucket_no];
162 while (!e.is_null()) {
163 _Tt_oid_access_ptr oa = e->oa();
164 if (memcmp(oa->key(), key, OID_KEY_LENGTH) == 0) {
173 * remove - remove the oid access element from both the LRU list and the lookup
174 * table. Does not delete the element.
178 _Tt_oid_access_queue::remove(_Tt_oid_access_ptr oa)
180 /* remove from the LRU list */
181 _Tt_oid_access_elem_ptr prev = 0;
182 _Tt_oid_access_elem_ptr cur = _head;
183 while (!cur.is_null()) {
184 if (cur->oa().is_eq(oa)) {
185 if (prev.is_null()) {
186 _head = _head->next();
188 prev->set_next(cur->next());
190 if (cur.is_eq(_tail)) {
199 /* remove from the lookup table */
200 int bucket = hash(oa->key());
202 cur = _table[bucket];
203 while (!cur.is_null()) {
204 if (cur->oa().is_eq(oa)) {
205 if (prev.is_null()) {
206 _table[bucket] = _table[bucket]->next();
208 prev->set_next(cur->next());
220 * promote - promote the oid access element to the front of the LRU list.
224 _Tt_oid_access_queue::promote(_Tt_oid_access_ptr oa)
226 if (_head->oa().is_eq(oa)) {
229 /* remove from the LRU list */
230 _Tt_oid_access_elem_ptr prev = _head;
231 _Tt_oid_access_elem_ptr cur = prev->next();
232 while (!cur.is_null()) {
233 if (cur->oa().is_eq(oa)) {
234 prev->set_next(cur->next());
235 if (cur.is_eq(_tail)) {
238 cur->set_next(_head);
249 * Remove the LRU element from the LRU list and lookup table and deletes it.
253 _Tt_oid_access_queue::dequeue()
255 if (!_tail.is_null()) {
261 _Tt_oid_access_queue::hash(const char *key)
264 for (int i = 0; i < OID_KEY_LENGTH; i++) {
265 hash_value += key[i] & 0177; /* ignore sign */
267 hash_value = abs(hash_value);
268 return (hash_value % DM_OID_ACCESS_BUCKETS);
272 _Tt_oid_access_queue::print(FILE *fs) const
274 fprintf(fs, "\nOID-ACCESS QUEUE list (len = %d):\n", _len);
275 _Tt_oid_access_elem_ptr e = _head;
276 while (!e.is_null()) {
280 fprintf(fs, "OID-ACCESS QUEUE table:\n");
281 for (int i = 0; i < DM_OID_ACCESS_BUCKETS; i++) {
284 fprintf(fs, "bucket %d:\n", i);
285 while (!e.is_null()) {
295 * class _Tt_link_access
298 _Tt_link_access::_Tt_link_access(char *kp)
300 memcpy(_key, kp, OID_KEY_LENGTH);
301 memcpy((char *)&_user, kp + OID_KEY_LENGTH, sizeof(uid_t));
302 memcpy((char *)&_group, kp + OID_KEY_LENGTH + sizeof(uid_t),
304 memcpy((char *)&_mode,
305 kp + OID_KEY_LENGTH + sizeof(uid_t) + sizeof(gid_t),
309 _Tt_link_access::_Tt_link_access(const char *key, uid_t user, gid_t group,
312 memcpy(_key, key, OID_KEY_LENGTH);
318 _Tt_link_access::~_Tt_link_access()
323 _Tt_link_access::rec()
325 memcpy(_tt_access_record, _key, OID_KEY_LENGTH);
326 memcpy(_tt_access_record + OID_KEY_LENGTH, (char *)&_user,
328 memcpy(_tt_access_record + OID_KEY_LENGTH + sizeof(uid_t),
329 (char *)&_group, sizeof(gid_t));
330 memcpy(_tt_access_record + OID_KEY_LENGTH + sizeof(uid_t) + sizeof(gid_t),
331 (char *)&_mode, sizeof(mode_t));
332 return _tt_access_record;
336 _Tt_link_access::print(FILE *fs) const
338 fprintf(fs, "link-access entry: ");
339 fprintf(fs, "key - <%d, %d, %d, %d>, user = %d\n",
340 *((short *) ((char *)_key)),
341 *((int *) ((char *)_key + 4)), *((int *) ((char *)_key + 8)),
342 *((int *) ((char *)_key + 12)), _user);
346 * class _Tt_link_access_elem
349 _Tt_link_access_elem::_Tt_link_access_elem(_Tt_link_access_ptr oa,
350 _Tt_link_access_elem_ptr next)
357 _Tt_link_access_elem::print(FILE *fs) const
363 * class _Tt_link_access_queue
366 _Tt_link_access_queue::_Tt_link_access_queue()
370 for (int i = 0; i < DM_OID_ACCESS_BUCKETS; i++) {
375 _Tt_link_access_queue::~_Tt_link_access_queue()
380 _Tt_link_access_queue::enqueue(_Tt_link_access_ptr oa)
382 if (_len == DM_MAX_ACCESS_ELEMS) {
385 /* put the new element on the LRU list */
386 _Tt_link_access_elem_ptr oae = new _Tt_link_access_elem(oa, _head);
387 if (_head.is_null()) {
391 /* put the new element in the lookup table */
392 int bucket = hash(oa->key());
393 _table[bucket] = new _Tt_link_access_elem(oa, _table[bucket]);
398 _Tt_link_access_queue::lookup(const char *key)
400 if (!key) { /* erroneous condition, read by record number needs key */
403 _Tt_link_access_elem_ptr e = _table[hash(key)];
404 while (!e.is_null()) {
405 _Tt_link_access_ptr oa = e->oa();
406 if (memcmp(oa->key(), key, OID_KEY_LENGTH) == 0) {
415 * remove - remove the oid access element from both the LRU list and the lookup
416 * table. Does not delete the element.
420 _Tt_link_access_queue::remove(_Tt_link_access_ptr oa)
422 /* remove from the LRU list */
423 _Tt_link_access_elem_ptr prev = 0;
424 _Tt_link_access_elem_ptr cur = _head;
425 while (!cur.is_null()) {
426 if (cur->oa().is_eq(oa)) {
427 if (prev.is_null()) {
428 _head = _head->next();
430 prev->set_next(cur->next());
432 if (cur.is_eq(_tail)) {
441 /* remove from the lookup table */
442 int bucket = hash(oa->key());
444 cur = _table[bucket];
445 while (!cur.is_null()) {
446 if (cur->oa().is_eq(oa)) {
447 if (prev.is_null()) {
448 _table[bucket] = _table[bucket]->next();
450 prev->set_next(cur->next());
462 * promote - promote the oid access element to the front of the LRU list.
466 _Tt_link_access_queue::promote(_Tt_link_access_ptr oa)
468 if (_head->oa().is_eq(oa)) {
471 /* remove from the LRU list */
472 _Tt_link_access_elem_ptr prev = _head;
473 _Tt_link_access_elem_ptr cur = prev->next();
474 while (!cur.is_null()) {
475 if (cur->oa().is_eq(oa)) {
476 prev->set_next(cur->next());
477 if (cur.is_eq(_tail)) {
480 cur->set_next(_head);
491 * Remove the LRU element from the LRU list and lookup table and deletes it.
495 _Tt_link_access_queue::dequeue()
497 if (!_tail.is_null()) {
503 _Tt_link_access_queue::hash(const char *key)
506 for (int i = 0; i < OID_KEY_LENGTH; i++) {
507 hash_value += key[i] & 0177; /* ignore sign */
509 hash_value = abs(hash_value);
510 return (hash_value % DM_OID_ACCESS_BUCKETS);
514 _Tt_link_access_queue::print(FILE *fs) const
516 fprintf(fs, "\nLINK-ACCESS QUEUE list (len = %d):\n", _len);
517 _Tt_link_access_elem_ptr e = _head;
518 while (!e.is_null()) {
522 fprintf(fs, "LINK-ACCESS QUEUE table:\n");
523 for (int i = 0; i < DM_OID_ACCESS_BUCKETS; i++) {
526 fprintf(fs, "bucket %d:\n", i);
527 while (!e.is_null()) {