mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-11 10:35:22 -04:00
added a memory management class for small objects.
git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4046 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
db19db7e60
commit
827dbd8cf3
8 changed files with 538 additions and 4 deletions
|
@ -7,8 +7,8 @@ RS_TOP_DIR = ../..
|
|||
include $(RS_TOP_DIR)/tests/scripts/config.mk
|
||||
###############################################################
|
||||
|
||||
TESTOBJ = netsetup_test.o random_test.o
|
||||
TESTS = netsetup_test random_test
|
||||
TESTOBJ = netsetup_test.o random_test.o memory_management_test.o
|
||||
TESTS = netsetup_test random_test memory_management_test
|
||||
|
||||
all: tests
|
||||
|
||||
|
@ -18,6 +18,9 @@ netsetup_test: netsetup_test.o
|
|||
random_test: random_test.o
|
||||
$(CC) $(CFLAGS) -o random_test random_test.o $(LIBS)
|
||||
|
||||
memory_management_test: memory_management_test.o
|
||||
$(CC) $(CFLAGS) -o memory_management_test memory_management_test.o $(LIBS)
|
||||
|
||||
###############################################################
|
||||
include $(RS_TOP_DIR)/tests/scripts/rules.mk
|
||||
###############################################################
|
||||
|
|
61
libretroshare/src/tests/general/memory_management_test.cc
Normal file
61
libretroshare/src/tests/general/memory_management_test.cc
Normal file
|
@ -0,0 +1,61 @@
|
|||
#ifdef LINUX
|
||||
#include <fenv.h>
|
||||
#endif
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "util/utest.h"
|
||||
#include "serialiser/rsserial.h"
|
||||
|
||||
INITTEST();
|
||||
|
||||
// Make a fake class of size n.
|
||||
//
|
||||
template<int n> class RsTestItem: public RsItem
|
||||
{
|
||||
public:
|
||||
unsigned char buff[n] ;
|
||||
|
||||
RsTestItem(uint32_t s) : RsItem(s) {}
|
||||
virtual void clear() {}
|
||||
virtual std::ostream& print(std::ostream& o, uint16_t) { return o ; }
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef LINUX
|
||||
feenableexcept(FE_INVALID) ;
|
||||
feenableexcept(FE_DIVBYZERO) ;
|
||||
#endif
|
||||
typedef RsTestItem<17> Test17 ;
|
||||
typedef RsTestItem<31> Test31 ;
|
||||
|
||||
std::vector<RsItem*> v;
|
||||
|
||||
for(int i=0;i<300;++i)
|
||||
v.push_back(new Test17(0)) ;
|
||||
for(int i=0;i<700;++i)
|
||||
v.push_back(new Test31(0)) ;
|
||||
|
||||
RsMemoryManagement::printStatistics() ;
|
||||
|
||||
// Now delete objects randomly.
|
||||
//
|
||||
for(int i=0;i<1000;++i)
|
||||
{
|
||||
int indx = lrand48()%(int)v.size() ;
|
||||
|
||||
delete v[indx] ;
|
||||
v[indx] = v.back() ;
|
||||
v.pop_back() ;
|
||||
}
|
||||
|
||||
std::cerr << "After memory free: " << std::endl;
|
||||
|
||||
RsMemoryManagement::printStatistics() ;
|
||||
|
||||
FINALREPORT("memory_management_test");
|
||||
exit(TESTRESULT());
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue