{"id":20435807,"url":"https://github.com/zsyty/stl-allocator","last_synced_at":"2025-04-12T21:36:21.764Z","repository":{"id":70496199,"uuid":"190999798","full_name":"ZSYTY/STL-allocator","owner":"ZSYTY","description":"Final project of Object-Oriented-Programming: STL allocator + memory pool","archived":false,"fork":false,"pushed_at":"2019-06-22T19:56:38.000Z","size":78,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-26T15:48:07.876Z","etag":null,"topics":["allocator","stl"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ZSYTY.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-06-09T11:47:01.000Z","updated_at":"2023-12-24T08:26:09.000Z","dependencies_parsed_at":"2023-03-11T08:41:25.138Z","dependency_job_id":null,"html_url":"https://github.com/ZSYTY/STL-allocator","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/ZSYTY%2FSTL-allocator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZSYTY%2FSTL-allocator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZSYTY%2FSTL-allocator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ZSYTY%2FSTL-allocator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ZSYTY","download_url":"https://codeload.github.com/ZSYTY/STL-allocator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248637337,"owners_count":21137531,"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":["allocator","stl"],"created_at":"2024-11-15T08:37:49.246Z","updated_at":"2025-04-12T21:36:21.743Z","avatar_url":"https://github.com/ZSYTY.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# STL-allocator\n\n## STL Allocator Interface\n\nAn allocator is used by standard library containers as a template parameter :\n\n```c++\ntemplate \u003c class T, class Alloc = allocator\u003cT\u003e \u003e class vector;\ntemplate \u003c class T, class Alloc = allocator\u003cT\u003e \u003e class list;\n```\n\nWhat does an ``allocator`` class have? Typically, it possesses:\n\n```c++\ntypedef void _Not_user_specialized;\ntypedef _Ty value_type;\ntypedef value_type *pointer;\ntypedef const value_type *const_pointer;\ntypedef value_type\u0026 reference;\ntypedef const value_type\u0026 const_reference;\ntypedef size_t size_type;\ntypedef ptrdiff_t difference_type;\ntypedef true_type propagate_on_container_move_assignment;\ntypedef true_type is_always_equal;\n\npointer address(reference _Val) const _NOEXCEPT\nconst_pointer address(const_reference _Val) const _NOEXCEPT\nvoid deallocate(pointer _Ptr, size_type _Count)\n_DECLSPEC_ALLOCATOR pointer allocate(size_type _Count)\ntemplate\u003cclass _Uty\u003e void destroy(_Uty *_Ptr)\ntemplate\u003cclass _Objty, class _Types\u003e\nvoid construct(_Objty *_Ptr, _Types\u0026\u0026... _Args)\n```\n\nThe above interface is just shown for illustration, please refer to [std::allocator](https://en.cppreference.com/w/cpp/memory/allocator) for the latest specification.\n\n## Memory Pool\n\nSTL provides you a default [std::allocator](https://en.cppreference.com/w/cpp/memory/allocator), but you can implement your own to replace it. For example, you can design a memory pool to speed up the dynamic allocation of a large number of small blocks (e.g., 8 bytes, 16 bytes, ...), and to reduce memory fragmentation.\n\n![Fig1](https://github.com/ZSYTY/STL-allocator/blob/master/resource/Fig1.png)\n\nFigure 1: Mem pool using block based allocation strategy.\n\n## Requirements\n\n- Two people as a group to finish this project (don't forget to write down the group member names, anyone of the - two can submit the final package on PTA).\n- Implement your own memory allocator for STL vector.\n- The allocator should optimize the memory allocation speed using memory pool.\n- The allocator should support arbitrary memory size allocation request.\n\n## How to Test Your Allocator\n\nBasically, you should:\n\n1. Create more than ten thousand vectors with different number of elements.\n2. Pick up 1000 random vectors and resize the vectors with random sizes.\n3. Release all the vectors.\n\nFeel free to extend the following code skeleton for your own tests:\n\n```c++\n#include \u003ciostream\u003e\n#include \u003crandom\u003e\n#include \u003cvector\u003e\n\n// include header of your allocator here\ntemplate\u003cclass T\u003e\nusing MyAllocator = std::allocator\u003cT\u003e; // replace the std::allocator with your allocator\nusing Point2D = std::pair\u003cint, int\u003e;\n\nconst int TestSize = 10000;\nconst int PickSize = 1000;\n\nint main()\n{\n  std::random_device rd;\n  std::mt19937 gen(rd());\n  std::uniform_int_distribution\u003c\u003e dis(1, TestSize);\n\n  // vector creation\n  using IntVec = std::vector\u003cint, MyAllocator\u003cint\u003e\u003e;\n  std::vector\u003cIntVec, MyAllocator\u003cIntVec\u003e\u003e vecints(TestSize);\n  for (int i = 0; i \u003c TestSize; i++)\n    vecints[i].resize(dis(gen));\n\n  using PointVec = std::vector\u003cPoint2D, MyAllocator\u003cPoint2D\u003e\u003e;\n  std::vector\u003cPointVec, MyAllocator\u003cPointVec\u003e\u003e vecpts(TestSize);\n  for (int i = 0; i \u003c TestSize; i++)\n    vecpts[i].resize(dis(gen));\n\n  // vector resize\n  for (int i = 0; i \u003c PickSize; i++)\n  {\n    int idx = dis(gen) - 1;\n    int size = dis(gen);\n    vecints[idx].resize(size);\n    vecpts[idx].resize(size);\n  }\n\n  // vector element assignment\n  {\n    int val = 10;\n    int idx1 = dis(gen) - 1;\n    int idx2 = vecints[idx1].size() / 2;\n    vecints[idx1][idx2] = val;\n    if (vecints[idx1][idx2] == val)\n      std::cout \u003c\u003c \"correct assignment in vecints: \" \u003c\u003c idx1 \u003c\u003c std::endl;\n    else\n      std::cout \u003c\u003c \"incorrect assignment in vecints: \" \u003c\u003c idx1 \u003c\u003c std::endl;\n  }\n  {\n    Point2D val(11, 15);\n    int idx1 = dis(gen) - 1;\n    int idx2 = vecpts[idx1].size() / 2;\n    vecpts[idx1][idx2] = val;\n    if (vecpts[idx1][idx2] == val)\n      std::cout \u003c\u003c \"correct assignment in vecpts: \" \u003c\u003c idx1 \u003c\u003c std::endl;\n    else\n      std::cout \u003c\u003c \"incorrect assignment in vecpts: \" \u003c\u003c idx1 \u003c\u003c std::endl;\n  }\n\n  return 0;\n}\n```\n## Evaluation Standard\n\n1. c++ code quality (clean, compact and reasonable)\n2. comments quality\n3. correctness and running performance of the allocator\n\n## Files to Submit\n\nPlease prepare a .zip package including the following items：\n\n1. the source code (including the testing code)\n2. makefile (for Mac or Linux users) or .exes (for Windows users, with necessary .dlls if you use MinGW) or CMakeLists.txt\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzsyty%2Fstl-allocator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzsyty%2Fstl-allocator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzsyty%2Fstl-allocator/lists"}