Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Forums
  - IRC
  - Community Discord
  - Wiki
  - GitHub
  - Issues
  - Get involved
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# std.experimental.allocator.building_blocks.scoped_allocator

Source std/experimental/allocator/building_blocks/scoped_allocator.d

struct `ScopedAllocator`(ParentAllocator);
  `ScopedAllocator` delegates all allocation requests to ParentAllocator. When destroyed, the `ScopedAllocator` object automatically calls deallocate for all memory allocated through its lifetime. (The deallocateAll function is also implemented with the same semantics.)
  deallocate is also supported, which is where most implementation effort and overhead of `ScopedAllocator` go. If deallocate is not needed, a simpler design combining AllocatorList with Region is recommended.
  Examples:

      import std.experimental.allocator.mallocator : Mallocator;
      import std.typecons : Ternary;
      ScopedAllocator!Mallocator alloc;
      writeln(alloc.empty); // Ternary.yes
      const b = alloc.allocate(10);
      writeln(b.length); // 10
      writeln(alloc.empty); // Ternary.no

  Allocator `parent`;
    If ParentAllocator is stateful, `parent` is a property giving access to an AffixAllocator!ParentAllocator. Otherwise, `parent` is an alias for AffixAllocator!ParentAllocator.instance.
  enum auto `alignment`;
    Alignment offered
  size_t `goodAllocSize`(size_t `n`);
    Forwards to parent.`goodAllocSize` (which accounts for the management overhead).
  void[] `allocate`(size_t `n`);
    Allocates memory. For management it actually allocates extra memory from the parent.
  bool `expand`(ref void[] `b`, size_t `delta`);
    Forwards to parent.expand(b, delta).
  bool `reallocate`(ref void[] `b`, size_t `s`);
    Reallocates `b` to new size `s`.
  Ternary `owns`(void[] `b`);
    Forwards to parent.`owns`(`b`).
  bool `deallocate`(void[] `b`);
    Deallocates `b`.
  bool `deallocateAll`();
    Deallocates all memory allocated.
  const pure nothrow @nogc @safe Ternary `empty`();
    Returns Ternary.yes if this allocator is not responsible for any memory, Ternary.no otherwise. (Never returns Ternary.unknown.)

Copyright © 1999-2021 by the D Language Foundation | Page generated by Ddoc on Sun Oct 10 11:26:09 2021