/
dumb_alloc.cc
63 lines (50 loc) · 1.32 KB
/
dumb_alloc.cc
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
// dumb_alloc.cc: Test this C++ mechanism as a lower bound on performance.
#include "dumb_alloc.h"
#include <stdio.h>
// 100 MiB of memory
char kMem[100 << 20];
int gMemPos = 0;
int gNumNew = 0;
int gNumDelete = 0;
// This global interface is silly ...
void* operator new(size_t size) {
char* p = &(kMem[gMemPos]);
//fprintf(stderr, "\tnew(%d) = %p\n", size, p);
//printf("__ %d\n", size);
gMemPos += size;
++gNumNew;
return p;
}
// noexcept fixes Clang warning
void operator delete(void* p) noexcept {
//fprintf(stderr, "\tdelete %p\n", p);
++gNumDelete;
}
char kMem2[100 << 20];
int gMemPos2 = 0;
int gNumMalloc = 0;
int gNumFree = 0;
void* dumb_malloc(size_t size) noexcept {
char* p = &(kMem2[gMemPos2]);
//fprintf(stderr, "malloc %d\n", size);
gMemPos2 += size;
++gNumMalloc;
return p;
}
void dumb_free(void* p) noexcept {
//fprintf(stderr, "free\n");
++gNumFree;
}
namespace dumb_alloc {
void Summarize() {
fprintf(stderr, "\n");
fprintf(stderr, "dumb_alloc:\n");
fprintf(stderr, "\tgNumNew = %d\n", gNumNew);
fprintf(stderr, "\tgNumDelete = %d\n", gNumDelete);
fprintf(stderr, "\tgMemPos = %d\n", gMemPos);
fprintf(stderr, "\n");
fprintf(stderr, "\tgNumMalloc = %d\n", gNumMalloc);
fprintf(stderr, "\tgNumFree = %d\n", gNumFree);
fprintf(stderr, "\tgMemPos2 = %d\n", gMemPos2);
}
};