1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB 1998-2016. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  *
20 
21  */
22 #include <stdlib.h>
23 #include "reg.h"
24 
do_purge(ei_reg * reg,int i)25 static ei_bucket *do_purge(ei_reg *reg, int i)
26 {
27   ei_hash *tab = reg->tab;
28   ei_bucket *head = tab->tab[i];
29   ei_bucket *this, *next;
30   ei_reg_obj *obj;
31 
32   /* first position special case */
33   while ((this=head)) {
34     obj = (ei_reg_obj*)(this->value); /* cast to eliminate 'const' warning */
35     if (obj->attr & EI_DELET) {
36       head = this->next;
37       ei_reg_free(reg,obj); /* free obj to freelist */
38       ei_hash_bfree(tab,this); /* free bucket to freelist */
39       tab->nelem--;
40     }
41     else break;
42   }
43 
44   /* check remaining positions */
45   this = head;
46   while (this && this->next) {
47     next = this->next;
48     obj = (ei_reg_obj*)(next->value); /* cast to eliminate 'const' warning */
49     if (obj->attr & EI_DELET) {
50       this->next = next->next;
51       ei_reg_free(reg,obj); /* free object to freelist */
52       ei_hash_bfree(tab,next); /* free bucket to freelist */
53       tab->nelem--;
54     }
55     else this = this->next;
56   }
57 
58   return head;
59 }
60 
ei_reg_purge(ei_reg * reg)61 int ei_reg_purge(ei_reg *reg)
62 {
63   ei_hash *tab;
64   int i;
65 
66   if (!reg) return -1;
67   tab = reg->tab;
68 
69   for (i=0;i<tab->size;i++) {
70     if ((tab->tab[i])) {
71       tab->tab[i] = do_purge(reg,i);
72       if (!tab->tab[i]) tab->npos--;
73     }
74   }
75 
76   return 0;
77 }
78