-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmapcache.h
More file actions
61 lines (51 loc) · 1.27 KB
/
Copy pathmapcache.h
File metadata and controls
61 lines (51 loc) · 1.27 KB
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
/*
* Copyright (C) 2019 Ricardo Leite. All rights reserved.
* Licenced under the MIT licence. See COPYING file in the project root for
* details.
*/
#ifndef __MAPCACHE_H_
#define __MAPCACHE_H_
#include "log.h"
#include "pages.h"
#include "size_classes.h"
#include <sys/mman.h>
#define MAPCACHE_SIZE 64
struct MapCacheBin {
private:
char* _block = nullptr;
uint32_t _blockNum = 0;
public:
// Map MAPCACHE_SIZE superblocks in one go and then consume 1 by 1
char* Alloc();
// Unmap superblocks immediately in SB_SIZE chunks
void Free(char*);
// Used for thread termination to unmap what remains
void Flush();
};
inline char* MapCacheBin::Alloc()
{
if (_blockNum == 0) {
_block = (char*)PageAlloc(SB_SIZE * MAPCACHE_SIZE);
if (_block == nullptr) {
return nullptr;
}
_blockNum = MAPCACHE_SIZE;
}
char* ret = _block;
_block += SB_SIZE;
_blockNum--;
return ret;
}
inline void MapCacheBin::Free(char* block)
{
PageFree(block, SB_SIZE);
}
inline void MapCacheBin::Flush()
{
if (_blockNum > 0) {
PageFree(_block, SB_SIZE * _blockNum);
}
}
// use tls init exec model
extern __thread MapCacheBin sMapCache LFMALLOC_TLS_INIT_EXEC LFMALLOC_CACHE_ALIGNED;
#endif // __MAPCACHE_H_