Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

__default_alloc_template Class Template Reference

#include <stl_alloc.h>

List of all members.

Static Public Methods

void * allocate (size_t n)
void deallocate (void *p, size_t n)
void * reallocate (void *p, size_t old_sz, size_t new_sz)

Private Types

enum  { __ALIGN = 8 }
enum  { __MAX_BYTES = 128 }
enum  { __NFREELISTS = __MAX_BYTES/__ALIGN }

Static Private Methods

size_t ROUND_UP (size_t bytes)
size_t FREELIST_INDEX (size_t bytes)
void * refill (size_t n)
char * chunk_alloc (size_t size, int &nobjs)

Private Attributes

__PRIVATE __pad0__: union obj { union obj * free_list_link
char client_data [1]

Static Private Attributes

obj *__VOLATILE free_list [__NFREELISTS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }
char * start_free = 0
char * end_free = 0
size_t heap_size = 0

Friends

class lock

template<bool threads, int inst>
class __default_alloc_template< threads, inst >


Member Enumeration Documentation

template<bool threads, int inst>
anonymous enum [private]
 

Enumeration values:
__ALIGN 

Definition at line 320 of file stl_alloc.h.

00320 {__ALIGN = 8};

template<bool threads, int inst>
anonymous enum [private]
 

Enumeration values:
__MAX_BYTES 

Definition at line 321 of file stl_alloc.h.

00321 {__MAX_BYTES = 128};

template<bool threads, int inst>
anonymous enum [private]
 

Enumeration values:
__NFREELISTS 

Definition at line 322 of file stl_alloc.h.


Member Function Documentation

template<bool threads, int inst>
void* __default_alloc_template< threads, inst >::allocate size_t    n [inline, static]
 

Definition at line 390 of file stl_alloc.h.

References __MAX_BYTES, __VOLATILE, __malloc_alloc_template::allocate, free_list, FREELIST_INDEX, lock, refill, and ROUND_UP.

Referenced by reallocate.

00391   {
00392     obj * __VOLATILE * my_free_list;
00393     obj * __RESTRICT result;
00394 
00395     if (n > (size_t) __MAX_BYTES) {
00396         return(malloc_alloc::allocate(n));
00397     }
00398     my_free_list = free_list + FREELIST_INDEX(n);
00399     // Acquire the lock here with a constructor call.
00400     // This ensures that it is released in exit or during stack
00401     // unwinding.
00402 #       ifndef _NOTHREADS
00403         /*REFERENCED*/
00404         lock lock_instance;
00405 #       endif
00406     result = *my_free_list;
00407     if (result == 0) {
00408         void *r = refill(ROUND_UP(n));
00409         return r;
00410     }
00411     *my_free_list = result -> free_list_link;
00412     return (result);
00413   };

template<bool threads, int inst>
char * __default_alloc_template< threads, inst >::chunk_alloc size_t    size,
int &    nobjs
[static, private]
 

Definition at line 451 of file stl_alloc.h.

References __ALIGN, __MAX_BYTES, __VOLATILE, __malloc_alloc_template::allocate, end_free, free_list, FREELIST_INDEX, heap_size, ROUND_UP, and start_free.

Referenced by refill.

00452 {
00453     char * result;
00454     size_t total_bytes = size * nobjs;
00455     size_t bytes_left = end_free - start_free;
00456 
00457     if (bytes_left >= total_bytes) {
00458         result = start_free;
00459         start_free += total_bytes;
00460         return(result);
00461     } else if (bytes_left >= size) {
00462         nobjs = bytes_left/size;
00463         total_bytes = size * nobjs;
00464         result = start_free;
00465         start_free += total_bytes;
00466         return(result);
00467     } else {
00468         size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4);
00469         // Try to make use of the left-over piece.
00470         if (bytes_left > 0) {
00471             obj * __VOLATILE * my_free_list =
00472                         free_list + FREELIST_INDEX(bytes_left);
00473 
00474             ((obj *)start_free) -> free_list_link = *my_free_list;
00475             *my_free_list = (obj *)start_free;
00476         }
00477         start_free = (char *)malloc(bytes_to_get);
00478         if (0 == start_free) {
00479             int i;
00480             obj * __VOLATILE * my_free_list, *p;
00481             // Try to make do with what we have.  That can't
00482             // hurt.  We do not try smaller requests, since that tends
00483             // to result in disaster on multi-process machines.
00484             for (i = size; i <= __MAX_BYTES; i += __ALIGN) {
00485                 my_free_list = free_list + FREELIST_INDEX(i);
00486                 p = *my_free_list;
00487                 if (0 != p) {
00488                     *my_free_list = p -> free_list_link;
00489                     start_free = (char *)p;
00490                     end_free = start_free + i;
00491                     return(chunk_alloc(size, nobjs));
00492                     // Any leftover piece will eventually make it to the
00493                     // right free list.
00494                 }
00495             }
00496             end_free = 0;       // In case of exception.
00497             start_free = (char *)malloc_alloc::allocate(bytes_to_get);
00498             // This should either throw an
00499             // exception or remedy the situation.  Thus we assume it
00500             // succeeded.
00501         }
00502         heap_size += bytes_to_get;
00503         end_free = start_free + bytes_to_get;
00504         return(chunk_alloc(size, nobjs));
00505     }
00506 }

template<bool threads, int inst>
void __default_alloc_template< threads, inst >::deallocate void *    p,
size_t    n
[inline, static]
 

Definition at line 416 of file stl_alloc.h.

References __MAX_BYTES, __VOLATILE, __malloc_alloc_template::deallocate, free_list, FREELIST_INDEX, and lock.

Referenced by reallocate.

00417   {
00418     obj *q = (obj *)p;
00419     obj * __VOLATILE * my_free_list;
00420 
00421     if (n > (size_t) __MAX_BYTES) {
00422         malloc_alloc::deallocate(p, n);
00423         return;
00424     }
00425     my_free_list = free_list + FREELIST_INDEX(n);
00426     // acquire lock
00427 #       ifndef _NOTHREADS
00428         /*REFERENCED*/
00429         lock lock_instance;
00430 #       endif /* _NOTHREADS */
00431     q -> free_list_link = *my_free_list;
00432     *my_free_list = q;
00433     // lock is released here
00434   }

template<bool threads, int inst>
size_t __default_alloc_template< threads, inst >::FREELIST_INDEX size_t    bytes [inline, static, private]
 

Definition at line 339 of file stl_alloc.h.

References __ALIGN.

Referenced by allocate, chunk_alloc, deallocate, and refill.

00339                                               {
00340         return (((bytes) + __ALIGN-1)/__ALIGN - 1);
00341   }

template<bool threads, int inst>
void * __default_alloc_template< threads, inst >::reallocate void *    p,
size_t    old_sz,
size_t    new_sz
[static]
 

Definition at line 543 of file stl_alloc.h.

References __MAX_BYTES, allocate, deallocate, and ROUND_UP.

00546 {
00547     void * result;
00548     size_t copy_sz;
00549 
00550     if (old_sz > (size_t) __MAX_BYTES && new_sz > (size_t) __MAX_BYTES) {
00551         return(realloc(p, new_sz));
00552     }
00553     if (ROUND_UP(old_sz) == ROUND_UP(new_sz)) return(p);
00554     result = allocate(new_sz);
00555     copy_sz = new_sz > old_sz? old_sz : new_sz;
00556     memcpy(result, p, copy_sz);
00557     deallocate(p, old_sz);
00558     return(result);
00559 }

template<bool threads, int inst>
void * __default_alloc_template< threads, inst >::refill size_t    n [static, private]
 

Definition at line 513 of file stl_alloc.h.

References __VOLATILE, chunk_alloc, free_list, and FREELIST_INDEX.

Referenced by allocate.

00514 {
00515     int nobjs = 20;
00516     char * chunk = chunk_alloc(n, nobjs);
00517     obj * __VOLATILE * my_free_list;
00518     obj * result;
00519     obj * current_obj, * next_obj;
00520     int i;
00521 
00522     if (1 == nobjs) return(chunk);
00523     my_free_list = free_list + FREELIST_INDEX(n);
00524 
00525     /* Build free list in chunk */
00526       result = (obj *)chunk;
00527       *my_free_list = next_obj = (obj *)(chunk + n);
00528       for (i = 1; ; i++) {
00529         current_obj = next_obj;
00530         next_obj = (obj *)((char *)next_obj + n);
00531         if (nobjs - 1 == i) {
00532             current_obj -> free_list_link = 0;
00533             break;
00534         } else {
00535             current_obj -> free_list_link = next_obj;
00536         }
00537       }
00538     return(result);
00539 }

template<bool threads, int inst>
size_t __default_alloc_template< threads, inst >::ROUND_UP size_t    bytes [inline, static, private]
 

Definition at line 324 of file stl_alloc.h.

References __ALIGN.

Referenced by allocate, chunk_alloc, and reallocate.

00324                                        {
00325         return (((bytes) + __ALIGN-1) & ~(__ALIGN - 1));
00326   }


Friends And Related Function Documentation

template<bool threads, int inst>
friend class lock [friend]
 

Definition at line 385 of file stl_alloc.h.

Referenced by allocate, and deallocate.


Member Data Documentation

template<bool threads, int inst>
__PRIVATE __default_alloc_template::__pad0__ [private]
 

Definition at line 329 of file stl_alloc.h.

template<bool threads, int inst>
char __default_alloc_template::client_data[1] [private]
 

Definition at line 330 of file stl_alloc.h.

template<bool threads, int inst>
char * __default_alloc_template< threads, inst >::end_free = 0 [static, private]
 

Definition at line 659 of file stl_alloc.h.

Referenced by chunk_alloc.

template<bool threads, int inst>
__default_alloc_template< threads, inst >::obj *__VOLATILE __default_alloc_template< threads, inst >::free_list = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, } [static, private]
 

Definition at line 672 of file stl_alloc.h.

Referenced by allocate, chunk_alloc, deallocate, and refill.

template<bool threads, int inst>
size_t __default_alloc_template< threads, inst >::heap_size = 0 [static, private]
 

Definition at line 662 of file stl_alloc.h.

Referenced by chunk_alloc.

template<bool threads, int inst>
char * __default_alloc_template< threads, inst >::start_free = 0 [static, private]
 

Definition at line 656 of file stl_alloc.h.

Referenced by chunk_alloc.


The documentation for this class was generated from the following file:
logo OpenMask

Documentation generated on Thu May 2 15:03:14 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001