1 //===-- jitcs_tmpalloc.h - Never-free allocator class -----------*- C++ -*-===//
3 // TempAllocator is a never-free allocator. All allocated data is written into
4 // a list of memory blocks. All data is freed en bloc. No destructors are
7 //===----------------------------------------------------------------------===//
9 #ifndef _JITCS_TMPALLOC_H_
10 #define _JITCS_TMPALLOC_H_
12 #include "jitcs_adt_ref.h"
13 #include "jitcs_adt_refcounter.h"
14 #include "jitcs_adt_slice.h"
15 #include "jitcs_base.h"
23 enum { MINIMUM_BLOCK_SIZE
= 4096 };
32 return reinterpret_cast<T
*>(_alloc(sizeof(T
), alignof(T
)));
35 Slice
<T
> allocTypedArray(size_t sz
) {
36 return Slice
<T
>(reinterpret_cast<T
*>(_alloc(sizeof(T
) * sz
, alignof(T
))),
41 // called from RefCounter
42 friend class RefCounter
<TempAllocator
>;
43 void _incRef() { ++_refCnt
; }
44 void _decRef() { if (!--_refCnt
) _clean(true); }
47 u8
* _alloc(size_t sz
, size_t alignment
);
48 // drop all current allocations to this allocator, and start from scratch
49 void _clean(bool keepbest
);
52 typedef std::list
< std::vector
<u8
> > BlockList
;
54 BlockList::iterator _curblock
;
57 size_t _allocatedSpace
;
61 // allocator helper object to allocate quickly from a TempAllocator
63 class StreamAllocator
{
65 StreamAllocator(TempAllocator
& a
, size_t n
)
70 _JITCS_ALWAYS_CHECK_(_allocN
> 0);
74 inline Ref
<T
> alloc() {
82 _ptr
= _allocator
.allocTypedArray
<T
>(_allocN
).ptr();
83 _end
= _ptr
+ _allocN
;
88 TempAllocator
&_allocator
;
93 } // end of namespace jitcs