* Copyright (c) 2020 Huawei Technologies Co.,Ltd.
*
* openGauss is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*
* http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* ---------------------------------------------------------------------------------------
*
* mmpool.h
*
*
*
* IDENTIFICATION
* src/include/utils/mmpool.h
*
* ---------------------------------------------------------------------------------------
*/
#ifndef SRC_INCLUDE_UTILS_MMPOOL_H_
#define SRC_INCLUDE_UTILS_MMPOOL_H_
#include "postgres.h"
#include "utils/memutils.h"
#include "nodes/pg_list.h"
struct MemoryBlock {
int64 blkSize;
bool isFree;
MemoryBlock* prev;
MemoryBlock* next;
};
#define MEMORY_BLOCK_HEADER TYPEALIGN(ALIGNOF_LONG, sizeof(MemoryBlock))
#define MEMORY_BLOCK_MIN_SIZE (2 * MEMORY_BLOCK_HEADER)
struct MemoryBlockListHeader {
MemoryBlock* start;
MemoryBlock* end;
};
#define POOL_INIT_SUCCESS 0
#define POOL_INIT_FAIL 1
class MemoryPool {
public:
MemoryPool();
~MemoryPool();
int CreatePool();
void ReleasePool();
void* Malloc(int64 sizeBytes);
void* Realloc(void* addr, int64 sizeBytes);
void Free(void* addr);
inline bool Ready()
{
return m_ready;
}
static int Init();
static void Deinit();
private:
bool SplitFreeList(int idx);
void MergeBlock(void* addr, MemoryBlock* block, MemoryBlock* blockBuddy, int idx);
MemoryBlock* FindBuddy(MemoryBlock* block, int64 size);
void AddLast(int idx, MemoryBlock* blk);
void AddFirst(int idx, MemoryBlock* blk);
void Remove(int idx, MemoryBlock* blk);
private:
char* m_buf;
int64 m_size;
int64 m_use;
pthread_mutex_t m_mutex;
MemoryBlockListHeader m_freeList[64];
volatile bool m_ready;
};
#endif