diff --git a/src/my_page.cpp b/src/my_page.cpp index d297d16f48..ce5dbca86a 100644 --- a/src/my_page.cpp +++ b/src/my_page.cpp @@ -89,12 +89,9 @@ MyPage::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 MyPage::~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::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 -T *MyPage::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::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 void MyPage::reset() { ndatum = nchunk = 0; index = ipage = 0; - page = pages[ipage]; + page = (pages != nullptr) ? pages[ipage] : nullptr; } /* ---------------------------------------------------------------------- */ @@ -258,6 +233,17 @@ void MyPage::allocate() { } } +/** Free all allocated pages of this class instance */ + +template +void MyPage::deallocate() { + reset(); + for (int i = 0; i < npage; i++) free(pages[i]); + free(pages); + pages = nullptr; + npage = 0; +} + // explicit instantiations namespace LAMMPS_NS { diff --git a/src/my_page.h b/src/my_page.h index 1f40eb48cf..9c3bcd1b61 100644 --- a/src/my_page.h +++ b/src/my_page.h @@ -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(); }; } diff --git a/unittest/utils/test_my_page.cpp b/unittest/utils/test_my_page.cpp index 2f56bea725..f6dbd3e3b4 100644 --- a/unittest/utils/test_my_page.cpp +++ b/unittest/utils/test_my_page.cpp @@ -18,7 +18,7 @@ using namespace LAMMPS_NS; -TEST(MyPage, int_default) { +TEST(MyPage, int) { MyPage 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 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);