about summary refs log tree commit diff
path: root/src/libstd/c_vec.rs
blob: 5bab771ab7d95fb6d66688cefc61808108f244e5 (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
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
/*
Module: c_vec

Library to interface with chunks of memory allocated in C.

It is often desirable to safely interface with memory allocated from C,
encapsulating the unsafety into allocation and destruction time.  Indeed,
allocating memory externally is currently the only way to give Rust shared
mutable state with C programs that keep their own references; vectors are
unsuitable because they could be reallocated or moved at any time, and
importing C memory into a vector takes a one-time snapshot of the memory.

This module simplifies the usage of such external blocks of memory.  Memory
is encapsulated into an opaque object after creation; the lifecycle of the
memory can be optionally managed by Rust, if an appropriate destructor
closure is provided.  Safety is ensured by bounds-checking accesses, which
are marshalled through get and set functions.

There are three unsafe functions: the two introduction forms, and the
pointer elimination form.  The introduction forms are unsafe for the obvious
reason (they act on a pointer that cannot be checked inside the method), but
the elimination form is somewhat more subtle in its unsafety.  By using a
pointer taken from a c_vec::t without keeping a reference to the c_vec::t
itself around, the c_vec could be garbage collected, and the memory within
could be destroyed.  There are legitimate uses for the pointer elimination
form -- for instance, to pass memory back into C -- but great care must be
taken to ensure that a reference to the c_vec::t is still held if needed.

 */

export t;
export create, create_with_dtor;
export get, set;
export len;
export ptr;

/*
 Type: t

 The type representing a native chunk of memory.  Wrapped in a tag for
 opacity; FIXME #818 when it is possible to have truly opaque types, this
 should be revisited.
 */

tag t<T> {
    t({ base: *mutable T, len: uint, rsrc: @dtor_res});
}

resource dtor_res(dtor: option::t<fn@()>) {
    alt dtor {
      option::none { }
      option::some(f) { f(); }
    }
}

/*
 Section: Introduction forms
 */

/*
Function: create

Create a c_vec::t from a native buffer with a given length.

Parameters:

base - A native pointer to a buffer
len - The number of elements in the buffer
*/
unsafe fn create<T>(base: *mutable T, len: uint) -> t<T> {
    ret t({base: base,
           len: len,
           rsrc: @dtor_res(option::none)
          });
}

/*
Function: create_with_dtor

Create a c_vec::t from a native buffer, with a given length,
and a function to run upon destruction.

Parameters:

base - A native pointer to a buffer
len - The number of elements in the buffer
dtor - A function to run when the value is destructed, useful
       for freeing the buffer, etc.
*/
unsafe fn create_with_dtor<T>(base: *mutable T, len: uint, dtor: fn@())
  -> t<T> {
    ret t({base: base,
           len: len,
           rsrc: @dtor_res(option::some(dtor))
          });
}

/*
 Section: Operations
 */

/*
Function: get

Retrieves an element at a given index

Failure:

If `ofs` is greater or equal to the length of the vector
*/
fn get<T: copy>(t: t<T>, ofs: uint) -> T {
    assert ofs < len(t);
    ret unsafe { *ptr::mut_offset((*t).base, ofs) };
}

/*
Function: set

Sets the value of an element at a given index

Failure:

If `ofs` is greater or equal to the length of the vector
*/
fn set<T: copy>(t: t<T>, ofs: uint, v: T) {
    assert ofs < len(t);
    unsafe { *ptr::mut_offset((*t).base, ofs) = v };
}

/*
 Section: Elimination forms
 */

/*
Function: len

Returns the length of the vector
*/
fn len<T>(t: t<T>) -> uint {
    ret (*t).len;
}

/*
Function: ptr

Returns a pointer to the first element of the vector
*/
unsafe fn ptr<T>(t: t<T>) -> *mutable T {
    ret (*t).base;
}

#[cfg(test)]
mod tests {
    import ctypes::*;

    #[nolink]
    #[abi = "cdecl"]
    native mod libc {
        fn malloc(n: size_t) -> *mutable u8;
        fn free(m: *mutable u8);
    }

    fn malloc(n: size_t) -> t<u8> {
        let mem = libc::malloc(n);

        assert mem as int != 0;

        ret unsafe { create_with_dtor(mem, n, bind libc::free(mem)) };
    }

    #[test]
    fn test_basic() {
        let cv = malloc(16u);

        set(cv, 3u, 8u8);
        set(cv, 4u, 9u8);
        assert get(cv, 3u) == 8u8;
        assert get(cv, 4u) == 9u8;
        assert len(cv) == 16u;
    }

    #[test]
    #[should_fail]
    #[ignore(cfg(target_os = "win32"))]
    fn test_overrun_get() {
        let cv = malloc(16u);

        get(cv, 17u);
    }

    #[test]
    #[should_fail]
    #[ignore(cfg(target_os = "win32"))]
    fn test_overrun_set() {
        let cv = malloc(16u);

        set(cv, 17u, 0u8);
    }

    #[test]
    fn test_and_I_mean_it() {
        let cv = malloc(16u);
        let p = unsafe { ptr(cv) };

        set(cv, 0u, 32u8);
        set(cv, 1u, 33u8);
        assert unsafe { *p } == 32u8;
        set(cv, 2u, 34u8); /* safety */
    }

}