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
5eeb125b
Commit
5eeb125b
authored
Jun 10, 2018
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Work around varying OS support for aligned_alloc().
parent
75d4177c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
2 deletions
+32
-2
table.c++
c++/src/kj/table.c++
+32
-2
No files found.
c++/src/kj/table.c++
View file @
5eeb125b
...
...
@@ -123,6 +123,12 @@ kj::Array<HashBucket> rehash(kj::ArrayPtr<const HashBucket> oldBuckets, size_t t
// =======================================================================================
// BTree
#if _WIN32
#define aligned_free _aligned_free
#else
#define aligned_free ::free
#endif
BTreeImpl
::
BTreeImpl
()
:
tree
(
const_cast
<
NodeUnion
*>
(
&
EMPTY_NODE
)),
treeCapacity
(
1
),
...
...
@@ -133,7 +139,7 @@ BTreeImpl::BTreeImpl()
endLeaf
(
0
)
{}
BTreeImpl
::~
BTreeImpl
()
noexcept
(
false
)
{
if
(
tree
!=
&
EMPTY_NODE
)
{
::
free
(
tree
);
aligned_
free
(
tree
);
}
}
...
...
@@ -221,12 +227,36 @@ void BTreeImpl::clear() {
void
BTreeImpl
::
growTree
(
uint
minCapacity
)
{
uint
newCapacity
=
kj
::
max
(
kj
::
max
(
minCapacity
,
treeCapacity
*
2
),
4
);
freelistSize
+=
newCapacity
-
treeCapacity
;
// Allocate some aligned memory! In theory this should be as simple as calling the C11 standard
// aligned_alloc() function. Unfortunately, many platforms don't implement it. Luckily, there
// are usually alternatives.
#if __APPLE__
// OSX lacks aligned_alloc(), but has posix_memalign(). Fine.
void
*
allocPtr
;
int
error
=
posix_memalign
(
&
allocPtr
,
sizeof
(
BTreeImpl
::
NodeUnion
),
newCapacity
*
sizeof
(
BTreeImpl
::
NodeUnion
));
if
(
error
!=
0
)
{
KJ_FAIL_SYSCALL
(
"posix_memalign"
,
error
);
}
NodeUnion
*
newTree
=
reinterpret_cast
<
NodeUnion
*>
(
allocPtr
);
#elif _WIN32
// Windows lacks aligned_alloc() but has its own _aligned_malloc() (which requires freeing using
// _aligned_free()).
NodeUnion
*
newTree
=
reinterpret_cast
<
NodeUnion
*>
(
_aligned_malloc
(
sizeof
(
BTreeImpl
::
NodeUnion
),
newCapacity
*
sizeof
(
BTreeImpl
::
NodeUnion
)));
KJ_ASSERT
(
newTree
!=
nullptr
,
"memory allocation failed"
,
newCapacity
);
#else
// Let's use the C11 standard.
NodeUnion
*
newTree
=
reinterpret_cast
<
NodeUnion
*>
(
aligned_alloc
(
sizeof
(
BTreeImpl
::
NodeUnion
),
newCapacity
*
sizeof
(
BTreeImpl
::
NodeUnion
)));
KJ_ASSERT
(
newTree
!=
nullptr
,
"memory allocation failed"
,
newCapacity
);
#endif
acopy
(
newTree
,
tree
,
treeCapacity
);
azero
(
newTree
+
treeCapacity
,
newCapacity
-
treeCapacity
);
if
(
tree
!=
&
EMPTY_NODE
)
::
free
(
tree
);
if
(
tree
!=
&
EMPTY_NODE
)
aligned_
free
(
tree
);
tree
=
newTree
;
treeCapacity
=
newCapacity
;
}
...
...
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