OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
DiscBuffer.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
27 
28 #include "Utility/DiscBuffer.h"
29 #include "Utility/PAssert.h"
30 #include <cstdlib>
31 
32 
34 // Method definitions for DiscBuffer
36 
37 // Static storage
38 
39 long DiscBuffer::size_s = 0;
40 char *DiscBuffer::buffer_s = 0;
41 
42 // Some static variables used for statistics, these are really
43 // hacks so don't count on them.
44 
45 double DiscBuffer::readtime = 0.0;
46 double DiscBuffer::writetime = 0.0;
47 long DiscBuffer::readbytes = 0;
48 long DiscBuffer::writebytes = 0;
49 
50 
52 // The default constructor does nothing, this class is mainly an
53 // interface to the static data.
54 
56 {
57 }
58 
59 
61 // The destructor will delete the buffer storage if it has been
62 // previously created.
63 
65 {
66  if (buffer_s != 0)
67 #ifdef IPPL_DIRECTIO
68  free(buffer_s);
69 #else
70  delete [] buffer_s;
71 #endif
72 
73  size_s = 0;
74  buffer_s = 0;
75 }
76 
77 
79 // Make sure the current buffer is at least as large as the given
80 // size. If it is not, reallocate (but do not copy). In either case,
81 // return the buffer pointer.
82 
83 void *DiscBuffer::resize(long sz)
84 {
85  PAssert_GE(sz, 0);
86 
87  if (sz > size_s)
88  {
89  // Reset our existing size
90 
91  size_s = sz;
92 
93  // Free the old buffer, if necessary, and create a new one
94 
95 #ifdef IPPL_DIRECTIO
96  if (buffer_s != 0)
97  {
98  free(buffer_s);
99  buffer_s = 0;
100  }
101  buffer_s = (char *)valloc(size_s);
102 #else
103  if (buffer_s != 0)
104  {
105  delete [] buffer_s;
106  buffer_s = 0;
107  }
108  buffer_s = new char[size_s];
109 #endif
110 
111  PAssert(buffer_s);
112  }
113 
114  return buffer();
115 }
116 
117 
119 // Create a single global instance of a DiscBuffer so that something
120 // will delete the storage when it is done.
121 
123 
124 
125 /***************************************************************************
126  * $RCSfile: DiscBuffer.cpp,v $ $Author: adelmann $
127  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:33 $
128  * IPPL_VERSION_ID: $Id: DiscBuffer.cpp,v 1.1.1.1 2003/01/23 07:40:33 adelmann Exp $
129  ***************************************************************************/
static long readbytes
Definition: DiscBuffer.h:84
static void * resize(long sz)
Definition: DiscBuffer.cpp:83
static double readtime
Definition: DiscBuffer.h:82
DiscBuffer ipplGlobalDiscBuffer_g
Definition: DiscBuffer.cpp:122
#define PAssert_GE(a, b)
Definition: PAssert.h:124
static char * buffer_s
Definition: DiscBuffer.h:94
static void * buffer()
Definition: DiscBuffer.h:55
static long writebytes
Definition: DiscBuffer.h:85
#define PAssert(c)
Definition: PAssert.h:117
static long size_s
Definition: DiscBuffer.h:90
static double writetime
Definition: DiscBuffer.h:83