{"id":18736860,"url":"https://github.com/bitwizeshift/msl","last_synced_at":"2025-11-17T04:30:14.657Z","repository":{"id":81485477,"uuid":"241257284","full_name":"bitwizeshift/MSL","owner":"bitwizeshift","description":"Memory Support Library","archived":false,"fork":false,"pushed_at":"2022-05-24T02:15:15.000Z","size":317,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-28T17:14:06.854Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/bitwizeshift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"docs/contributing.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["bitwizeshift"],"custom":"https://www.buymeacoffee.com/dsq3XCcBE"}},"created_at":"2020-02-18T02:46:46.000Z","updated_at":"2022-12-21T05:28:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"a991d384-ac64-46c2-8963-aaddb51cddde","html_url":"https://github.com/bitwizeshift/MSL","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/bitwizeshift%2FMSL","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwizeshift%2FMSL/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwizeshift%2FMSL/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitwizeshift%2FMSL/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitwizeshift","download_url":"https://codeload.github.com/bitwizeshift/MSL/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239611993,"owners_count":19668274,"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":[],"created_at":"2024-11-07T15:22:45.326Z","updated_at":"2025-11-17T04:30:14.613Z","avatar_url":"https://github.com/bitwizeshift.png","language":"C++","funding_links":["https://github.com/sponsors/bitwizeshift","https://www.buymeacoffee.com/dsq3XCcBE"],"categories":[],"sub_categories":[],"readme":"[![Ubuntu Build Status](https://github.com/bitwizeshift/msl/workflows/Ubuntu/badge.svg?branch=master)](https://github.com/bitwizeshift/msl/actions?query=workflow%3AUbuntu)\n[![Windows Build Status](https://github.com/bitwizeshift/msl/workflows/Windows/badge.svg?branch=master)](https://github.com/bitwizeshift/msl/actions?query=workflow%3AWindows)\n[![Coverage Status](https://coveralls.io/repos/github/bitwizeshift/MSL/badge.svg?branch=master)](https://coveralls.io/github/bitwizeshift/MSL?branch=master)\n[![Github Issues](https://img.shields.io/github/issues/bitwizeshift/MSL.svg)](http://github.com/bitwizeshift/msl/issues)\n\u003cbr\u003e\n[![Github Releases](https://img.shields.io/github/v/release/bitwizeshift/msl.svg?include_prereleases)](https://github.com/bitwizeshift/msl/releases)\n[![GitHub Sponsors](https://img.shields.io/badge/GitHub-Sponsors-ff69b4)](https://github.com/sponsors/bitwizeshift)\n\n# Memory Support Library\n\nThis library is an experimental redesign of the entire C++ Allocator model and\nparadigm, written with type-safety and optimizations in mind.\n\nThis places as much of an allocation's information into the **type-system** as\npossible to provide better hints to the compiler, and to have better static\nsafety and type disambiguation.\n\n## Teaser\n\n```cpp\n\n// Unaligned dot-product\nauto dot(const msl::cell\u003cfloat[]\u003e\u0026 lhs, const msl::cell\u003cfloat[]\u003e\u0026 rhs) -\u003e float { ... }\n\n// Over-aligned dot-product. Use SIMD instructions to optimize this.\nauto dot(const msl::cell\u003cfloat[],64u\u003e\u0026 lhs, const msl::cell\u003cfloat[],64u\u003e\u0026 rhs) -\u003e float { ... }\n```\n\n## Features\n\n✔ Written in modern \u003ckbd\u003eC++20\u003c/kbd\u003e \\\n✔ Encodes your pointers' reachability and alignment into the **type-system** \\\n✔ Hints alignment to the compiler for optimal code-generation \\\n✔ Homogeneous allocator types \\\n✔ Composable, reusable allocator abstractions \\\n✔ Adaptable into the standard C++ allocator model\n\n## Quick References\n\n* [🔍 Background](#background) \\\n  A background on the problem **MSL** solves\n* [💾 Installation](doc/installing.md) \\\n  For a quick guide on how to install/use this in other projects\n* [📄 API Reference](https://bitwizeshift.github.io/msl/api/latest/) \\\n  For doxygen-generated API information\n* [🚀 Contributing](.github/CONTRIBUTING.md) \\\n  How to contribute to the **Result** project\n* [💼 Attribution](docs/legal.md) \\\n  Information about how to attribute this project\n* [❓ FAQ](docs/faq.md) \\\n  A list of frequently asked questions\n\n## Background\n\nTODO\n\n### Comparison with standard allocators\n\n#### Homogeneous Allocators\n\nThe C++ allocator requires a lot of rebinding to work correctly, since\nallocators are fixed in terms of type `T`. C++17 made the inner machinery a\nlittle nicer by allowing `memory_resource`s that are written in terms of `void*`.\nC++20 cleaned this up further by allowing `polymorphic_allocator\u003c\u003e` objects to have\n`allocate_bytes()` and `allocate_objects()` -- but this does not change the\nfact that `allocator` remains as a `template`.\n\n**MSL** defines exactly 1 type of object for allocators, simply called\n`msl::allocator`, which may be built out of any composition of\n`memory_resource`s.\n\n#### Better type safety\n\nThe C++ allocator model simply distributes pointers (`T*`) for _all_ allocated\nobjects -- irrespective of whether the pointer refers to an object with an\nactive lifetime or not.\n\nAdditionally, these pointers have no reachability guarantees -- and expressions\nlike `p - q` are a form of undefined behavior assuming `p` and `q` were both\nallocated; but this is allowed from the type system that is used in the\nstandard.\n\n**MSL** addresses this by using different types to represent the various\nstates of objects, and each of these come with their own respective limitations\nto eliminate undefined behavior:\n\n* `cell` \\\n  An object that represents an allocated object that does not yet contain a\n  lifetime. This type is effectively a dummy wrapper that knows the type and\n  reachability, but prevents the user from doing most things that would cause\n  undefined behavior. `cell` objects are returned by allocation calls that\n  do not construct the objects.\n\n* `active_cell` \\\n  An object that represents an allocated object that _does_ contain a lifetime.\n  This behaves more like a typical pointer, allowing users to dereference and\n  access the underlying object(s). `active_cell` objects are returned from\n  allocation calls that do construct the objects.\n\n* `unsafe_cell` \\\n  A `cell` object whose lifetime is unknown. It may represent an `active_cell`\n  or a `cell`. It is deemed `unsafe` for this matter, and requires the user\n  to manually be aware of its uses and lifetime. `unsafe_cell`s are only ever\n  constructed manually by the user. For it to be used with an `allocator`, it\n  must be converted back to one of the strongly-typed `cell` or `active_cell`\n  objects.\n\n* `memory_block` \\\n  A contiguous sequence of bytes that is used internally to represent blocks\n  inside `memory_resource` objects. These get converted to `cell`s internally.\n\n#### Allocated pointers contain information in their types\n\nThe C++ allocator model fails to encode two crucial pieces of detail in\nallocated objects: the length of the allocation, and the alignment. This forces\nthe caller to be aware of the former by storing the size in a disjoint area, and\nalso causes the latter alignment to be potentially lost to potential\noptimizations that over-aligning could bring, since this occurs at runtime.\n\n**MSL** addresses this by encoding as much detail as it can into the types\nthat are returned by allocators.\n\n* `cell` is actually a `template` on `T` (the type) and its `Align`ment. This\n  is done because alignments are almost always known at compile-time since they\n  are used to achieve better optimizations for target hardware. Templates help\n  the code-generation since they can be statically analyzed and used with\n  compiler intrinsics to indicate that these pointers are always aligned.\n\n  * By encoding the alignment in the type, we also gain the ability to\n    differentiate overloads based on alignment -- something that can statically\n    trigger better code-generation as desired. For example, you can now\n    overload:\n    ```c++\n    // Calls SIMD instructions with no overhead\n    void simd_add(active_cell\u003cfloat[4],64\u003e\u0026 out, const active_cell\u003cfloat[4],64\u003e\u0026 to_add);\n\n    // Incurs overhead of unaligned SIMD loads first\n    void simd_add(active_cell\u003cfloat[4]\u003e\u0026 out, const active_cell\u003cfloat[4]\u003e\u0026 to_add);\n    ```\n\n* `cell` objects of known array bounds, such as `cell\u003cT[N]\u003e`, are always as\n  lightweight as a pointer -- since the size is now encoded in the type.\n\n* `cell` objects of unknown array bounds, such as `cell\u003cT[]\u003e`, are the same as\n  a `T*` and a `size_t` pair. This is the same effective cost that the C++\n  allocator model imposes already indirectly, but couples the sizes together.\n\n#### Support for reallocation and allocation *expansion*\n\nC offers the capability to expand memory regions with `realloc`, since all\nobjects in C are trivially copyable. C++ cannot offer this guarantee unless\nthe objects themselves are trivially copyable -- and thus the C++ allocator\nmodel is unable to do this.\n\n**MSL** is able to offer this capability quite easily though, thanks to\ndifferentiating `active_cell` objects from `cell` objects.\n\n* If the allocation being reallocated is a `cell` object, it can always be\n  reallocated because no objects are constructed yet in the cell -- so there\n  exists nothing to trivially copy.\n* If the allocation being reallocated is an `active_cell` object, it can\n  always be reallocated if the type is trivially copyable. If the type is not\n  trivially copyable, then the active cell range can call the move constructor\n  on each object being moved first.\n\nIn addition to this, **MSL** also introduces the concept of _resizing_\nallocations rather than simply moving to larger allocations. This can allow\nallocations to be expanded in-place without the cost of reallocating objects.\n\nThis functionality makes a big difference in the implementation of types like\n`std::vector`, which can try to `resize_allocation`s first -- which can allow\nfor an unbounded `vector` using a `monotonic_memory_resource` to grow to the\ncorrect size without needing to `reserve` the space first or incur a\nreallocation.\n\n#### Support for fused allocate/construct and destroy/deallocate\n\nThe C++ allocator model, prior to C++20, forced users to use a disconnected\n`construct` to construct objects in pointers allocated by `allocate`. C++20\nintroduces a `new_object`/`delete_object` to finally accomplish this, but it\nonly exists in `polymorphic_allocator\u003cstd::byte\u003e` -- which is not particularly\nintuitive.\n\nThe allocator model in **MSL** is designed from the bottom-up with this idea\nin mind -- wherein the `allocator` concept supports:\n\n* `make_object`,\n* `make_objects`, and\n* `dispose`\n\nWhere the former allocates and constructs a single or array of objects\nrespectively, and the latter destroys and deallocates those objects.\n\nSince **MSL** uses a stronger type for encoding this data, this makes it easy\nand intuitive for any user to make use of this.\n\n## FAQ\n\n### What does **MSL** do to prevent UB?\n\n**MSL** encodes active objects, their alignment, and (conditionally) an array's\nlength into a new _type_ rather than coalescing all of these things into a\ngeneric pointer. This type imposes restrictions on what the user may or may not\ndo with those values.\n\nAs with almost anything in C++, it's always possible for someone hell-bent on\ndoing something wrong to introduce undefined behavior. However, it is quite\ndifficult to _unintentionally_ do something wrong with this library.\n\n## License\n\n\u003e Copyright \u0026copy; 2020-2021 Matthew Rodusek\n\u003e\n\u003e Permission is hereby granted, free of charge, to any person obtaining a copy\n\u003e of this software and associated documentation files (the \"Software\"), to deal\n\u003e in the Software without restriction, including without limitation the rights\n\u003e to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\u003e copies of the Software, and to permit persons to whom the Software is\n\u003e furnished to do so, subject to the following conditions:\n\u003e\n\u003e The above copyright notice and this permission notice shall be included in all\n\u003e copies or substantial portions of the Software.\n\u003e\n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\u003e IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\u003e FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\u003e AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\u003e LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\u003e OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\u003e SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitwizeshift%2Fmsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitwizeshift%2Fmsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitwizeshift%2Fmsl/lists"}