The lighter "struct mutex" is in a private header, so we can't inline it in a public one, so just use pthread_mutex here. By adjusting padding, the class size stays the same (72 bytes on 32-bit, 96 bytes on 64-bit; confirmed via static_assert.) A quick benchmark running "new/delete BMessage" in a loop on 4 threads at once goes from 30-36 seconds before this commit to around 13-17 seconds, sometimes as low as 3 seconds, afterwards, so clearly this is a significant improvement. This also eliminates another BLocker allocated on application startup.
50 lines
911 B
C++
50 lines
911 B
C++
/*
|
|
* Copyright 2026, Haiku, Inc. All rights reserved.
|
|
* Distributed under the terms of the MIT License.
|
|
*/
|
|
#ifndef _BLOCK_CACHE_H
|
|
#define _BLOCK_CACHE_H
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
enum {
|
|
B_OBJECT_CACHE = 0,
|
|
B_MALLOC_CACHE = 1
|
|
};
|
|
|
|
class BBlockCache {
|
|
public:
|
|
BBlockCache(uint32 blockCount, size_t blockSize,
|
|
uint32 allocationType);
|
|
virtual ~BBlockCache();
|
|
|
|
void* Get(size_t blockSize);
|
|
void Save(void *pointer, size_t blockSize);
|
|
|
|
private:
|
|
virtual void _ReservedBlockCache1();
|
|
virtual void _ReservedBlockCache2();
|
|
|
|
BBlockCache(const BBlockCache &);
|
|
BBlockCache &operator=(const BBlockCache &);
|
|
|
|
private:
|
|
struct _FreeBlock;
|
|
|
|
_FreeBlock* fFreeList;
|
|
const size_t fBlockSize;
|
|
void* (*fAlloc)(size_t size);
|
|
void (*fFree)(void *pointer);
|
|
|
|
pthread_mutex_t fLock;
|
|
int32 fFreeBlocks;
|
|
int32 fBlockCount;
|
|
|
|
uint32 _reserved[6];
|
|
};
|
|
|
|
|
|
#endif // _BLOCK_CACHE_H
|