{"id":22031087,"url":"https://github.com/russbaz/mini-alloc","last_synced_at":"2025-06-28T11:06:08.353Z","repository":{"id":257812270,"uuid":"868105614","full_name":"RussBaz/mini-alloc","owner":"RussBaz","description":"Small container for retaining Swift object references - aka mini allocator","archived":false,"fork":false,"pushed_at":"2024-10-08T18:55:55.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-29T04:27:47.823Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/RussBaz.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-10-05T13:46:49.000Z","updated_at":"2024-10-08T18:55:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"669de324-287f-44ec-b111-1ea5b7e57c84","html_url":"https://github.com/RussBaz/mini-alloc","commit_stats":null,"previous_names":["russbaz/mini-alloc"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/RussBaz/mini-alloc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussBaz%2Fmini-alloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussBaz%2Fmini-alloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussBaz%2Fmini-alloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussBaz%2Fmini-alloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RussBaz","download_url":"https://codeload.github.com/RussBaz/mini-alloc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussBaz%2Fmini-alloc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262419747,"owners_count":23308098,"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-30T08:14:09.963Z","updated_at":"2025-06-28T11:06:08.325Z","avatar_url":"https://github.com/RussBaz.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mini-Alloc\n\nA basic, thread-safe, sparsed container to hold references to Swift objects.\n\nThe intended use case is when you need to create an unknown number of objects and pass them by reference to some C code, and be sure that they will not be garbage collected before the C code finishes processing of those objects.\n\nIt does not compact the internal storage, unless you run the `releaseAll` method and provide a new default size.\n\nThis project currently requires Swift 6.0+\n\nSample usage:\n\n```swift\n// Basic Usage\nfinal class TestObject {\n    var counter = 0\n}\n\nlet allocator1 = MAContainer\u003cTestObject\u003e()\n\nlet id1 = allocator1.retain(TestObject())\n\n// and when done\nallocator1.release(id1)\n\n// Or if your object requires knowing its own id\n\nfinal class TestObjectWithId {\n    let id: Int\n    var counter = 0\n\n    init(id: Int = 0) {\n        self.id = id\n    }\n}\n\nlet allocator2 = MAContainer\u003cTestObjectWithId\u003e()\n\nallocator2.retain { .init(id: $0) }\n```\n\nAll methods:\n\n```swift\ninit(initialSize size: Int = 8) // size states how much space to reserved for references\n\n// T: AnyObject\n@discardableResult func retain(using: (Int) -\u003e T?) -\u003e Int? // returns a retained object id\n@discardableResult func retain(_ item: T) -\u003e Int? // returns a retained object id\n\n@discardableResult func release(_ id: Int) -\u003e Bool // returns true only if a retained object with the given id was released\nfunc release(_ ids: [Int])\n\nfunc find(by id: Int) -\u003e T? // returns a retained object if it exists\n\n@discardableResult func update\u003cU\u003e(with id: Int, using: (inout T) -\u003e U) -\u003e U? // returns an update function result if the retained object with a given id is found\n\n// do something with every retained object in the container\nfunc map\u003cU\u003e(using: (T) -\u003e U) -\u003e [U]\nfunc each(using: (T) -\u003e Void)\nfunc filter(_ predicate: (T) -\u003e Bool) -\u003e [T]\n\nfunc releaseAll(size: Int? = nil) // if the size is provided, it will resize the internal storage to the specified size. Otherwise, it will leave it as it is.\n\nfunc pointer(to id: Int) -\u003e UnsafeMutablePointer\u003cT\u003e? // returns a pointer to a retained object with the provided id if it exists\n```\n\nSPM installation:\n\n- Add the package to your package dependencies\n\n```swift\n.package(url: \"https://github.com/RussBaz/mini-alloc\", from: \"1.0.2\"),\n```\n\n- Then add it to your target dependencies\n\n```swift\n.product(name: \"MA\", package: \"mini-alloc\"),\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussbaz%2Fmini-alloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frussbaz%2Fmini-alloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussbaz%2Fmini-alloc/lists"}