diff --git a/include/tvm/runtime/crt/logging.h b/include/tvm/runtime/crt/logging.h index ac5b658c628e4..1ff90fb510fc9 100644 --- a/include/tvm/runtime/crt/logging.h +++ b/include/tvm/runtime/crt/logging.h @@ -26,6 +26,7 @@ #ifndef TVM_RUNTIME_CRT_LOGGING_H_ #define TVM_RUNTIME_CRT_LOGGING_H_ +#ifndef CHECK #define CHECK(x) \ do { \ if (!(x)) { \ @@ -33,7 +34,9 @@ exit(-1); \ } \ }while(0) +#endif +#ifndef CHECK_BINARY_OP #define CHECK_BINARY_OP(op, x, y, fmt, ...) \ do { \ if (!(x op y)) { \ @@ -41,12 +44,30 @@ exit(-1); \ } \ }while(0) +#endif +#ifndef CHECK_LT #define CHECK_LT(x, y, fmt, ...) CHECK_BINARY_OP(<, x, y, fmt, ##__VA_ARGS__) +#endif + +#ifndef CHECK_GT #define CHECK_GT(x, y, fmt, ...) CHECK_BINARY_OP(>, x, y, fmt, ##__VA_ARGS__) +#endif + +#ifndef CHECK_LE #define CHECK_LE(x, y, fmt, ...) CHECK_BINARY_OP(<=, x, y, fmt, ##__VA_ARGS__) +#endif + +#ifndef CHECK_GE #define CHECK_GE(x, y, fmt, ...) CHECK_BINARY_OP(>=, x, y, fmt, ##__VA_ARGS__) +#endif + +#ifndef CHECK_EQ #define CHECK_EQ(x, y, fmt, ...) CHECK_BINARY_OP(==, x, y, fmt, ##__VA_ARGS__) +#endif + +#ifndef CHECK_NE #define CHECK_NE(x, y, fmt, ...) CHECK_BINARY_OP(!=, x, y, fmt, ##__VA_ARGS__) +#endif #endif // TVM_RUNTIME_CRT_LOGGING_H_ diff --git a/src/runtime/crt/memory.c b/src/runtime/crt/memory.c index b6346ecdc1cc4..d73a662491076 100644 --- a/src/runtime/crt/memory.c +++ b/src/runtime/crt/memory.c @@ -209,7 +209,7 @@ typedef struct MemoryManager { * \return The virtual address */ void* MemoryManager_Alloc(MemoryManager * mgr, tvm_index_t size) { - void * data = 0; + char * data = 0; tvm_index_t npage = (size + kPageSize - 1) / kPageSize; MultiMap * free_map = &(mgr->free_map); IndexedEntry * it = free_map->lower_bound(free_map, npage); @@ -248,7 +248,7 @@ void* MemoryManager_Alloc(MemoryManager * mgr, tvm_index_t size) { * \return The virtual address */ void* MemoryManager_Realloc(MemoryManager * mgr, void * ptr, tvm_index_t size) { - void * data = ptr; + char * data = (char*)ptr; // NOLINT(*) PageTable * ptable = &(mgr->ptable); TLB * pmap = &(mgr->pmap); MultiMap * free_map = &(mgr->free_map); @@ -257,7 +257,7 @@ void* MemoryManager_Realloc(MemoryManager * mgr, void * ptr, tvm_index_t size) { if (ptr) { // get page size for given pointer CHECK_NE(pmap->count, 0, "invalid translation look-aside buffer."); - PageEntry * entry = pmap->find(pmap, ptr); + PageEntry * entry = pmap->find(pmap, (char*)ptr); // NOLINT(*) CHECK_NE(entry, 0, "no valid page entry found."); Page * pptr = &(entry->page); // if the page size is smaller than target page size, @@ -327,7 +327,7 @@ void* MemoryManager_Realloc(MemoryManager * mgr, void * ptr, tvm_index_t size) { void MemoryManager_Free(MemoryManager * mgr, void* ptr) { TLB * pmap = &(mgr->pmap); CHECK_NE(pmap->count, 0, "invalid translation look-aside buffer."); - PageEntry * entry = pmap->find(pmap, ptr); + PageEntry * entry = pmap->find(pmap, (char*)ptr); // NOLINT(*) CHECK_NE(entry, 0, "no valid page entry found."); Page * p = &(entry->page); MultiMap * free_map = &(mgr->free_map); diff --git a/tests/cpp/crt_memory_test.cc b/tests/cpp/crt_memory_test.cc new file mode 100644 index 0000000000000..668208a1b79e5 --- /dev/null +++ b/tests/cpp/crt_memory_test.cc @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#define TVM_CRT_LOG_VIRT_MEM_SIZE 16 +#define TVM_CRT_PAGE_BYTES 4096 + +#include +#include +#include + +#include "../../src/runtime/crt/memory.c" + +TEST(CRTMemory, Alloc) { + for (int idx = 0; idx < 65536; idx++) { + void * a = vmalloc(1); + EXPECT_EQ(vleak_size, 1); + vfree(a); + EXPECT_EQ(vleak_size, 0); + } +} + +TEST(CRTMemory, Realloc) { + for (int idx = 0; idx < 65536; idx++) { + void * a = vrealloc(0, 1); + EXPECT_EQ(vleak_size, 1); + void * b = vrealloc(a, 1); + EXPECT_EQ(a, b); + EXPECT_EQ(vleak_size, 1); + vfree(a); + EXPECT_EQ(vleak_size, 0); + } +} + +int main(int argc, char ** argv) { + testing::InitGoogleTest(&argc, argv); + testing::FLAGS_gtest_death_test_style = "threadsafe"; + return RUN_ALL_TESTS(); +}