Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
367d8b22
Commit
367d8b22
authored
Jun 30, 2015
by
Chris Toshok
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
factor out find_cf to pyston::binarySearch.
parent
b322d2b2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
27 deletions
+37
-27
src/codegen/unwinding.cpp
src/codegen/unwinding.cpp
+14
-27
src/core/types.h
src/core/types.h
+23
-0
No files found.
src/codegen/unwinding.cpp
View file @
367d8b22
...
@@ -118,35 +118,22 @@ void registerDynamicEhFrame(uint64_t code_addr, size_t code_size, uint64_t eh_fr
...
@@ -118,35 +118,22 @@ void registerDynamicEhFrame(uint64_t code_addr, size_t code_size, uint64_t eh_fr
// dyn_info that contains a binary search table.
// dyn_info that contains a binary search table.
}
}
struct
compare_cf
{
int
operator
()(
const
uint64_t
&
key
,
const
CompiledFunction
*
item
)
const
{
// key is the return address of the callsite, so we will check it against
// the region (start, end] (opposite-endedness of normal half-open regions)
if
(
key
<=
item
->
code_start
)
return
-
1
;
else
if
(
key
>
item
->
code_start
+
item
->
code_size
)
return
1
;
return
0
;
}
};
class
CFRegistry
{
class
CFRegistry
{
private:
private:
std
::
vector
<
CompiledFunction
*>
cfs
;
std
::
vector
<
CompiledFunction
*>
cfs
;
// similar to Java's Array.binarySearch:
// return values are either:
// >= 0 : the index where a given item was found
// < 0 : a negative number that can be transformed (using "-num-1") into the insertion point
//
// addr is the return address of the callsite, so we will check it against
// the region (start, end] (opposite-endedness of normal half-open regions)
//
int
find_cf
(
uint64_t
addr
)
{
int
l
=
0
;
int
r
=
cfs
.
size
()
-
1
;
while
(
l
<=
r
)
{
int
mid
=
l
+
(
r
-
l
)
/
2
;
auto
mid_cf
=
cfs
[
mid
];
if
(
addr
<=
mid_cf
->
code_start
)
{
r
=
mid
-
1
;
}
else
if
(
addr
>
mid_cf
->
code_start
+
mid_cf
->
code_size
)
{
l
=
mid
+
1
;
}
else
{
return
mid
;
}
}
return
-
(
l
+
1
);
}
public:
public:
void
registerCF
(
CompiledFunction
*
cf
)
{
void
registerCF
(
CompiledFunction
*
cf
)
{
if
(
cfs
.
empty
())
{
if
(
cfs
.
empty
())
{
...
@@ -154,7 +141,7 @@ public:
...
@@ -154,7 +141,7 @@ public:
return
;
return
;
}
}
int
idx
=
find_cf
((
uint64_t
)
cf
->
code_start
);
int
idx
=
binarySearch
((
uint64_t
)
cf
->
code_start
,
cfs
.
begin
(),
cfs
.
end
(),
compare_cf
()
);
if
(
idx
>=
0
)
if
(
idx
>=
0
)
RELEASE_ASSERT
(
0
,
"CompiledFunction registered twice?"
);
RELEASE_ASSERT
(
0
,
"CompiledFunction registered twice?"
);
...
@@ -165,7 +152,7 @@ public:
...
@@ -165,7 +152,7 @@ public:
if
(
cfs
.
empty
())
if
(
cfs
.
empty
())
return
NULL
;
return
NULL
;
int
idx
=
find_cf
(
addr
);
int
idx
=
binarySearch
(
addr
,
cfs
.
begin
(),
cfs
.
end
(),
compare_cf
()
);
if
(
idx
>=
0
)
if
(
idx
>=
0
)
return
cfs
[
idx
];
return
cfs
[
idx
];
...
...
src/core/types.h
View file @
367d8b22
...
@@ -747,6 +747,29 @@ struct CallattrFlags {
...
@@ -747,6 +747,29 @@ struct CallattrFlags {
char
asInt
()
{
return
(
cls_only
<<
0
)
+
(
null_on_nonexistent
<<
1
);
}
char
asInt
()
{
return
(
cls_only
<<
0
)
+
(
null_on_nonexistent
<<
1
);
}
};
};
// similar to Java's Array.binarySearch:
// return values are either:
// >= 0 : the index where a given item was found
// < 0 : a negative number that can be transformed (using "-num-1") into the insertion point
//
template
<
typename
T
,
typename
RandomAccessIterator
,
typename
Cmp
>
int
binarySearch
(
T
needle
,
RandomAccessIterator
start
,
RandomAccessIterator
end
,
Cmp
cmp
)
{
int
l
=
0
;
int
r
=
end
-
start
-
1
;
while
(
l
<=
r
)
{
int
mid
=
l
+
(
r
-
l
)
/
2
;
auto
mid_item
=
*
(
start
+
mid
);
int
c
=
cmp
(
needle
,
mid_item
);
if
(
c
<
0
)
r
=
mid
-
1
;
else
if
(
c
>
0
)
l
=
mid
+
1
;
else
return
mid
;
}
return
-
(
l
+
1
);
}
}
}
namespace
std
{
namespace
std
{
...
...
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