{"id":18449487,"url":"https://github.com/nic-obert/buddy-allocator","last_synced_at":"2025-10-28T00:38:39.930Z","repository":{"id":252415732,"uuid":"839762110","full_name":"nic-obert/buddy-allocator","owner":"nic-obert","description":"A buddy allocator implemented in Rust","archived":false,"fork":false,"pushed_at":"2024-08-10T10:42:31.000Z","size":26,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T13:34:57.867Z","etag":null,"topics":["allocator","buddy-allocator","memory-management","rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/nic-obert.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":"2024-08-08T09:22:46.000Z","updated_at":"2024-08-15T13:41:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"eafc0be8-7756-450f-a680-4662861fb6f2","html_url":"https://github.com/nic-obert/buddy-allocator","commit_stats":null,"previous_names":["nic-obert/buddy-allocator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nic-obert%2Fbuddy-allocator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nic-obert%2Fbuddy-allocator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nic-obert%2Fbuddy-allocator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nic-obert%2Fbuddy-allocator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nic-obert","download_url":"https://codeload.github.com/nic-obert/buddy-allocator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249128266,"owners_count":21217111,"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","buddy-allocator","memory-management","rust"],"created_at":"2024-11-06T07:20:19.491Z","updated_at":"2025-10-28T00:38:34.884Z","avatar_url":"https://github.com/nic-obert.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Buddy Allocator\n\nA buddy allocator implemented in Rust.\n\n- [Buddy Allocator](#buddy-allocator)\n- [Basic usage](#basic-usage)\n- [How it works](#how-it-works)\n- [License](#license)\n\n# Basic usage\n\nAllocate and free memory:\n\n```rust\n// Create a buddy allocator with a heap size of 1024 bytes and a zero-order block of 8 bytes.\nlet mut alloc = BuddyAllocator::\u003c1024, 8\u003e::new(false);\n\nlet size_to_alloc: usize = 16;\n\n// Allocate a memory block.\nlet my_pointer: NonNull\u003cu8\u003e = alloc.as_mut().alloc_bytes(size_to_alloc).unwrap_or_else(\n    |err| panic!(\"Allocation failed with error {:?}\", err)\n);\n\n// Do stuff with the pointer...\n\n// Free the memory block\nalloc.as_mut().free_nonnull(my_pointer).unwrap_or_else(\n    |err| panic!(\"Failed to free pointer {:?} with error {:?}\", my_pointer, err)\n); \n```\n\nAllocate memory for a structure:\n\n```rust\n// Create a buddy allocator with a heap size of 1024 bytes and a zero-order block of 8 bytes.\nlet mut alloc = BuddyAllocator::\u003c1024, 8\u003e::new(false);\n\nstruct MyStruct (usize, usize, u32);\n\n// Allocate a memory block that fits an instance of MyStruct.\nlet my_ptr = alloc.as_mut().alloc::\u003cMyStruct\u003e()\n    .unwrap_or_else(|err| panic!(\"Allocation failed with error {:?}\", err));\n\n// You can also cast the NonNull\u003cT\u003e to a raw pointer.\nlet my_ptr = my_ptr.as_ptr();\n\n// Initialize the struct.\nunsafe {\n    *my_ptr = MyStruct (32, 3, 90);\n}\n\n// Free the block that contains the struct.\nalloc.as_mut().free(my_ptr)\n    .unwrap_or_else(|err| panic!(\"Failed to free pointer {:?} with error {:?}\", my_ptr, err)\n);\n```\n\nConstruct the allocator directly on the stack. This approach removes any dependency on the standard system allocator, which is suitable for embedded development or in `#![no_std]` environments where an allocator may not be avalilable.\n\n```rust\n// Create a buddy allocator with a heap size of 1024 bytes and a zero-order block of 8 bytes.\n// The allocator must be readily pinned because it will live on the stack.\nlet mut alloc = pin!( unsafe { \n    BuddyAllocator::\u003c1024, 8\u003e::new_unpinned(false)\n});\n// Initializing a stack-pinned allocator is mandatory to use it safely\nunsafe {\n    alloc.as_mut().init_pinned()\n}\n\nlet size_to_alloc: usize = 16;\n\n// Allocate a memory block.\nlet my_pointer: NonNull\u003cu8\u003e = alloc.as_mut().alloc_bytes(size_to_alloc).unwrap_or_else(\n    |err| panic!(\"Allocation failed with error {:?}\", err)\n);\n\n// Do stuff with the pointer...\n\n// Free the memory block\nalloc.as_mut().free_nonnull(my_pointer).unwrap_or_else(\n    |err| panic!(\"Failed to free pointer {:?} with error {:?}\", my_pointer, err)\n); \n\n\n// Also works with structs\n\nstruct MyStruct (usize, usize, u32);\n\n// Allocate a memory block that fits an instance of MyStruct.\nlet my_ptr = alloc.as_mut().alloc::\u003cMyStruct\u003e()\n    .unwrap_or_else(|err| panic!(\"Allocation failed with error {:?}\", err));\n\n// You can also cast the NonNull\u003cT\u003e to a raw pointer.\nlet my_ptr = my_ptr.as_ptr();\n\n// Initialize the struct.\nunsafe {\n    *my_ptr = MyStruct (32, 3, 90);\n}\n\n// Free the block that contains the struct.\nalloc.as_mut().free(my_ptr)\n    .unwrap_or_else(|err| panic!(\"Failed to free pointer {:?} with error {:?}\", my_ptr, err)\n);\n```\n\n# How it works\n\nThis buddy allocator implementation keeps a record of the allocated and free blocks using a binary tree, where each leaf node represents a memory block. Adjacent free nodes are merged to avoid fragmentation and big memory blocks are split in half is the requested allocation is small enough.\n\nA more detailed explanation is available in the source code through comments.\n\n\n# License\n\nThis repository and the code within it are published under the [MIT license](LICENSE).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnic-obert%2Fbuddy-allocator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnic-obert%2Fbuddy-allocator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnic-obert%2Fbuddy-allocator/lists"}