[#absl-CordBuffer-CreateWithCustomLimit]
= xref:absl.adoc[absl]::xref:absl/CordBuffer.adoc[CordBuffer]::CreateWithCustomLimit
:relfileprefix: ../../
:mrdocs:

Creates a CordBuffer instance of the desired `capacity` rounded to an appropriate power of 2 size less than, or equal to `block&lowbar;size`&period; Requires `block&lowbar;size` to be a power of 2&period;

== Synopsis

Declared in `&lt;absl&sol;strings&sol;cord&lowbar;buffer&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
static
xref:absl/CordBuffer.adoc[CordBuffer]
CreateWithCustomLimit(
    size&lowbar;t block&lowbar;size,
    size&lowbar;t capacity);
----

== Description

If `capacity` is less than or equal to `kDefaultLimit`, then this method behaves identical to `CreateWithDefaultLimit`, which means that the caller is guaranteed to get a buffer of at least the requested capacity&period;

If `capacity` is greater than or equal to `block&lowbar;size`, then this method returns a buffer with an `allocated size` of `block&lowbar;size` bytes&period; Otherwise, this methods returns a buffer with a suitable smaller power of 2 block size to satisfy the request&period; The actual size depends on a number of factors, and is typically (but not necessarily) the highest or second highest power of 2 value less than or equal to `capacity`&period;

The &apos;allocated size&apos; includes a small amount of overhead required for internal state, which is currently 13 bytes on 64&hyphen;bit platforms&period; For example&colon; a buffer created with `block&lowbar;size` and &grave;capacity&apos; set to 8KiB will have an allocated size of 8KiB, and an effective internal `capacity` of 8KiB &hyphen; 13 &equals; 8179 bytes&period;

To demonstrate this in practice, let&apos;s assume we want to read data from somewhat larger files using approximately 64KiB buffers&colon;

absl&colon;&colon;Cord ReadFromFile(int fd, size&lowbar;t n) &lcub; absl&colon;&colon;Cord cord&semi; while (n &gt; 0) &lcub; CordBuffer buffer &equals; CordBuffer&colon;&colon;CreateWithCustomLimit(64 &lt;&lt; 10, n)&semi; absl&colon;&colon;Span&lt;char&gt; data &equals; buffer&period;available&lowbar;up&lowbar;to(n)&semi; ReadFileDataOrDie(fd, data&period;data(), data&period;size())&semi; buffer&period;IncreaseLengthBy(data&period;size())&semi; cord&period;Append(std&colon;&colon;move(buffer))&semi; n &hyphen;&equals; data&period;size()&semi; &rcub; return cord&semi; &rcub;

If we&apos;d use this function to read a file of 659KiB, we may get the following pattern of allocated cord buffer sizes&colon;

CreateWithCustomLimit(64KiB, 674816) &hyphen;&hyphen;&gt; &#126;64KiB (65523) CreateWithCustomLimit(64KiB, 674816) &hyphen;&hyphen;&gt; &#126;64KiB (65523) &period;&period;&period; CreateWithCustomLimit(64KiB, 19586) &hyphen;&hyphen;&gt; &#126;16KiB (16371) CreateWithCustomLimit(64KiB, 3215) &hyphen;&hyphen;&gt; 3215 (at least 3215)

The reason the method returns a 16K buffer instead of a roughly 19K buffer is to reduce memory overhead and fragmentation risks&period; Using carefully chosen power of 2 values reduces the entropy of allocated memory sizes&period;

Additionally, let&apos;s assume we&apos;d use the above function on files that are generally smaller than 64K&period; If we&apos;d use &apos;precise&apos; sized buffers for such files, than we&apos;d get a very wide distribution of allocated memory sizes rounded to 4K page sizes, and we&apos;d end up with a lot of unused capacity&period;

In general, application should only use custom sizes if the data they are consuming or storing is expected to be many times the chosen block size, and be based on objective data and performance metrics&period; For example, a compress function may work faster and consume less CPU when using larger buffers&period; Such an application should pick a size offering a reasonable trade&hyphen;off between expected data size, compute savings with larger buffers, and the cost or fragmentation effect of larger buffers&period; Applications must pick a reasonable spot on that curve, and make sure their data meets their expectations in size distributions such as &quot;mostly large&quot;&period;

== Return Value

A new CordBuffer sized according to `block&lowbar;size` and `capacity`&period;

== Parameters

[cols="1,4"]
|===
| Name| Description
| *block&lowbar;size*
| The power&hyphen;of&hyphen;2 upper bound on the allocated size&period;
| *capacity*
| The desired capacity of the buffer&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
