{"id":23427699,"url":"https://github.com/sunxfancy/smartptr","last_synced_at":"2025-04-09T12:43:11.970Z","repository":{"id":76138952,"uuid":"53783208","full_name":"sunxfancy/SmartPtr","owner":"sunxfancy","description":"C++ smart pointer library with GC","archived":false,"fork":false,"pushed_at":"2016-03-24T10:22:55.000Z","size":64,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-15T06:42:21.002Z","etag":null,"topics":["cpp","garbage-collection","memory-management","smartpointer"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunxfancy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-13T12:10:22.000Z","updated_at":"2021-10-14T00:28:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"428cf722-ecb1-48d3-8a49-1818fe2d6433","html_url":"https://github.com/sunxfancy/SmartPtr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunxfancy%2FSmartPtr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunxfancy%2FSmartPtr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunxfancy%2FSmartPtr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunxfancy%2FSmartPtr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunxfancy","download_url":"https://codeload.github.com/sunxfancy/SmartPtr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045217,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","garbage-collection","memory-management","smartpointer"],"created_at":"2024-12-23T06:35:23.248Z","updated_at":"2025-04-09T12:43:11.935Z","avatar_url":"https://github.com/sunxfancy.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SmartPtr\nC++ smart pointer library with GC\n\n\nIt's hard for C++ to manange memory, because of the downward compatibility for C. We usually use smart pointer based on reference count to prevent the memory leak, but it's useless when come across the circular reference.\n\nSo I want to design a kind of smart pointer with GC, that sometimes will free the memory which is in the status of circular reference. \n\n\n## Design\n\nThe fundamental of this smart pointer is the reference count, but we store the reference number in front of the memory.\n\n![pointer](asserts/ptr.png)\n\nWe have a global singleton manager to alloc memory. When the smart pointer were constructed on the stack, it would register the pointer to the manager.\n\n```c\n\nstruct MemObjNode {\n    size_t size;\n    int count;\n    void* data[];\n};\n\n```\n\n\nThe meta is used to scan the pointer in its data, so we have to know the size about the memory. The next and prev pointers constitute a double linked list which can know the memories it alloced.\n\nBesides the metadata, other parts will be regraded as an array full of pointers. We should test all the pointers in it and find the right pointer we alloced.\n\n\n\nHere our demo:\n\n```cpp\n\n#include \"../include/sptr.hpp\"\n\nclass Test {\npublic:\n    sptr\u003cTest\u003e next;\n};\n\nint test() {\n    sptr_stack\u003cTest\u003e p = new (MemHeap::getInstance()) Test();\n    return 0;\n}\n\n\nint main(int argc, char const *argv[]) {\n    test();\n    sptr_stack\u003cTest\u003e p = new (MemHeap::getInstance()) Test();\n    p-\u003enext = p;\n    p = NULL;\n    MemHeap::runGC();\n\n    return 0;\n}\n\n```\n\nAfter running:\n\n```\nallocate 0x1716cf0\nrelease 0x1716cf0\nallocate 0x1716cf0\nrelease 0x1716cf0\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunxfancy%2Fsmartptr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunxfancy%2Fsmartptr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunxfancy%2Fsmartptr/lists"}