/usr/include/liggghts/my_page.h is in libliggghts-dev 3.0.3+repack-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | /* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------
MyPage = templated class for storing chunks of datums in pages
chunks are not returnable, can only reset and start over
replaces many small mallocs with a few large mallocs
pages are never freed, so can reuse w/out reallocs
usage:
request one datum at a time, repeat, clear
request chunks of datums in each get() or vget(), repeat, clear
chunk size can vary from request to request
chunk size can be known in advance or registered after usage via vgot()
inputs:
template T = one datum, e.g. int, double, struct, int[3]
for int[3], access datum as ivec[i][2]
methods:
T *get() = return ptr to one datum
T *get(N) = return ptr to N datums, N < maxchunk required
T *vget() = return ptr to maxchunk datums, use as needed, then call vgot()
all gets return NULL if error encountered
vgot(N) = used N datums of previous vget(), N < maxchunk required
void init(maxchunk, pagesize, pagedelta)
define allocation params and allocate first page(s)
call right after constructor
can call again to reset allocation params and free previous pages
maxchunk = max # of datums in one chunk, default = 1
pagesize = # of datums in one page, default = 1024
should be big enough to store multiple chunks
pagedelta = # of pages to allocate at a time, default = 1
return 1 if bad params
void reset() = clear pages w/out freeing
int size() = return total size of allocated pages in bytes
int status() = return error status
0 = ok, 1 = chunksize > maxchunk, 2 = allocation error
------------------------------------------------------------------------- */
#ifndef LAMMPS_MY_PAGE_H
#define LAMMPS_MY_PAGE_H
#include "stdlib.h"
namespace LAMMPS_NS {
template<class T>
class MyPage {
int ndatum; // total # of stored datums
int nchunk; // total # of stored chunks
public:
MyPage() {
ndatum = nchunk = 0;
pages = NULL;
npage = 0;
errorflag = 0;
zeroize = false;
}
// (re)initialize allocation params
// also allocate first page(s)
int init(int user_maxchunk = 1, int user_pagesize = 1024,
int user_pagedelta = 1) {
maxchunk = user_maxchunk;
pagesize = user_pagesize;
pagedelta = user_pagedelta;
if (maxchunk <= 0 || pagesize <= 0 || pagedelta <= 0) return 1;
if (maxchunk > pagesize) return 1;
// free any previously allocated pages
for (int i = 0; i < npage; i++) free(pages[i]);
free(pages);
// initial page allocation
ndatum = nchunk = 0;
pages = NULL;
npage = 0;
allocate();
if (errorflag) return 2;
ipage = index = 0;
page = pages[ipage];
return 0;
}
// free all allocated pages
~MyPage() {
for (int i = 0; i < npage; i++) free(pages[i]);
free(pages);
}
// get ptr to one datum
// return NULL if run out of memory
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++];
}
// get ptr to location that can store N datums
// error if N > maxchunk
// return NULL if run out of memory
T *get(int n) {
if (n > maxchunk) {
errorflag = 1;
return NULL;
}
ndatum += n;
nchunk++;
if (index+n <= pagesize) {
int start = index;
index += n;
return &page[start];
}
ipage++;
if (ipage == npage) {
allocate();
if (errorflag) return NULL;
}
page = pages[ipage];
index = n;
return &page[0];
}
// get ptr to location that can store maxchunk datums
// will return same ptr as previous call if vgot() not called
// return NULL if run out of memory
T *vget() {
if (index+maxchunk <= pagesize) return &page[index];
ipage++;
if (ipage == npage) {
allocate();
if (errorflag) return NULL;
}
page = pages[ipage];
index = 0;
return &page[index];
}
// increment by N = # of values stored in loc returned by vget()
// OK to not call if vget() ptr was not used
// error if N > maxchunk
void vgot(int n) {
if (n > maxchunk) errorflag = 1;
ndatum += n;
nchunk++;
index += n;
}
// clear all pages, without freeing any memory
void reset() {
ndatum = nchunk = 0;
index = ipage = 0;
page = pages[ipage];
}
// clear all pages, zeroize allocated memory and
// ensure that all future pages are initialized with zero
void reset(bool zeroize_pages) {
ndatum = nchunk = 0;
index = ipage = 0;
page = pages[ipage];
zeroize = zeroize_pages;
if(zeroize) {
for(int i=0; i < npage; i++) {
memset(pages[i], 0, pagesize*sizeof(T));
}
}
}
// return total size of allocated pages
int size() const {
return npage*pagesize*sizeof(T);
}
// return error status
int status() const {
return errorflag;
}
private:
T **pages; // list of allocated pages
T *page; // ptr to current page
int npage; // # of allocated pages
int ipage; // index of current page
int index; // current index on current page
int maxchunk; // max # of datums in one requested chunk
int pagesize; // # of datums in one page, default = 1024
int pagedelta; // # of pages to allocate at once, default = 1
int errorflag; // flag > 0 if error has occurred
// 1 = chunk size exceeded maxchunk
// 2 = memory allocation error
bool zeroize; // new pages allocated are initialized with zero
void allocate() {
npage += pagedelta;
pages = (T **) realloc(pages,npage*sizeof(T *));
if (!pages) {
errorflag = 2;
return;
}
for (int i = npage-pagedelta; i < npage; i++) {
#if defined(LAMMPS_MEMALIGN)
void *ptr;
if (posix_memalign(&ptr, LAMMPS_MEMALIGN, pagesize*sizeof(T)))
errorflag = 2;
pages[i] = (T *) ptr;
#else
if(zeroize)
pages[i] = (T *) calloc(pagesize,sizeof(T));
else
pages[i] = (T *) malloc(pagesize*sizeof(T));
if (!pages[i]) errorflag = 2;
#endif
}
}
};
}
#endif
|