# dmd.root.rmem

Allocate memory using `malloc` or the GC depending on the configuration.

#### Authors

Walter Bright, http://www.digitalmars.com

#### License

Boost License 1.0

Source: root/rmem.d

Documentation: https://dlang.org/phobos/dmd_root_rmem.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/rmem.d

- `pureMalloc`

  #### Declaration

  `pure nothrow @nogc @trusted void* `pureMalloc`(size_t `size`);
  pure nothrow @nogc @trusted void* `pureCalloc`(size_t `nmemb`, size_t `size`);
  pure nothrow @nogc @system void* `pureRealloc`(void* `ptr`, size_t `size`);
  pure nothrow @nogc @system void `pureFree`(void* `ptr`);`

  Pure variants of C's memory allocation functions `malloc`, `calloc`, and `realloc` and deallocation function `free`.

  #### Discussion

  UNIX 98 requires that errno be set to ENOMEM upon failure. https://linux.die.net/man/3/malloc However, this is irrelevant for DMD's purposes, and best practice protocol for using errno is to treat it as an `out` parameter, and not something with state that can be relied on across function calls. So, we'll ignore it.

  #### See Also

  D's rules for purity, which allow for memory allocation under specific circumstances.

- `xarraydup`

  #### Declaration

  `pure nothrow char[] `xarraydup`(const(char)[] `s`);`

  Makes a `null`-terminated copy of the given string on newly allocated memory. The `null`-terminator won't be part of the returned string slice. It will be at position `n` where `n` is the length of the input string.

  #### Parameters

  `const(char)[] `s`` | string to copy

  #### Return Value

  A `null`-terminated copy of the input array.

  #### Examples

  1. `auto s1 = "foo"; auto s2 = s1.xarraydup; s2[0] = 'b'; assert(s1 == "foo"); assert(s2 == "boo"); assert(*(s2.ptr + s2.length) == '\0'); string sEmpty; assert(sEmpty.xarraydup is null);`
- `arraydup`

  #### Declaration

  `pure nothrow T[] `arraydup`(T)(scope const T[] `s`);`

  Makes a copy of the given array on newly allocated memory.

  #### Parameters

  `T[] `s`` | array to copy

  #### Return Value

  A copy of the input array.

  #### Examples

  1. `auto s1 = [0, 1, 2]; auto s2 = s1.arraydup; s2[0] = 4; assert(s1 == [0, 1, 2]); assert(s2 == [4, 1, 2]); string sEmpty; assert(sEmpty.arraydup is null);`
- `Pool`

  #### Declaration

  `struct `Pool`(T) if (is(T == class));`

  Defines a pool for class objects. Objects can be fetched from the pool with make() and returned to the pool with dispose(). Using a reference that has been dispose()d has undefined behavior. make() may return memory that has been previously dispose()d.

  #### Discussion

  Currently the pool has effect only if the GC is NOT used (i.e. either `version(GC)` or `mem.isGCEnabled` is `false`). Otherwise `make` just forwards to `new` and `dispose` does nothing.
  Internally the implementation uses a singly-linked freelist with a global root. The "next" pointer is stored in the first word of each disposed object.

  - `make`

    #### Declaration

    `T `make`(string f = __FILE__, uint l = __LINE__, A...)(auto ref A `args`);`

    Returns a reference to a new object in the same state as if created with new T(`args`).

  - `dispose`

    #### Declaration

    `void `dispose`(string f = __FILE__, uint l = __LINE__, A...)(T `goner`);`

    Signals to the pool that this object is no longer used, so it can recycle its memory.