GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Memory.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * aLong_t with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef GWS_MEMORY_H
22 #define GWS_MEMORY_H
23 
24 #ifndef GWS_BASE_H
25 #include "Base.h"
26 #endif
27 
28 #ifndef ROOT_Bytes
29 #include <Bytes.h>
30 #endif
31 
32 
33 namespace Gws
34 {
35 
36 const Int_t BYTE = 8;
37 const Int_t KBYTE = 1024;
38 const Int_t MBYTE = KBYTE*KBYTE;
39 const Int_t GBYTE = KBYTE*MBYTE;
40 
42 
48 class Memory : public Base
49 {
50 public:
59  enum EEndian { kLittle = 0, kBig = 1 } ;
60 
61 private:
63 
66  class Agent {
67  Int_t fEndian;
68  public:
69  Agent();
70  Int_t GetEndian() { return fEndian; }
71  };
72  static Agent *gAgent;
73 
74 public:
75  Memory();
76  virtual ~Memory();
77 
79 
82  static bool IsBytes(Memory::EEndian e) { return gAgent->GetEndian() == e; }
83  static Int_t GetEndian() { return gAgent->GetEndian() ; }
84 
86 
90  static Char_t *New(Char_t *p, Int_t nb = 32*KBYTE);
91  static Short_t *New(Short_t *p, Int_t nb = 32*KBYTE);
92  static Int_t *New(Int_t *p, Int_t nb = 32*KBYTE);
93  static UChar_t *New(UChar_t *p, Int_t nb = 32*KBYTE);
94  static UShort_t *New(UShort_t *p, Int_t nb = 32*KBYTE);
95  static UInt_t *New(UInt_t *p, Int_t nb = 32*KBYTE);
96  static Float_t * New(Float_t *p, Int_t nb = 32*KBYTE);
97  static Double_t * New(Double_t *p, Int_t nb = 32*KBYTE);
98 // static Long_t *New(Long_t *p, Int_t nb = 32*KBYTE);
99 // static ULong_t *New(ULong_t *p, Int_t nb = 32*KBYTE);
100 
101  static Char_t *New(Char_t *p, UInt_t nb = 32*KBYTE);
102  static Short_t *New(Short_t *p, UInt_t nb = 32*KBYTE);
103  static Int_t *New(Int_t *p, UInt_t nb = 32*KBYTE);
104  static Long_t *New(Long_t *p, UInt_t nb = 32*KBYTE);
105  static UChar_t *New(UChar_t *p, UInt_t nb = 32*KBYTE);
106  static UShort_t *New(UShort_t *p, UInt_t nb = 32*KBYTE);
107  static UInt_t *New(UInt_t *p, UInt_t nb = 32*KBYTE);
108  static Float_t * New(Float_t *p, UInt_t nb = 32*KBYTE);
109  static Double_t * New(Double_t *p, UInt_t nb = 32*KBYTE);
110 
111 // static Long_t *New(Long_t *p, UInt_t nb = 32*KBYTE);
112 // static ULong_t *New(ULong_t *p, UInt_t nb = 32*KBYTE);
113 
114  static void Delete(Char_t *p) { ::delete [] p; p = NULL; }
115  static void Delete(Short_t *p) { ::delete [] p; p = NULL; }
116  static void Delete(Int_t *p) { ::delete [] p; p = NULL; }
117  static void Delete(UChar_t *p) { ::delete [] p; p = NULL; }
118  static void Delete(UShort_t *p) { ::delete [] p; p = NULL; }
119  static void Delete(UInt_t *p) { ::delete [] p; p = NULL; }
120  static void Delete(Float_t *p) { ::delete [] p; p = NULL; }
121  static void Delete(Double_t *p) { ::delete [] p; p = NULL; }
122 
123 // static void Delete(Long_t *p) { ::delete [] p; p = NULL; }
124 // static void Delete(ULong_t *p) { ::delete [] p; p = NULL; }
125 
127 
130  static void Zero(Char_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,nb); }
131  static void Zero(Short_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Short_t)*nb); }
132  static void Zero(Int_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Int_t)*nb); }
133  static void Zero(UChar_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,nb); }
134  static void Zero(UShort_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(UShort_t)*nb); }
135  static void Zero(UInt_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(UInt_t)*nb); }
136  static void Zero(Float_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Float_t)*nb); }
137  static void Zero(Double_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Double_t)*nb); }
138 
139 // static void Zero(Long_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Long_t)*nb); }
140 // static void Zero(ULong_t *p, Int_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(ULong_t)*nb); }
141 
142  static void Zero(Char_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,nb); }
143  static void Zero(Short_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Short_t)*nb); }
144  static void Zero(Int_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Int_t)*nb); }
145  static void Zero(UChar_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,nb); }
146  static void Zero(UShort_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(UShort_t)*nb); }
147  static void Zero(UInt_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(UInt_t)*nb); }
148  static void Zero(Float_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Float_t)*nb); }
149  static void Zero(Double_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Double_t)*nb); }
150 
151 // static void Zero(Long_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(Long_t)*nb); }
152 // static void Zero(ULong_t *p, UInt_t nb) { if ( nb < 1 ) return; ::memset(p,0,sizeof(ULong_t)*nb); }
153 
155 
158  static void Copy(const Char_t *from, Char_t *to, Int_t size) { ::memcpy(to,from,size); }
159 
161 
166  static void Swap(Short_t, Char_t *);
167  static void Swap(UShort_t, Char_t *);
168  static void Swap(Int_t, Char_t *);
169  static void Swap(UInt_t, Char_t *);
170  static void Swap(Float_t, Char_t *);
171  static void Swap(Double_t, Char_t *);
172 // static void Swap(Long_t, Char_t *);
173 // static void Swap(ULong_t, Char_t *);
174 
175  static void Swap(Char_t *,Short_t &);
176  static void Swap(Char_t *,UShort_t &);
177  static void Swap(Char_t *,Int_t &);
178  static void Swap(Char_t *,UInt_t &);
179  static void Swap(Char_t *,Float_t &);
180  static void Swap(Char_t *,Double_t &);
181 // static void Swap(Char_t *,Long_t &);
182 // static void Swap(Char_t *,ULong_t &);
183 };
184 
185 // Memory inline members //////////////////////////////////////////////////////
186 inline Char_t * Memory::New(Char_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Char_t[nb]; if ( p ) Zero(p,nb); return p; }
187 inline Short_t * Memory::New(Short_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Short_t[nb]; if ( p ) Zero(p,nb); return p; }
188 inline Int_t * Memory::New(Int_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Int_t[nb]; if ( p ) Zero(p,nb); return p; }
189 inline UChar_t * Memory::New(UChar_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new UChar_t[nb]; if ( p ) Zero(p,nb); return p; }
190 inline UShort_t * Memory::New(UShort_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new UShort_t[nb]; if ( p ) Zero(p,nb); return p; }
191 inline UInt_t * Memory::New(UInt_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new UInt_t[nb]; if ( p ) Zero(p,nb); return p; }
192 inline Float_t * Memory::New(Float_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Float_t[nb]; if ( p ) Zero(p,nb); return p; }
193 inline Double_t * Memory::New (Double_t * p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Double_t[nb]; if ( p ) Zero(p,nb); return p; }
194 //inline Long_t * Memory::New(Long_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new Long_t[nb]; if ( p ) Zero(p,nb); return p; }
195 //inline ULong_t * Memory::New(ULong_t *p,Int_t nb) { if ( nb < 1 ) return NULL; p = ::new ULong_t[nb]; if ( p ) Zero(p,nb); return p; }
196 
197 inline Char_t * Memory::New(Char_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Char_t[nb]; if ( p ) Zero(p,nb); return p; }
198 inline Short_t * Memory::New(Short_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Short_t[nb]; if ( p ) Zero(p,nb); return p; }
199 inline Int_t * Memory::New(Int_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Int_t[nb]; if ( p ) Zero(p,nb); return p; }
200 inline UChar_t * Memory::New(UChar_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new UChar_t[nb]; if ( p ) Zero(p,nb); return p; }
201 inline UShort_t * Memory::New(UShort_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new UShort_t[nb]; if ( p ) Zero(p,nb); return p; }
202 inline UInt_t * Memory::New(UInt_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new UInt_t[nb]; if ( p ) Zero(p,nb); return p; }
203 inline Float_t * Memory::New(Float_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Float_t[nb]; if ( p ) Zero(p,nb); return p; }
204 inline Double_t * Memory::New (Double_t * p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Double_t[nb]; if ( p ) Zero(p,nb); return p; }
205 //inline Long_t * Memory::New(Long_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new Long_t[nb]; if ( p ) Zero(p,nb); return p; }
206 //inline ULong_t * Memory::New(ULong_t *p,UInt_t nb) { if ( nb < 1 ) return NULL; p = ::new ULong_t[nb]; if ( p ) Zero(p,nb); return p; }
207 
208 
209 inline void Memory::Swap(UShort_t x, Char_t *buf)
210 {
211 # if defined(__linux) && defined(__i386__)
212  *((UShort_t *)buf) = Rbswap_16(x);
213 # else
214  // To work around a stupid optimization bug in MSVC++ 6.0
215  const UShort_t *intermediary = &x;
216  char *sw = (char *) intermediary;
217  buf[0] = sw[1];
218  buf[1] = sw[0];
219 # endif
220 }
221 inline void Memory::Swap(Short_t x, Char_t *buf) { Memory::Swap((UShort_t) x,buf) ;}
222 
223 inline void Memory::Swap(UInt_t x, Char_t *buf)
224 {
225 # if defined(__linux) && defined(__i386__)
226  *((UInt_t *)buf) = Rbswap_32(x);
227 # else
228  // To work around a stupid optimization bug in MSVC++ 6.0
229  const UInt_t *intermediary = &x;
230  char *sw = (char *)intermediary;
231  buf[0] = sw[3];
232  buf[1] = sw[2];
233  buf[2] = sw[1];
234  buf[3] = sw[0];
235 # endif
236 }
237 inline void Memory::Swap(Int_t x, Char_t *buf) { Memory::Swap((UInt_t) x,buf);}
238 
239 inline void Memory::Swap(Float_t x, Char_t *buf)
240 {
241 # if defined(__linux) && defined(__i386__) && \
242  defined(__GNUC__) && __GNUC__ >= 2
243  *((UInt_t *)buf) = Rbswap_32(*((UInt_t *)&x));
244 # elif defined(R__KCC)
245  // Use an union to prevent over-zealous optimization by KCC
246  // related to aliasing float.
247  // + Use a volatile here to work around error in KCC optimizer
248  union {
249  volatile char c[4];
250  volatile Float_t f;
251  } u;
252  u.f = x;
253  buf[0] = u.c[3];
254  buf[1] = u.c[2];
255  buf[2] = u.c[1];
256  buf[3] = u.c[0];
257 # else
258  char *sw = (char *)&x;
259  buf[0] = sw[3];
260  buf[1] = sw[2];
261  buf[2] = sw[1];
262  buf[3] = sw[0];
263 # endif
264 }
265 
266 inline void Memory::Swap(Double_t x, Char_t *buf)
267 {
268 # if defined(__EXTENSIONS__) && defined(__linux) && defined(__i386__) && \
269  defined(__GNUC__) && __GNUC__ >= 2
270  *((ULong64_t *)buf) = Rbswap_64(*((ULong64_t *)&x));
271 # elif defined(R__KCC)
272  // Use an union to prevent over-zealous optimization by KCC
273  // related to aliasing double.
274  // + Use a volatile here to work around error in KCC optimizer
275  union {
276  volatile char c[8];
277  volatile Double_t d;
278  } u;
279  u.d = x;
280  buf[0] = u.c[7];
281  buf[1] = u.c[6];
282  buf[2] = u.c[5];
283  buf[3] = u.c[4];
284  buf[4] = u.c[3];
285  buf[5] = u.c[2];
286  buf[6] = u.c[1];
287  buf[7] = u.c[0];
288 # else
289  char *sw = (char *)&x;
290  buf[0] = sw[7];
291  buf[1] = sw[6];
292  buf[2] = sw[5];
293  buf[3] = sw[4];
294  buf[4] = sw[3];
295  buf[5] = sw[2];
296  buf[6] = sw[1];
297  buf[7] = sw[0];
298 # endif
299 }
300 
301 inline void Memory::Swap(Char_t *buf, UShort_t &x)
302 {
303 # if defined(__linux) && defined(__i386__)
304  x = Rbswap_16(*((UShort_t *)buf));
305 # else
306  char *sw = (char *)&x;
307  sw[0] = buf[1];
308  sw[1] = buf[0];
309 # endif
310 }
311 inline void Memory::Swap(Char_t *buf, Short_t &x)
312 {
313 # if defined(__linux) && defined(__i386__)
314  x = Rbswap_16(*((Short_t *)buf));
315 # else
316  char *sw = (char *)&x;
317  sw[0] = buf[1];
318  sw[1] = buf[0];
319 # endif
320 }
321 
322 inline void Memory::Swap(Char_t *buf, UInt_t &x)
323 {
324 # if defined(__linux) && defined(__i386__)
325  x = Rbswap_32(*((UInt_t *)buf));
326 # else
327  char *sw = (char *)&x;
328  sw[0] = buf[3];
329  sw[1] = buf[2];
330  sw[2] = buf[1];
331  sw[3] = buf[0];
332 # endif
333 }
334 
335 inline void Memory::Swap(Char_t *buf, Int_t &x)
336 {
337 # if defined(__linux) && defined(__i386__)
338  x = Rbswap_32(*((Int_t *)buf));
339 # else
340  char *sw = (char *)&x;
341  sw[0] = buf[3];
342  sw[1] = buf[2];
343  sw[2] = buf[1];
344  sw[3] = buf[0];
345 # endif
346 }
347 inline void Memory::Swap(Char_t *buf, Float_t &x)
348 {
349 # if defined(__linux) && defined(__i386__) && \
350  defined(__GNUC__) && __GNUC__ >= 2
351  // Use a union to allow strict-aliasing
352  union {
353  volatile UInt_t i;
354  volatile Float_t f;
355  } u;
356  u.i = Rbswap_32(*((UInt_t *)buf));
357  x = u.f;
358 # elif defined(R__KCC)
359  // Use an union to prevent over-zealous optimization by KCC
360  // related to aliasing float.
361  // + Use a volatile here to work around error in KCC optimizer
362  union {
363  volatile char c[4];
364  volatile Float_t f;
365  } u;
366  u.c[0] = buf[3];
367  u.c[1] = buf[2];
368  u.c[2] = buf[1];
369  u.c[3] = buf[0];
370  x = u.f;
371 # else
372  char *sw = (char *)&x;
373  sw[0] = buf[3];
374  sw[1] = buf[2];
375  sw[2] = buf[1];
376  sw[3] = buf[0];
377 # endif
378 }
379 
380 inline void Memory::Swap(Char_t *buf, Double_t &x)
381 {
382 # if defined(__EXTENSIONS__) && defined(__linux) && defined(__i386__) && \
383  defined(__GNUC__) && __GNUC__ >= 2
384  // Use a union to allow strict-aliasing
385  union {
386  volatile ULong64_t l;
387  volatile Double_t d;
388  } u;
389  u.l = Rbswap_64(*((ULong64_t *)buf));
390  x = u.d;
391 # elif defined(R__KCC)
392  // Use an union to prevent over-zealous optimization by KCC
393  // related to aliasing double.
394  // + Use a volatile here to work around error in KCC optimizer
395  union {
396  volatile char c[8];
397  volatile Double_t d;
398  } u;
399  u.c[0] = buf[7];
400  u.c[1] = buf[6];
401  u.c[2] = buf[5];
402  u.c[3] = buf[4];
403  u.c[4] = buf[3];
404  u.c[5] = buf[2];
405  u.c[6] = buf[1];
406  u.c[7] = buf[0];
407  x = u.d;
408 # else
409  char *sw = (char *)&x;
410  sw[0] = buf[7];
411  sw[1] = buf[6];
412  sw[2] = buf[5];
413  sw[3] = buf[4];
414  sw[4] = buf[3];
415  sw[5] = buf[2];
416  sw[6] = buf[1];
417  sw[7] = buf[0];
418 # endif
419 }
422 
423 } // namespace gws
424 
425 
426 
427 #endif
428 
429 
430 
431 
432 
EEndian
The adjectives big-endian and little-endian refer to which bytes are most significant in multi-byte d...
Definition: Memory.h:59
const Int_t MBYTE
Definition: Memory.h:38
static void Delete(UChar_t *p)
Definition: Memory.h:117
virtual ~Memory()
Definition: Memory.cpp:53
const Int_t GBYTE
Definition: Memory.h:39
static void Delete(UInt_t *p)
Definition: Memory.h:119
Interface to the memory.
Definition: Memory.h:48
static void Zero(Int_t *p, Int_t nb)
Definition: Memory.h:132
static void Zero(Double_t *p, Int_t nb)
Definition: Memory.h:137
static void Zero(Float_t *p, Int_t nb)
Definition: Memory.h:136
static Int_t GetEndian()
Definition: Memory.h:83
static void Delete(Double_t *p)
Definition: Memory.h:121
static void Delete(Float_t *p)
Definition: Memory.h:120
static void Zero(Char_t *p, UInt_t nb)
Definition: Memory.h:142
static bool IsBytes(Memory::EEndian e)
check out the endian type of the running system
Definition: Memory.h:82
static void Zero(UInt_t *p, UInt_t nb)
Definition: Memory.h:147
const Int_t KBYTE
Definition: Memory.h:37
static void Swap(Short_t, Char_t *)
the Swap members
Definition: Memory.h:221
const Int_t BYTE
Definition: Memory.h:36
static void Zero(Double_t *p, UInt_t nb)
Definition: Memory.h:149
static void Zero(Char_t *p, Int_t nb)
fast initialization of the buffer at 0
Definition: Memory.h:130
static void Zero(Short_t *p, Int_t nb)
Definition: Memory.h:131
static void Delete(Int_t *p)
Definition: Memory.h:116
static void Zero(UChar_t *p, UInt_t nb)
Definition: Memory.h:145
static Char_t * New(Char_t *p, Int_t nb=32 *KBYTE)
To allocate a buffer that is set to 0 (some compilers don't do it)
Definition: Memory.h:186
static void Zero(UShort_t *p, UInt_t nb)
Definition: Memory.h:146
static void Delete(UShort_t *p)
Definition: Memory.h:118
static void Delete(Short_t *p)
Definition: Memory.h:115
static void Zero(Float_t *p, UInt_t nb)
Definition: Memory.h:148
static void Zero(UInt_t *p, Int_t nb)
Definition: Memory.h:135
interface to the operating system
static void Copy(const Char_t *from, Char_t *to, Int_t size)
copy the content from -> to
Definition: Memory.h:158
const Int_t size
Definition: BenchIO.C:24
static void Zero(UChar_t *p, Int_t nb)
Definition: Memory.h:133
Interface to the operating system.
Definition: Base.h:91
static void Delete(Char_t *p)
Definition: Memory.h:114
static void Zero(UShort_t *p, Int_t nb)
Definition: Memory.h:134
static void Zero(Short_t *p, UInt_t nb)
Definition: Memory.h:143
static void Zero(Int_t *p, UInt_t nb)
Definition: Memory.h:144