1#include "rsync.h"
2
3#define POOL_DEF_EXTENT	(32 * 1024)
4
5struct alloc_pool
6{
7	size_t			size;		/* extent size		*/
8	size_t			quantum;	/* allocation quantum	*/
9	struct pool_extent	*live;		/* current extent for
10						 * allocations		*/
11	struct pool_extent	*free;		/* unfreed extent list	*/
12	void			(*bomb)();
13						/* function to call if
14						 * malloc fails		*/
15	int			flags;
16
17	/* statistical data */
18	unsigned long		e_created;	/* extents created	*/
19	unsigned long		e_freed;	/* extents detroyed	*/
20	int64			n_allocated;	/* calls to alloc	*/
21	int64			n_freed;	/* calls to free	*/
22	int64			b_allocated;	/* cum. bytes allocated	*/
23	int64			b_freed;	/* cum. bytes freed	*/
24};
25
26struct pool_extent
27{
28	void			*start;		/* starting address	*/
29	size_t			free;		/* free bytecount	*/
30	size_t			bound;		/* bytes bound by padding,
31						 * overhead and freed	*/
32	struct pool_extent	*next;
33};
34
35struct align_test {
36    void *foo;
37    int64 bar;
38};
39
40#define MINALIGN	offsetof(struct align_test, bar)
41
42/* Temporarily cast a void* var into a char* var when adding an offset (to
43 * keep some compilers from complaining about the pointer arithmetic). */
44#define PTR_ADD(b,o)	( (void*) ((char*)(b) + (o)) )
45
46alloc_pool_t
47pool_create(size_t size, size_t quantum,
48    void (*bomb)(char *), int flags)
49{
50	struct alloc_pool	*pool;
51
52	if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
53		return pool;
54	memset(pool, 0, sizeof (struct alloc_pool));
55
56	pool->size = size	/* round extent size to min alignment reqs */
57	    ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
58	    : POOL_DEF_EXTENT;
59	if (pool->flags & POOL_INTERN) {
60		pool->size -= sizeof (struct pool_extent);
61		flags |= POOL_APPEND;
62	}
63	pool->quantum = quantum ? quantum : MINALIGN;
64	pool->bomb = bomb;
65	pool->flags = flags;
66
67	return pool;
68}
69
70void
71pool_destroy(alloc_pool_t p)
72{
73	struct alloc_pool *pool = (struct alloc_pool *) p;
74	struct pool_extent	*cur, *next;
75
76	if (!pool)
77		return;
78
79	if (pool->live) {
80		cur = pool->live;
81		free(cur->start);
82		if (!(pool->flags & POOL_APPEND))
83			free(cur);
84	}
85	for (cur = pool->free; cur; cur = next) {
86		next = cur->next;
87		free(cur->start);
88		if (!(pool->flags & POOL_APPEND))
89			free(cur);
90	}
91	free(pool);
92}
93
94void *
95pool_alloc(alloc_pool_t p, size_t len, char *bomb)
96{
97	struct alloc_pool *pool = (struct alloc_pool *) p;
98	if (!pool)
99		return NULL;
100
101	if (!len)
102		len = pool->quantum;
103	else if (pool->quantum > 1 && len % pool->quantum)
104		len += pool->quantum - len % pool->quantum;
105
106	if (len > pool->size)
107		goto bomb;
108
109	if (!pool->live || len > pool->live->free) {
110		void	*start;
111		size_t	free;
112		size_t	bound;
113		size_t	sqew;
114		size_t	asize;
115
116		if (pool->live) {
117			pool->live->next = pool->free;
118			pool->free = pool->live;
119		}
120
121		free = pool->size;
122		bound = 0;
123
124		asize = pool->size;
125		if (pool->flags & POOL_APPEND)
126			asize += sizeof (struct pool_extent);
127
128		if (!(start = (void *) malloc(asize)))
129			goto bomb;
130
131		if (pool->flags & POOL_CLEAR)
132			memset(start, 0, pool->size);
133
134		if (pool->flags & POOL_APPEND)
135			pool->live = PTR_ADD(start, free);
136		else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
137			goto bomb;
138		if (pool->flags & POOL_QALIGN && pool->quantum > 1
139		    && (sqew = (size_t)PTR_ADD(start, free) % pool->quantum)) {
140			bound  += sqew;
141			free -= sqew;
142		}
143		pool->live->start = start;
144		pool->live->free = free;
145		pool->live->bound = bound;
146		pool->live->next = NULL;
147
148		pool->e_created++;
149	}
150
151	pool->n_allocated++;
152	pool->b_allocated += len;
153
154	pool->live->free -= len;
155
156	return PTR_ADD(pool->live->start, pool->live->free);
157
158bomb:
159	if (pool->bomb)
160		(*pool->bomb)(bomb);
161	return NULL;
162}
163
164void
165pool_free(alloc_pool_t p, size_t len, void *addr)
166{
167	struct alloc_pool *pool = (struct alloc_pool *) p;
168	struct pool_extent	*cur;
169	struct pool_extent	*prev;
170
171	if (!pool)
172		return;
173
174	if (!len)
175		len = pool->quantum;
176	else if (pool->quantum > 1 && len % pool->quantum)
177		len += pool->quantum - len % pool->quantum;
178
179	if (!addr && pool->live) {
180		pool->live->next = pool->free;
181		pool->free = pool->live;
182		pool->live = NULL;
183		return;
184	}
185	pool->n_freed++;
186	pool->b_freed += len;
187
188	cur = pool->live;
189	if (cur && addr >= cur->start
190	    && addr < PTR_ADD(cur->start, pool->size)) {
191		if (addr == PTR_ADD(cur->start, cur->free)) {
192			if (pool->flags & POOL_CLEAR)
193				memset(addr, 0, len);
194			pool->b_freed += len;
195		} else
196			cur->bound += len;
197		if (cur->free + cur->bound >= pool->size) {
198			size_t sqew;
199
200			cur->free = pool->size;
201			cur->bound = 0;
202			if (pool->flags & POOL_QALIGN && pool->quantum > 1
203			    && (sqew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) {
204				cur->bound += sqew;
205				cur->free -= sqew;
206			}
207		}
208		return;
209	}
210	for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next) {
211		if (addr >= cur->start
212		    && addr < PTR_ADD(cur->start, pool->size))
213			break;
214	}
215	if (!cur)
216		return;
217
218	if (prev) {
219		prev->next = cur->next;
220		cur->next = pool->free;
221		pool->free = cur;
222	}
223	cur->bound += len;
224
225	if (cur->free + cur->bound >= pool->size) {
226		pool->free = cur->next;
227
228		free(cur->start);
229		if (!(pool->flags & POOL_APPEND))
230			free(cur);
231		pool->e_freed++;
232	}
233	return;
234}
235
236#define FDPRINT(label, value) \
237	snprintf(buf, sizeof buf, label, value), \
238	write(fd, buf, strlen(buf))
239
240#define FDEXTSTAT(ext) \
241	snprintf(buf, sizeof buf, "  %12ld  %5ld\n", \
242		(long) ext->free, \
243		(long) ext->bound), \
244	write(fd, buf, strlen(buf))
245
246void
247pool_stats(alloc_pool_t p, int fd, int summarize)
248{
249	struct alloc_pool *pool = (struct alloc_pool *) p;
250	struct pool_extent	*cur;
251	char buf[BUFSIZ];
252
253	if (!pool)
254		return;
255
256	FDPRINT("  Extent size:       %12ld\n",	(long)	pool->size);
257	FDPRINT("  Alloc quantum:     %12ld\n",	(long)	pool->quantum);
258	FDPRINT("  Extents created:   %12ld\n",		pool->e_created);
259	FDPRINT("  Extents freed:     %12ld\n",		pool->e_freed);
260	FDPRINT("  Alloc count:       %12.0f\n", (double) pool->n_allocated);
261	FDPRINT("  Free Count:        %12.0f\n", (double) pool->n_freed);
262	FDPRINT("  Alloc bytes:       %12.0f\n", (double) pool->b_allocated);
263	FDPRINT("  Free bytes:        %12.0f\n", (double) pool->b_freed);
264
265	if (summarize)
266		return;
267
268	if (!pool->live && !pool->free)
269		return;
270
271	write(fd, "\n", 1);
272
273	if (pool->live)
274		FDEXTSTAT(pool->live);
275	strlcpy(buf, "   FREE    BOUND\n", sizeof buf);
276	write(fd, buf, strlen(buf));
277
278	for (cur = pool->free; cur; cur = cur->next)
279		FDEXTSTAT(cur);
280}
281