postgresql之对象池(slab)

news/2024/7/9 22:39:26 标签: postgresql

创建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;
			else
				slab->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;
	}
	else
		dlist_push_head(&slab->freelist[block->nfree], &block->node);


http://www.niftyadmin.cn/n/4930748.html

相关文章

图像像素梯度

梯度 在高数中,梯度是一个向量,是有方向有大小。假设一二元函数f(x,y),在某点的梯度有: 结果为: 即方向导数。梯度的方向是函数变化最快的方向,沿着梯度的方向容易找到最大值。 图像梯度 在一幅模糊图…

纯前端 -- html转pdf插件总结

一、html2canvasjsPDF(文字会被截断): 将HTML元素呈现给添加到PDF中的画布对象,不能仅使用jsPDF,需要html2canvas或rasterizeHTML html2canvasjsPDF的具体使用链接 二、html2pdf(内容显示不全文字会被截断…

2009年上半年 软件设计师 下午试卷

博主介绍:✌全网粉丝3W,全栈开发工程师,从事多年软件开发,在大厂呆过。持有软件中级、六级等证书。可提供微服务项目搭建与毕业项目实战,博主也曾写过优秀论文,查重率极低,在这方面有丰富的经验…

由于找不到msvcp100.dll无法继续执行代码怎么解决

当遇到程序无法正常运行,提示缺少msvcp100.dll文件时,最初的反应可能是困惑和不知所措。然而,通过修复msvcp100.dll文件,我发现这个问题实际上并不复杂,并且可以通过一些简单的步骤解决。 在修复msvcp100.dll文件的时候…

nodejs+vue+elementui招聘求职网站系统的设计与实现-173lo

(1)管理员的功能是最高的,可以对系统所在功能进行查看,修改和删除,包括企业和用户功能。管理员用例如下: 图3-1管理员用例图 (2)企业关键功能包含个人中心、岗位类型管理、招聘信息…

Dubbo1-架构的演变

分布式系统上的相关概念 项目:传统项目、互联网项目 传统项目: 一般为公司内部使用,或者小群体小范围的使用,一般不要求性能,美观,并发等 互联网项目的特点: 1.用户多 2.流量大,并…

Python数据分析实战-列表字符串、字符串列表、字符串的转化(附源码和实现效果)

实现功能 str([None,master,hh]) ---> [None,"master","hh"] ---> "None,master,hh" 实现代码 import re import astx1 str([None,master,hh]) print(x1)x2 ast.literal_eval(x1) print(x2)x3 ",".join(str(item) for item…

深入探索Python元组常用函数及实例应用

Python作为一种功能强大且易于学习的编程语言,提供了多种数据结构来处理不同类型的数据。其中,元组(Tuple)是一种不可变的序列类型,具有多种常用函数,本篇博客将深入介绍元组的常用函数,并通过实…