51
51
use clone:: Clone ;
52
52
use iter:: { range, Iterator } ;
53
53
use kinds:: Send ;
54
- use libc;
55
- use mem;
54
+ use mem:: { forget, min_align_of, size_of, transmute} ;
56
55
use ops:: Drop ;
57
56
use option:: { Option , Some , None } ;
58
57
use owned:: Box ;
@@ -62,6 +61,7 @@ use slice::ImmutableVector;
62
61
use sync:: arc:: UnsafeArc ;
63
62
use sync:: atomics:: { AtomicInt , AtomicPtr , SeqCst } ;
64
63
use unstable:: sync:: Exclusive ;
64
+ use rt:: heap:: { allocate, deallocate} ;
65
65
use vec:: Vec ;
66
66
67
67
// Once the queue is less than 1/K full, then it will be downsized. Note that
@@ -229,7 +229,7 @@ impl<T: Send> Deque<T> {
229
229
Deque {
230
230
bottom : AtomicInt :: new ( 0 ) ,
231
231
top : AtomicInt :: new ( 0 ) ,
232
- array : AtomicPtr :: new ( unsafe { mem :: transmute ( buf) } ) ,
232
+ array : AtomicPtr :: new ( unsafe { transmute ( buf) } ) ,
233
233
pool : pool,
234
234
}
235
235
}
@@ -271,7 +271,7 @@ impl<T: Send> Deque<T> {
271
271
return Some ( data) ;
272
272
} else {
273
273
self . bottom . store ( t + 1 , SeqCst ) ;
274
- mem :: forget ( data) ; // someone else stole this value
274
+ forget ( data) ; // someone else stole this value
275
275
return None ;
276
276
}
277
277
}
@@ -293,7 +293,7 @@ impl<T: Send> Deque<T> {
293
293
if self . top . compare_and_swap ( t, t + 1 , SeqCst ) == t {
294
294
Data ( data)
295
295
} else {
296
- mem :: forget ( data) ; // someone else stole this value
296
+ forget ( data) ; // someone else stole this value
297
297
Abort
298
298
}
299
299
}
@@ -314,15 +314,15 @@ impl<T: Send> Deque<T> {
314
314
// continue to be read after we flag this buffer for reclamation.
315
315
unsafe fn swap_buffer ( & mut self , b : int , old : * mut Buffer < T > ,
316
316
buf : Buffer < T > ) -> * mut Buffer < T > {
317
- let newbuf: * mut Buffer < T > = mem :: transmute ( box buf) ;
317
+ let newbuf: * mut Buffer < T > = transmute ( box buf) ;
318
318
self . array . store ( newbuf, SeqCst ) ;
319
319
let ss = ( * newbuf) . size ( ) ;
320
320
self . bottom . store ( b + ss, SeqCst ) ;
321
321
let t = self . top . load ( SeqCst ) ;
322
322
if self . top . compare_and_swap ( t, t + ss, SeqCst ) != t {
323
323
self . bottom . store ( b, SeqCst ) ;
324
324
}
325
- self . pool . free ( mem :: transmute ( old) ) ;
325
+ self . pool . free ( transmute ( old) ) ;
326
326
return newbuf;
327
327
}
328
328
}
@@ -339,15 +339,19 @@ impl<T: Send> Drop for Deque<T> {
339
339
for i in range ( t, b) {
340
340
let _: T = unsafe { ( * a) . get ( i) } ;
341
341
}
342
- self . pool . free ( unsafe { mem :: transmute ( a) } ) ;
342
+ self . pool . free ( unsafe { transmute ( a) } ) ;
343
343
}
344
344
}
345
345
346
+ #[ inline]
347
+ fn buffer_alloc_size < T > ( log_size : int ) -> uint {
348
+ ( 1 << log_size) * size_of :: < T > ( )
349
+ }
350
+
346
351
impl < T : Send > Buffer < T > {
347
352
unsafe fn new ( log_size : int ) -> Buffer < T > {
348
- let size = ( 1 << log_size) * mem:: size_of :: < T > ( ) ;
349
- let buffer = libc:: malloc ( size as libc:: size_t ) ;
350
- assert ! ( !buffer. is_null( ) ) ;
353
+ let size = buffer_alloc_size :: < T > ( log_size) ;
354
+ let buffer = allocate ( size, min_align_of :: < T > ( ) ) ;
351
355
Buffer {
352
356
storage : buffer as * T ,
353
357
log_size : log_size,
@@ -372,7 +376,7 @@ impl<T: Send> Buffer<T> {
372
376
unsafe fn put ( & mut self , i : int , t : T ) {
373
377
let ptr = self . storage . offset ( i & self . mask ( ) ) ;
374
378
ptr:: copy_nonoverlapping_memory ( ptr as * mut T , & t as * T , 1 ) ;
375
- mem :: forget ( t) ;
379
+ forget ( t) ;
376
380
}
377
381
378
382
// Again, unsafe because this has incredibly dubious ownership violations.
@@ -390,7 +394,8 @@ impl<T: Send> Buffer<T> {
390
394
impl < T : Send > Drop for Buffer < T > {
391
395
fn drop ( & mut self ) {
392
396
// It is assumed that all buffers are empty on drop.
393
- unsafe { libc:: free ( self . storage as * mut libc:: c_void ) }
397
+ let size = buffer_alloc_size :: < T > ( self . log_size ) ;
398
+ unsafe { deallocate ( self . storage as * mut u8 , size, min_align_of :: < T > ( ) ) }
394
399
}
395
400
}
396
401
@@ -606,8 +611,7 @@ mod tests {
606
611
let s = s. clone ( ) ;
607
612
let unique_box = box AtomicUint :: new ( 0 ) ;
608
613
let thread_box = unsafe {
609
- * mem:: transmute :: < & Box < AtomicUint > ,
610
- * * mut AtomicUint > ( & unique_box)
614
+ * mem:: transmute :: < & Box < AtomicUint > , * * mut AtomicUint > ( & unique_box)
611
615
} ;
612
616
( Thread :: start ( proc ( ) {
613
617
unsafe {
0 commit comments