Commit e0b08360 authored by Yasser Ganjisaffar's avatar Yasser Ganjisaffar Committed by Sara Golemon

bug fix when rallocm fails

Summary:
rallocm sets the value of the second argument even if it has failed to allocate the requested size:
https://github.com/jemalloc/jemalloc/blob/898960247a8b2e6534738b7a3a244855f379faf9/src/jemalloc.c#L1903-L1906

As a result newAllocatedCapacity was being set to a value less than the original value and the logic was broken.

Test Plan: unit tests pass and my code which was crashing before now works!

Reviewed By: soren@fb.com

FB internal diff: D1144314
parent 3ac402b1
/*
* Copyright 2013 Facebook, Inc.
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -714,11 +714,16 @@ void IOBuf::reserveSlow(uint32_t minHeadroom, uint32_t minTailroom) {
size_t allocatedCapacity = capacity() + sizeof(SharedInfo);
void* p = buf_;
if (allocatedCapacity >= jemallocMinInPlaceExpandable) {
int r = rallocm(&p, &newAllocatedCapacity, newAllocatedCapacity,
// rallocm can write to its 2nd arg even if it returns
// ALLOCM_ERR_NOT_MOVED. So, we pass a temporary to its 2nd arg and
// update newAllocatedCapacity only on success.
size_t allocatedSize;
int r = rallocm(&p, &allocatedSize, newAllocatedCapacity,
0, ALLOCM_NO_MOVE);
if (r == ALLOCM_SUCCESS) {
newBuffer = static_cast<uint8_t*>(p);
newHeadroom = oldHeadroom;
newAllocatedCapacity = allocatedSize;
} else if (r == ALLOCM_ERR_OOM) {
// shouldn't happen as we don't actually allocate new memory
// (due to ALLOCM_NO_MOVE)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment