postgresql之对象池(slab)

news/2025/2/11 7:39:55/

创建SlabContext

 分配对象

创建对象池

  • 分配空间
  • 初始化分配的空间
  • 将block加入循环双向链表

 

 从对象池中获取对象

从双向循环链表中获取一个block

/* grab the block from the freelist (even the new block is there) */
block = dlist_head_element(SlabBlock, node,&slab->freelist[slab->minFreeChunks]);

从block中获取空闲chunk的索引

/* we know index of the first free chunk in the block */
idx = block->firstFreeChunk;

获取chunk

/* compute the chunk location block start (after the block header) */chunk = SlabBlockGetChunk(slab, block, idx);

减少空用chunk个数

/** Update the block nfree count, and also the minFreeChunks as we've* decreased nfree for a block with the minimum number of free chunks* (because that's how we chose the block).*/block->nfree--;slab->minFreeChunks = block->nfree;

更新下一个空闲chunk索引

block->firstFreeChunk = *(int32 *) SlabChunkGetPointer(chunk);

将block重新更新到下一个循环双向链表中

/* move the whole block to the right place in the freelist */dlist_delete(&block->node);dlist_push_head(&slab->freelist[block->nfree], &block->node);

 当block中的chunk分配完后,整个block就移动到了freelist[0]的位置,freelist[0]下的循环双向链表的节点都是分配完的block

 

释放对象

根据释放对象,获取到对应的block以及chunk

SlabChunk  *chunk = SlabPointerGetChunk(pointer);
SlabBlock  *block = chunk->block;

计算当前chunk相对于block的索引

/* compute index of the chunk with respect to block start */
idx = SlabChunkIndex(slab, block, chunk);

 更新空闲chunk索引以及空闲chunk个数

/* add chunk to freelist, and update block nfree count */
*(int32 *) pointer = block->firstFreeChunk;
block->firstFreeChunk = idx;
block->nfree++;

 将当前block从现有循环双向链表中删除

/* remove the block from a freelist */
dlist_delete(&block->node);

如果释放的chunk所属的block是正用于申请空间的freelist,并且freelist也空了,则更新minFreeChunks

	/** See if we need to update the minFreeChunks field for the slab - we only* need to do that if there the block had that number of free chunks* before we freed one. In that case, we check if there still are blocks* in the original freelist and we either keep the current value (if there* still are blocks) or increment it by one (the new block is still the* one with minimum free chunks).** The one exception is when the block will get completely free - in that* case we will free it, se we can't use it for minFreeChunks. It however* means there are no more blocks with free chunks.*/if (slab->minFreeChunks == (block->nfree - 1)){/* Have we removed the last chunk from the freelist? */if (dlist_is_empty(&slab->freelist[slab->minFreeChunks])){/* but if we made the block entirely free, we'll free it */if (block->nfree == slab->chunksPerBlock)slab->minFreeChunks = 0;elseslab->minFreeChunks++;}}

释放block或这加入另外一个循环双向链表中

/* If the block is now completely empty, free it. */if (block->nfree == slab->chunksPerBlock){free(block);slab->nblocks--;context->mem_allocated -= slab->blockSize;}elsedlist_push_head(&slab->freelist[block->nfree], &block->node);


http://www.ppmy.cn/news/1024535.html

相关文章

AMASS database

AMASS是一个由不同的光学标记运动捕捉数据集统一表示在一个公共框架和参数化下的大型人体运动数据库。它包含了超过40小时的运动数据,涵盖了300多个主体和11000多个运动。它使用了SMPL人体模型,它是一种基于混合形状和姿态空间的生成式人体模型&#xff…

java种的hutool库接口说明和整理

1. Hutool库基本介绍 1.1. 地址 官网地址:https://www.hutool.cn/ 1.2. 基本介绍 Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅&#xf…

关于红外雨量计(光学雨量传感器)的红外光学测量技术

关于红外雨量计(光学雨量传感器)的红外光学测量技术 红外雨量计是一种常用的雨量测量设备,它通过红外光学测量技术来测量雨量。红外光学测量技术是指利用光学原理和仪器对物体的红外辐射进行测量、分析和处理。在红外雨量计中,利…

在序列化、反序列化下如何保持单例(Singleton)模式

1、序列化、反序列化 在 Java 中,当一个对象被序列化后再被反序列化,通常情况下会创建一个新的对象实例。这是因为序列化将对象的状态保存到字节流中,而反序列化则是将字节流重新转化为对象。在这个过程中,通常会使用类的构造函数…

Ubuntu下跑Aplaca报错:torch.cuda.0utofMemoryError: CUDA out of memory.解决办法(查看CUDA占用情况清除GPU缓存)

缓存不够!!!!并非内存容量不够 错误提示: torch.cuda.0utofMemoryError: CUDA out of memory.Tried to allocate 2.00 MiB (PU 0; 23.69 GiB total capacity; 237 BiB already allocated; 18.38 MiB fre; 2.50 GiB re…

个保新标 | 《信息安全技术 敏感个人信息处理安全要求》(征求意见稿)发布

8 月 9 日,全国信息安全标准化技术委员会公开发布关于国家标准《信息安全技术 敏感个人信息处理安全要求》(征求意见稿)(以下简称《标准》)的通知,面向社会广泛征求意见。 《标准》的制定背景是为支撑《个人…

excel将主信息和明细信息整理为多对多(每隔几行空白如何填充)

excel导出的数据是主信息和明细信息形式。 方法如下:1、首先,从第一个单元格开始选中要填充的数据区域。2、按CtrlG或者F5调出定位对话框,点击左下角的【定位条件】。3、在【定位条件】中选择【空值】,然后点击【确定】按钮。4、按照上述操作…

CMU 15-445 -- Distributed OLTP Databases -20

CMU 15-445 -- Distributed OLTP Databases -20 引言AssumptionAgendaAtomic Commit ProtocolsTwo-Phase Commit (2PC)2PC Success2PC Abort2PC OptimizationsFault Tolerant PaxosMulti-Paxos 2PC vs. Paxos ReplicationReplication ConfigurationApproach #1: Master-Replica…