@@ -34,7 +34,8 @@ use middle::region;
34
34
use std:: iter;
35
35
use std:: io;
36
36
use std:: ops:: { Deref , DerefMut } ;
37
- use std:: hash:: Hash ;
37
+ use std:: cmp;
38
+ use std:: hash:: { Hash , Hasher } ;
38
39
use syntax:: ast:: Mutability ;
39
40
use rustc_serialize:: { Encoder , Decodable , Encodable } ;
40
41
use rustc_data_structures:: sorted_map:: SortedMap ;
@@ -217,9 +218,56 @@ impl<'tcx, Tag> Pointer<Tag> {
217
218
}
218
219
219
220
220
- #[ derive( Copy , Clone , Eq , Hash , Ord , PartialEq , PartialOrd , Debug ) ]
221
+ #[ derive( Copy , Clone , Debug ) ]
221
222
pub struct AllocId ( pub u64 ) ;
222
223
224
+ impl Eq for AllocId { }
225
+
226
+ impl Hash for AllocId {
227
+ fn hash < H : Hasher > ( & self , state : & mut H ) {
228
+ ty:: tls:: with ( |tcx| {
229
+ let alloc_type = tcx. alloc_map . lock ( ) . get ( * self ) ;
230
+ match alloc_type {
231
+ Some ( alloc_type) => alloc_type. hash ( state) ,
232
+ None => self . 0 . hash ( state) ,
233
+ }
234
+ } )
235
+ }
236
+ }
237
+
238
+ impl PartialEq for AllocId {
239
+ fn eq ( & self , other : & Self ) -> bool {
240
+ ty:: tls:: with ( |tcx| {
241
+ let ( s, o) = {
242
+ let map = tcx. alloc_map . lock ( ) ;
243
+ ( map. get ( * self ) , map. get ( * other) )
244
+ } ;
245
+ s == o
246
+ } )
247
+ }
248
+ }
249
+
250
+ impl Ord for AllocId {
251
+ fn cmp ( & self , other : & Self ) -> cmp:: Ordering {
252
+ match self . partial_cmp ( other) {
253
+ Some ( ord) => ord,
254
+ None => self . 0 . cmp ( & other. 0 )
255
+ }
256
+ }
257
+ }
258
+
259
+ impl PartialOrd for AllocId {
260
+ fn partial_cmp ( & self , other : & Self ) -> Option < cmp:: Ordering > {
261
+ ty:: tls:: with ( |tcx| {
262
+ let ( s, o) = {
263
+ let map = tcx. alloc_map . lock ( ) ;
264
+ ( map. get ( * self ) ?, map. get ( * other) ?)
265
+ } ;
266
+ s. partial_cmp ( & o)
267
+ } )
268
+ }
269
+ }
270
+
223
271
impl :: rustc_serialize:: UseSpecializedEncodable for AllocId { }
224
272
impl :: rustc_serialize:: UseSpecializedDecodable for AllocId { }
225
273
@@ -427,7 +475,7 @@ impl fmt::Display for AllocId {
427
475
}
428
476
}
429
477
430
- #[ derive( Debug , Clone , Eq , PartialEq , Hash , RustcDecodable , RustcEncodable ) ]
478
+ #[ derive( Debug , Clone , Eq , PartialEq , Ord , PartialOrd , Hash , RustcDecodable , RustcEncodable ) ]
431
479
pub enum AllocType < ' tcx , M > {
432
480
/// The alloc id is used as a function pointer
433
481
Function ( Instance < ' tcx > ) ,
@@ -440,7 +488,7 @@ pub enum AllocType<'tcx, M> {
440
488
441
489
pub struct AllocMap < ' tcx , M > {
442
490
/// Lets you know what an AllocId refers to
443
- id_to_type : FxHashMap < AllocId , AllocType < ' tcx , M > > ,
491
+ id_to_type : FxHashMap < u64 , AllocType < ' tcx , M > > ,
444
492
445
493
/// Used to ensure that functions and statics only get one associated AllocId
446
494
type_interner : FxHashMap < AllocType < ' tcx , M > , AllocId > ,
@@ -479,7 +527,7 @@ impl<'tcx, M: fmt::Debug + Eq + Hash + Clone> AllocMap<'tcx, M> {
479
527
}
480
528
let id = self . reserve ( ) ;
481
529
debug ! ( "creating alloc_type {:?} with id {}" , alloc_type, id) ;
482
- self . id_to_type . insert ( id, alloc_type. clone ( ) ) ;
530
+ self . id_to_type . insert ( id. 0 , alloc_type. clone ( ) ) ;
483
531
self . type_interner . insert ( alloc_type, id) ;
484
532
id
485
533
}
@@ -492,7 +540,7 @@ impl<'tcx, M: fmt::Debug + Eq + Hash + Clone> AllocMap<'tcx, M> {
492
540
}
493
541
494
542
pub fn get ( & self , id : AllocId ) -> Option < AllocType < ' tcx , M > > {
495
- self . id_to_type . get ( & id) . cloned ( )
543
+ self . id_to_type . get ( & id. 0 ) . cloned ( )
496
544
}
497
545
498
546
pub fn unwrap_memory ( & self , id : AllocId ) -> M {
@@ -513,13 +561,13 @@ impl<'tcx, M: fmt::Debug + Eq + Hash + Clone> AllocMap<'tcx, M> {
513
561
}
514
562
515
563
pub fn set_id_memory ( & mut self , id : AllocId , mem : M ) {
516
- if let Some ( old) = self . id_to_type . insert ( id, AllocType :: Memory ( mem) ) {
564
+ if let Some ( old) = self . id_to_type . insert ( id. 0 , AllocType :: Memory ( mem) ) {
517
565
bug ! ( "tried to set allocation id {}, but it was already existing as {:#?}" , id, old) ;
518
566
}
519
567
}
520
568
521
569
pub fn set_id_same_memory ( & mut self , id : AllocId , mem : M ) {
522
- self . id_to_type . insert_same ( id, AllocType :: Memory ( mem) ) ;
570
+ self . id_to_type . insert_same ( id. 0 , AllocType :: Memory ( mem) ) ;
523
571
}
524
572
}
525
573
0 commit comments