mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-07-22 06:09:09 -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
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