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 <malloc.h>
#include "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.

Namespaces

namespace  TBCI

Classes

class  TBCI::tbci_memalloc< T >
struct  TBCI::tbci_memalloc_cache_tls< T >
struct  TBCI::tbci_memalloc_cache< T >
 For specializations of the memory allocator:
  • Freed (dealloced) memory blocks are not actually freed, but kept in a little cache, so an allocation to a block of the same size shortly after will succeed without a call to new/malloc/memalign.
More...
class  TBCI::tbci_memalloc< double >
class  TBCI::tbci_memalloc< doubleptr >
class  TBCI::tbci_memalloc< float >
class  TBCI::tbci_memalloc< floatptr >
class  TBCI::tbci_memalloc< unsigned >
class  TBCI::tbci_memalloc< uintptr >
class  TBCI::tbci_memalloc< int >
class  TBCI::tbci_memalloc< intptr >
class  TBCI::tbci_memalloc< unsigned char >
class  TBCI::tbci_memalloc< signed char >
class  TBCI::tbci_memalloc< charptr >
class  TBCI::tbci_memalloc< ucharptr >
class  TBCI::tbci_memalloc< long >
class  TBCI::tbci_memalloc< longptr >
class  TBCI::tbci_memalloc< unsigned long >
class  TBCI::tbci_memalloc< ulongptr >
class  TBCI::tbci_memalloc< unsigned short >
class  TBCI::tbci_memalloc< voidptr >

Defines

#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.
#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)   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 * TBCI::doubleptr
typedef float * TBCI::floatptr
typedef unsigned int * TBCI::uintptr
typedef int * TBCI::intptr
typedef char * TBCI::charptr
typedef unsigned char * TBCI::ucharptr
typedef long * TBCI::longptr
typedef unsigned long * TBCI::ulongptr
typedef void * TBCI::voidptr

Functions

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

Variables

struct
TBCI::tbci_memalloc_cache_tls 
TBCI::aligned


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@garloff.de>
Date:
2002-07-31 License: GNU LGPL
Version:
Id
malloc_cache.h,v 1.1.2.59 2009/04/01 11:36:45 garloff Exp

Definition in file malloc_cache.h.


Define Documentation

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

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

Definition at line 124 of file malloc_cache.h.

Referenced by TBCI::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::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::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 (  ) 

Definition at line 76 of file malloc_cache.h.

#define FGD2   FGDT

Definition at line 77 of file malloc_cache.h.

#define INC (  )     ++x;

Definition at line 137 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)

Definition at line 630 of file malloc_cache.h.

Referenced by TBCI::BdMatrix< T >::adddiag(), TBCI::BVector< T >::append(), TBCI::BVector< T >::resize(), and TBCI::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 236 of file malloc_cache.h.

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

#define SMP_UNLOCK

#define SPECIALIZE_MEMALLOC ( TYPE   ) 

Value:

SPECIALIZE_MEMALLOC_CLASS(TYPE)                 \
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;            \
}

Definition at line 572 of file malloc_cache.h.

#define SPECIALIZE_MEMALLOC2 ( TYPE,
SHTP   ) 

Value:

SPECIALIZE_MEMALLOC_CLASS(TYPE)                 \
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;            \
}

Definition at line 581 of file malloc_cache.h.

#define SPECIALIZE_MEMALLOC_CLASS ( TYPE   ) 

Value:

template <>                                                             \
class tbci_memalloc<TYPE > {                    \
        typedef TYPE T;                                         \
    protected:                                                  \
        tbci_memalloc_cache<T > m_cache;        \
    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); }            \
};
Allocator specialization macro, subsequently used to specialize the allocator for the elementary types.

Definition at line 543 of file malloc_cache.h.

#define SSE_VARS_MAY_BE_UNALIGNED

Definition at line 127 of file malloc_cache.h.

#define TBCI_MALLOC_ALIGN_FACT   1

Definition at line 120 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 117 of file malloc_cache.h.

Referenced by do_mat_tsv_mult(), and do_mat_vec_mult().

#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 629 of file malloc_cache.h.


Generated on Wed Nov 20 09:28:14 2013 for TBCI Numerical high perf. C++ Library by  doxygen 1.5.6