onnxruntime/onnxruntime/test/util/test_allocator.cc
Ryan Hill 98a92547bf
Ryanunderhill/c api 8 (#297)
* Make OrtAllocator not be reference counted

* Make the allocator interface more type safe

* Fix build break

* Build break fix

* Build break fix

* Mistake in previous build fix.

* Fix review comments + build break

* Missed the export symbols

* C specific error, need 'struct' keyword in one case.

* Function calling OrtReleaseObject instead of OrtReleaseEnv
2019-01-10 02:06:29 -08:00

41 lines
1.3 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "test_allocator.h"
MockedOrtAllocator::MockedOrtAllocator() {
OrtAllocator::Alloc = [](OrtAllocator* this_, size_t size) { return static_cast<MockedOrtAllocator*>(this_)->Alloc(size); };
OrtAllocator::Free = [](OrtAllocator* this_, void* p) { static_cast<MockedOrtAllocator*>(this_)->Free(p); };
OrtAllocator::Info = [](const OrtAllocator* this_) { return static_cast<const MockedOrtAllocator*>(this_)->Info(); };
ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault, &cpuAllocatorInfo));
}
MockedOrtAllocator::~MockedOrtAllocator() {
OrtReleaseAllocatorInfo(cpuAllocatorInfo);
}
void* MockedOrtAllocator::Alloc(size_t size) {
constexpr size_t extra_len = sizeof(size_t);
memory_inuse.fetch_add(size += extra_len);
void* p = ::malloc(size);
*(size_t*)p = size;
return (char*)p + extra_len;
}
void MockedOrtAllocator::Free(void* p) {
constexpr size_t extra_len = sizeof(size_t);
if (!p) return;
p = (char*)p - extra_len;
size_t len = *(size_t*)p;
memory_inuse.fetch_sub(len);
return ::free(p);
}
const OrtAllocatorInfo* MockedOrtAllocator::Info() const {
return cpuAllocatorInfo;
}
void MockedOrtAllocator::LeakCheck() {
if (memory_inuse.load())
throw std::runtime_error("memory leak!!!");
}