Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a5b7b8a0 authored by Santiago Aboy Solanes's avatar Santiago Aboy Solanes Committed by Android (Google) Code Review
Browse files

Revert "SharedBuffer: Use AllocatorTracker to allocate the buffer"

This reverts commit b0a4f325.

Reason for revert: Breaks haiku-trunk_staging-userdebug https://android-build.corp.google.com/build_explorer/build_details/13849997/haiku-trunk_staging-userdebug/
Flag: EXEMPT fixing build brekage
Test: Reverted locally and tested the build

Bug: 414621670
Change-Id: I4ad63761c437da1d4282b36d15df5cbe65ce7795
parent b0a4f325
Loading
Loading
Loading
Loading
+11 −15
Original line number Diff line number Diff line
@@ -20,13 +20,9 @@

#include <stdlib.h>
#include <string.h>
#include <cstddef>
#include <new>

#include <log/log.h>

#include <utils/AllocatorTracker.h>

// ---------------------------------------------------------------------------

namespace android {
@@ -37,9 +33,8 @@ SharedBuffer* SharedBuffer::alloc(size_t size)
    // size_max.
    LOG_ALWAYS_FATAL_IF((size >= (SIZE_MAX - sizeof(SharedBuffer))),
                        "Invalid buffer size %zu", size);
    SharedBuffer* sb =
            static_cast<SharedBuffer*>(ANDROID_NEW_BUFFER_NOTHROW(sizeof(SharedBuffer) + size));

    SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
    if (sb) {
        // Should be std::atomic_init(&sb->mRefs, 1);
        // But that generates a warning with some compilers.
@@ -51,10 +46,10 @@ SharedBuffer* SharedBuffer::alloc(size_t size)
    return sb;
}


void SharedBuffer::dealloc(const SharedBuffer* released)
{
  SharedBuffer* buffer = const_cast<SharedBuffer*>(released);
  ANDROID_DELETE_BUFFER(buffer);
    free(const_cast<SharedBuffer*>(released));
}

SharedBuffer* SharedBuffer::edit() const
@@ -141,4 +136,5 @@ int32_t SharedBuffer::release(uint32_t flags) const
    return prevRefCount;
}


}; // namespace android
+0 −42
Original line number Diff line number Diff line
@@ -14,36 +14,17 @@
 * limitations under the License.
 */

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <memory>
#include <stdint.h>

#include <utils/AllocatorTracker.h>

#include "SharedBuffer.h"

extern "C" void __hwasan_init() __attribute__((weak));
#define SKIP_WITH_HWASAN \
    if (&__hwasan_init != 0) GTEST_SKIP()

#ifdef ANDROID_UTILS_CUSTOM_ALLOCATOR

using ::testing::_;

// A simple allocator that can be used to check that the allocator is being
// called when expected.
// A simple allocator that always returns nullptr.
class MockAllocator : public android::Allocator {
  public:
    MOCK_METHOD(void*, allocate, (size_t size, size_t alignment), (override));
    MOCK_METHOD(void, deallocate, (void* ptr), (override));
    MOCK_METHOD(void, abort, (), (override));
};

#endif  // ANDROID_UTILS_CUSTOM_ALLOCATOR

TEST(SharedBufferTest, alloc_death) {
    EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX), "");
    EXPECT_DEATH(android::SharedBuffer::alloc(SIZE_MAX - sizeof(android::SharedBuffer)), "");
@@ -103,26 +84,3 @@ TEST(SharedBufferTest, editResize_zero_size) {
    ASSERT_EQ(0U, buf->size());
    buf->release();
}

#ifdef ANDROID_UTILS_CUSTOM_ALLOCATOR
TEST(SharedBufferTest, custom_allocator) {
    MockAllocator allocator;
    android::AllocatorTracker::getInstance().setAllocator(&allocator);

    // Expect allocate to be called and return memory allocated by malloc.
    EXPECT_CALL(allocator, allocate(_, _)).WillOnce([](size_t size, size_t) {
        return malloc(size);
    });

    android::SharedBuffer* buf = android::SharedBuffer::alloc(10);
    ASSERT_NE(buf, nullptr);

    // Expect deallocate to be called and free the memory.
    EXPECT_CALL(allocator, deallocate(_)).WillOnce([](void* ptr) { free(ptr); });

    // This should trigger the deallocate call.
    buf->release();

    android::AllocatorTracker::getInstance().setAllocator(nullptr);
}
#endif  // ANDROID_UTILS_CUSTOM_ALLOCATOR