本文整理汇总了C++中mozilla::RoundUpPow2方法的典型用法代码示例。如果您正苦于以下问题:C++ mozilla::RoundUpPow2方法的具体用法?C++ mozilla::RoundUpPow2怎么用?C++ mozilla::RoundUpPow2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mozilla
的用法示例。
在下文中一共展示了mozilla::RoundUpPow2方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sizeof
static MOZ_ALWAYS_INLINE bool
AllocChars(ThreadSafeContext *maybecx, size_t length, CharT **chars, size_t *capacity)
{
/*
* String length doesn't include the null char, so include it here before
* doubling. Adding the null char after doubling would interact poorly with
* round-up malloc schemes.
*/
size_t numChars = length + 1;
/*
* Grow by 12.5% if the buffer is very large. Otherwise, round up to the
* next power of 2. This is similar to what we do with arrays; see
* JSObject::ensureDenseArrayElements.
*/
static const size_t DOUBLING_MAX = 1024 * 1024;
numChars = numChars > DOUBLING_MAX ? numChars + (numChars / 8) : RoundUpPow2(numChars);
/* Like length, capacity does not include the null char, so take it out. */
*capacity = numChars - 1;
JS_STATIC_ASSERT(JSString::MAX_LENGTH * sizeof(CharT) < UINT32_MAX);
size_t bytes = numChars * sizeof(CharT);
*chars = (CharT *)(maybecx ? maybecx->malloc_(bytes) : js_malloc(bytes));
return *chars != nullptr;
}