mirror of https://github.com/lammps/lammps.git
refactor some more to reduce redundant code.
This commit is contained in:
parent
bbb81a8dd0
commit
ab5c81f7d6
|
@ -89,12 +89,9 @@ MyPage<T>::MyPage() : ndatum(0), nchunk(0), pages(nullptr), page(nullptr),
|
|||
npage(0), ipage(-1), index(-1), maxchunk(-1),
|
||||
pagesize(-1), pagedelta(1), errorflag(0) {};
|
||||
|
||||
/** Free all allocated pages of this class instance */
|
||||
|
||||
template <class T>
|
||||
MyPage<T>::~MyPage() {
|
||||
for (int i = 0; i < npage; i++) free(pages[i]);
|
||||
free(pages);
|
||||
deallocate();
|
||||
}
|
||||
|
||||
/** (Re-)initialize the set of pages and allocation parameters.
|
||||
|
@ -117,44 +114,18 @@ int MyPage<T>::init(int user_maxchunk, int user_pagesize,
|
|||
if (maxchunk <= 0 || pagesize <= 0 || pagedelta <= 0) return 1;
|
||||
if (maxchunk > pagesize) return 1;
|
||||
|
||||
// free any previously allocated pages
|
||||
// free storage if re-initialized
|
||||
|
||||
for (int i = 0; i < npage; i++) free(pages[i]);
|
||||
free(pages);
|
||||
deallocate();
|
||||
|
||||
// initial page allocation
|
||||
|
||||
ndatum = nchunk = 0;
|
||||
pages = NULL;
|
||||
npage = 0;
|
||||
allocate();
|
||||
if (errorflag) return 2;
|
||||
ipage = index = 0;
|
||||
page = pages[ipage];
|
||||
reset();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Pointer to location that can store one item.
|
||||
*
|
||||
* This will allocate more pages as needed.
|
||||
*
|
||||
* \return memory location or null pointer, if memory allocation failed */
|
||||
|
||||
template <class T>
|
||||
T *MyPage<T>::get() {
|
||||
ndatum++;
|
||||
nchunk++;
|
||||
if (index < pagesize) return &page[index++];
|
||||
ipage++;
|
||||
if (ipage == npage) {
|
||||
allocate();
|
||||
if (errorflag) return NULL;
|
||||
}
|
||||
page = pages[ipage];
|
||||
index = 0;
|
||||
return &page[index++];
|
||||
}
|
||||
|
||||
/** Pointer to location that can store N items.
|
||||
*
|
||||
* This will allocate more pages as needed.
|
||||
|
@ -172,11 +143,15 @@ T *MyPage<T>::get(int n) {
|
|||
}
|
||||
ndatum += n;
|
||||
nchunk++;
|
||||
|
||||
// return pointer from current page
|
||||
if (index+n <= pagesize) {
|
||||
int start = index;
|
||||
index += n;
|
||||
return &page[start];
|
||||
}
|
||||
|
||||
// allocate new page
|
||||
ipage++;
|
||||
if (ipage == npage) {
|
||||
allocate();
|
||||
|
@ -231,7 +206,7 @@ template <class T>
|
|||
void MyPage<T>::reset() {
|
||||
ndatum = nchunk = 0;
|
||||
index = ipage = 0;
|
||||
page = pages[ipage];
|
||||
page = (pages != nullptr) ? pages[ipage] : nullptr;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
@ -258,6 +233,17 @@ void MyPage<T>::allocate() {
|
|||
}
|
||||
}
|
||||
|
||||
/** Free all allocated pages of this class instance */
|
||||
|
||||
template <class T>
|
||||
void MyPage<T>::deallocate() {
|
||||
reset();
|
||||
for (int i = 0; i < npage; i++) free(pages[i]);
|
||||
free(pages);
|
||||
pages = nullptr;
|
||||
npage = 0;
|
||||
}
|
||||
|
||||
// explicit instantiations
|
||||
|
||||
namespace LAMMPS_NS {
|
||||
|
|
|
@ -39,12 +39,10 @@ class MyPage {
|
|||
MyPage();
|
||||
virtual ~MyPage();
|
||||
|
||||
int init(int user_maxchunk = 1, int user_pagesize = 1024,
|
||||
int user_pagedelta = 1);
|
||||
|
||||
T *get();
|
||||
T *get(int n);
|
||||
int init(int user_maxchunk=1, int user_pagesize=1024,
|
||||
int user_pagedelta=1);
|
||||
|
||||
T *get(int n=1);
|
||||
T *vget();
|
||||
void vgot(int n);
|
||||
|
||||
|
@ -79,6 +77,7 @@ class MyPage {
|
|||
// 1 = chunk size exceeded maxchunk
|
||||
// 2 = memory allocation error
|
||||
void allocate();
|
||||
void deallocate();
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
TEST(MyPage, int_default) {
|
||||
TEST(MyPage, int) {
|
||||
MyPage<int> p;
|
||||
|
||||
// default init. maxchunk=1, pagesize=1024
|
||||
|
@ -53,12 +53,8 @@ TEST(MyPage, int_default) {
|
|||
ASSERT_EQ(iptr,p.get(1));
|
||||
ASSERT_EQ(p.ndatum,3);
|
||||
ASSERT_EQ(p.nchunk,3);
|
||||
}
|
||||
|
||||
TEST(MyPage, int_custom) {
|
||||
MyPage<int> p;
|
||||
|
||||
// default init. maxchunk=16, pagesize=256
|
||||
// restart with custom init. maxchunk=16, pagesize=256
|
||||
int rv = p.init(16,256);
|
||||
ASSERT_EQ(rv,0);
|
||||
|
||||
|
|
Loading…
Reference in New Issue