diff --git a/lib/std/heap.zig b/lib/std/heap.zig index 8e3d81d26db43deecf167ed824c7efd81e359757..ffa9cc1862c01875d1c09547d965c8a8df6b3efa 100644 --- a/lib/std/heap.zig +++ b/lib/std/heap.zig @@ -30,13 +30,6 @@ pub fn MemoryPool(comptime Item: type) type { } pub const memory_pool = @import("heap/memory_pool.zig"); -/// Deprecated; use `memory_pool.Aligned`. -pub const MemoryPoolAligned = memory_pool.Aligned; -/// Deprecated; use `memory_pool.Extra`. -pub const MemoryPoolExtra = memory_pool.Extra; -/// Deprecated; use `memory_pool.Options`. -pub const MemoryPoolOptions = memory_pool.Options; - /// comptime-known minimum page size of the target. /// /// All pointers from `mmap` or `NtAllocateVirtualMemory` are aligned to at least diff --git a/lib/std/heap/memory_pool.zig b/lib/std/heap/memory_pool.zig index 30691ba1299064d423a8be5d5ba8bab679ad0ab9..63582b868725c6bb3b441ecdcbc6248b40ff746d 100644 --- a/lib/std/heap/memory_pool.zig +++ b/lib/std/heap/memory_pool.zig @@ -3,11 +3,6 @@ const Allocator = std.mem.Allocator; const Alignment = std.mem.Alignment; const MemoryPool = std.heap.MemoryPool; -/// Deprecated. -pub fn Managed(comptime Item: type) type { - return ExtraManaged(Item, .{ .alignment = null }); -} - /// A memory pool that can allocate objects of a single type very quickly. /// Use this when you need to allocate a lot of objects of the same type, /// because it outperforms general purpose allocators. @@ -18,11 +13,6 @@ pub fn Aligned(comptime Item: type, comptime alignment: Alignment) type { return Extra(Item, .{ .alignment = alignment }); } -/// Deprecated. -pub fn AlignedManaged(comptime Item: type, comptime alignment: Alignment) type { - return ExtraManaged(Item, .{ .alignment = alignment }); -} - pub const Options = struct { /// The alignment of the memory pool items. Use `null` for natural alignment. alignment: ?Alignment = null, @@ -83,13 +73,6 @@ pub fn Extra(comptime Item: type, comptime pool_options: Options) type { pool.* = undefined; } - pub fn toManaged(pool: Pool, allocator: Allocator) ExtraManaged(Item, pool_options) { - return .{ - .allocator = allocator, - .unmanaged = pool, - }; - } - /// Pre-allocates `num` items and adds them to the memory pool. /// This allows at least `num` active allocations before an /// `OutOfMemory` error might happen when calling `create()`. @@ -114,7 +97,7 @@ pub fn Extra(comptime Item: type, comptime pool_options: Options) type { /// NOTE: If `mode` is `free_all`, the function will always return `true`. pub fn reset(pool: *Pool, allocator: Allocator, mode: ResetMode) bool { // TODO: Potentially store all allocated objects in a list as well, allowing to - // just move them into the free list instead of actually releasing the memory. + // just move them into the free list instead of actually releasing the memory. var arena = pool.arena_state.promote(allocator); defer pool.arena_state = arena.state; @@ -155,182 +138,56 @@ pub fn Extra(comptime Item: type, comptime pool_options: Options) type { }; } -/// Deprecated. -pub fn ExtraManaged(comptime Item: type, comptime pool_options: Options) type { - if (pool_options.alignment) |a| { - if (a.compare(.eq, .of(Item))) { - var new_options = pool_options; - new_options.alignment = null; - return ExtraManaged(Item, new_options); - } - } - return struct { - const Pool = @This(); - - allocator: Allocator, - unmanaged: Unmanaged, - - pub const Unmanaged = Extra(Item, pool_options); - pub const item_size = Unmanaged.item_size; - pub const item_alignment = Unmanaged.item_alignment; - - const ItemPtr = Unmanaged.ItemPtr; - - /// Creates a new memory pool. - pub fn init(allocator: Allocator) Pool { - return Unmanaged.empty.toManaged(allocator); - } - - /// Creates a new memory pool and pre-allocates `num` items. - /// This allows up to `num` active allocations before an - /// `OutOfMemory` error might happen when calling `create()`. - pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Pool { - return (try Unmanaged.initCapacity(allocator, num)).toManaged(allocator); - } - - /// Destroys the memory pool and frees all allocated memory. - pub fn deinit(pool: *Pool) void { - pool.unmanaged.deinit(pool.allocator); - pool.* = undefined; - } - - /// Pre-allocates `num` items and adds them to the memory pool. - /// This allows at least `num` active allocations before an - /// `OutOfMemory` error might happen when calling `create()`. - pub fn addCapacity(pool: *Pool, num: usize) Allocator.Error!void { - return pool.unmanaged.addCapacity(pool.allocator, num); - } - - pub const ResetMode = Unmanaged.ResetMode; - - /// Resets the memory pool and destroys all allocated items. - /// This can be used to batch-destroy all objects without invalidating the memory pool. - /// - /// The function will return whether the reset operation was successful or not. - /// If the reallocation failed `false` is returned. The pool will still be fully - /// functional in that case, all memory is released. Future allocations just might - /// be slower. - /// - /// NOTE: If `mode` is `free_all`, the function will always return `true`. - pub fn reset(pool: *Pool, mode: ResetMode) bool { - return pool.unmanaged.reset(pool.allocator, mode); - } - - /// Creates a new item and adds it to the memory pool. - pub fn create(pool: *Pool) Allocator.Error!ItemPtr { - return pool.unmanaged.create(pool.allocator); - } - - /// Destroys a previously created item. - /// Only pass items to `ptr` that were previously created with `create()` of the same memory pool! - pub fn destroy(pool: *Pool, ptr: ItemPtr) void { - return pool.unmanaged.destroy(ptr); - } - - fn allocNew(pool: *Pool) Allocator.Error!*align(item_alignment) [item_size]u8 { - return pool.unmanaged.allocNew(pool.allocator); - } - }; -} - test "basic" { const a = std.testing.allocator; - { - var pool: MemoryPool(u32) = .empty; - defer pool.deinit(a); + var pool: MemoryPool(u32) = .empty; + defer pool.deinit(a); - const p1 = try pool.create(a); - const p2 = try pool.create(a); - const p3 = try pool.create(a); + const p1 = try pool.create(a); + const p2 = try pool.create(a); + const p3 = try pool.create(a); - // Assert uniqueness - try std.testing.expect(p1 != p2); - try std.testing.expect(p1 != p3); - try std.testing.expect(p2 != p3); + // Assert uniqueness + try std.testing.expect(p1 != p2); + try std.testing.expect(p1 != p3); + try std.testing.expect(p2 != p3); - pool.destroy(p2); - const p4 = try pool.create(a); + pool.destroy(p2); + const p4 = try pool.create(a); - // Assert memory reuse - try std.testing.expect(p2 == p4); - } - - { - var pool: Managed(u32) = .init(std.testing.allocator); - defer pool.deinit(); - - const p1 = try pool.create(); - const p2 = try pool.create(); - const p3 = try pool.create(); - - // Assert uniqueness - try std.testing.expect(p1 != p2); - try std.testing.expect(p1 != p3); - try std.testing.expect(p2 != p3); - - pool.destroy(p2); - const p4 = try pool.create(); - - // Assert memory reuse - try std.testing.expect(p2 == p4); - } + // Assert memory reuse + try std.testing.expect(p2 == p4); } test "initCapacity (success)" { const a = std.testing.allocator; - { - var pool: MemoryPool(u32) = try .initCapacity(a, 4); - defer pool.deinit(a); + var pool: MemoryPool(u32) = try .initCapacity(a, 4); + defer pool.deinit(a); - _ = try pool.create(a); - _ = try pool.create(a); - _ = try pool.create(a); - } - - { - var pool: Managed(u32) = try .initCapacity(a, 4); - defer pool.deinit(); - - _ = try pool.create(); - _ = try pool.create(); - _ = try pool.create(); - } + _ = try pool.create(a); + _ = try pool.create(a); + _ = try pool.create(a); } test "initCapacity (failure)" { const failer = std.testing.failing_allocator; try std.testing.expectError(error.OutOfMemory, MemoryPool(u32).initCapacity(failer, 5)); - try std.testing.expectError(error.OutOfMemory, Managed(u32).initCapacity(failer, 5)); } test "growable" { const a = std.testing.allocator; - { - var pool: Extra(u32, .{ .growable = false }) = try .initCapacity(a, 4); - defer pool.deinit(a); + var pool: Extra(u32, .{ .growable = false }) = try .initCapacity(a, 4); + defer pool.deinit(a); - _ = try pool.create(a); - _ = try pool.create(a); - _ = try pool.create(a); - _ = try pool.create(a); + _ = try pool.create(a); + _ = try pool.create(a); + _ = try pool.create(a); + _ = try pool.create(a); - try std.testing.expectError(error.OutOfMemory, pool.create(a)); - } - - { - var pool: ExtraManaged(u32, .{ .growable = false }) = try .initCapacity(a, 4); - defer pool.deinit(); - - _ = try pool.create(); - _ = try pool.create(); - _ = try pool.create(); - _ = try pool.create(); - - try std.testing.expectError(error.OutOfMemory, pool.create()); - } + try std.testing.expectError(error.OutOfMemory, pool.create(a)); } test "greater than pointer default alignment" { @@ -339,21 +196,11 @@ test "greater than pointer default alignment" { }; const a = std.testing.allocator; - { - var pool: MemoryPool(Foo) = .empty; - defer pool.deinit(a); + var pool: MemoryPool(Foo) = .empty; + defer pool.deinit(a); - const foo: *Foo = try pool.create(a); - pool.destroy(foo); - } - - { - var pool: Managed(Foo) = .init(a); - defer pool.deinit(); - - const foo: *Foo = try pool.create(); - pool.destroy(foo); - } + const foo: *Foo = try pool.create(a); + pool.destroy(foo); } test "greater than pointer manual alignment" { @@ -362,19 +209,9 @@ test "greater than pointer manual alignment" { }; const a = std.testing.allocator; - { - var pool: Aligned(Foo, .@"16") = .empty; - defer pool.deinit(a); + var pool: Aligned(Foo, .@"16") = .empty; + defer pool.deinit(a); - const foo: *align(16) Foo = try pool.create(a); - pool.destroy(foo); - } - - { - var pool: AlignedManaged(Foo, .@"16") = .init(a); - defer pool.deinit(); - - const foo: *align(16) Foo = try pool.create(); - pool.destroy(foo); - } + const foo: *align(16) Foo = try pool.create(a); + pool.destroy(foo); }