Vowpal Wabbit
Macros | Typedefs | Functions
memory.h File Reference
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <memory>
#include "vw_exception.h"

Go to the source code of this file.

Macros

#define calloc_mergable_or_throw   calloc_or_throw
 

Typedefs

using free_fn = void(*)(void *)
 
template<class T >
using free_ptr = std::unique_ptr< T, free_fn >
 

Functions

template<class T >
T * calloc_or_throw (size_t nmemb)
 
template<class T >
T & calloc_or_throw ()
 
template<class T >
void destroy_free (void *temp)
 
template<class T , typename... Args>
free_ptr< T > scoped_calloc_or_throw (Args &&... args)
 
void free_it (void *ptr)
 

Macro Definition Documentation

◆ calloc_mergable_or_throw

#define calloc_mergable_or_throw   calloc_or_throw

Definition at line 91 of file memory.h.

Referenced by scoped_calloc_or_throw().

Typedef Documentation

◆ free_fn

using free_fn = void (*)(void*)

Definition at line 31 of file memory.h.

◆ free_ptr

template<class T >
using free_ptr = std::unique_ptr<T, free_fn>

Definition at line 34 of file memory.h.

Function Documentation

◆ calloc_or_throw() [1/2]

template<class T >
T* calloc_or_throw ( size_t  nmemb)

Definition at line 9 of file memory.h.

References THROW_OR_RETURN.

10 {
11  if (nmemb == 0)
12  return nullptr;
13 
14  void* data = calloc(nmemb, sizeof(T));
15  if (data == nullptr)
16  {
17  const char* msg = "internal error: memory allocation failed!\n";
18  // use low-level function since we're already out of memory.
19  fputs(msg, stderr);
20  THROW_OR_RETURN(msg, nullptr);
21  }
22  return (T*)data;
23 }
#define THROW_OR_RETURN(...)
Definition: vw_exception.h:208

◆ calloc_or_throw() [2/2]

template<class T >
T& calloc_or_throw ( )

Definition at line 26 of file memory.h.

27 {
28  return *calloc_or_throw<T>(1);
29 }

◆ destroy_free()

template<class T >
void destroy_free ( void *  temp)

Definition at line 37 of file memory.h.

38 {
39  ((T*)temp)->~T();
40  free(temp);
41 }

◆ free_it()

void free_it ( void *  ptr)
inline

Definition at line 94 of file memory.h.

Referenced by deleter(), VW::finish(), free_svm_model(), VW::return_features(), VW::seed_vw_model(), cbify::~cbify(), and warm_cb::~warm_cb().

95 {
96  if (ptr != nullptr)
97  free(ptr);
98 }

◆ scoped_calloc_or_throw()

template<class T , typename... Args>
free_ptr<T> scoped_calloc_or_throw ( Args &&...  args)

Definition at line 44 of file memory.h.

References calloc_mergable_or_throw, and THROW.

45 {
46  T* temp = calloc_or_throw<T>(1);
47  new (temp) T(std::forward<Args>(args)...);
48  return std::unique_ptr<T, free_fn>(temp, destroy_free<T>);
49 }