Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
b378b8eb
Unverified
Commit
b378b8eb
authored
Nov 29, 2018
by
Robert
Committed by
GitHub
Nov 29, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix create_vector_of_strings to use the stack, and test it. (#5074)
parent
9635d494
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
4 deletions
+10
-4
builder.rs
rust/flatbuffers/src/builder.rs
+7
-3
alloc_check.rs
tests/rust_usage_test/bin/alloc_check.rs
+3
-1
No files found.
rust/flatbuffers/src/builder.rs
View file @
b378b8eb
...
...
@@ -29,6 +29,8 @@ use vtable::{VTable, field_index_to_field_offset};
use
vtable_writer
::
VTableWriter
;
use
vector
::{
SafeSliceAccess
,
Vector
};
pub
const
N_SMALLVEC_STRING_VECTOR_CAPACITY
:
usize
=
16
;
#[derive(Clone,
Copy,
Debug)]
struct
FieldLoc
{
off
:
UOffsetT
,
...
...
@@ -268,10 +270,12 @@ impl<'fbb> FlatBufferBuilder<'fbb> {
#[inline]
pub
fn
create_vector_of_strings
<
'a
,
'b
>
(
&
'a
mut
self
,
xs
:
&
'b
[
&
'b
str
])
->
WIPOffset
<
Vector
<
'fbb
,
ForwardsUOffset
<&
'fbb
str
>>>
{
self
.assert_not_nested
(
"create_vector_of_strings can not be called when a table or vector is under construction"
);
// internally, smallvec can be a stack-allocated or heap-allocated vector
.
//
we expect it to usually be stack-allocated
.
let
mut
offsets
:
smallvec
::
SmallVec
<
[
WIPOffset
<&
str
>
;
0
]
>
=
smallvec
::
SmallVec
::
with_capacity
(
xs
.len
());
// internally, smallvec can be a stack-allocated or heap-allocated vector
:
//
if xs.len() > N_SMALLVEC_STRING_VECTOR_CAPACITY then it will overflow to the heap
.
let
mut
offsets
:
smallvec
::
SmallVec
<
[
WIPOffset
<&
str
>
;
N_SMALLVEC_STRING_VECTOR_CAPACITY
]
>
=
smallvec
::
SmallVec
::
with_capacity
(
xs
.len
());
unsafe
{
offsets
.set_len
(
xs
.len
());
}
// note that this happens in reverse, because the buffer is built back-to-front:
for
(
i
,
&
s
)
in
xs
.iter
()
.enumerate
()
.rev
()
{
let
o
=
self
.create_string
(
s
);
offsets
[
i
]
=
o
;
...
...
tests/rust_usage_test/bin/alloc_check.rs
View file @
b378b8eb
...
...
@@ -35,6 +35,8 @@ pub use monster_test_generated::my_game;
// verbatim from the test suite:
fn
create_serialized_example_with_generated_code
(
builder
:
&
mut
flatbuffers
::
FlatBufferBuilder
)
{
let
mon
=
{
let
_
=
builder
.create_vector_of_strings
(
&
[
"these"
,
"unused"
,
"strings"
,
"check"
,
"the"
,
"create_vector_of_strings"
,
"function"
]);
let
s0
=
builder
.create_string
(
"test1"
);
let
s1
=
builder
.create_string
(
"test2"
);
let
fred_name
=
builder
.create_string
(
"Fred"
);
...
...
@@ -79,7 +81,7 @@ fn main() {
create_serialized_example_with_generated_code
(
builder
);
}
// reset the builder, clearing its heap-allocted memory:
// reset the builder, clearing its heap-alloc
a
ted memory:
builder
.reset
();
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment