An introduction to pmemobj (part 4) - transactional dynamic memory allocation

参考链接:https://pmem.io/2015/06/17/tx-alloc.html

TX_** 系列函数的定义

参考链接:https://github.com/pmem/pmdk/blob/master/src/include/libpmemobj/tx.h

Homework

An introduction to pmemobj (part 4) - transactional dynamic memory allocation_第1张图片

个人看法:

这段代码的实际含义:

void rectangle_modify(TOID(struct rectangle) rect, int new_a, int new_b) {
	TX_BEGIN(pop) {

            TX_ADD_FIELD(rect, a);
            D_RW(rect)->a = (new_a);

            TX_ADD_FIELD(rect, b);
            D_RW(rect)->b = (new_b);

	} TX_END
}

//一次转换

void rectangle_modify(TOID(struct rectangle) rect, int new_a, int new_b) {
	TX_BEGIN(pop) {

            TX_ADD_DIRECT(&(D_RO(rect)->a);
            D_RW(rect)->a = (new_a);

            TX_ADD_DIRECT(&(D_RO(rect)->b);
            D_RW(rect)->b = (new_b);

	} TX_END
}

//二次转换

void rectangle_modify(TOID(struct rectangle) rect, int new_a, int new_b) {
	TX_BEGIN(pop) {

            pmemobj_tx_add_range_direct(&(D_RO(rect)->a, sizeof(*(&(D_RO(rect)->a)))
            D_RW(rect)->a = (new_a);

            pmemobj_tx_add_range_direct(&(D_RO(rect)->b, sizeof(*(&(D_RO(rect)->b)))
            D_RW(rect)->b = (new_b);

	} TX_END
}

首先前面已经给 a,b 做了定义,是 rect 的成员函数

struct rectangle {
	int a;
	int b;
};

然后,本段代码中用的是 TX_SET 会导致两次 pmemobj_tx_add_range_direct 函数调用(个人看法),每次调用就需要给 a,b 继续内存分配,这样开销较大。可以直接使用 TX_ADD(rect)

void rectangle_modify(TOID(struct rectangle) rect, int new_a, int new_b) {
	TX_BEGIN(pop) {

            TX_ADD(rect);
               D_RW(rect)->a = new_a;
	       D_RW(rect)->b = new_b;
		
	} TX_END
}

 

 

 

 

 

 

你可能感兴趣的:(An introduction to pmemobj (part 4) - transactional dynamic memory allocation)