Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
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
capnproto
Commits
02ea85af
Commit
02ea85af
authored
Jul 18, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove allocator hack in SchemaLoader, use kj::Arena.
parent
e607fc95
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
45 deletions
+18
-45
message.h
c++/src/capnp/message.h
+0
-2
schema-loader.c++
c++/src/capnp/schema-loader.c++
+18
-43
No files found.
c++/src/capnp/message.h
View file @
02ea85af
...
@@ -171,8 +171,6 @@ private:
...
@@ -171,8 +171,6 @@ private:
_
::
StructBuilder
initRoot
(
_
::
StructSize
size
);
_
::
StructBuilder
initRoot
(
_
::
StructSize
size
);
void
setRootInternal
(
_
::
StructReader
reader
);
void
setRootInternal
(
_
::
StructReader
reader
);
_
::
StructBuilder
getRoot
(
_
::
StructSize
size
);
_
::
StructBuilder
getRoot
(
_
::
StructSize
size
);
friend
class
SchemaLoader
;
// for a dirty hack, see schema-loader.c++.
};
};
template
<
typename
RootType
>
template
<
typename
RootType
>
...
...
c++/src/capnp/schema-loader.c++
View file @
02ea85af
...
@@ -29,13 +29,12 @@
...
@@ -29,13 +29,12 @@
#include "arena.h"
#include "arena.h"
#include <kj/debug.h>
#include <kj/debug.h>
#include <kj/exception.h>
#include <kj/exception.h>
#include <kj/arena.h>
namespace
capnp
{
namespace
capnp
{
class
SchemaLoader
::
Impl
{
class
SchemaLoader
::
Impl
{
public
:
public
:
Impl
();
_
::
RawSchema
*
load
(
const
schema
::
Node
::
Reader
&
reader
);
_
::
RawSchema
*
load
(
const
schema
::
Node
::
Reader
&
reader
);
_
::
RawSchema
*
loadNative
(
const
_
::
RawSchema
*
nativeSchema
);
_
::
RawSchema
*
loadNative
(
const
_
::
RawSchema
*
nativeSchema
);
...
@@ -46,30 +45,9 @@ public:
...
@@ -46,30 +45,9 @@ public:
_
::
RawSchema
*
tryGet
(
uint64_t
typeId
)
const
;
_
::
RawSchema
*
tryGet
(
uint64_t
typeId
)
const
;
kj
::
Array
<
Schema
>
getAllLoaded
()
const
;
kj
::
Array
<
Schema
>
getAllLoaded
()
const
;
template
<
typename
T
>
kj
::
Arena
arena
;
T
*
allocate
(
size_t
count
=
1
)
{
ByteCount
bytes
=
count
*
sizeof
(
T
)
*
BYTES
;
static_assert
(
sizeof
(
word
)
==
8
,
"This code assumes 64-bit words."
);
WordCount
words
=
(
bytes
+
7
*
BYTES
)
/
BYTES_PER_WORD
;
while
(
true
)
{
word
*
result
=
segment
->
allocate
(
words
);
if
(
result
!=
nullptr
)
{
return
reinterpret_cast
<
T
*>
(
result
);
}
segment
=
arena
->
getSegmentWithAvailable
(
words
);
}
}
private
:
private
:
MallocMessageBuilder
allocator
;
_
::
BuilderArena
*
arena
;
_
::
SegmentBuilder
*
segment
;
// HACK: We don't actually use these to build messages, we use them to allocate memory that
// should be freed when the loader is freed. We're reusing BuilderArena as a convenient
// implementation of the general concept of arenas.
// TODO(cleanup): Develop a stand-alone Arena class that can be used for things like this.
std
::
unordered_map
<
uint64_t
,
_
::
RawSchema
*>
schemas
;
std
::
unordered_map
<
uint64_t
,
_
::
RawSchema
*>
schemas
;
};
};
...
@@ -113,27 +91,27 @@ public:
...
@@ -113,27 +91,27 @@ public:
const
_
::
RawSchema
**
makeDependencyArray
(
uint32_t
*
count
)
{
const
_
::
RawSchema
**
makeDependencyArray
(
uint32_t
*
count
)
{
*
count
=
dependencies
.
size
();
*
count
=
dependencies
.
size
();
const
_
::
RawSchema
**
result
=
kj
::
ArrayPtr
<
const
_
::
RawSchema
*>
result
=
loader
.
a
llocate
<
const
_
::
RawSchema
*>
(
*
count
);
loader
.
a
rena
.
allocateArray
<
const
_
::
RawSchema
*>
(
*
count
);
uint
pos
=
0
;
uint
pos
=
0
;
for
(
auto
&
dep
:
dependencies
)
{
for
(
auto
&
dep
:
dependencies
)
{
result
[
pos
++
]
=
dep
.
second
;
result
[
pos
++
]
=
dep
.
second
;
}
}
KJ_DASSERT
(
pos
==
*
count
);
KJ_DASSERT
(
pos
==
*
count
);
return
result
;
return
result
.
begin
()
;
}
}
const
_
::
RawSchema
::
MemberInfo
*
makeMemberInfoArray
(
uint32_t
*
count
)
{
const
_
::
RawSchema
::
MemberInfo
*
makeMemberInfoArray
(
uint32_t
*
count
)
{
*
count
=
members
.
size
();
*
count
=
members
.
size
();
_
::
RawSchema
::
MemberInfo
*
result
=
kj
::
ArrayPtr
<
_
::
RawSchema
::
MemberInfo
>
result
=
loader
.
a
llocate
<
_
::
RawSchema
::
MemberInfo
>
(
*
count
);
loader
.
a
rena
.
allocateArray
<
_
::
RawSchema
::
MemberInfo
>
(
*
count
);
uint
pos
=
0
;
uint
pos
=
0
;
for
(
auto
&
member
:
members
)
{
for
(
auto
&
member
:
members
)
{
result
[
pos
++
]
=
{
kj
::
implicitCast
<
uint16_t
>
(
member
.
first
.
first
),
result
[
pos
++
]
=
{
kj
::
implicitCast
<
uint16_t
>
(
member
.
first
.
first
),
kj
::
implicitCast
<
uint16_t
>
(
member
.
second
)};
kj
::
implicitCast
<
uint16_t
>
(
member
.
second
)};
}
}
KJ_DASSERT
(
pos
==
*
count
);
KJ_DASSERT
(
pos
==
*
count
);
return
result
;
return
result
.
begin
()
;
}
}
private
:
private
:
...
@@ -981,19 +959,16 @@ private:
...
@@ -981,19 +959,16 @@ private:
// =======================================================================================
// =======================================================================================
SchemaLoader
::
Impl
::
Impl
()
:
arena
(
allocator
.
arena
()),
segment
(
allocator
.
getRootSegment
())
{}
_
::
RawSchema
*
SchemaLoader
::
Impl
::
load
(
const
schema
::
Node
::
Reader
&
reader
)
{
_
::
RawSchema
*
SchemaLoader
::
Impl
::
load
(
const
schema
::
Node
::
Reader
&
reader
)
{
// Make a copy of the node which can be used unchecked.
// Make a copy of the node which can be used unchecked.
size_t
size
=
reader
.
totalSizeInWords
()
+
1
;
size_t
size
=
reader
.
totalSizeInWords
()
+
1
;
word
*
validated
=
allocate
<
word
>
(
size
);
kj
::
ArrayPtr
<
word
>
validated
=
arena
.
allocateArray
<
word
>
(
size
);
copyToUnchecked
(
reader
,
kj
::
arrayPtr
(
validated
,
size
));
memset
(
validated
.
begin
(),
0
,
size
*
sizeof
(
word
));
copyToUnchecked
(
reader
,
validated
);
// Validate the copy.
// Validate the copy.
Validator
validator
(
*
this
);
Validator
validator
(
*
this
);
auto
validatedReader
=
readMessageUnchecked
<
schema
::
Node
>
(
validated
);
auto
validatedReader
=
readMessageUnchecked
<
schema
::
Node
>
(
validated
.
begin
()
);
if
(
!
validator
.
validate
(
validatedReader
))
{
if
(
!
validator
.
validate
(
validatedReader
))
{
// Not valid. Construct an empty schema of the same type and return that.
// Not valid. Construct an empty schema of the same type and return that.
...
@@ -1006,7 +981,7 @@ _::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader) {
...
@@ -1006,7 +981,7 @@ _::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader) {
_
::
RawSchema
*&
slot
=
schemas
[
validatedReader
.
getId
()];
_
::
RawSchema
*&
slot
=
schemas
[
validatedReader
.
getId
()];
if
(
slot
==
nullptr
)
{
if
(
slot
==
nullptr
)
{
// Nope, allocate a new RawSchema.
// Nope, allocate a new RawSchema.
slot
=
allocate
<
_
::
RawSchema
>
();
slot
=
&
arena
.
allocate
<
_
::
RawSchema
>
();
}
else
{
}
else
{
// Yes, check if it is compatible and figure out which schema is newer.
// Yes, check if it is compatible and figure out which schema is newer.
auto
existing
=
readMessageUnchecked
<
schema
::
Node
>
(
slot
->
encodedNode
);
auto
existing
=
readMessageUnchecked
<
schema
::
Node
>
(
slot
->
encodedNode
);
...
@@ -1018,7 +993,7 @@ _::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader) {
...
@@ -1018,7 +993,7 @@ _::RawSchema* SchemaLoader::Impl::load(const schema::Node::Reader& reader) {
}
}
// Initialize the RawSchema.
// Initialize the RawSchema.
slot
->
encodedNode
=
validated
;
slot
->
encodedNode
=
validated
.
begin
()
;
slot
->
dependencies
=
validator
.
makeDependencyArray
(
&
slot
->
dependencyCount
);
slot
->
dependencies
=
validator
.
makeDependencyArray
(
&
slot
->
dependencyCount
);
slot
->
membersByName
=
validator
.
makeMemberInfoArray
(
&
slot
->
memberCount
);
slot
->
membersByName
=
validator
.
makeMemberInfoArray
(
&
slot
->
memberCount
);
...
@@ -1029,7 +1004,7 @@ _::RawSchema* SchemaLoader::Impl::loadNative(const _::RawSchema* nativeSchema) {
...
@@ -1029,7 +1004,7 @@ _::RawSchema* SchemaLoader::Impl::loadNative(const _::RawSchema* nativeSchema) {
auto
reader
=
readMessageUnchecked
<
schema
::
Node
>
(
nativeSchema
->
encodedNode
);
auto
reader
=
readMessageUnchecked
<
schema
::
Node
>
(
nativeSchema
->
encodedNode
);
_
::
RawSchema
*&
slot
=
schemas
[
reader
.
getId
()];
_
::
RawSchema
*&
slot
=
schemas
[
reader
.
getId
()];
if
(
slot
==
nullptr
)
{
if
(
slot
==
nullptr
)
{
slot
=
allocate
<
_
::
RawSchema
>
();
slot
=
&
arena
.
allocate
<
_
::
RawSchema
>
();
}
else
if
(
slot
->
canCastTo
!=
nullptr
)
{
}
else
if
(
slot
->
canCastTo
!=
nullptr
)
{
KJ_REQUIRE
(
slot
->
canCastTo
==
nativeSchema
,
KJ_REQUIRE
(
slot
->
canCastTo
==
nativeSchema
,
"two different compiled-in type have the same type ID"
,
"two different compiled-in type have the same type ID"
,
...
@@ -1058,12 +1033,12 @@ _::RawSchema* SchemaLoader::Impl::loadNative(const _::RawSchema* nativeSchema) {
...
@@ -1058,12 +1033,12 @@ _::RawSchema* SchemaLoader::Impl::loadNative(const _::RawSchema* nativeSchema) {
slot
->
canCastTo
=
nativeSchema
;
slot
->
canCastTo
=
nativeSchema
;
// Except that we need to set the dependency list to point at other loader-owned RawSchemas.
// Except that we need to set the dependency list to point at other loader-owned RawSchemas.
const
_
::
RawSchema
**
dependencies
=
kj
::
ArrayPtr
<
const
_
::
RawSchema
*>
dependencies
=
a
llocate
<
const
_
::
RawSchema
*>
(
slot
->
dependencyCount
);
a
rena
.
allocateArray
<
const
_
::
RawSchema
*>
(
slot
->
dependencyCount
);
for
(
uint
i
=
0
;
i
<
nativeSchema
->
dependencyCount
;
i
++
)
{
for
(
uint
i
=
0
;
i
<
nativeSchema
->
dependencyCount
;
i
++
)
{
dependencies
[
i
]
=
loadNative
(
nativeSchema
->
dependencies
[
i
]);
dependencies
[
i
]
=
loadNative
(
nativeSchema
->
dependencies
[
i
]);
}
}
slot
->
dependencies
=
dependencies
;
slot
->
dependencies
=
dependencies
.
begin
()
;
return
slot
;
return
slot
;
}
}
...
...
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