From e0b08360d75ad0a188116358786fcb527786498f Mon Sep 17 00:00:00 2001 From: Yasser Ganjisaffar Date: Mon, 27 Jan 2014 10:36:22 -0800 Subject: [PATCH] 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 --- folly/io/IOBuf.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/folly/io/IOBuf.cpp b/folly/io/IOBuf.cpp index 950e6293..b3cd4125 100644 --- a/folly/io/IOBuf.cpp +++ b/folly/io/IOBuf.cpp @@ -1,5 +1,5 @@ /* - * 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(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) -- 2.34.1