TBCI Numerical high perf. C++ Library  2.8.0
Classes | Macros | Typedefs | Functions | Variables
malloc_cache.h File Reference

Caches memory blocks to avoid calls to __builtin_free()/new() It performs very well for strictly recursive new/delete pairs. More...

#include <cstdio>
#include "tbci/smp.h"
Include dependency graph for malloc_cache.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  tbci_memalloc< T >
 
class  tbci_memalloc< T >
 
struct  tbci_memalloc_cache_tls< T >
 
struct  tbci_memalloc_cache< T >
 For specializations of the memory allocator: More...
 
class  tbci_memalloc< double >
 
class  tbci_memalloc< doubleptr >
 
class  tbci_memalloc< float >
 
class  tbci_memalloc< floatptr >
 
class  tbci_memalloc< unsigned >
 
class  tbci_memalloc< uintptr >
 
class  tbci_memalloc< int >
 
class  tbci_memalloc< intptr >
 
class  tbci_memalloc< unsigned char >
 
class  tbci_memalloc< signed char >
 
class  tbci_memalloc< charptr >
 
class  tbci_memalloc< ucharptr >
 
class  tbci_memalloc< long >
 
class  tbci_memalloc< longptr >
 
class  tbci_memalloc< unsigned long >
 
class  tbci_memalloc< ulongptr >
 
class  tbci_memalloc< unsigned short >
 
class  tbci_memalloc< voidptr >
 

Macros

#define MALLOC_CACHE   1
 
#define MALLOC
 
#define TBCI_MALLOC_LOCK   1
 
#define DEBUG_PRINTF1(arg1)   do {} while (0)
 
#define DEBUG_PRINTF2(a1, a2)   do {} while (0)
 
#define DEBUG_PRINTF3(a1, a2, a3)   do {} while (0)
 
#define DEBUG_PRINTF4(a1, a2, a3, a4)   do {} while (0)
 
#define TBCI_MALLOC_POOLSZ   8
 
#define TBCI_MALLOC_LIMIT   8192
 
#define DUMMY1
 
#define DUMMY2(TYPE)
 
#define DUMMY3(t)
 
#define FGD2   FGDT
 
#define MINLINE   inline
 
#define NO_SINGLETON_PUBLIC
 
#define TBCI_SIMD_ALIGN   16
 
#define TBCI_MALLOC_ALIGN_FACT   1
 
#define _MC_NEW(T, SZ)   new T[SZ]
 
#define _MC_DELETE(T, PTR)   delete[] (T*)PTR
 
#define SSE_VARS_MAY_BE_UNALIGNED
 
#define INC(x)   ++x;
 
#define SMP_LOCK
 SMP locking: The TBCI worker threads don't do memory allocations so the memalloc_cache code does not need to be multithreading safe. More...
 
#define SMP_UNLOCK
 
#define SPECIALIZE_MEMALLOC_CLASS(TYPE)
 Allocator specialization macro, subsequently used to specialize the allocator for the elementary types. More...
 
#define SPECIALIZE_MEMALLOC(TYPE)
 
#define SPECIALIZE_MEMALLOC2(TYPE, SHTP)
 
#define NEW(t, s)   tbci_s_allocator<t>(DUMMY3(t)).alloc(s)
 
#define TBCIDELETE(t, v, sz)   tbci_s_allocator<t>(DUMMY3(t)).dealloc(v, sz)
 
#define TBCIDELETE_RO(t, v, sz)   tbci_s_allocator<t>(DUMMY3(t)).dealloc(v, sz)
 
#define REALLOC(v, os, t, s)
 

Typedefs

typedef double * doubleptr
 
typedef float * floatptr
 
typedef unsigned int * uintptr
 
typedef int * intptr
 
typedef char * charptr
 
typedef unsigned char * ucharptr
 
typedef long * longptr
 
typedef unsigned long * ulongptr
 
typedef void * voidptr
 

Functions

template<typename T >
tbci_memalloc< T > & tbci_s_allocator ()
 
template<typename T >
struct tbci_memalloc_cache_tls ALIGN (64)
 
template<>
tbci_memalloc< double > & tbci_s_allocator< double > ()
 
template<>
tbci_memalloc< doubleptr > & tbci_s_allocator< doubleptr > ()
 
template<>
tbci_memalloc< float > & tbci_s_allocator< float > ()
 
template<>
tbci_memalloc< floatptr > & tbci_s_allocator< floatptr > ()
 
template<>
tbci_memalloc< unsigned > & tbci_s_allocator< unsigned > ()
 
template<>
tbci_memalloc< uintptr > & tbci_s_allocator< uintptr > ()
 
template<>
tbci_memalloc< int > & tbci_s_allocator< int > ()
 
template<>
tbci_memalloc< intptr > & tbci_s_allocator< intptr > ()
 
template<>
tbci_memalloc< unsigned char > & tbci_s_allocator< unsigned char > ()
 
template<>
tbci_memalloc< signed char > & tbci_s_allocator< signed char > ()
 
template<>
tbci_memalloc< charptr > & tbci_s_allocator< charptr > ()
 
template<>
tbci_memalloc< ucharptr > & tbci_s_allocator< ucharptr > ()
 
template<>
tbci_memalloc< long > & tbci_s_allocator< long > ()
 
template<>
tbci_memalloc< longptr > & tbci_s_allocator< longptr > ()
 
template<>
tbci_memalloc< unsigned long > & tbci_s_allocator< unsigned long > ()
 
template<>
tbci_memalloc< ulongptr > & tbci_s_allocator< ulongptr > ()
 
template<>
tbci_memalloc< unsigned short > & tbci_s_allocator< unsigned short > ()
 
template<>
tbci_memalloc< voidptr > & tbci_s_allocator< voidptr > ()
 

Variables

const Tfree_pt [8]
 
unsigned short free_sz [8]
 
unsigned char free_idx
 
volatile unsigned int miss
 
volatile unsigned int hit
 
volatile unsigned int srch
 
volatile unsigned int all
 
struct tbci_memalloc_cache ALIGN
 

Detailed Description

Caches memory blocks to avoid calls to __builtin_free()/new() It performs very well for strictly recursive new/delete pairs.

Author
Kurt Garloff kurt@.nosp@m.garl.nosp@m.off.d.nosp@m.e
Date
2002-07-31 License: GNU LGPL
Version
Id:
malloc_cache.h,v 1.1.2.61 2019/05/31 06:30:18 garloff Exp

Definition in file malloc_cache.h.

Macro Definition Documentation

#define _MC_DELETE (   T,
  PTR 
)    delete[] (T*)PTR
#define _MC_NEW (   T,
  SZ 
)    new T[SZ]

Definition at line 130 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::alloc().

#define DEBUG_PRINTF1 (   arg1)    do {} while (0)
#define DEBUG_PRINTF2 (   a1,
  a2 
)    do {} while (0)

Definition at line 55 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::find_by_sz().

#define DEBUG_PRINTF3 (   a1,
  a2,
  a3 
)    do {} while (0)
#define DEBUG_PRINTF4 (   a1,
  a2,
  a3,
  a4 
)    do {} while (0)

Definition at line 57 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::free_and_enter().

#define DUMMY1

Definition at line 74 of file malloc_cache.h.

#define DUMMY2 (   TYPE)

Definition at line 75 of file malloc_cache.h.

#define DUMMY3 (   t)

Definition at line 76 of file malloc_cache.h.

#define FGD2   FGDT

Definition at line 77 of file malloc_cache.h.

#define INC (   x)    ++x;

Definition at line 143 of file malloc_cache.h.

#define MALLOC

Definition at line 29 of file malloc_cache.h.

#define MALLOC_CACHE   1

Definition at line 14 of file malloc_cache.h.

#define MINLINE   inline

Definition at line 80 of file malloc_cache.h.

#define NEW (   t,
 
)    tbci_s_allocator<t>(DUMMY3(t)).alloc(s)
#define NO_SINGLETON_PUBLIC

Definition at line 89 of file malloc_cache.h.

#define REALLOC (   v,
  os,
  t,
 
)
Value:
do { \
t *ptr = NEW(t, s); \
if (LIKELY(v)) { \
TBCICOPY(ptr, v, t, MIN(os, s));\
TBCIDELETE(t, v, os); \
} \
v = ptr; \
} while (0)
#define TBCICOPY(n, o, t, s)
Definition: basics.h:894
#define MIN(a, b)
Definition: basics.h:655
#define NEW(t, s)
Definition: malloc_cache.h:633
if(value==0) return 1
#define TBCIDELETE(t, v, sz)
Definition: malloc_cache.h:634
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int char v
&lt; find minimun of func on grid with resolution res
Definition: LM_fit.h:205
#define LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
Definition: basics.h:100

Definition at line 636 of file malloc_cache.h.

Referenced by BdMatrix< T >::adddiag(), BVector< T >::append(), BVector< T >::resize(), and TMatrix< T >::row_expand().

#define SMP_LOCK

SMP locking: The TBCI worker threads don't do memory allocations so the memalloc_cache code does not need to be multithreading safe.

However, to make the TBCI threading framework more useful, we should allow this in the future.

The current implementation does the following:

  • If the pthread library offers spinlocks, we use them to ensure that only one process can be in the critical section at a time. define TBCI_MALLOC_NOSPINLOCK to switch off this behavior. Off is currently default if you have TLS, as for some reason (preemption?) the performance with spinlocks is not as good as limiting memalloc_cache allocations to the main thread. define TBCI_MALLOC_FORCESPINLOCK to override this setting.
  • On all other architectures, we simply fall back to normal malloc/free (new[]/delete[]) in case we have been compiled with SMP support AND if threads usage really has been prepared (by init_threads()). If we have thread local variables, we will still use malloc_cache from the main thread.
  • define TBCI_MALLOC_NOLOCK if you want to avoid this behavior and use memalloc_cache unconditionally. This will result in memory corrpution if you do memory allocations from threads!!!

Possible future work:

  • Use a per-thread memalloc pool

Definition at line 242 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::find_and_rmv(), and tbci_memalloc_cache< T >::free_and_enter().

#define SMP_UNLOCK
#define SPECIALIZE_MEMALLOC (   TYPE)
Value:
template <> \
MINLINE tbci_memalloc<TYPE >& tbci_s_allocator<TYPE > (DUMMY2(TYPE)) \
{ /* the one and only */ \
static tbci_memalloc<TYPE > _tbci_s_alloc_##TYPE; \
return _tbci_s_alloc_##TYPE; \
}
#define DUMMY2(TYPE)
Definition: malloc_cache.h:75
#define SPECIALIZE_MEMALLOC_CLASS(TYPE)
Allocator specialization macro, subsequently used to specialize the allocator for the elementary type...
Definition: malloc_cache.h:549
return
Definition: LM_fit.h:82
#define MINLINE
Definition: malloc_cache.h:80

Definition at line 578 of file malloc_cache.h.

#define SPECIALIZE_MEMALLOC2 (   TYPE,
  SHTP 
)
Value:
template <> \
MINLINE tbci_memalloc<TYPE >& tbci_s_allocator<TYPE > (DUMMY2(TYPE)) \
{ /* the one and only */ \
static tbci_memalloc<TYPE > _tbci_s_alloc_##SHTP; \
return _tbci_s_alloc_##SHTP; \
}
#define DUMMY2(TYPE)
Definition: malloc_cache.h:75
#define SPECIALIZE_MEMALLOC_CLASS(TYPE)
Allocator specialization macro, subsequently used to specialize the allocator for the elementary type...
Definition: malloc_cache.h:549
return
Definition: LM_fit.h:82
#define MINLINE
Definition: malloc_cache.h:80

Definition at line 587 of file malloc_cache.h.

#define SPECIALIZE_MEMALLOC_CLASS (   TYPE)
Value:
template <> \
typedef TYPE T; \
protected: \
tbci_memalloc_cache<T > m_cache; \
NO_SINGLETON_PUBLIC \
tbci_memalloc() {} \
~tbci_memalloc() {} \
public: \
T* alloc(const unsigned long sz) { return m_cache.alloc(sz); } \
void dealloc(const T* const ptr, const unsigned long sz) \
{ m_cache.dealloc(ptr,sz); } \
};
#define DUMMY1
Definition: malloc_cache.h:74
tbci_memalloc< T > & tbci_s_allocator()
Definition: malloc_cache.h:567
#define FGD2
Definition: malloc_cache.h:77
#define T
Definition: bdmatlib.cc:20

Allocator specialization macro, subsequently used to specialize the allocator for the elementary types.

Definition at line 549 of file malloc_cache.h.

#define SSE_VARS_MAY_BE_UNALIGNED

Definition at line 133 of file malloc_cache.h.

#define TBCI_MALLOC_ALIGN_FACT   1

Definition at line 126 of file malloc_cache.h.

#define TBCI_MALLOC_LIMIT   8192
#define TBCI_MALLOC_LOCK   1

Definition at line 37 of file malloc_cache.h.

#define TBCI_MALLOC_POOLSZ   8
#define TBCI_SIMD_ALIGN   16

Definition at line 122 of file malloc_cache.h.

#define TBCIDELETE (   t,
  v,
  sz 
)    tbci_s_allocator<t>(DUMMY3(t)).dealloc(v, sz)
#define TBCIDELETE_RO (   t,
  v,
  sz 
)    tbci_s_allocator<t>(DUMMY3(t)).dealloc(v, sz)

Definition at line 635 of file malloc_cache.h.

Typedef Documentation

typedef char* charptr

Definition at line 617 of file malloc_cache.h.

typedef double* doubleptr

Definition at line 604 of file malloc_cache.h.

typedef float* floatptr

Definition at line 607 of file malloc_cache.h.

typedef int* intptr

Definition at line 613 of file malloc_cache.h.

typedef long* longptr

Definition at line 622 of file malloc_cache.h.

typedef unsigned char* ucharptr

Definition at line 619 of file malloc_cache.h.

typedef unsigned int* uintptr

Definition at line 610 of file malloc_cache.h.

typedef unsigned long* ulongptr

Definition at line 625 of file malloc_cache.h.

typedef void* voidptr

Definition at line 628 of file malloc_cache.h.

Function Documentation

template<typename T >
struct tbci_memalloc_cache_tls ALIGN ( 64  )
template<typename T >
tbci_memalloc< T > & tbci_s_allocator ( )
inline

Definition at line 567 of file malloc_cache.h.

template<>
tbci_memalloc< charptr >& tbci_s_allocator< charptr > ( )
inline

Definition at line 618 of file malloc_cache.h.

template<>
tbci_memalloc< double >& tbci_s_allocator< double > ( )
inline

Definition at line 603 of file malloc_cache.h.

template<>
tbci_memalloc< doubleptr >& tbci_s_allocator< doubleptr > ( )
inline

Definition at line 605 of file malloc_cache.h.

template<>
tbci_memalloc< float >& tbci_s_allocator< float > ( )
inline

Definition at line 606 of file malloc_cache.h.

template<>
tbci_memalloc< floatptr >& tbci_s_allocator< floatptr > ( )
inline

Definition at line 608 of file malloc_cache.h.

template<>
tbci_memalloc< int >& tbci_s_allocator< int > ( )
inline

Definition at line 612 of file malloc_cache.h.

template<>
tbci_memalloc< intptr >& tbci_s_allocator< intptr > ( )
inline

Definition at line 614 of file malloc_cache.h.

template<>
tbci_memalloc< long >& tbci_s_allocator< long > ( )
inline

Definition at line 621 of file malloc_cache.h.

template<>
tbci_memalloc< longptr >& tbci_s_allocator< longptr > ( )
inline

Definition at line 623 of file malloc_cache.h.

template<>
tbci_memalloc< signed char >& tbci_s_allocator< signed char > ( )
inline

Definition at line 616 of file malloc_cache.h.

template<>
tbci_memalloc< ucharptr >& tbci_s_allocator< ucharptr > ( )
inline

Definition at line 620 of file malloc_cache.h.

template<>
tbci_memalloc< uintptr >& tbci_s_allocator< uintptr > ( )
inline

Definition at line 611 of file malloc_cache.h.

template<>
tbci_memalloc< ulongptr >& tbci_s_allocator< ulongptr > ( )
inline

Definition at line 626 of file malloc_cache.h.

template<>
tbci_memalloc< unsigned >& tbci_s_allocator< unsigned > ( )
inline

Definition at line 609 of file malloc_cache.h.

template<>
tbci_memalloc< unsigned char >& tbci_s_allocator< unsigned char > ( )
inline

Definition at line 615 of file malloc_cache.h.

template<>
tbci_memalloc< unsigned long >& tbci_s_allocator< unsigned long > ( )
inline

Definition at line 624 of file malloc_cache.h.

template<>
tbci_memalloc< unsigned short >& tbci_s_allocator< unsigned short > ( )
inline

Definition at line 627 of file malloc_cache.h.

template<>
tbci_memalloc< voidptr >& tbci_s_allocator< voidptr > ( )
inline

Definition at line 629 of file malloc_cache.h.

Variable Documentation

T min ALIGN
volatile unsigned int all

Definition at line 98 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::smp_deinit().

unsigned char free_idx

Definition at line 96 of file malloc_cache.h.

const T* free_pt[8]

Definition at line 94 of file malloc_cache.h.

unsigned short free_sz[8]

Definition at line 95 of file malloc_cache.h.

volatile unsigned int hit

Definition at line 98 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::smp_deinit().

volatile unsigned int miss

Definition at line 97 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::smp_deinit().

volatile unsigned int srch

Definition at line 98 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::smp_deinit().