TBCI Numerical high perf. C++ Library 2.8.0
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 >
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)
#define DEBUG_PRINTF2(a1, a2)
#define DEBUG_PRINTF3(a1, a2, a3)
#define DEBUG_PRINTF4(a1, a2, a3, a4)
#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)
#define _MC_DELETE(T, PTR)
#define SSE_VARS_MAY_BE_UNALIGNED
#define INC(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.
#define SMP_UNLOCK
#define SPECIALIZE_MEMALLOC_CLASS(TYPE)
 Allocator specialization macro, subsequently used to specialize the allocator for the elementary types.
#define SPECIALIZE_MEMALLOC(TYPE)
#define SPECIALIZE_MEMALLOC2(TYPE, SHTP)
#define NEW(t, s)
#define TBCIDELETE(t, v, sz)
#define TBCIDELETE_RO(t, 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

◆ _MC_DELETE

#define _MC_DELETE ( T,
PTR )
Value:
delete[] (T*)PTR
#define T
Definition bdmatlib.cc:20

Definition at line 131 of file malloc_cache.h.

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

◆ _MC_NEW

#define _MC_NEW ( T,
SZ )
Value:
new T[SZ]

Definition at line 130 of file malloc_cache.h.

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

◆ DEBUG_PRINTF1

#define DEBUG_PRINTF1 ( arg1)
Value:
do {} while (0)

Definition at line 54 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::deinit(), and tbci_memalloc_cache< T >::find_by_sz().

◆ DEBUG_PRINTF2

#define DEBUG_PRINTF2 ( a1,
a2 )
Value:
do {} while (0)

Definition at line 55 of file malloc_cache.h.

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

◆ DEBUG_PRINTF3

#define DEBUG_PRINTF3 ( a1,
a2,
a3 )
Value:
do {} while (0)

Definition at line 56 of file malloc_cache.h.

Referenced by tbci_memalloc_cache< T >::alloc(), and tbci_memalloc_cache< T >::deinit().

◆ DEBUG_PRINTF4

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

Definition at line 57 of file malloc_cache.h.

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

◆ DUMMY1

#define DUMMY1

Definition at line 74 of file malloc_cache.h.

Referenced by ALIGN(), tbci_memalloc< T >::FGDT, and tbci_s_allocator().

◆ DUMMY2

#define DUMMY2 ( TYPE)

Definition at line 75 of file malloc_cache.h.

◆ DUMMY3

#define DUMMY3 ( t)

Definition at line 76 of file malloc_cache.h.

◆ FGD2

#define FGD2   FGDT

Definition at line 77 of file malloc_cache.h.

◆ INC

#define INC ( x)
Value:
++x;
const Vector< T > const Vector< T > & x
Definition LM_fit.h:97

Definition at line 143 of file malloc_cache.h.

◆ MALLOC

#define MALLOC

Definition at line 29 of file malloc_cache.h.

◆ MALLOC_CACHE

#define MALLOC_CACHE   1

Definition at line 14 of file malloc_cache.h.

◆ MINLINE

#define MINLINE   inline

Definition at line 80 of file malloc_cache.h.

Referenced by tbci_s_allocator().

◆ NEW

◆ NO_SINGLETON_PUBLIC

#define NO_SINGLETON_PUBLIC

Definition at line 89 of file malloc_cache.h.

◆ REALLOC

#define REALLOC ( v,
os,
t,
s )
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 LIKELY(expr)
branch prediction note that we sometimes on purpose mark the unlikely possibility likely and vice ver...
Definition basics.h:100
#define MIN(a, b)
Definition basics.h:655
#define NEW(t, s)

Definition at line 636 of file malloc_cache.h.

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

◆ SMP_LOCK

#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().

◆ SMP_UNLOCK

#define SMP_UNLOCK

◆ SPECIALIZE_MEMALLOC

#define SPECIALIZE_MEMALLOC ( TYPE)

◆ SPECIALIZE_MEMALLOC2

#define SPECIALIZE_MEMALLOC2 ( TYPE,
SHTP )
Value:
template <> \
{ /* the one and only */ \
static tbci_memalloc<TYPE > _tbci_s_alloc_##SHTP; \
return _tbci_s_alloc_##SHTP; \
}

Definition at line 587 of file malloc_cache.h.

Referenced by tbci_memalloc< signed char >::alloc(), tbci_memalloc< unsigned char >::alloc(), tbci_memalloc< unsigned long >::alloc(), and tbci_memalloc< unsigned short >::alloc().

◆ SPECIALIZE_MEMALLOC_CLASS

#define SPECIALIZE_MEMALLOC_CLASS ( TYPE)
Value:
template <> \
class tbci_memalloc<TYPE > { \
typedef TYPE T; \
protected: \
NO_SINGLETON_PUBLIC \
tbci_memalloc() {} \
~tbci_memalloc() {} \
public: \
friend tbci_memalloc<T >& tbci_s_allocator FGD2 (DUMMY1); \
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
#define FGD2
For specializations of the memory allocator:
T * alloc(const unsigned long)
void dealloc(const T *const, const unsigned long)

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

Definition at line 549 of file malloc_cache.h.

◆ SSE_VARS_MAY_BE_UNALIGNED

#define SSE_VARS_MAY_BE_UNALIGNED

Definition at line 133 of file malloc_cache.h.

◆ TBCI_MALLOC_ALIGN_FACT

#define TBCI_MALLOC_ALIGN_FACT   1

Definition at line 126 of file malloc_cache.h.

◆ TBCI_MALLOC_LIMIT

#define TBCI_MALLOC_LIMIT   8192

◆ TBCI_MALLOC_LOCK

#define TBCI_MALLOC_LOCK   1

Definition at line 37 of file malloc_cache.h.

◆ TBCI_MALLOC_POOLSZ

◆ TBCI_SIMD_ALIGN

#define TBCI_SIMD_ALIGN   16

Definition at line 122 of file malloc_cache.h.

◆ TBCIDELETE

◆ TBCIDELETE_RO

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

Definition at line 635 of file malloc_cache.h.

Typedef Documentation

◆ charptr

typedef char* charptr

Definition at line 617 of file malloc_cache.h.

◆ doubleptr

typedef double* doubleptr

Definition at line 604 of file malloc_cache.h.

◆ floatptr

typedef float* floatptr

Definition at line 607 of file malloc_cache.h.

◆ intptr

typedef int* intptr

Definition at line 613 of file malloc_cache.h.

◆ longptr

typedef long* longptr

Definition at line 622 of file malloc_cache.h.

◆ ucharptr

typedef unsigned char* ucharptr

Definition at line 619 of file malloc_cache.h.

◆ uintptr

typedef unsigned int* uintptr

Definition at line 610 of file malloc_cache.h.

◆ ulongptr

typedef unsigned long* ulongptr

Definition at line 625 of file malloc_cache.h.

◆ voidptr

typedef void* voidptr

Definition at line 628 of file malloc_cache.h.

Function Documentation

◆ ALIGN()

template<typename T>
struct tbci_memalloc_cache_tls ALIGN ( 64 )

References DUMMY1.

◆ tbci_s_allocator()

template<typename T>
tbci_memalloc< T > & tbci_s_allocator ( )
inline

Definition at line 567 of file malloc_cache.h.

References DUMMY1, and MINLINE.

Referenced by tbci_memalloc< T >::FGDT.

◆ tbci_s_allocator< charptr >()

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

Definition at line 617 of file malloc_cache.h.

◆ tbci_s_allocator< double >()

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

Definition at line 567 of file malloc_cache.h.

◆ tbci_s_allocator< doubleptr >()

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

Definition at line 604 of file malloc_cache.h.

◆ tbci_s_allocator< float >()

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

Definition at line 604 of file malloc_cache.h.

◆ tbci_s_allocator< floatptr >()

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

Definition at line 607 of file malloc_cache.h.

◆ tbci_s_allocator< int >()

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

Definition at line 610 of file malloc_cache.h.

◆ tbci_s_allocator< intptr >()

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

Definition at line 613 of file malloc_cache.h.

◆ tbci_s_allocator< long >()

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

Definition at line 619 of file malloc_cache.h.

◆ tbci_s_allocator< longptr >()

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

Definition at line 622 of file malloc_cache.h.

◆ tbci_s_allocator< signed char >()

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

Definition at line 613 of file malloc_cache.h.

◆ tbci_s_allocator< ucharptr >()

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

Definition at line 619 of file malloc_cache.h.

◆ tbci_s_allocator< uintptr >()

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

Definition at line 610 of file malloc_cache.h.

◆ tbci_s_allocator< ulongptr >()

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

Definition at line 625 of file malloc_cache.h.

◆ tbci_s_allocator< unsigned >()

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

Definition at line 607 of file malloc_cache.h.

◆ tbci_s_allocator< unsigned char >()

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

Definition at line 613 of file malloc_cache.h.

◆ tbci_s_allocator< unsigned long >()

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

Definition at line 622 of file malloc_cache.h.

◆ tbci_s_allocator< unsigned short >()

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

Definition at line 625 of file malloc_cache.h.

◆ tbci_s_allocator< voidptr >()

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

Definition at line 628 of file malloc_cache.h.

Variable Documentation

◆ ALIGN

struct tbci_memalloc_cache ALIGN

◆ all

volatile unsigned int all

Definition at line 4 of file malloc_cache.h.

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

◆ free_idx

unsigned char free_idx

Definition at line 2 of file malloc_cache.h.

◆ free_pt

const T* free_pt[8]

Definition at line 0 of file malloc_cache.h.

◆ free_sz

unsigned short free_sz[8]

Definition at line 1 of file malloc_cache.h.

◆ hit

volatile unsigned int hit

Definition at line 4 of file malloc_cache.h.

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

◆ miss

volatile unsigned int miss

Definition at line 3 of file malloc_cache.h.

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

◆ srch

volatile unsigned int srch

Definition at line 4 of file malloc_cache.h.

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