-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Small shared two-way pointer #2072
Small shared two-way pointer #2072
Conversation
3fc9221
to
8bc745e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, though I'm surprised we want something so specialised (use-count with the referee AND only two subtypes allowed). How much loss would it be to lift the latter restriction -- so you can have whatever subtypes you please, but require a vtable for destruction?
I'd like to avoid having a vtable pointer in the managed object if not necessary as memory consumption is an issue in our work so we're trying to save wherever we can. I think it would work though to support an arbitrary number of subtypes by using template parameter packs (kinda like |
How about the target (base) type has to (a) hold a refcount and (b) store some enum that provides enough information to fake a virtual delete operation, and it can do either as compactly as it likes? This pointer wouldn't then hard-code the implicit Then if I'm incredibly tight for memory I can have I'm also free to be conventional or unconventional with regards to deletion: Conventional style:
Avoiding a vptr but allowing a few subtypes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice. Just 2 minor comments, otherwise very solid. Thanks
|
||
explicit small_shared_two_way_ptrt(U *u) : p(u) | ||
{ | ||
PRECONDITION(u != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are building with C++11, nullptr
is an instance of the type nullptr_t
, which is implicitly cast to boolean. So I think just PRECONDITION(u)
should suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually prefer to have the nullptr
there as I find it slightly more readable. But I can change it if that's what's done in the rest of the codebase.
src/util/small_shared_two_way_ptr.h
Outdated
const bool b2 = other.p == nullptr; | ||
|
||
if(b1 || b2) | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this work? Why if either of these is true, then are they the same? Isn't it the case that the object is in a compromised logical state if p == nullptr
or that the function is semantically void if other.p == nullptr
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected: if(b1 || b2) return b1 == b2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function checks whether both pointers are convertible to either U *
or V *
, so since nullptr
can be converted to both we return true here. Maybe I should change the name of the function to is_same_type()
or something like that. Any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, is_same_type
sounds better to me.
src/util/small_shared_two_way_ptr.h
Outdated
PRECONDITION(u != nullptr); | ||
PRECONDITION(u->use_count() == 0); | ||
|
||
p->set_derived1(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this refer to U
instead of using 1
? Maybe just set_derived_u
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's probably more readable.
src/util/small_shared_two_way_ptr.h
Outdated
|
||
bool is_derived1() const | ||
{ | ||
return p == nullptr || p->is_derived1(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected p != nullptr && p->is_derived1()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This essentially checks whether the conversion static_cast<U *>(p)
is valid so we also allow nullptr
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it would be good to add once sentence of documentation to make clear this is the expected behaviour. (I don't think I'd have inferred that from "is_derived" - it's more "can_convert".)
src/util/small_shared_two_way_ptr.h
Outdated
const bool b2 = other.p == nullptr; | ||
|
||
if(b1 || b2) | ||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected: if(b1 || b2) return b1 == b2;
src/util/small_shared_two_way_ptr.h
Outdated
static_assert(std::is_unsigned<Num>::value, ""); | ||
|
||
static constexpr int bit_idx = std::numeric_limits<Num>::digits - 1; | ||
static constexpr Num mask = ~(1 << bit_idx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to make sure that 1
is of type Num
as otherwise either the bit-shift may be undefined or you could get surprising results upon bitwise negation.
unit/Makefile
Outdated
@@ -54,6 +54,7 @@ SRC += unit_tests.cpp \ | |||
catch_example.cpp \ | |||
java_bytecode/java_virtual_functions/virtual_functions.cpp \ | |||
java_bytecode/java_bytecode_parse_generics/parse_generic_superclasses.cpp \ | |||
small_shared_two_way_ptr.cpp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this go in the util/
subfolder?
8bc745e
to
dae08e9
Compare
@smowton Yes, that's a good idea. I think we'd want to supply a variadic template that allows to plug in the possible subtypes, and then generates the deletion code (like in your switch statement above), accessor methods, and use count/typeid bitfield. That way the shared pointer logic would be encapsulated in this utility class. The accessors would then be similar to those of If possible, I'd like to leave this further generalization to a future PR and raise an issue for it now. |
42dc58b
to
35c4be7
Compare
31c47c2 Merge pull request diffblue#2113 from diffblue/java_new_array_data fb3025d Merge pull request diffblue#2107 from thk123/feature/TG-3271/interpreter-tracking-mock-exceptions 32bf48e Merge pull request diffblue#2105 from tautschnig/determinise-test 7254a2a show java_new_array_data side effects 0f1482c Merge pull request diffblue#2094 from smowton/smowton/fix/tmp-object-factory-prefix 3cfec66 Merge pull request diffblue#2106 from diffblue/ptrmember_on_array bf4c39c Merge pull request diffblue#1966 from JohnDumbell/JohnDumbell/Update-Assertion-Validation 74a37c6 Merge pull request diffblue#1988 from tautschnig/cadical 74dc576 Merge pull request diffblue#2003 from tautschnig/bitfield-offset 8916906 Merge pull request diffblue#2008 from tautschnig/section-bug 0bd83ab Extension to interpreter class a0ca0ba fix array->f typechecking e1f4120 Make virtual function resolution independent of string table entry ordering 714ccff Merge pull request diffblue#2072 from danpoe/feature/small-shared-two-way-ptr 16b6c20 Merge pull request diffblue#2046 from thk123/gs_tg2922 04cb909 Merge pull request diffblue#2102 from thk123/formatting/sort-includes-clang-format 7070ba1 Sort includes using clang-format 9874a6b Reformatting touched output function f2a4054 Remove redundant default constructor 54fb9ab Use format rather than from_expr for output 781bf7c Introduce exceptions for all conversion steps. 21997b2 Add documentation to convert_bitvector 015b284 Test demonstrating logging with clause for dealing with Windows 9d41b0c Disable nested exception printing for Windows b866015 Provide the original goto statement in the error a97bc28 Introduce throwing a guard conversion exception 12f25c2 Introduce throwing bv_conversion expection 9bd5222 Convert flatten_byte_extract to use structured exceptions 3207291 Introduce nested exception printing 35c4be7 Small shared two way pointer 7d247da Merge pull request diffblue#2099 from mgudemann/bugfix/build/glucose_syrup 1776a9e Merge pull request diffblue#1950 from romainbrenguier/refactor/prop_conv_straightforward 4147243 Change set_variable_name API to consume irep_idt 2d8be06 Rename `it` to pair in boolbvt::print_assignment 4365c28 Simplify boolbvt::set_to b18109f Make make_(free_)bv_expr return exprt 5724a35 Simplify loop in prop_conv::get 4987f3a Remove useless comments 13e87a9 Simplify dec_solve a0500f6 Use standard algorithm for finding an element ba13c94 Use auto for iterator types 9179571 Remove useless includes a905a07 Replace throws by invariant or preconditions 7db44fc Remove virtual keyword where not needed 990f33e Initialize at declaration instead of construction c1a93b3 Renaming `it` to symbol 8eb20f6 Use ranged for dc799e0 Assert replaced by unreachable c34e073 Add support for CaDiCaL 1bd9efd Merge pull request diffblue#2097 from peterschrammel/java-cleanup-replace a079f46 Clang-format moved file 2eb3714 Move replace_java_nondet to java_bytecode 9a8c292 Remove unnecessary include c8cf100 Remove Java refs from ANSI-C docs 0090952 Merge pull request diffblue#2096 from diffblue/cleanout-java aa3caa3 Fix CMake build for Glucose Syrup 1156930 Merge pull request diffblue#1244 from tautschnig/goto-gcc-at-fix 706e391 Merge pull request diffblue#2093 from owen-jones-diffblue/owen-jones-diffblue/remove_unnecessary_irep_id_hash 290feb4 Merge pull request diffblue#2095 from diffblue/get_json_stream_precondition ac2df21 Merge pull request diffblue#2027 from tautschnig/linking-multiple-conflicts dd0d602 Merge pull request diffblue#2030 from tautschnig/goto-cc-linux-kernel 42e58d4 Merge pull request diffblue#2085 from tautschnig/from_expr-cleanup 692f92d remove dependency on java_bytecode 8c6165d precondition for get_json_stream() e28a662 Remove unused typedef 5626fb7 Merge pull request diffblue#2092 from smowton/smowton/cleanup/diffblue-spelling 4840154 Replace stack by deque and use range-based for loop 987edbe Use range-based for loops 92ac82c Remove redundant irep_id_hash for unordered maps dc2b436 Remove redundant irep_id_hash for sets 5aa2c2d Attribute main function arguments to __CPROVER_start b7ef5af Merge pull request diffblue#2053 from owen-jones-diffblue/owen-jones-diffblue/bugfix/make-callgraph-include-uncalled-functions 252474f String tests: DiffBlue -> Diffblue de1915a Merge pull request diffblue#2074 from owen-jones-diffblue/owen-jones-diffblue/lazy-methods-no-candidate-callees d73f6bc Make directed callgraph include nodes with no edges 2a45e61 Only the top-level section should be considered for renaming 9c66a66 fixup! Support __attribute__((section("x")) e133964 C front-end: Section/ASM renaming also needs to be applied to aliases 0cfc72f Test --call-graph and --reachable-call-graph 1c34d22 Test lazy-loading when there are no candidates 289a439 Deal with virtual function calls with no candidate targets 9347615 Remove incorrect comment 18b1962 Fix order of parameters in function header 82058da Store virtual function calls instead of virtual call-sites 3653550 Use unordered set of irep_ids in ci_lazy_methods c31d43f Remove code duplication 945f885 Rename two variables and make one more local b7d70e7 Replace do-while loop with equally valid while loop 58b990d Use from_{expr,type} matching the language of the expression/type 177c8c1 goto-cc: support thin ar archives, refactoring e80008e goto-cc: support GCC's print-sysroot* options 38e6fa5 Accept the --build-id option in goto-ld f3bbb12 Linking: report multiple conflicts 495f109 Fixing member offset computation in presence of bitfields 5109eab Add @<file> arguments to the original command line 97d556e Update desc file to add pass variables. eea76ec Add a regression test. c14e907 Increase AssertionError arguments allowed from 2 to 3 git-subtree-dir: cbmc git-subtree-split: 31c47c2
This is a feature that is used in a future refactoring of
sharing_mapt
.The class is similar to
small_shared_ptrt
and boost'sintrusive_ptr
. Like those, it stores the use count with the pointed-to object instead of in a separate control block. Additionally, it uses the MSB of the use count to indicate the type of the managed object (which is either of typeU
orV
).A possible use case is the implementation of data structures with sharing that consist of two different types of objects (such as a tree with internal nodes and leaf nodes). Storing the type with the use count avoids having to keep a separate
type
member or usingtypeid
ordynamic_cast
. Moreover, since the shared pointer is aware of the concrete type of the object being stored, it can delete it without requiring a virtual destructor or custom delete function (likestd::shared_ptr
).