C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

buffer.h
1 /* file: buffer.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 #ifndef __SERVICES_INTERNAL_BUFFER_H__
19 #define __SERVICES_INTERNAL_BUFFER_H__
20 
21 #include "services/base.h"
22 #include "services/buffer_view.h"
23 
24 namespace daal
25 {
26 namespace services
27 {
28 namespace internal
29 {
43 template<typename T>
44 class Buffer : public Base
45 {
46 public:
47  Buffer() :
48  _buffer(NULL),
49  _size(0) { }
50 
51  explicit Buffer(size_t size, services::Status *status = NULL)
52  {
53  services::Status localStatus = reallocate(size);
54  services::internal::tryAssignStatusAndThrow(status, localStatus);
55  }
56 
57  virtual ~Buffer()
58  {
59  destroy();
60  }
61 
62  void destroy()
63  {
64  services::daal_free((void *)_buffer);
65  _buffer = NULL;
66  _size = 0;
67  }
68 
69  services::Status reallocate(size_t size, bool copy = false)
70  {
71  if (_size == size)
72  { return services::Status(); }
73 
74  T *buffer = (T *)services::daal_calloc( sizeof(T) * size );
75  if (!buffer)
76  { return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
77 
78  if (copy)
79  {
80  for (size_t i = 0; i < _size; i++)
81  { _buffer[i] = buffer[i]; }
82  }
83 
84  destroy();
85 
86  _size = size;
87  _buffer = buffer;
88  return services::Status();
89  }
90 
91  services::Status enlarge(size_t factor = 2, bool copy = false)
92  {
93  return reallocate(_size * factor, copy);
94  }
95 
96  size_t size() const
97  {
98  return _size;
99  }
100 
101  T *data() const
102  {
103  return _buffer;
104  }
105 
106  T *offset(size_t elementsOffset) const
107  {
108  DAAL_ASSERT( elementsOffset <= _size );
109  return _buffer + elementsOffset;
110  }
111 
112  T &operator [] (size_t index)
113  {
114  DAAL_ASSERT( index < _size );
115  return _buffer[index];
116  }
117 
118  const T &operator [] (size_t index) const
119  {
120  DAAL_ASSERT( index < _size );
121  return _buffer[index];
122  }
123 
124  services::BufferView<T> view() const
125  {
126  return services::BufferView<T>(_buffer, _size);
127  }
128 
129 private:
130  Buffer(const Buffer &);
131  Buffer &operator = (const Buffer &);
132 
133 private:
134  T *_buffer;
135  size_t _size;
136 };
139 } // namespace internal
140 } // namespace services
141 } // namespace daal
142 
143 #endif
daal::services::internal::Buffer
Class that provides simple memory management routines for handling blocks of continues memory...
Definition: buffer.h:44
daal::services::daal_calloc
DAAL_EXPORT void * daal_calloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::Base
Base class for Intel(R) Data Analytics Acceleration Library objects
Definition: base.h:41
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:150

For more complete information about compiler optimizations, see our Optimization Notice.