git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
blob 992e354e125eecf430a144e40cf2296480abadbf 2660 bytes (raw)
name: mem-pool.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
 
/*
 * Memory Pool implementation logic.
 */

#include "cache.h"
#include "mem-pool.h"

static struct mp_block *mem_pool_alloc_block(struct mem_pool *mem_pool)
{
	struct mp_block *p;

	mem_pool->pool_alloc += sizeof(struct mp_block) + mem_pool->block_alloc;
	p = xmalloc(st_add(sizeof(struct mp_block), mem_pool->block_alloc));
	p->next_block = mem_pool->mp_block;
	p->next_free = (char *)p->space;
	p->end = p->next_free + mem_pool->block_alloc;
	mem_pool->mp_block = p;

	return p;
}

/*
 * Allocates a block of memory with a specific size and
 * appends it to the memory pool's list of blocks.
 *
 * This function is used to allocate blocks with sizes
 * different than the default "block_alloc" size for the mem_pool.
 *
 * There are two use cases:
 *  1) The initial block allocation for a memory pool.
 *
 *  2) large" blocks of a specific size, where the entire memory block
 *     is going to be used. This means the block will not have any
 *     available memory for future allocations. The block is placed at
 *     the end of the list so that it will be the last block searched
 *     for available space.
 */
static struct mp_block *mem_pool_alloc_block_with_size(struct mem_pool *mem_pool, size_t block_alloc)
{
	struct mp_block *p, *block;

	mem_pool->pool_alloc += sizeof(struct mp_block) + block_alloc;
	p = xmalloc(st_add(sizeof(struct mp_block), block_alloc));

	block = mem_pool->mp_block;
	if (block) {
		while (block->next_block)
			block = block->next_block;

		block->next_block = p;
	} else {
		mem_pool->mp_block = p;
	}

	p->next_block = NULL;
	p->next_free = (char *)p->space;
	p->end = p->next_free + block_alloc;

	return p;
}

void *mem_pool_alloc(struct mem_pool *mem_pool, size_t len)
{
	struct mp_block *p;
	void *r;

	/* round up to a 'uintmax_t' alignment */
	if (len & (sizeof(uintmax_t) - 1))
		len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1));

	p = mem_pool->mp_block;

	/*
	 * In performance profiling, there was a minor perf benefit to
	 * check for available memory in the head block via the if
	 * statement, and only going through the loop when needed.
	 */
	if (p &&
	   (p->end - p->next_free < len)) {
		for (p = p->next_block; p; p = p->next_block)
			if (p->end - p->next_free >= len)
				break;
	}

	if (!p) {
		if (len >= (mem_pool->block_alloc / 2))
			p = mem_pool_alloc_block_with_size(mem_pool, len);
		else
			p = mem_pool_alloc_block(mem_pool);
	}

	r = p->next_free;
	p->next_free += len;
	return r;
}

void *mem_pool_calloc(struct mem_pool *mem_pool, size_t count, size_t size)
{
	size_t len = st_mult(count, size);
	void *r = mem_pool_alloc(mem_pool, len);
	memset(r, 0, len);
	return r;
}

debug log:

solving 992e354e12 ...
found 992e354e12 in https://public-inbox.org/git/20180323144408.213145-5-jamill@microsoft.com/

applying [1/1] https://public-inbox.org/git/20180323144408.213145-5-jamill@microsoft.com/
diff --git a/mem-pool.c b/mem-pool.c
new file mode 100644
index 0000000000..992e354e12

Checking patch mem-pool.c...
Applied patch mem-pool.c cleanly.

index at:
100644 992e354e125eecf430a144e40cf2296480abadbf	mem-pool.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).