Fix the problem that hashtable free memory cannot be recovered immediately
This commit is contained in:
120
deps/oblib/unittest/lib/hash/test_simpleallocer.cpp
vendored
120
deps/oblib/unittest/lib/hash/test_simpleallocer.cpp
vendored
@ -26,107 +26,41 @@ using namespace oceanbase;
|
||||
using namespace common;
|
||||
using namespace hash;
|
||||
|
||||
const int64_t ITEM_NUM = 1024 * 128;
|
||||
|
||||
struct data_t
|
||||
std::set<void *> ptr_set;
|
||||
class MyAllocator : public ObIAllocator
|
||||
{
|
||||
uint32_t construct_flag;
|
||||
char data[1024];
|
||||
data_t()
|
||||
public:
|
||||
void *alloc(const int64_t size)
|
||||
{
|
||||
construct_flag = 0xffffffff;
|
||||
};
|
||||
};
|
||||
|
||||
typedef SimpleAllocer<data_t, 128> allocer_t;
|
||||
|
||||
template <class T>
|
||||
void shuffle(T *array, const int64_t array_size)
|
||||
{
|
||||
srand(static_cast<int32_t>(time(NULL)));
|
||||
for (int64_t i = 0; i < array_size; i++)
|
||||
{
|
||||
int64_t swap_pos = rand() % array_size;
|
||||
std::swap(array[i], array[swap_pos]);
|
||||
void *ptr = ob_malloc(size);
|
||||
ptr_set.insert(ptr);
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
void *alloc(const int64_t , const ObMemAttr &)
|
||||
{ return NULL; }
|
||||
void free(void *ptr)
|
||||
{
|
||||
ptr_set.erase(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
TEST(TestSimpleAllocer, allocate)
|
||||
{
|
||||
allocer_t al;
|
||||
data_t *nil = NULL;
|
||||
|
||||
data_t *store[ITEM_NUM];
|
||||
for (int64_t i = 0; i < ITEM_NUM; i++)
|
||||
{
|
||||
EXPECT_NE(nil, (store[i] = al.alloc()));
|
||||
EXPECT_EQ(0xffffffff, store[i]->construct_flag);
|
||||
SimpleAllocer<int, 10, SpinMutexDefendMode, MyAllocator> alloc;
|
||||
const int N = 100;
|
||||
int *ptrs[N];
|
||||
int i = N;
|
||||
while (i--) {
|
||||
int *ptr = alloc.alloc();
|
||||
EXPECT_TRUE(ptr != NULL);
|
||||
ptrs[i] = ptr;
|
||||
}
|
||||
for (int64_t i = 0; i < ITEM_NUM; i++)
|
||||
{
|
||||
store[i]->construct_flag = 0x00000000;
|
||||
al.free(store[i]);
|
||||
i= N;
|
||||
EXPECT_TRUE(!ptr_set.empty());
|
||||
while (i--) {
|
||||
alloc.free(ptrs[i]);
|
||||
}
|
||||
for (int64_t i = ITEM_NUM - 1; i >= 0; i--)
|
||||
{
|
||||
data_t *tmp = NULL;
|
||||
EXPECT_NE(nil, (tmp = al.alloc()));
|
||||
EXPECT_EQ(0xffffffff, tmp->construct_flag);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TestSimpleAllocer, free)
|
||||
{
|
||||
allocer_t al;
|
||||
|
||||
data_t *store[ITEM_NUM];
|
||||
for (int64_t i = 0; i < ITEM_NUM; i++)
|
||||
{
|
||||
store[i] = al.alloc();
|
||||
}
|
||||
al.free(NULL);
|
||||
shuffle(store, ITEM_NUM);
|
||||
for (int64_t i = 0; i < ITEM_NUM; i++)
|
||||
{
|
||||
al.free(store[i]);
|
||||
}
|
||||
for (int64_t i = ITEM_NUM - 1; i >= 0; i--)
|
||||
{
|
||||
data_t *tmp = NULL;
|
||||
EXPECT_EQ(store[i], (tmp = al.alloc()));
|
||||
}
|
||||
for (int64_t i = 0; i < ITEM_NUM; i++)
|
||||
{
|
||||
al.free(store[i]);
|
||||
if (0 == i % 128)
|
||||
{
|
||||
al.gc();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(TestSimpleAllocer, DISABLED_overflow)
|
||||
{
|
||||
allocer_t al;
|
||||
data_t *nil = NULL;
|
||||
data_t *data[2];
|
||||
data_t tmp;
|
||||
|
||||
EXPECT_NE(nil, (data[0] = al.alloc()));
|
||||
// this case is used to test overflow
|
||||
// copy more bytes than sizeof(data[0]). (actually safe)
|
||||
// but Coverity treat it as a potential bug
|
||||
memcpy(&tmp, data[0], sizeof(data_t) + sizeof(uint32_t));
|
||||
memset(data[0], -1, sizeof(data_t) + sizeof(uint32_t));
|
||||
al.free(data[0]);
|
||||
memcpy(data[0], &tmp, sizeof(data_t) + sizeof(uint32_t));
|
||||
al.free(data[0]);
|
||||
|
||||
memset(data[0], -1, sizeof(data_t) + sizeof(uint32_t));
|
||||
EXPECT_EQ(nil, (data[1] = al.alloc()));
|
||||
memcpy(data[0], &tmp, sizeof(data_t) + sizeof(uint32_t));
|
||||
EXPECT_NE(nil, (data[1] = al.alloc()));
|
||||
EXPECT_EQ(data[0], data[1]);
|
||||
EXPECT_TRUE(ptr_set.empty());
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
||||
Reference in New Issue
Block a user