1. 程式人生 > >An introduction to pmemobj (part 4) - transactional dynamic memory allocation

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

個人看法:

這段程式碼的實際含義:

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
}