summaryrefslogtreecommitdiff
path: root/usr.bin/ctfconv/pool.c
blob: 24129248527e79c7f7b27dd9f0cbcaef2b255ab2 (plain)
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
104
105
/*	$OpenBSD: pool.c,v 1.3 2017/08/29 21:10:20 deraadt Exp $ */

/*
 * Copyright (c) 2017 Martin Pieuchot
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef NOPOOL

#include <sys/types.h>
#include <sys/types.h>
#include <sys/queue.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xmalloc.h"
#include "pool.h"

#define MAXIMUM(a, b)	(((a) > (b)) ? (a) : (b))

struct pool_item {
	SLIST_ENTRY(pool_item) pi_list;
};

SIMPLEQ_HEAD(, pool) pool_head = SIMPLEQ_HEAD_INITIALIZER(pool_head);

void
pool_init(struct pool *pp, const char *name, size_t nmemb, size_t size)
{
	size = MAXIMUM(size, sizeof(struct pool_item));

	SLIST_INIT(&pp->pr_free);
	pp->pr_name = name;
	pp->pr_nmemb = nmemb;
	pp->pr_size = size;
	pp->pr_nitems = 0;
	pp->pr_nfree = 0;

	SIMPLEQ_INSERT_TAIL(&pool_head, pp, pr_list);
}

void *
pool_get(struct pool *pp)
{
	struct pool_item *pi;

	if (SLIST_EMPTY(&pp->pr_free)) {
		char *p;
		size_t i;

		p = xreallocarray(NULL, pp->pr_nmemb, pp->pr_size);
		for (i = 0; i < pp->pr_nmemb; i++) {
			pi = (struct pool_item *)p;
			SLIST_INSERT_HEAD(&pp->pr_free, pi, pi_list);
			p += pp->pr_size;
		}
		pp->pr_nitems += pp->pr_nmemb;
		pp->pr_nfree += pp->pr_nmemb;
	}

	pi = SLIST_FIRST(&pp->pr_free);
	SLIST_REMOVE_HEAD(&pp->pr_free, pi_list);
	pp->pr_nfree--;

	return pi;
}

void
pool_put(struct pool *pp, void *p)
{
	struct pool_item *pi = (struct pool_item *)p;

	if (pi == NULL)
		return;

	assert(pp->pr_nfree < pp->pr_nitems);

	SLIST_INSERT_HEAD(&pp->pr_free, pi, pi_list);
	pp->pr_nfree++;
}

void
pool_dump(void)
{
	struct pool *pp;

	SIMPLEQ_FOREACH(pp, &pool_head, pr_list)
		printf("%s: %zd items, %zd free\n", pp->pr_name, pp->pr_nitems,
		    pp->pr_nfree);
}
#endif /* NOPOOL */