- Timestamp:
- 08/21/11 12:30:35 (13 years ago)
- Location:
- ps/trunk/source/lib/allocators
- Files:
-
- 2 edited
-
bucket.cpp (modified) (2 diffs)
-
bucket.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
ps/trunk/source/lib/allocators/bucket.cpp
r9410 r10049 34 34 // power-of-2 isn't required; value is arbitrary. 35 35 const size_t bucketSize = 4000; 36 37 38 template<typename T, class Allocator = Allocator_Heap, size_t bucketSize = pageSize> 39 class Buckets 40 { 41 public: 42 // (must round up because freelist stores pointers inside objects) 43 static const size_t objectSize = ROUND_UP(sizeof(T), sizeof(intptr_t)); 44 45 Buckets(size_t maxObjects) 46 : storage(maxObjects*objectSize) 47 { 48 } 49 50 size_t RemainingObjects() 51 { 52 return (storage.MaxCapacity() - end) / objectSize; 53 } 54 55 T* Allocate() 56 { 57 void* p = mem_freelist_Detach(freelist); 58 if(p) 59 { 60 ASSERT(Contains(p)); 61 return (T*)p; 62 } 63 64 return (T*)StorageAppend(storage, end, objectSize); 65 } 66 67 void Deallocate(T* p) 68 { 69 ASSERT(Contains(p)); 70 mem_freelist_AddToFront(freelist, p); 71 } 72 73 private: 74 Storage storage; 75 size_t end; 76 void* freelist; 77 }; 78 36 79 37 80 … … 132 175 return ret; 133 176 } 134 135 136 void bucket_free(Bucket* b, void* el)137 {138 if(b->el_size == 0)139 {140 DEBUG_WARN_ERR(ERR::LOGIC); // cannot free variable-size items141 return;142 }143 144 mem_freelist_AddToFront(b->freelist, el);145 146 // note: checking if <el> was actually allocated from <b> is difficult:147 // it may not be in the currently open bucket, so we'd have to148 // iterate over the list - too much work.149 } -
ps/trunk/source/lib/allocators/bucket.h
r9410 r10049 100 100 LIB_API void* bucket_fast_alloc(Bucket* b); 101 101 102 /**103 * make an entry available for reuse in the given Bucket.104 *105 * this is not allowed if created for variable-size elements.106 * rationale: avoids having to pass el_size here and compare with size when107 * allocating; also prevents fragmentation and leaking memory.108 *109 * @param b Bucket*110 * @param el entry allocated via bucket_alloc.111 **/112 LIB_API void bucket_free(Bucket* b, void* el);113 114 102 #endif // #ifndef INCLUDED_ALLOCATORS_BUCKET
Note:
See TracChangeset
for help on using the changeset viewer.
