Skip to content

Commit

Permalink
Wrap common allocate-and-swap code in a macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Cahill committed Aug 26, 2013
1 parent cc12bc0 commit 8825706
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
43 changes: 9 additions & 34 deletions src/btree/row_modify.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,8 @@ __wt_row_modify(WT_SESSION_IMPL *session, WT_CURSOR_BTREE *cbt, int is_remove)
if (cbt->compare == 0) {
if (cbt->ins == NULL) {
/* Allocate an update array as necessary. */
if ((upd_entry = page->u.row.upd) == NULL) {
WT_ERR(__wt_calloc_def(
session, page->entries, &upd_entry));
if (WT_ATOMIC_CAS(
page->u.row.upd, NULL, upd_entry))
__wt_cache_page_inmem_incr(session,
page, page->entries *
sizeof(WT_UPDATE *));
else
__wt_free(session, upd_entry);
upd_entry = NULL;
}
WT_PAGE_ALLOC_AND_SWAP(session, page,
page->u.row.upd, upd_entry, page->entries);

/* Set the WT_UPDATE array reference. */
upd_entry = &page->u.row.upd[cbt->slot];
Expand Down Expand Up @@ -94,31 +84,16 @@ __wt_row_modify(WT_SESSION_IMPL *session, WT_CURSOR_BTREE *cbt, int is_remove)
* the returned slot, then we're using the smallest-key insert
* slot. That's hard, so we set a flag.
*/
ins_slot = F_ISSET(
cbt, WT_CBT_SEARCH_SMALLEST) ? page->entries : cbt->slot;

if ((ins_headp = page->u.row.ins) == NULL) {
WT_ERR(__wt_calloc_def(
session, page->entries + 1, &ins_headp));
if (WT_ATOMIC_CAS(page->u.row.ins, NULL, ins_headp))
__wt_cache_page_inmem_incr(session,
page, (page->entries + 1) *
sizeof(WT_INSERT_HEAD *));
else
__wt_free(session, ins_headp);
}
WT_PAGE_ALLOC_AND_SWAP(session, page,
page->u.row.ins, ins_headp, page->entries + 1);

ins_slot = F_ISSET(cbt, WT_CBT_SEARCH_SMALLEST) ?
page->entries : cbt->slot;
ins_headp = &page->u.row.ins[ins_slot];

/* Allocate the WT_INSERT_HEAD structure as necessary. */
if ((ins_head = *ins_headp) == NULL) {
WT_ERR(__wt_calloc_def(session, 1, &ins_head));
if (WT_ATOMIC_CAS(*ins_headp, NULL, ins_head))
__wt_cache_page_inmem_incr(
session, page, sizeof(WT_INSERT_HEAD));
else
__wt_free(session, ins_head);
ins_head = *ins_headp;
}
WT_PAGE_ALLOC_AND_SWAP(session, page, *ins_headp, ins_head, 1);
ins_head = *ins_headp;

/* Choose a skiplist depth for this insert. */
skipdepth = __wt_skip_choose_depth();
Expand Down
14 changes: 14 additions & 0 deletions src/include/btmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,20 @@ struct __wt_insert {
(ins) != NULL; \
(ins) = WT_SKIP_NEXT(ins))

/*
* Atomically allocate and swap a structure or array into place.
*/
#define WT_PAGE_ALLOC_AND_SWAP(s, page, dest, v, count) do { \
if ((v = dest) == NULL) { \
WT_ERR(__wt_calloc_def((s), (count), &v)); \
if (WT_ATOMIC_CAS(dest, NULL, v)) \
__wt_cache_page_inmem_incr(session, \
(page), (count) * sizeof (*v)); \
else \
__wt_free(session, v); \
} \
} while (0)

/*
* WT_INSERT_HEAD --
* The head of a skiplist of WT_INSERT items.
Expand Down

0 comments on commit 8825706

Please sign in to comment.