@@ -84,7 +84,7 @@ pub enum Error {
84
84
/// The client passed a memory instance which is smaller than previously observed.
85
85
MemoryShrinked ,
86
86
// TODO: wtf is "Other"?
87
- Other ( & ' static str ) ,
87
+ Other ,
88
88
}
89
89
90
90
/// The maximum number of bytes that can be allocated at one time.
@@ -102,11 +102,6 @@ const ALIGNMENT: u32 = 8;
102
102
// to which it belongs.
103
103
const HEADER_SIZE : u32 = 8 ;
104
104
105
- /// Create an allocator error.
106
- fn error ( msg : & ' static str ) -> Error {
107
- Error :: Other ( msg)
108
- }
109
-
110
105
// The minimum possible allocation size is chosen to be 8 bytes because in that case we would have
111
106
// easier time to provide the guaranteed alignment of 8.
112
107
//
@@ -144,7 +139,7 @@ impl Order {
144
139
if order < N_ORDERS as u32 {
145
140
Ok ( Self ( order) )
146
141
} else {
147
- Err ( error ( "invalid order" ) )
142
+ Err ( Error :: Other )
148
143
}
149
144
}
150
145
@@ -381,7 +376,7 @@ impl FreeingBumpHeapAllocator {
381
376
/// - `size` - size in bytes of the allocation request
382
377
pub fn allocate < M : Memory + ?Sized > ( & mut self , mem : & mut M , size : u32 ) -> Result < u32 , Error > {
383
378
if self . poisoned {
384
- return Err ( error ( "the allocator has been poisoned" ) ) ;
379
+ return Err ( Error :: Other ) ;
385
380
}
386
381
387
382
let bomb = PoisonBomb {
@@ -402,7 +397,7 @@ impl FreeingBumpHeapAllocator {
402
397
// Remove this header from the free list.
403
398
let next_free = Header :: read_from ( mem, header_ptr) ?
404
399
. into_free ( )
405
- . ok_or_else ( || error ( "free list points to a occupied header" ) ) ?;
400
+ . ok_or_else ( || Error :: Other ) ?;
406
401
self . free_lists [ order] = next_free;
407
402
408
403
header_ptr
@@ -443,7 +438,7 @@ impl FreeingBumpHeapAllocator {
443
438
/// - `ptr` - pointer to the allocated chunk
444
439
pub fn deallocate < M : Memory + ?Sized > ( & mut self , mem : & mut M , ptr : u32 ) -> Result < ( ) , Error > {
445
440
if self . poisoned {
446
- return Err ( error ( "the allocator has been poisoned" ) ) ;
441
+ return Err ( Error :: Other ) ;
447
442
}
448
443
449
444
let bomb = PoisonBomb {
@@ -454,11 +449,11 @@ impl FreeingBumpHeapAllocator {
454
449
455
450
let header_ptr = u32:: from ( ptr)
456
451
. checked_sub ( HEADER_SIZE )
457
- . ok_or_else ( || error ( "Invalid pointer for deallocation" ) ) ?;
452
+ . ok_or_else ( || Error :: Other ) ?;
458
453
459
454
let order = Header :: read_from ( mem, header_ptr) ?
460
455
. into_occupied ( )
461
- . ok_or_else ( || error ( "the allocation points to an empty header" ) ) ?;
456
+ . ok_or_else ( || Error :: Other ) ?;
462
457
463
458
// Update the just freed header and knit it back to the free list.
464
459
let prev_head = self . free_lists . replace ( order, Link :: Ptr ( header_ptr) ) ;
@@ -468,7 +463,7 @@ impl FreeingBumpHeapAllocator {
468
463
self . total_size = self
469
464
. total_size
470
465
. checked_sub ( order. size ( ) + HEADER_SIZE )
471
- . ok_or_else ( || error ( "Unable to subtract from total heap size without overflow" ) ) ?;
466
+ . ok_or_else ( || Error :: Other ) ?;
472
467
473
468
bomb. disarm ( ) ;
474
469
Ok ( ( ) )
@@ -520,16 +515,14 @@ pub trait Memory {
520
515
521
516
impl Memory for [ u8 ] {
522
517
fn read_le_u64 ( & self , ptr : u32 ) -> Result < u64 , Error > {
523
- let range =
524
- heap_range ( ptr, 8 , self . len ( ) ) . ok_or_else ( || error ( "read out of heap bounds" ) ) ?;
518
+ let range = heap_range ( ptr, 8 , self . len ( ) ) . ok_or_else ( || Error :: Other ) ?;
525
519
let bytes = self [ range]
526
520
. try_into ( )
527
521
. expect ( "[u8] slice of length 8 must be convertible to [u8; 8]" ) ;
528
522
Ok ( u64:: from_le_bytes ( bytes) )
529
523
}
530
524
fn write_le_u64 ( & mut self , ptr : u32 , val : u64 ) -> Result < ( ) , Error > {
531
- let range =
532
- heap_range ( ptr, 8 , self . len ( ) ) . ok_or_else ( || error ( "write out of heap bounds" ) ) ?;
525
+ let range = heap_range ( ptr, 8 , self . len ( ) ) . ok_or_else ( || Error :: Other ) ?;
533
526
let bytes = val. to_le_bytes ( ) ;
534
527
self [ range] . copy_from_slice ( & bytes[ ..] ) ;
535
528
Ok ( ( ) )
0 commit comments