fw4spl
BufferAllocationPolicy.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwMemory/BufferAllocationPolicy.hpp"
8 
9 #include "fwMemory/ByteSize.hpp"
10 #include "fwMemory/exception/Memory.hpp"
11 
12 namespace fwMemory
13 {
14 
15 //------------------------------------------------------------------------------
16 
17 void BufferMallocPolicy::allocate(BufferType& buffer,
18  BufferAllocationPolicy::SizeType size)
19 {
20  if (size > 0)
21  {
22  try
23  {
24  buffer = malloc( size );
25  }
26  catch(...)
27  {
28  buffer = nullptr;
29  }
30 
31  if (buffer == nullptr)
32  {
33  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
34  "Cannot allocate memory ("
35  << ::fwMemory::ByteSize(::fwMemory::ByteSize::SizeType(size)) <<").");
36  }
37  }
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 void BufferMallocPolicy::reallocate(BufferType& buffer,
43  BufferAllocationPolicy::SizeType size)
44 {
45  BufferType newBuffer;
46  if (size > 0)
47  {
48  newBuffer = realloc( buffer, size );
49  }
50  else
51  {
52  free( buffer );
53  newBuffer = NULL;
54  }
55  if (newBuffer == NULL && size > 0)
56  {
57  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
58  "Cannot allocate memory ("
59  << ::fwMemory::ByteSize(::fwMemory::ByteSize::SizeType(size)) <<").");
60  }
61  buffer = newBuffer;
62 }
63 
64 //------------------------------------------------------------------------------
65 
66 void BufferMallocPolicy::destroy(BufferType& buffer)
67 {
68  free( buffer );
69  buffer = 0;
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 BufferAllocationPolicy::sptr BufferMallocPolicy::New()
75 {
76  return BufferAllocationPolicy::sptr(new BufferMallocPolicy);
77 }
78 
79 //------------------------------------------------------------------------------
80 
81 void BufferNewPolicy::allocate(BufferType& buffer,
82  BufferAllocationPolicy::SizeType size)
83 {
84  try
85  {
86  if (size > 0)
87  {
88  buffer = new char[size];
89  }
90  }
91  catch (std::bad_alloc& ba)
92  {
93  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
94  "bad_alloc caught: " << ba.what());
95  }
96 }
97 
98 //------------------------------------------------------------------------------
99 
100 void BufferNewPolicy::reallocate(BufferType& buffer,
101  BufferAllocationPolicy::SizeType size)
102 {
103  FwCoreNotUsedMacro(buffer);
104  FwCoreNotUsedMacro(size);
105  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
106  "Reallocation not managed for buffer allocated with 'new' operator.");
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 void BufferNewPolicy::destroy(BufferType& buffer)
112 {
113  delete[] static_cast<char*>(buffer);
114  buffer = 0;
115 }
116 
117 //------------------------------------------------------------------------------
118 
119 BufferAllocationPolicy::sptr BufferNewPolicy::New()
120 {
121  return BufferAllocationPolicy::sptr(new BufferNewPolicy);
122 }
123 
124 //------------------------------------------------------------------------------
125 
126 void BufferNoAllocPolicy::allocate(BufferType& buffer,
127  BufferAllocationPolicy::SizeType size)
128 {
129  FwCoreNotUsedMacro(buffer);
130  FwCoreNotUsedMacro(size);
131  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
132  "No Allocation Policy should not be called.");
133 }
134 
135 //------------------------------------------------------------------------------
136 
137 void BufferNoAllocPolicy::reallocate(BufferType& buffer,
138  BufferAllocationPolicy::SizeType size)
139 {
140  FwCoreNotUsedMacro(buffer);
141  FwCoreNotUsedMacro(size);
142  FW_RAISE_EXCEPTION_MSG( ::fwMemory::exception::Memory,
143  "No Allocation Policy should not be called.");
144 }
145 
146 //------------------------------------------------------------------------------
147 
148 void BufferNoAllocPolicy::destroy(BufferType& buffer)
149 {
150  FwCoreNotUsedMacro(buffer);
151  SLM_ASSERT("No Alloc Policy should not be called", 0);
152 }
153 
154 //------------------------------------------------------------------------------
155 
156 BufferAllocationPolicy::sptr BufferNoAllocPolicy::New()
157 {
158  return BufferAllocationPolicy::sptr(new BufferNoAllocPolicy);
159 }
160 
161 //------------------------------------------------------------------------------
162 
163 } //namespace fwMemory
Conversion helper for size in bytes Converts string to number of bytes and vice-versa. This class is also able to manage conversions between units standards (IEC, SI)
Definition: ByteSize.hpp:25
The namespace fwMemory contains tools to manage memory. Use for dump.
Definition: SReader.hpp:20
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Implements an exception class for fwMemory.
Definition: Memory.hpp:23