refactor some more to reduce redundant code.

This commit is contained in:
Axel Kohlmeyer
2020-09-07 08:57:37 -04:00
parent bbb81a8dd0
commit ab5c81f7d6
3 changed files with 26 additions and 45 deletions

View File

@ -89,12 +89,9 @@ MyPage<T>::MyPage() : ndatum(0), nchunk(0), pages(nullptr), page(nullptr),
npage(0), ipage(-1), index(-1), maxchunk(-1), npage(0), ipage(-1), index(-1), maxchunk(-1),
pagesize(-1), pagedelta(1), errorflag(0) {}; pagesize(-1), pagedelta(1), errorflag(0) {};
/** Free all allocated pages of this class instance */
template <class T> template <class T>
MyPage<T>::~MyPage() { MyPage<T>::~MyPage() {
for (int i = 0; i < npage; i++) free(pages[i]); deallocate();
free(pages);
} }
/** (Re-)initialize the set of pages and allocation parameters. /** (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 <= 0 || pagesize <= 0 || pagedelta <= 0) return 1;
if (maxchunk > pagesize) 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]); deallocate();
free(pages);
// initial page allocation // initial page allocation
ndatum = nchunk = 0;
pages = NULL;
npage = 0;
allocate(); allocate();
if (errorflag) return 2; if (errorflag) return 2;
ipage = index = 0; reset();
page = pages[ipage];
return 0; 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. /** Pointer to location that can store N items.
* *
* This will allocate more pages as needed. * This will allocate more pages as needed.
@ -172,11 +143,15 @@ T *MyPage<T>::get(int n) {
} }
ndatum += n; ndatum += n;
nchunk++; nchunk++;
// return pointer from current page
if (index+n <= pagesize) { if (index+n <= pagesize) {
int start = index; int start = index;
index += n; index += n;
return &page[start]; return &page[start];
} }
// allocate new page
ipage++; ipage++;
if (ipage == npage) { if (ipage == npage) {
allocate(); allocate();
@ -231,7 +206,7 @@ template <class T>
void MyPage<T>::reset() { void MyPage<T>::reset() {
ndatum = nchunk = 0; ndatum = nchunk = 0;
index = ipage = 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 // explicit instantiations
namespace LAMMPS_NS { namespace LAMMPS_NS {

View File

@ -42,9 +42,7 @@ class MyPage {
int init(int user_maxchunk=1, int user_pagesize=1024, int init(int user_maxchunk=1, int user_pagesize=1024,
int user_pagedelta=1); int user_pagedelta=1);
T *get(); T *get(int n=1);
T *get(int n);
T *vget(); T *vget();
void vgot(int n); void vgot(int n);
@ -79,6 +77,7 @@ class MyPage {
// 1 = chunk size exceeded maxchunk // 1 = chunk size exceeded maxchunk
// 2 = memory allocation error // 2 = memory allocation error
void allocate(); void allocate();
void deallocate();
}; };
} }

View File

@ -18,7 +18,7 @@
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
TEST(MyPage, int_default) { TEST(MyPage, int) {
MyPage<int> p; MyPage<int> p;
// default init. maxchunk=1, pagesize=1024 // default init. maxchunk=1, pagesize=1024
@ -53,12 +53,8 @@ TEST(MyPage, int_default) {
ASSERT_EQ(iptr,p.get(1)); ASSERT_EQ(iptr,p.get(1));
ASSERT_EQ(p.ndatum,3); ASSERT_EQ(p.ndatum,3);
ASSERT_EQ(p.nchunk,3); ASSERT_EQ(p.nchunk,3);
}
TEST(MyPage, int_custom) { // restart with custom init. maxchunk=16, pagesize=256
MyPage<int> p;
// default init. maxchunk=16, pagesize=256
int rv = p.init(16,256); int rv = p.init(16,256);
ASSERT_EQ(rv,0); ASSERT_EQ(rv,0);