Skip to content

Commit

Permalink
Merge pull request #409 from Tarsnap/libcperciva-import
Browse files Browse the repository at this point in the history
Libcperciva import
  • Loading branch information
cperciva authored Dec 20, 2024
2 parents 9702f5d + de8bf18 commit ebd87bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libcperciva/alg/sha256_sse2.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mm_bswap_epi32(__m128i a)
{

/* Swap bytes in each 16-bit word. */
a = _mm_or_si128(_mm_slli_epi16(a, 8), _mm_srli_epi16(a, 8));
a = _mm_or_si128(_mm_slli_epi16(a, 8), _mm_srli_epi16(a, 8));

/* Swap all 16-bit words. */
a = _mm_shufflelo_epi16(a, _MM_SHUFFLE(2, 3, 0, 1));
Expand Down
21 changes: 21 additions & 0 deletions libcperciva/datastruct/mpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ static inline void
mpool_atexit(struct mpool * M)
{

/* Free all items on the stack. */
while (M->stacklen)
free(M->allocs[--M->stacklen]);

/* If we allocated a stack, free it. */
if (M->allocs != M->allocs_static)
free(M->allocs);
}
Expand All @@ -43,14 +46,23 @@ static inline void *
mpool_malloc(struct mpool * M, size_t len)
{

/* Count the total number of allocation requests. */
M->nallocs++;

/* If we have an object on the stack, use that. */
if (M->stacklen)
return (M->allocs[--(M->stacklen)]);

/* Count allocation requests where the pool was already empty. */
M->nempties++;

/* Initialize the atexit function (the first time we reach here). */
if (M->state == 0) {
atexit(M->atexitfunc);
M->state = 1;
}

/* Allocate a new object. */
return (malloc(len));
}

Expand All @@ -59,18 +71,25 @@ mpool_free(struct mpool * M, void * p)
{
void ** allocs_new;

/* Behave consistently with free(NULL). */
if (p == NULL)
return;

/* If we have space in the stack, cache the object. */
if (M->stacklen < M->allocsize) {
M->allocs[M->stacklen++] = p;
return;
}

/*
* Autotuning: If more than 1/256 of mpool_malloc() calls resulted in
* a malloc(), double the stack.
*/
if (M->nempties > (M->nallocs >> 8)) {
/* Sanity check. */
assert(M->allocsize > 0);

/* Allocate new stack and copy pointers into it. */
allocs_new = (void **)malloc(M->allocsize * 2 * sizeof(void *));
if (allocs_new) {
memcpy(allocs_new, M->allocs,
Expand All @@ -84,6 +103,8 @@ mpool_free(struct mpool * M, void * p)
free(p);
} else
free(p);

/* Reset statistics. */
M->nempties = 0;
M->nallocs = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion libcperciva/netbuf/netbuf_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void
netbuf_read_free(struct netbuf_read * R)
{

/* Behave consistently with free(NULL). */
/* Behave consistently with free(NULL). */
if (R == NULL)
return;

Expand Down

0 comments on commit ebd87bd

Please sign in to comment.