Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
reformat pma
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Dec 11, 2015
1 parent f835226 commit d5a410e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -63,6 +63,6 @@ $(STATIC): banner
@ar crs $@ $(LIB_OBJS)

$(BENCH): banner $(BENCH_OBJS) $(LIB_OBJS)
$(CC) $(BENCH_OBJS) $(LIB_OBJS) $(WITH_SAN) -o $@
$(CC) $(BENCH_OBJS) $(PLATFORM_LDFLAGS) $(LIB_OBJS) $(WITH_SAN) -o $@

.PHONY: all banner clean
9 changes: 4 additions & 5 deletions tree/lmb.c
Expand Up @@ -62,11 +62,10 @@ void lmb_put(struct lmb *lmb,
le->keyp = base + LEAFENTRY_SIZE;

xmemcpy(le->keyp, key->data, key->size);
pma_insertat(lmb->pma,
(void*)base,
_lmb_entry_key_compare,
(void*)lmb,
&coord);
pma_insert(lmb->pma,
(void*)base,
_lmb_entry_key_compare,
(void*)lmb);
lmb->count++;
}

Expand Down
55 changes: 24 additions & 31 deletions util/pma.c
Expand Up @@ -171,6 +171,7 @@ static inline int _chain_find_lowerbound(struct pma *p, void *k, compare_func f,
int hi = p->used - 1;
int best = hi;
struct array **chain = p->chain;
// need chain lock

if (p->used == 0)
return 0;
Expand All @@ -191,21 +192,9 @@ static inline int _chain_find_lowerbound(struct pma *p, void *k, compare_func f,
return best;
}

void pma_insertat(struct pma *p, void *k, compare_func f, void *extra, struct pma_coord *coord)
void chain_maybe_resize(struct pma *p, int chain_idx, compare_func f, void *extra)
{
struct array *array = NULL;
int pma_used = p->used;
int chain_idx = coord->chain_idx;
int array_idx = coord->array_idx;

nassert(chain_idx <= pma_used);
if (pma_used == 0) {
array = _array_new(UNROLLED_LIMIT);
_chain_insertat(p, array, p->used);
} else {
array = p->chain[chain_idx];
}
_array_insertat(array, k, array_idx);
struct array *array = p->chain[chain_idx];
if (array->used > UNROLLED_LIMIT) {
struct array *new_arr;
int half = array->used / 2;
Expand Down Expand Up @@ -254,40 +243,44 @@ void pma_free(struct pma *p)
void pma_insert(struct pma *p, void *k, compare_func f, void *extra)
{
int array_idx = 0;
int chain_idx = _chain_find_lowerbound(p, k, f, extra);
struct array *arr = p->chain[chain_idx];

if (arr)
array_idx = _array_find_greater_than(arr, k, f, extra);
int chain_idx = 0;
struct array **arr;

chain_idx = _chain_find_lowerbound(p, k, f, extra);
arr = &p->chain[chain_idx];
if (*arr)
array_idx = _array_find_greater_than(*arr, k, f, extra);
else {
*arr = _array_new(UNROLLED_LIMIT);
}

/* if array_idx is -1, means that we got the end of the array */
if (nessunlikely(array_idx == -1))
array_idx = arr->used;

struct pma_coord coord = {.chain_idx = chain_idx, .array_idx = array_idx};
array_idx = (*arr)->used;

pma_insertat(p, k, f, extra, &coord);
_array_insertat(*arr, k, array_idx);
chain_maybe_resize(p, chain_idx, f, extra);
p->count++;
}

void pma_append(struct pma *p, void *k, compare_func f, void *extra)
{
int array_idx = 0;
int chain_idx = 0;
struct array **arr;

if (p->used > 0)
chain_idx = p->used - 1;

if (chain_idx > 0) {
struct array *arr = p->chain[chain_idx];

if (arr && (arr->used > 1))
array_idx = arr->used - 1;
}
arr = &p->chain[chain_idx];
if (!*arr)
*arr = _array_new(UNROLLED_LIMIT);

struct pma_coord coord = {.chain_idx = chain_idx, .array_idx = array_idx};
pma_insertat(p, k, f, extra, &coord);
if ((*arr)->used > 0)
array_idx = (*arr)->used - 1;

_array_insertat(*arr, k, array_idx);
chain_maybe_resize(p, chain_idx, f, extra);
p->count++;
}

Expand Down
2 changes: 0 additions & 2 deletions util/pma.h
Expand Up @@ -7,7 +7,6 @@
#ifndef nessDB_PMA_H_
#define nessDB_PMA_H_

#define ARRAY_CACHE_SIZE (64)
typedef int (*compare_func)(void *, void *, void *);

struct pma_coord {
Expand All @@ -32,7 +31,6 @@ struct pma *pma_new();
void pma_free(struct pma *);

void pma_insert(struct pma *, void *, compare_func f, void *);
void pma_insertat(struct pma *, void *, compare_func f, void *, struct pma_coord *);
void pma_append(struct pma *, void *, compare_func f, void *);
uint32_t pma_count(struct pma *);

Expand Down
26 changes: 12 additions & 14 deletions util/xtable.c
Expand Up @@ -63,22 +63,20 @@ void xtable_add(struct xtable *xtbl, void *v)
void xtable_remove(struct xtable *xtbl, void *v)
{
int hash;
struct xtable_entry *curr;
struct xtable_entry *prev = NULL;
struct xtable_entry *e;
struct xtable_entry **ptr;

hash = xtbl->hash_func(v) & (xtbl->slot - 1);
curr = xtbl->slots[hash];
while (curr && curr->v != v) {
prev = curr;
curr = curr->next;
}

if (curr) {
if (prev)
prev->next = curr->next;
else
xtbl->slots[hash] = curr->next;
xfree(curr);
e = xtbl->slots[hash];
ptr = &e;
while (e) {
if (e->v == v) {
*ptr = e->next;
xfree(e);
break;
}
e = e->next;
ptr = &e;
}
}

Expand Down

0 comments on commit d5a410e

Please sign in to comment.