libcsdbg  1.28
C++ exception (and generic) stack trace debug library
string & csdbg::string::memalign ( u32  len,
bool  keep = false 
)
protectedvirtual

Allocate aligned memory, mandate a minimum buffer size.

Parameters
[in]lenthe mandatory length (without the trailing \0)
[in]keeptrue to keep the current data
Returns
*this
Exceptions
std::bad_alloc

Definition at line 26 of file string.cpp.

References __D_ASSERT, clear(), csdbg::g_memblock_sz, m_data, m_length, m_size, and unlikely.

Referenced by append(), format(), insert(), set(), and string().

27 {
28  if ( unlikely(len < m_size) )
29  return (keep) ? *this : clear();
30 
31  i8 *copy = NULL;
32  if ( unlikely(keep) ) {
33  __D_ASSERT(m_data != NULL);
34  __D_ASSERT(strlen(m_data) == m_length);
35 
36  copy = new i8[m_length + 1];
37  strcpy(copy, m_data);
38  }
39 
40  /* Aligned size */
43 
44  try {
45  delete[] m_data;
46  m_data = NULL;
47  m_data = new i8[m_size];
48  }
49 
50  catch (...) {
51  delete[] copy;
52  throw;
53  }
54 
55  if ( unlikely(keep) ) {
56  strcpy(m_data, copy);
57  delete[] copy;
58  return *this;
59  }
60 
61  return clear();
62 }
char i8
8-bit signed integer
Definition: config.hpp:72
static const u16 g_memblock_sz
Block size (allocation alignment)
Definition: config.hpp:202
virtual string & clear()
Clear contents.
Definition: string.cpp:387
u32 m_size
Buffer size.
Definition: string.hpp:46
i8 * m_data
String data.
Definition: string.hpp:42
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
u32 m_length
Character count.
Definition: string.hpp:44
#define __D_ASSERT(x)
Assertion macro.
Definition: config.hpp:268

+ Here is the call graph for this function:

+ Here is the caller graph for this function: