BufferClass.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel BufferClass.hpp */
40 /* */
41 /* Primitive Buffer Class Definitions */
42 /* */
43 /******************************************************************************/
44 #ifndef Included_RexxBuffer
45 #define Included_RexxBuffer
46 
47 class RexxBufferBase : public RexxObject
48 {
49 public:
50 
51  inline RexxBufferBase() {;};
52 
53  inline size_t getDataLength() { return this->dataLength; }
54  inline size_t getBufferSize() { return this->bufferSize; }
55  inline void setDataLength(size_t l) { this->dataLength = l; }
56  virtual char *getData() = 0;
57  inline void copyData(size_t offset, const char *string, size_t l) { memcpy(this->getData() + offset, string, l); }
58  inline void copyData(CONSTRXSTRING &r) { copyData(0, r.strptr, r.strlength); }
59  inline void copyData(RXSTRING &r) { copyData(0, r.strptr, r.strlength); }
60  inline void openGap(size_t offset, size_t _size, size_t tailSize)
61  {
62  memmove(getData() + offset + _size, getData() + offset, tailSize);
63  }
64  inline void closeGap(size_t offset, size_t _size, size_t tailSize)
65  {
66  memmove(getData() + offset, getData() + offset + _size, tailSize);
67  }
68 
69  inline void adjustGap(size_t offset, size_t _size, size_t _newSize)
70  {
71  // We have _size bytes to replace by _newSize bytes
72  // 0 1 2 3 4 5 6 7 8 9 offset = 3, XX to be replaced by YYY
73  // a b c X X f g _size = 2
74  // a b c Y Y Y f g _newSize = 3
75  size_t offset_from = offset + _size; // offset of the 1st byte to move
76  if (offset_from < getDataLength())
77  {
78  size_t offset_to = offset + _newSize; // destination offset of the 1st byte to move
79  size_t tailSize = getDataLength() - offset_from;
80  memmove(getData() + offset_to, getData() + offset_from, tailSize);
81  }
82  }
83  inline void setData(size_t offset, char character, size_t l)
84  {
85  memset(getData() + offset, character, l);
86  }
87 protected:
88 
89  // the following field is padding required to get the start of the of the
90  // buffer data aligned on an object grain boundary. Since we unflatten saved programs
91  // by reducing the size of the surrounding buffer to reveal the exposed data, we need
92  // to ensure appropriate data alignment. Fortunately, because the sizes of all of the
93  // fields doubles when going to 64-bit, this single padding item is sufficient to
94  // get everything lined up on all platforms.
95  size_t reserved;
96  size_t bufferSize; // size of the buffer
97  size_t dataLength; // length of the buffer data (freqently the same)
98 };
99 
100 
102 {
103 public:
104  void *operator new(size_t, size_t);
105  inline void *operator new(size_t size, void *ptr) {return ptr;};
106  inline void operator delete(void *) { ; }
107  inline void operator delete(void *, size_t) { ; }
108  inline void operator delete(void *, void *) { ; }
109 
110  inline RexxBuffer() {;}
111  inline RexxBuffer(RESTORETYPE restoreType) { ; }
112 
113  RexxBuffer *expand(size_t);
114  RexxObject *newRexx(RexxObject **args, size_t argc, size_t named_argc);
115  virtual char *getData() { return data; }
116 
117  static void createInstance();
118 
119  static RexxClass *classInstance; // singleton class instance
120 
121 protected:
122  char data[4]; /* actual data length */
123 };
124 
125 
126  inline RexxBuffer *new_buffer(size_t s) { return new (s) RexxBuffer; }
128  {
130  b->copyData(r);
131  return b;
132  }
133 
135  {
137  b->copyData(r);
138  return b;
139  }
140 
141  inline RexxBuffer *new_buffer(const char *data, size_t length)
142  {
143  RexxBuffer *b = new_buffer(length);
144  b->copyData(0, data, length);
145  return b;
146  }
147 #endif
RexxBuffer * new_buffer(size_t s)
RESTORETYPE
Definition: ObjectClass.hpp:82
void setData(size_t offset, char character, size_t l)
Definition: BufferClass.hpp:83
void closeGap(size_t offset, size_t _size, size_t tailSize)
Definition: BufferClass.hpp:64
void openGap(size_t offset, size_t _size, size_t tailSize)
Definition: BufferClass.hpp:60
void copyData(CONSTRXSTRING &r)
Definition: BufferClass.hpp:58
size_t getBufferSize()
Definition: BufferClass.hpp:54
void setDataLength(size_t l)
Definition: BufferClass.hpp:55
virtual char * getData()=0
void copyData(size_t offset, const char *string, size_t l)
Definition: BufferClass.hpp:57
size_t getDataLength()
Definition: BufferClass.hpp:53
void copyData(RXSTRING &r)
Definition: BufferClass.hpp:59
void adjustGap(size_t offset, size_t _size, size_t _newSize)
Definition: BufferClass.hpp:69
static void createInstance()
Definition: BufferClass.cpp:54
virtual char * getData()
RexxBuffer * expand(size_t)
Definition: BufferClass.cpp:63
RexxObject * newRexx(RexxObject **args, size_t argc, size_t named_argc)
RexxBuffer(RESTORETYPE restoreType)
char data[4]
static RexxClass * classInstance
const char * strptr
Definition: rexx.h:163
size_t strlength
Definition: rexx.h:162
size_t strlength
Definition: rexx.h:157
char * strptr
Definition: rexx.h:158