Back to index

texmacs  1.0.7.15
Functions | Variables
fast_alloc.cpp File Reference
#include "fast_alloc.hpp"

Go to the source code of this file.

Functions

int mem_used ()
void * safe_malloc (register size_t sz)
void * enlarge_malloc (register size_t sz)
void * fast_alloc (register size_t sz)
void fast_free (register void *ptr, register size_t sz)
void * fast_new (register size_t s)
void fast_delete (register void *ptr)
void * fast_alloc_mw (register size_t s)
void fast_free_mw (register void *ptr, register size_t s)
int compute_free (void *ptr)
void mem_info ()

Variables

char alloc_table [MAX_FAST]
char * alloc_mem = NULL
size_t alloc_remains = 0
int allocated = 0
int fast_chunks = 0
int large_uses = 0
int MEM_DEBUG = 0

Function Documentation

int compute_free ( void *  ptr)

Definition at line 197 of file fast_alloc.cpp.

                         {
  int i=-1;
  while (ptr!=NULL) {
    i++;
    ptr= ind (ptr);
  }
  return i;
}

Here is the caller graph for this function:

void* enlarge_malloc ( register size_t  sz)

Definition at line 43 of file fast_alloc.cpp.

                                    {
  if (alloc_remains<sz) {
    alloc_mem    = (char *) safe_malloc (BLOCK_SIZE);
    alloc_remains= BLOCK_SIZE;
    fast_chunks++;
  }
  register void* ptr= alloc_mem;
  alloc_mem    += sz;
  alloc_remains-= sz;
  return ptr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* fast_alloc ( register size_t  sz)

Definition at line 56 of file fast_alloc.cpp.

                                {
  sz= (sz+WORD_LENGTH_INC)&WORD_MASK;
  if (sz<MAX_FAST) {
    register void *ptr= alloc_ptr (sz);
    if (ptr==NULL) return enlarge_malloc (sz);
    alloc_ptr (sz)= ind (ptr);
    return ptr;
  }
  else {
    if (MEM_DEBUG>=3) cout << "Big alloc of " << sz << " bytes\n";
    if (MEM_DEBUG>=3) cout << "Memory used: " << mem_used () << " bytes\n";
    large_uses += sz;
    return safe_malloc (sz);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* fast_alloc_mw ( register size_t  s)

Definition at line 171 of file fast_alloc.cpp.

{
  if (s<MAX_FAST) {
    register void *ptr= alloc_ptr(s);
    if (ptr==NULL) return enlarge_malloc (s);
    alloc_ptr(s)= ind(ptr);
    return ptr;
  }
  else return safe_malloc (s);
}

Here is the call graph for this function:

void fast_delete ( register void *  ptr)

Definition at line 150 of file fast_alloc.cpp.

                                 {
  ptr= (void*) (((char*) ptr)- WORD_LENGTH);
  register size_t s= *((size_t *) ptr);
  if (s<MAX_FAST) {
    ind(ptr)    = alloc_ptr(s);
    alloc_ptr(s)= ptr;
  }
  else {
    if (MEM_DEBUG>=3) cout << "Big free of " << s << " bytes\n";
    //if ((((int) ptr) & 15) != 0) cout << "Unaligned delete " << ptr << "\n";
    free (ptr);
    large_uses -= s;
    if (MEM_DEBUG>=3) cout << "Memory used: " << mem_used () << " bytes\n";
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void fast_free ( register void *  ptr,
register size_t  sz 
)

Definition at line 73 of file fast_alloc.cpp.

                                                   {
  sz=(sz+WORD_LENGTH_INC)&WORD_MASK;
  if (sz<MAX_FAST) {
    ind (ptr)     = alloc_ptr (sz);
    alloc_ptr (sz)= ptr;
  }
  else {
    if (MEM_DEBUG>=3) cout << "Big free of " << sz << " bytes\n";
    large_uses -= sz;    
    free (ptr);
    if (MEM_DEBUG>=3) cout << "Memory used: " << mem_used () << " bytes\n";
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void fast_free_mw ( register void *  ptr,
register size_t  s 
)

Definition at line 183 of file fast_alloc.cpp.

{
  if (s<MAX_FAST) {
    ind(ptr)    = alloc_ptr(s);
    alloc_ptr(s)= ptr;
  }
  else free (ptr);
}
void* fast_new ( register size_t  s)

Definition at line 130 of file fast_alloc.cpp.

                             {
  register void* ptr;
  s= (s+ WORD_LENGTH+ WORD_LENGTH_INC)&WORD_MASK;
  if (s<MAX_FAST) {
    ptr= alloc_ptr(s);
    if (ptr==NULL) ptr= enlarge_malloc (s);
    else alloc_ptr(s)= ind(ptr);
  }
  else {
    if (MEM_DEBUG>=3) cout << "Big alloc of " << s << " bytes\n";
    if (MEM_DEBUG>=3) cout << "Memory used: " << mem_used () << " bytes\n";
    ptr= safe_malloc (s);
    //if ((((int) ptr) & 15) != 0) cout << "Unaligned new " << ptr << "\n";
    large_uses += s;
  }
  *((size_t *) ptr)=s;
  return (void*) (((char*) ptr)+ WORD_LENGTH);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void mem_info ( )

Definition at line 218 of file fast_alloc.cpp.

            {
  cout << "\n---------------- memory statistics ----------------\n";
  int free_bytes= alloc_remains;
  int chunks_use= BLOCK_SIZE*fast_chunks;
  int i;
  for (i=WORD_LENGTH; i<MAX_FAST; i+=WORD_LENGTH)
    free_bytes += i*compute_free (alloc_table+i);
  int small_uses= chunks_use- free_bytes;
  int total_uses= small_uses+ large_uses;
  // cout << "Fast chunks   : " << chunks_use << " bytes\n";
  // cout << "Free on chunks: " << alloc_remains << " bytes\n";
  cout << "User          : " << total_uses << " bytes\n";
  cout << "Allocator     : " << chunks_use+ large_uses << " bytes\n";
  cout << "Small mallocs : "
       << ((100*((float) small_uses))/((float) total_uses)) << "%\n";
}

Here is the call graph for this function:

Here is the caller graph for this function:

int mem_used ( )

Definition at line 207 of file fast_alloc.cpp.

            {
  int free_bytes= alloc_remains;
  int chunks_use= BLOCK_SIZE*fast_chunks;
  int i;
  for (i=WORD_LENGTH; i<MAX_FAST; i+=WORD_LENGTH)
    free_bytes += i*compute_free (alloc_table+i);
  int small_uses= chunks_use- free_bytes;
  return small_uses+ large_uses;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* safe_malloc ( register size_t  sz)

Definition at line 33 of file fast_alloc.cpp.

                                 {
  void* ptr= malloc (sz);
  if (ptr==NULL) {
    cerr << "Fatal error: out of memory\n";
    abort ();
  }
  return ptr;
}

Here is the caller graph for this function:


Variable Documentation

char* alloc_mem = NULL

Definition at line 20 of file fast_alloc.cpp.

size_t alloc_remains = 0

Definition at line 21 of file fast_alloc.cpp.

char alloc_table[MAX_FAST]

Definition at line 19 of file fast_alloc.cpp.

int allocated = 0

Definition at line 22 of file fast_alloc.cpp.

int fast_chunks = 0

Definition at line 23 of file fast_alloc.cpp.

int large_uses = 0

Definition at line 24 of file fast_alloc.cpp.

int MEM_DEBUG = 0

Definition at line 25 of file fast_alloc.cpp.