aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/transport/zero_copy.cpp
blob: b91eaae1dfea3be1a667fde649d23416b577c80e (plain)
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
//
// Copyright 2010-2011 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <uhd/transport/zero_copy.hpp>

using namespace uhd::transport;

/***********************************************************************
 * Safe managed receive buffer
 **********************************************************************/
static void release_nop(void){
    /* NOP */
}

class safe_managed_receive_buffer : public managed_recv_buffer{
public:
    safe_managed_receive_buffer(
        const void *buff, size_t size, const release_fcn_t &release_fcn
    ):
        _buff(buff), _size(size), _release_fcn(release_fcn)
    {
        /* NOP */
    }

    ~safe_managed_receive_buffer(void){
        _release_fcn();
    }

    void release(void){
        release_fcn_t release_fcn = _release_fcn;
        _release_fcn = &release_nop;
        return release_fcn();
    }

private:
    const void *get_buff(void) const{
        return _buff;
    }

    size_t get_size(void) const{
        return _size;
    }

    const void *_buff;
    size_t _size;
    release_fcn_t _release_fcn;
};

managed_recv_buffer::sptr managed_recv_buffer::make_safe(
    const void *buff, size_t size, const release_fcn_t &release_fcn
){
    return sptr(new safe_managed_receive_buffer(buff, size, release_fcn));
}

/***********************************************************************
 * Safe managed send buffer
 **********************************************************************/
static void commit_nop(size_t){
    /* NOP */
}

class safe_managed_send_buffer : public managed_send_buffer{
public:
    safe_managed_send_buffer(
        void *buff, size_t size, const commit_fcn_t &commit_fcn
    ):
        _buff(buff), _size(size), _commit_fcn(commit_fcn)
    {
        /* NOP */
    }

    ~safe_managed_send_buffer(void){
        _commit_fcn(0);
    }

    void commit(size_t num_bytes){
        commit_fcn_t commit_fcn = _commit_fcn;
        _commit_fcn = &commit_nop;
        return commit_fcn(num_bytes);
    }

private:
    void *get_buff(void) const{
        return _buff;
    }

    size_t get_size(void) const{
        return _size;
    }

    void *_buff;
    size_t _size;
    commit_fcn_t _commit_fcn;
};

safe_managed_send_buffer::sptr managed_send_buffer::make_safe(
    void *buff, size_t size, const commit_fcn_t &commit_fcn
){
    return sptr(new safe_managed_send_buffer(buff, size, commit_fcn));
}