1 /* Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
25 #include "DbeLinkList.h"
28 typedef unsigned long hash_ty
;
30 template <class ITEM
> class DbeSyncMap
: public DbeLock
33 DbeSyncMap (int _chunkSize
= DefaultChunkSize
);
34 virtual ~DbeSyncMap ();
36 ITEM
*get (const char *nm
, int64_t chksum
);
37 ITEM
*sync_create_item (const char *nm
, int64_t chksum
);
38 ITEM
*get (const char *nm
, const char *path
, DbeFile
*df
);
39 ITEM
*sync_create_item (const char *nm
, const char *path
, DbeFile
*df
);
49 hash_ty
hash (const char *key
);
51 DbeLinkList
<ITEM
*> **chunk
;
52 Vector
<ITEM
*> *items
;
57 DefaultChunkSize
= 1024
62 DbeSyncMap
<ITEM
>::DbeSyncMap (int _chunkSize
)
64 chunkSize
= _chunkSize
;
65 chunk
= new DbeLinkList
<ITEM
*> * [chunkSize
];
66 for (long i
= 0; i
< chunkSize
; i
++)
68 items
= new Vector
<ITEM
*>(512);
72 DbeSyncMap
<ITEM
>::~DbeSyncMap ()
74 for (long i
= 0; i
< chunkSize
; i
++)
82 DbeSyncMap
<ITEM
>::reset ()
84 for (long i
= 0; i
< chunkSize
; i
++)
94 DbeSyncMap
<ITEM
>::get (const char *nm
, int64_t chksum
)
96 hash_ty h
= hash (nm
);
97 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
99 ITEM
*item
= dl
->get_item ();
100 if (item
->compare (nm
, chksum
))
106 template <class ITEM
>
108 DbeSyncMap
<ITEM
>::hash (const char *key
)
110 return (hash_ty
) (crc64 (key
, strlen (key
)) % chunkSize
);
113 template <class ITEM
>
115 DbeSyncMap
<ITEM
>::sync_create_item (const char *nm
, int64_t chksum
)
117 hash_ty h
= hash (nm
);
118 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
120 ITEM
*item
= dl
->get_item ();
121 if (item
->compare (nm
, chksum
))
125 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
127 ITEM
*item
= dl
->get_item ();
128 if (item
->compare (nm
, chksum
))
134 ITEM
*item
= ITEM::create_item (nm
, chksum
);
135 DbeLinkList
<ITEM
*> *dl
= new DbeLinkList
<ITEM
*>(item
);
136 dl
->set_next (chunk
[h
]);
138 items
->append (item
);
143 template <class ITEM
>
145 DbeSyncMap
<ITEM
>::get (const char *nm
, const char *path
, DbeFile
*df
)
147 int mask
= 1 + (path
!= NULL
? 2 : 0) + (df
!= NULL
? 4 : 0);
148 hash_ty h
= hash (nm
);
149 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
151 ITEM
*item
= dl
->get_item ();
152 if (mask
== item
->compare (nm
, path
, df
))
158 template <class ITEM
>
160 DbeSyncMap
<ITEM
>::sync_create_item (const char *nm
, const char *path
, DbeFile
*df
)
164 mask
+= CMP_RUNTIMEPATH
;
167 hash_ty h
= hash (nm
);
168 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
170 ITEM
*item
= dl
->get_item ();
171 if (mask
== item
->compare (nm
, path
, df
))
175 for (DbeLinkList
<ITEM
*> *dl
= chunk
[h
]; dl
; dl
= dl
->get_next ())
177 ITEM
*item
= dl
->get_item ();
178 if (mask
== item
->compare (nm
, path
, df
))
184 ITEM
*item
= ITEM::create_item (nm
, path
, df
);
185 DbeLinkList
<ITEM
*> *dl
= new DbeLinkList
<ITEM
*>(item
);
186 dl
->set_next (chunk
[h
]);
188 items
->append (item
);
193 template <class ITEM
>
195 DbeSyncMap
<ITEM
>::dump ()
197 Dprintf (1, NTXT ("\nDbeSyncMap::dump: vals=%ld\n"), (long) VecSize (items
));
200 for (long i
= 0; i
< chunkSize
; i
++)
202 DbeLinkList
<ITEM
*> *lp
= chunk
[i
];
206 for (DbeLinkList
<ITEM
*> *lp1
= lp
; lp1
; lp1
= lp1
->get_next ())
212 for (DbeLinkList
<ITEM
*> *lp1
= lp
; lp1
; lp1
= lp1
->get_next ())
214 ITEM
*p
= lp1
->get_item ();
215 Dprintf (1, NTXT (" %2d %s\n"), cnt
, p
->get_name ());
220 Dprintf (1, NTXT ("\nDbeSyncMap::dump: vals=%ld max_cnt=%d tot=%d\n"),
221 (long) VecSize (items
), max_cnt
, tot
);
224 #endif /* _DbeSyncMap_h */