BufferClass.cpp
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 */
40 /* */
41 /* Primitive Buffer Class */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include <string.h>
46 #include "RexxCore.h"
47 #include "RexxActivity.hpp"
48 #include "ActivityManager.hpp"
49 #include "BufferClass.hpp"
50 
51 
52 RexxClass *RexxBuffer::classInstance = OREF_NULL; // singleton class instance
53 
55 /******************************************************************************/
56 /* Function: Create initial bootstrap objects */
57 /******************************************************************************/
58 {
59  CLASS_CREATE(Buffer, "Buffer", RexxClass);
60 }
61 
62 
64  size_t l) /* minimum space needed */
65 /******************************************************************************/
66 /* Function: Create a larger buffer and copy existing data into it */
67 /******************************************************************************/
68 {
69  RexxBuffer * newBuffer; /* returned new buffer */
70 
71  /* we will either return a buffer */
72  /* twice the size of the current */
73  /* buffer, or this size of */
74  /* current(this)buffer + requested */
75  /* minimum length. */
76  if (l > this->getBufferSize()) /* need more than double? */
77  {
78  /* increase by the requested amount */
79  newBuffer = new_buffer(this->getBufferSize() + l);
80  }
81  else /* just double the existing length */
82  {
83  newBuffer = new_buffer(this->getBufferSize() * 2);
84  }
85  /* have new buffer, so copy data from*/
86  /* current buffer into new buffer. */
87  memcpy(newBuffer->getData(), this->getData(), this->getDataLength());
88  return newBuffer; /* all done, return new buffer */
89 
90 }
91 
92 void *RexxBuffer::operator new(size_t size, size_t _length)
93 /******************************************************************************/
94 /* Function: Create a new buffer object */
95 /******************************************************************************/
96 {
97  /* Get new object */
98  RexxBuffer *newBuffer = (RexxBuffer *) new_object(size + _length, T_Buffer);
99  /* Initialize this new buffer */
100  // JLF remember : this is problematic !!!!
101  // This operator is called by :
102  // inline RexxBuffer *new_buffer(size_t s) { return new (size_v(s)) RexxBuffer; }
103  // The attributes are initialized BEFORE the constructor is called (is it a good practice ??? for me, no !).
104  // When I use a strong type for size_t, then then constructors of bufferSize and dataLength are called
105  // by the default constructor RexxBuffer() which itself calls RexxBufferBase().
106  // Consequence : the assignments below are overwritten by the default constructor of size_t which stores 0.
107  // The good practice is to initialize the attributes from the constructor, not from the new operator.
108  // I keep the following lines, but I also add a new constructor RexxBuffer(size_t _length) and RexxBufferBase(size_t _length)
109  // [JLF] I had the same problem in the parser, but more difficult to fix... So I decided to no longer assign 0. And I removed the new constructors described above.
110  newBuffer->bufferSize = _length; /* set the length of the buffer */
111  newBuffer->dataLength = _length; // by default, the data length and size are the same
112  newBuffer->setHasNoReferences(); /* this has no references */
113  return(void *)newBuffer; /* return the new buffer */
114 }
115 
116 
117 RexxObject *RexxBuffer::newRexx(RexxObject **args, size_t argc, size_t named_argc)
118 /******************************************************************************/
119 /* Function: Allocate a buffer object from Rexx code. */
120 /******************************************************************************/
121 {
122  // we do not allow these to be allocated from Rexx code...
124  return TheNilObject;
125 }
126 
void reportException(wholenumber_t error)
RexxBuffer * new_buffer(size_t s)
@ T_Buffer
#define OREF_NULL
Definition: RexxCore.h:61
#define TheNilObject
Definition: RexxCore.h:191
#define Error_Unsupported_new_method
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:436
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:503
size_t getBufferSize()
Definition: BufferClass.hpp:54
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)
static RexxClass * classInstance