File: BigAlloc.h

package info (click to toggle)
snap-aligner 1.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,988 kB
  • sloc: cpp: 36,500; ansic: 5,239; python: 227; makefile: 85; sh: 28
file content (179 lines) | stat: -rw-r--r-- 4,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*++

Module Name:

    bigalloc.h

Abstract:

    Headers for an allocator that uses big pages where appropriate and possible.

Authors:

    Bill Bolosky, August, 2011

Environment:

    User mode service.

Revision History:

--*/

#pragma once

inline unsigned RoundUpToPageSize(unsigned size)
{
    const unsigned pageSize = 4096;
    return ((size + pageSize - 1) / pageSize) * pageSize;
}

//#define PROFILE_BIGALLOC

#ifdef PROFILE_BIGALLOC

#define BigAlloc(s) BigAllocProfile((s), NULL, __FUNCTION__)
#define BigAlloc2(s,p) BigAllocProfile((s), (p), __FUNCTION__)
#define BigReserve(s) BigReserveProfile((s), NULL, NULL, __FUNCTION__)
#define BigCommit(p, s) BigCommitProfile((p), (s), __FUNCTION__)

void *BigAllocProfile(
        size_t      sizeToAllocate,
        size_t      *sizeAllocated = NULL,
        const char* caller = NULL);

void *BigReserveProfile(
    size_t      sizeToReserve,
    size_t      *sizeReserved = NULL,
    size_t      *pageSize = NULL,
    const char* caller = NULL);

bool BigCommitProfile(
    void        *memoryToCommit,
    size_t      sizeToCommit,
    const char* caller = NULL);

#else

void *BigAlloc(
        size_t      sizeToAllocate,
        size_t      *sizeAllocated = NULL);
#define BigAlloc2(s,p) BigAlloc((s), (p))

void *BigReserve(
    size_t      sizeToReserve,
    size_t      *sizeReserved = NULL,
    size_t      *pageSize = NULL);

bool BigCommit(
    void        *memoryToCommit,
    size_t      sizeToCommit);

#endif

void PrintBigAllocProfile();

void BigDealloc(void *memory);

//
// This class is used to allocate a group of objects all onto a single set of big pages.  It requires knowing
// the amount of memory to be allocated when it's created.  It does not support deleting memory other than
// all at once.
//
class BigAllocator {
public:
    BigAllocator(size_t i_maxMemory, size_t i_allocationGranularity = 8);
    ~BigAllocator();

    virtual void *allocate(size_t amountToAllocate);

    size_t amountAllocated();

#ifdef _DEBUG
    void checkCanaries();
#else  // DEBUG
    void checkCanaries() {}
#endif  // DEBUG
private:

    char    *basePointer;
    char    *allocPointer;
    size_t  maxMemory;
    size_t  allocationGranularity;

#ifdef _DEBUG
    //
    // Stick a canary between each allocation and 
    unsigned    nCanaries;
    static const unsigned maxCanaries = 100;
    static const unsigned canaryValue = 0xca4a71e5;
    unsigned    *canaries[maxCanaries];
#endif  // DEBUG
};

//
// An allocator that doesn't actually allocate, it just counts how much it would allocate.  The idea is that
// you can write allocations in a fairly normal looking way, call them with this to see how much would be
// allocated, then create a real BigAllocator with that amount of memory.  That way, you don't need to
// keep in sync the actual allocation and the code that knows how much memory will be needed.
//
class CountingBigAllocator : public BigAllocator
{
public:
    CountingBigAllocator(size_t i_allocationGranularity = 8) :size(0), allocations(NULL), BigAllocator(0), allocationGranularity(i_allocationGranularity) {}
    ~CountingBigAllocator();

    virtual void *allocate(size_t amountToAllocate);
    virtual void assertAllMemoryUsed() {}
    size_t getMemoryUsed() {return size;}

private:
    size_t  size;
    size_t  allocationGranularity;

    struct Allocation {
        void *ptr;
        Allocation *next;
    } *allocations;
};

extern bool BigAllocUseHugePages;


// trivial per-thread heap for use in zalloc
struct ThreadHeap
{
    char* start;
    char* end;
    char* next;
    ThreadHeap(size_t bytes)
    {
        next = start = (char*) BigAlloc(bytes);
        end = start + bytes;
    }
    void* alloc(size_t bytes)
    {
        if (next + bytes <= end) {
            void* result = next;
            next += bytes;
            return result;
        }
        return NULL;
    }
    bool free(void* p)
    {
        return (char*)p >= start && (char*) p <= end;
    }
    void reset()
    {
        next = start;
    }
    ~ThreadHeap()
    {
        BigDealloc(start);
    }
};

void* zalloc(void* opaque, unsigned items, unsigned size);

void zfree(void* opaque, void* p);