Skip to content

Commit

Permalink
extend test coverage of vmalloc
Browse files Browse the repository at this point in the history
Change-Id: Ie4ff6b64fdfe6810836cf8fd44dace82a20c4581
  • Loading branch information
liangfu committed Mar 26, 2020
1 parent b269a99 commit 3bb1a95
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
21 changes: 21 additions & 0 deletions include/tvm/runtime/crt/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,48 @@
#ifndef TVM_RUNTIME_CRT_LOGGING_H_
#define TVM_RUNTIME_CRT_LOGGING_H_

#ifndef CHECK
#define CHECK(x) \
do { \
if (!(x)) { \
fprintf(stderr, "Check failed: %s\n", #x); \
exit(-1); \
} \
}while(0)
#endif

#ifndef CHECK_BINARY_OP
#define CHECK_BINARY_OP(op, x, y, fmt, ...) \
do { \
if (!(x op y)) { \
fprintf(stderr, "Check failed: %s %s %s: " fmt "\n", #x, #op, #y, ##__VA_ARGS__); \
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_
8 changes: 4 additions & 4 deletions src/runtime/crt/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
54 changes: 54 additions & 0 deletions tests/cpp/crt_memory_test.cc
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#include <tvm/runtime/crt/logging.h>
#include <tvm/runtime/crt/memory.h>

#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();
}

0 comments on commit 3bb1a95

Please sign in to comment.