Changeset 1758 for trunk


Ignore:
Timestamp:
Dec 8, 2011, 8:26:30 PM (8 years ago)
Author:
ksherdy
Message:

Added aligned and unaligned allocation methods.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/allocator.hpp

    r1750 r1758  
    1919public:
    2020        void * allocate ();
     21        void * allocate_aligned ();
    2122        void destroy ();
    2223protected:
     
    2526};
    2627
    27 /* Aligned Memory Allocator - A fast memory allocator. Trade memory for speed.
     28/* Fast Memory Pool Allocator - Trade memory for speed.
    2829 *
    2930 * Allocates BASE_SIZE stack bytes.
     
    3435 */
    3536template<uint32_t BASE_SIZE>
    36 class aligned_pool_allocator : public pool_allocator {
     37class fast_pool_allocator : public pool_allocator {
    3738
    3839public:
    39         aligned_pool_allocator(const uint8_t align=64, const uint8_t exp=2): ALIGNMENT(align), EXPANSION_FACTOR(exp) {
     40        fast_pool_allocator(const uint8_t align=64, const uint8_t exp=2): ALIGNMENT(align), EXPANSION_FACTOR(exp) {
    4041                tail = &head;
    4142                tail->segment = stack_segment;
     
    4546        }
    4647
    47         ~aligned_pool_allocator() {}
     48        ~fast_pool_allocator() {}
    4849
    4950        void * allocate(uint64_t n) {
     51
     52                if(n > available) {
     53                        segment_size = next_segment_size(n);
     54
     55                        node * next = (node *) malloc(sizeof(node));
     56                        if (next == NULL) {
     57                                cerr << "Out of Memory" << endl;
     58                                abort();
     59                        }
     60
     61                        next->segment = (uint8_t *) malloc(segment_size);
     62                        if ((next->segment) == NULL) {
     63                                cerr << "Out of Memory" << endl;
     64                                abort();
     65                        }
     66
     67                        available = segment_size;
     68
     69                        next->next = NULL;
     70                        tail->next = next;
     71                        tail = next;
     72                        next = NULL;
     73                }
     74
     75                uint32_t i = segment_size - available;
     76                uint64_t address = reinterpret_cast<uint64_t>(&(tail->segment[i]));
     77
     78                return (void *) (address);
     79        }
     80
     81        void * allocate_aligned(uint64_t n) {
    5082                uint64_t n_padded = (n+ALIGNMENT-1);
    5183
     
    74106
    75107                uint32_t i = segment_size - available;
    76                 uint64_t address = reinterpret_cast<uint64_t>(&(tail->segment[i]));
     108                uint64_t address = reinterpret_cast<uint64_t>(&(tail->segment[i]));
    77109                uint64_t padding = ((address % ALIGNMENT) == 0) ? 0 :  ALIGNMENT - (address % ALIGNMENT);
    78110                available -= (padding + n);
Note: See TracChangeset for help on using the changeset viewer.