2 * Copyright (c) 2001 - 2003
3 * NetGroup, Politecnico di Torino (Italy)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Politecnico di Torino nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "normal_lookup.h"
41 #include <net/tme/tme.h>
42 #include <net/tme/normal_lookup.h>
45 #include <tme/normal_lookup.h>
51 /* lookup in the table, seen as an hash */
52 /* if not found, inserts an element */
53 /* returns TME_TRUE if the entry is found or created, */
54 /* returns TME_FALSE if no more blocks are available */
55 uint32
normal_lut_w_insert(uint8
*key
, TME_DATA
*data
, MEM_TYPE
*mem_ex
, struct time_conv
*time_ref
)
59 uint32
*key32
=(uint32
*) key
;
60 uint32 shrinked_key
=0;
62 RECORD
*records
=(RECORD
*)data
->lut_base_address
;
64 uint32 key_len
=data
->key_len
;
65 /*the key is shrinked into a 32-bit value */
66 for (i
=0; i
<key_len
;i
++)
67 shrinked_key
^=key32
[i
];
68 /*the first index in the table is calculated*/
69 index
=shrinked_key
% data
->lut_entries
;
71 while (tocs
<=data
->filled_entries
)
74 if (records
[index
].block
==0)
75 { /*creation of a new entry*/
77 if (data
->filled_blocks
==data
->shared_memory_blocks
)
79 /*no more free blocks*/
80 GET_TIME((struct timeval
*)(data
->shared_memory_base_address
+4*key_len
),time_ref
);
81 data
->last_found
=NULL
;
85 /*offset=absolute pointer to the block associated*/
86 /*with the newly created entry*/
87 offset
=data
->shared_memory_base_address
+
88 data
->block_size
*data
->filled_blocks
;
90 /*copy the key in the block*/
91 COPY_MEMORY(offset
,key32
,key_len
*4);
92 GET_TIME((struct timeval
*)(offset
+4*key_len
),time_ref
);
93 /*assign the block relative offset to the entry, in NBO*/
94 SW_ULONG_ASSIGN(&records
[index
].block
,offset
-mem_ex
->buffer
);
96 data
->filled_blocks
++;
98 /*assign the exec function ID to the entry, in NBO*/
99 SW_ULONG_ASSIGN(&records
[index
].exec_fcn
,data
->default_exec
);
100 data
->filled_entries
++;
102 data
->last_found
=(uint8
*)&records
[index
];
106 /*offset contains the absolute pointer to the block*/
107 /*associated with the current entry */
108 offset
=mem_ex
->buffer
+SW_ULONG_AT(&records
[index
].block
,0);
110 for (i
=0; (i
<key_len
) && (key32
[i
]==ULONG_AT(offset
,i
*4)); i
++);
114 /*key in the block matches the one provided, right entry*/
115 GET_TIME((struct timeval
*)(offset
+4*key_len
),time_ref
);
116 data
->last_found
=(uint8
*)&records
[index
];
121 /* wrong entry, rehashing */
122 if (IS_DELETABLE(offset
+key_len
*4,data
))
124 ZERO_MEMORY(offset
,data
->block_size
);
125 COPY_MEMORY(offset
,key32
,key_len
*4);
126 SW_ULONG_ASSIGN(&records
[index
].exec_fcn
,data
->default_exec
);
127 GET_TIME((struct timeval
*)(offset
+key_len
*4),time_ref
);
128 data
->last_found
=(uint8
*)&records
[index
];
133 index
=(index
+data
->rehashing_value
) % data
->lut_entries
;
139 /* nothing found, last found= out of lut */
140 GET_TIME((struct timeval
*)(data
->shared_memory_base_address
+4*key_len
),time_ref
);
141 data
->last_found
=NULL
;
146 /* lookup in the table, seen as an hash */
147 /* if not found, returns out of count entry index */
148 /* returns TME_TRUE if the entry is found */
149 /* returns TME_FALSE if the entry is not found */
150 uint32
normal_lut_wo_insert(uint8
*key
, TME_DATA
*data
, MEM_TYPE
*mem_ex
, struct time_conv
*time_ref
)
154 uint32
*key32
=(uint32
*) key
;
155 uint32 shrinked_key
=0;
157 RECORD
*records
=(RECORD
*)data
->lut_base_address
;
159 uint32 key_len
=data
->key_len
;
160 /*the key is shrinked into a 32-bit value */
161 for (i
=0; i
<key_len
;i
++)
162 shrinked_key
^=key32
[i
];
163 /*the first index in the table is calculated*/
164 index
=shrinked_key
% data
->lut_entries
;
166 while (tocs
<=data
->filled_entries
)
169 if (records
[index
].block
==0)
170 { /*out of table, insertion is not allowed*/
171 GET_TIME((struct timeval
*)(data
->shared_memory_base_address
+4*key_len
),time_ref
);
172 data
->last_found
=NULL
;
175 /*offset contains the absolute pointer to the block*/
176 /*associated with the current entry */
178 offset
=mem_ex
->buffer
+SW_ULONG_AT(&records
[index
].block
,0);
180 for (i
=0; (i
<key_len
) && (key32
[i
]==ULONG_AT(offset
,i
*4)); i
++);
184 /*key in the block matches the one provided, right entry*/
185 GET_TIME((struct timeval
*)(offset
+4*key_len
),time_ref
);
186 data
->last_found
=(uint8
*)&records
[index
];
191 /*wrong entry, rehashing*/
192 index
=(index
+data
->rehashing_value
) % data
->lut_entries
;
197 /*nothing found, last found= out of lut*/
198 GET_TIME((struct timeval
*)(data
->shared_memory_base_address
+4*key_len
),time_ref
);
199 data
->last_found
=NULL
;