Line | |
---|
1 | /* -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef _BINHEAP_H_ |
---|
4 | #define _BINHEAP_H_ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | |
---|
8 | |
---|
9 | template <int Bins[], int NumBins, class Super> |
---|
10 | class BinHeap { |
---|
11 | public: |
---|
12 | |
---|
13 | inline void * malloc (size_t sz) { |
---|
14 | // Find bin. |
---|
15 | int bin = findBin (sz); |
---|
16 | void * ptr = myHeaps[bin].malloc (sz); |
---|
17 | return ptr; |
---|
18 | } |
---|
19 | |
---|
20 | inline void free (void * ptr) { |
---|
21 | size_t sz = Super::size (ptr); |
---|
22 | int bin = findBin (sz); |
---|
23 | myHeaps[bin].free (ptr); |
---|
24 | } |
---|
25 | |
---|
26 | private: |
---|
27 | |
---|
28 | inline int findBin (size_t sz) { |
---|
29 | int i; |
---|
30 | for (i = 0; i < NumBins; i++) { |
---|
31 | if (Bins[i] >= sz) { |
---|
32 | break; |
---|
33 | } |
---|
34 | } |
---|
35 | return i; |
---|
36 | } |
---|
37 | |
---|
38 | Super myHeaps[NumBins + 1]; |
---|
39 | |
---|
40 | }; |
---|
41 | |
---|
42 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.