C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

buffer.h
1 /* file: buffer.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 #ifndef __SERVICES_INTERNAL_BUFFER_H__
17 #define __SERVICES_INTERNAL_BUFFER_H__
18 
19 #include "services/base.h"
20 #include "services/buffer_view.h"
21 
22 namespace daal
23 {
24 namespace services
25 {
26 namespace internal
27 {
41 template<typename T>
42 class Buffer : public Base
43 {
44 public:
45  Buffer() :
46  _buffer(NULL),
47  _size(0) { }
48 
49  explicit Buffer(size_t size, services::Status *status = NULL)
50  {
51  services::Status localStatus = reallocate(size);
52  services::internal::tryAssignStatusAndThrow(status, localStatus);
53  }
54 
55  virtual ~Buffer()
56  {
57  destroy();
58  }
59 
60  void destroy()
61  {
62  services::daal_free((void *)_buffer);
63  _buffer = NULL;
64  _size = 0;
65  }
66 
67  services::Status reallocate(size_t size, bool copy = false)
68  {
69  if (_size == size)
70  { return services::Status(); }
71 
72  T *buffer = (T *)services::daal_malloc( sizeof(T) * size );
73  if (!buffer)
74  { return services::throwIfPossible(services::ErrorMemoryAllocationFailed); }
75 
76  if (copy)
77  {
78  for (size_t i = 0; i < _size; i++)
79  { _buffer[i] = buffer[i]; }
80  }
81 
82  destroy();
83 
84  _size = size;
85  _buffer = buffer;
86  return services::Status();
87  }
88 
89  services::Status enlarge(size_t factor = 2, bool copy = false)
90  {
91  return reallocate(_size * factor, copy);
92  }
93 
94  size_t size() const
95  {
96  return _size;
97  }
98 
99  T *data() const
100  {
101  return _buffer;
102  }
103 
104  T *offset(size_t elementsOffset) const
105  {
106  DAAL_ASSERT( elementsOffset <= _size );
107  return _buffer + elementsOffset;
108  }
109 
110  T &operator [] (size_t index)
111  {
112  DAAL_ASSERT( index < _size );
113  return _buffer[index];
114  }
115 
116  const T &operator [] (size_t index) const
117  {
118  DAAL_ASSERT( index < _size );
119  return _buffer[index];
120  }
121 
122  services::BufferView<T> view() const
123  {
124  return services::BufferView<T>(_buffer, _size);
125  }
126 
127 private:
128  Buffer(const Buffer &);
129  Buffer &operator = (const Buffer &);
130 
131 private:
132  T *_buffer;
133  size_t _size;
134 };
137 } // namespace internal
138 } // namespace services
139 } // namespace daal
140 
141 #endif
daal::services::internal::Buffer
Class that provides simple memory management routines for handling blocks of continues memory...
Definition: buffer.h:42
daal
Definition: algorithm_base_common.h:31
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:147
daal::services::daal_malloc
DAAL_EXPORT void * daal_malloc(size_t size, size_t alignment=DAAL_MALLOC_DEFAULT_ALIGNMENT)
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::Base
Base class for Intel(R) Data Analytics Acceleration Library objects
Definition: base.h:39

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