ntex_io/
buf.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use std::{cell::Cell, fmt};

use ntex_bytes::{BytesVec, PoolRef};
use ntex_util::future::Either;

use crate::IoRef;

#[derive(Default)]
pub(crate) struct Buffer(Cell<Option<BytesVec>>, Cell<Option<BytesVec>>);

impl fmt::Debug for Buffer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let b0 = self.0.take();
        let b1 = self.1.take();
        let res = f
            .debug_struct("Buffer")
            .field("0", &b0)
            .field("1", &b1)
            .finish();
        self.0.set(b0);
        self.1.set(b1);
        res
    }
}

#[derive(Debug)]
pub struct Stack {
    len: usize,
    buffers: Either<[Buffer; 3], Vec<Buffer>>,
}

impl Stack {
    pub(crate) fn new() -> Self {
        Self {
            len: 1,
            buffers: Either::Left(Default::default()),
        }
    }

    pub(crate) fn add_layer(&mut self) {
        match &mut self.buffers {
            Either::Left(b) => {
                // move to vec
                if self.len == 3 {
                    let mut vec = vec![Buffer(Cell::new(None), Cell::new(None))];
                    for item in b.iter_mut().take(self.len) {
                        vec.push(Buffer(
                            Cell::new(item.0.take()),
                            Cell::new(item.1.take()),
                        ));
                    }
                    self.len += 1;
                    self.buffers = Either::Right(vec);
                } else {
                    let mut idx = self.len;
                    while idx > 0 {
                        let item = Buffer(
                            Cell::new(b[idx - 1].0.take()),
                            Cell::new(b[idx - 1].1.take()),
                        );
                        b[idx] = item;
                        idx -= 1;
                    }
                    b[0] = Buffer(Cell::new(None), Cell::new(None));
                    self.len += 1;
                }
            }
            Either::Right(vec) => {
                self.len += 1;
                vec.insert(0, Buffer(Cell::new(None), Cell::new(None)));
            }
        }
    }

    fn get_buffers<F, R>(&self, idx: usize, f: F) -> R
    where
        F: FnOnce(&Buffer, &Buffer) -> R,
    {
        let buffers = match self.buffers {
            Either::Left(ref b) => &b[..],
            Either::Right(ref b) => &b[..],
        };

        let next = idx + 1;
        if self.len > next {
            f(&buffers[idx], &buffers[next])
        } else {
            let curr = Buffer(Cell::new(buffers[idx].0.take()), Cell::new(None));
            let next = Buffer(Cell::new(None), Cell::new(buffers[idx].1.take()));

            let result = f(&curr, &next);
            buffers[idx].0.set(curr.0.take());
            buffers[idx].1.set(next.1.take());
            result
        }
    }

    fn get_first_level(&self) -> &Buffer {
        match &self.buffers {
            Either::Left(b) => &b[0],
            Either::Right(b) => &b[0],
        }
    }

    fn get_last_level(&self) -> &Buffer {
        match &self.buffers {
            Either::Left(b) => &b[self.len - 1],
            Either::Right(b) => &b[self.len - 1],
        }
    }

    pub(crate) fn read_buf<F, R>(&self, io: &IoRef, idx: usize, nbytes: usize, f: F) -> R
    where
        F: FnOnce(&ReadBuf<'_>) -> R,
    {
        self.get_buffers(idx, |curr, next| {
            let buf = ReadBuf {
                io,
                nbytes,
                curr,
                next,
                need_write: Cell::new(false),
            };
            f(&buf)
        })
    }

    pub(crate) fn write_buf<F, R>(&self, io: &IoRef, idx: usize, f: F) -> R
    where
        F: FnOnce(&WriteBuf<'_>) -> R,
    {
        self.get_buffers(idx, |curr, next| {
            let buf = WriteBuf {
                io,
                curr,
                next,
                need_write: Cell::new(false),
            };
            f(&buf)
        })
    }

    pub(crate) fn get_read_source(&self) -> Option<BytesVec> {
        self.get_last_level().0.take()
    }

    pub(crate) fn set_read_source(&self, io: &IoRef, buf: BytesVec) {
        if buf.is_empty() {
            io.memory_pool().release_read_buf(buf);
        } else {
            self.get_last_level().0.set(Some(buf));
        }
    }

    pub(crate) fn with_read_destination<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut BytesVec) -> R,
    {
        let item = self.get_first_level();
        let mut rb = item.0.take();
        if rb.is_none() {
            rb = Some(io.memory_pool().get_read_buf());
        }

        let result = f(rb.as_mut().unwrap());

        // check nested updates
        if item.0.take().is_some() {
            log::error!("Nested read io operation is detected");
            io.force_close();
        }

        if let Some(b) = rb {
            if b.is_empty() {
                io.memory_pool().release_read_buf(b);
            } else {
                item.0.set(Some(b));
            }
        }
        result
    }

    pub(crate) fn with_write_source<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut BytesVec) -> R,
    {
        let item = self.get_first_level();
        let mut wb = item.1.take();
        if wb.is_none() {
            wb = Some(io.memory_pool().get_write_buf());
        }

        let result = f(wb.as_mut().unwrap());
        if let Some(b) = wb {
            if b.is_empty() {
                io.memory_pool().release_write_buf(b);
            } else {
                item.1.set(Some(b));
            }
        }
        result
    }

    pub(crate) fn get_write_destination(&self) -> Option<BytesVec> {
        self.get_last_level().1.take()
    }

    pub(crate) fn set_write_destination(&self, buf: BytesVec) -> Option<BytesVec> {
        let b = self.get_last_level().1.take();
        if b.is_some() {
            self.get_last_level().1.set(b);
            Some(buf)
        } else {
            self.get_last_level().1.set(Some(buf));
            None
        }
    }

    pub(crate) fn with_write_destination<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut Option<BytesVec>) -> R,
    {
        let item = self.get_last_level();
        let mut wb = item.1.take();

        let result = f(&mut wb);

        // check nested updates
        if item.1.take().is_some() {
            log::error!("Nested write io operation is detected");
            io.force_close();
        }

        if let Some(b) = wb {
            if b.is_empty() {
                io.memory_pool().release_write_buf(b);
            } else {
                item.1.set(Some(b));
            }
        }
        result
    }

    pub(crate) fn read_destination_size(&self) -> usize {
        let item = self.get_first_level();
        let rb = item.0.take();
        let size = rb.as_ref().map(|b| b.len()).unwrap_or(0);
        item.0.set(rb);
        size
    }

    pub(crate) fn write_destination_size(&self) -> usize {
        let item = self.get_last_level();
        let wb = item.1.take();
        let size = wb.as_ref().map(|b| b.len()).unwrap_or(0);
        item.1.set(wb);
        size
    }

    pub(crate) fn release(&self, pool: PoolRef) {
        let items = match &self.buffers {
            Either::Left(b) => &b[..],
            Either::Right(b) => &b[..],
        };

        for item in items {
            if let Some(buf) = item.0.take() {
                pool.release_read_buf(buf);
            }
            if let Some(buf) = item.1.take() {
                pool.release_write_buf(buf);
            }
        }
    }

    pub(crate) fn set_memory_pool(&self, pool: PoolRef) {
        let items = match &self.buffers {
            Either::Left(b) => &b[..],
            Either::Right(b) => &b[..],
        };
        for item in items {
            if let Some(mut b) = item.0.take() {
                pool.move_vec_in(&mut b);
                item.0.set(Some(b));
            }
            if let Some(mut b) = item.1.take() {
                pool.move_vec_in(&mut b);
                item.1.set(Some(b));
            }
        }
    }
}

#[derive(Debug)]
pub struct ReadBuf<'a> {
    pub(crate) io: &'a IoRef,
    pub(crate) curr: &'a Buffer,
    pub(crate) next: &'a Buffer,
    pub(crate) nbytes: usize,
    pub(crate) need_write: Cell<bool>,
}

impl<'a> ReadBuf<'a> {
    #[inline]
    /// Get io tag
    pub fn tag(&self) -> &'static str {
        self.io.tag()
    }

    #[inline]
    /// Get number of newly added bytes
    pub fn nbytes(&self) -> usize {
        self.nbytes
    }

    #[inline]
    /// Initiate graceful io stream shutdown
    pub fn want_shutdown(&self) {
        self.io.want_shutdown()
    }

    #[inline]
    /// Make sure buffer has enough free space
    pub fn resize_buf(&self, buf: &mut BytesVec) {
        self.io.memory_pool().resize_read_buf(buf);
    }

    #[inline]
    /// Get reference to source read buffer
    pub fn with_src<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Option<BytesVec>) -> R,
    {
        let mut item = self.next.0.take();
        let result = f(&mut item);

        if let Some(b) = item {
            if b.is_empty() {
                self.io.memory_pool().release_read_buf(b);
            } else {
                self.next.0.set(Some(b));
            }
        }
        result
    }

    #[inline]
    /// Get reference to destination read buffer
    pub fn with_dst<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytesVec) -> R,
    {
        let mut item = self.curr.0.take();
        if item.is_none() {
            item = Some(self.io.memory_pool().get_read_buf());
        }
        let result = f(item.as_mut().unwrap());
        if let Some(b) = item {
            if b.is_empty() {
                self.io.memory_pool().release_read_buf(b);
            } else {
                self.curr.0.set(Some(b));
            }
        }
        result
    }

    #[inline]
    /// Take source read buffer
    pub fn take_src(&self) -> Option<BytesVec> {
        self.next.0.take().and_then(|b| {
            if b.is_empty() {
                self.io.memory_pool().release_read_buf(b);
                None
            } else {
                Some(b)
            }
        })
    }

    #[inline]
    /// Set source read buffer
    pub fn set_src(&self, src: Option<BytesVec>) {
        if let Some(src) = src {
            if src.is_empty() {
                self.io.memory_pool().release_read_buf(src);
            } else if let Some(mut buf) = self.next.0.take() {
                buf.extend_from_slice(&src);
                self.next.0.set(Some(buf));
                self.io.memory_pool().release_read_buf(src);
            } else {
                self.next.0.set(Some(src));
            }
        }
    }

    #[inline]
    /// Take destination read buffer
    pub fn take_dst(&self) -> BytesVec {
        self.curr
            .0
            .take()
            .unwrap_or_else(|| self.io.memory_pool().get_read_buf())
    }

    #[inline]
    /// Set destination read buffer
    pub fn set_dst(&self, dst: Option<BytesVec>) {
        if let Some(dst) = dst {
            if dst.is_empty() {
                self.io.memory_pool().release_read_buf(dst);
            } else if let Some(mut buf) = self.curr.0.take() {
                buf.extend_from_slice(&dst);
                self.curr.0.set(Some(buf));
                self.io.memory_pool().release_read_buf(dst);
            } else {
                self.curr.0.set(Some(dst));
            }
        }
    }

    #[inline]
    pub fn with_write_buf<'b, F, R>(&'b self, f: F) -> R
    where
        F: FnOnce(&WriteBuf<'b>) -> R,
    {
        let mut buf = WriteBuf {
            io: self.io,
            curr: self.curr,
            next: self.next,
            need_write: Cell::new(self.need_write.get()),
        };
        let result = f(&mut buf);
        self.need_write.set(buf.need_write.get());
        result
    }
}

#[derive(Debug)]
pub struct WriteBuf<'a> {
    pub(crate) io: &'a IoRef,
    pub(crate) curr: &'a Buffer,
    pub(crate) next: &'a Buffer,
    pub(crate) need_write: Cell<bool>,
}

impl<'a> WriteBuf<'a> {
    #[inline]
    /// Get io tag
    pub fn tag(&self) -> &'static str {
        self.io.tag()
    }

    #[inline]
    /// Initiate graceful io stream shutdown
    pub fn want_shutdown(&self) {
        self.io.want_shutdown()
    }

    #[inline]
    /// Make sure buffer has enough free space
    pub fn resize_buf(&self, buf: &mut BytesVec) {
        self.io.memory_pool().resize_write_buf(buf);
    }

    #[inline]
    /// Get reference to source write buffer
    pub fn with_src<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Option<BytesVec>) -> R,
    {
        let mut item = self.curr.1.take();
        let result = f(&mut item);
        if let Some(b) = item {
            if b.is_empty() {
                self.io.memory_pool().release_write_buf(b);
            } else {
                self.curr.1.set(Some(b));
            }
        }
        result
    }

    #[inline]
    /// Get reference to destination write buffer
    pub fn with_dst<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytesVec) -> R,
    {
        let mut item = self.next.1.take();
        if item.is_none() {
            item = Some(self.io.memory_pool().get_write_buf());
        }
        let buf = item.as_mut().unwrap();
        let total = buf.len();
        let result = f(buf);

        if buf.is_empty() {
            self.io.memory_pool().release_write_buf(item.unwrap());
        } else {
            self.need_write
                .set(self.need_write.get() | (total != buf.len()));
            self.next.1.set(item);
        }
        result
    }

    #[inline]
    /// Take source write buffer
    pub fn take_src(&self) -> Option<BytesVec> {
        self.curr.1.take().and_then(|b| {
            if b.is_empty() {
                self.io.memory_pool().release_write_buf(b);
                None
            } else {
                Some(b)
            }
        })
    }

    #[inline]
    /// Set source write buffer
    pub fn set_src(&self, src: Option<BytesVec>) {
        if let Some(src) = src {
            if src.is_empty() {
                self.io.memory_pool().release_write_buf(src);
            } else if let Some(mut buf) = self.curr.1.take() {
                buf.extend_from_slice(&src);
                self.curr.1.set(Some(buf));
                self.io.memory_pool().release_write_buf(src);
            } else {
                self.curr.1.set(Some(src));
            }
        }
    }

    #[inline]
    /// Take destination write buffer
    pub fn take_dst(&self) -> BytesVec {
        self.next
            .1
            .take()
            .unwrap_or_else(|| self.io.memory_pool().get_write_buf())
    }

    #[inline]
    /// Set destination write buffer
    pub fn set_dst(&self, dst: Option<BytesVec>) {
        if let Some(dst) = dst {
            if dst.is_empty() {
                self.io.memory_pool().release_write_buf(dst);
            } else {
                self.need_write.set(true);

                if let Some(mut buf) = self.next.1.take() {
                    buf.extend_from_slice(&dst);
                    self.next.1.set(Some(buf));
                    self.io.memory_pool().release_write_buf(dst);
                } else {
                    self.next.1.set(Some(dst));
                }
            }
        }
    }

    #[inline]
    pub fn with_read_buf<'b, F, R>(&'b self, f: F) -> R
    where
        F: FnOnce(&ReadBuf<'b>) -> R,
    {
        let mut buf = ReadBuf {
            io: self.io,
            curr: self.curr,
            next: self.next,
            nbytes: 0,
            need_write: Cell::new(self.need_write.get()),
        };
        let result = f(&mut buf);
        self.need_write.set(buf.need_write.get());
        result
    }
}