summaryrefslogtreecommitdiffstats
path: root/lib/base/pool.cpp
blob: 2299309b8dc29130a9e5ca21736e2bf5645b9951 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/*
 * Generic pool handling routines.
 *
 * Hopefully these reduce the number of malloc/free calls.
 *
 *
 * Thread warning:
 *	This implementation is thread safe.  However, simultaneous 
 *	mallocs/frees to the same "pool" are not safe.  If you wish to
 *	use this module across multiple threads, you should define
 *	POOL_LOCKING which will make the malloc pools safe. 
 *
 * Mike Belshe
 * 11-20-95
 *
 */

#include "netsite.h"
#include "base/systems.h"
#include "base/systhr.h"

#ifdef MALLOC_POOLS
#include "base/pool.h"
#include "base/ereport.h"
#include "base/util.h"
#include "base/crit.h"

#include "base/dbtbase.h"

#ifdef DEBUG
#define POOL_ZERO_DEBUG
#endif
#undef POOL_LOCKING

#define BLOCK_SIZE		(32 * 1024)
#define MAX_FREELIST_SIZE	(BLOCK_SIZE * 32)

/* WORD SIZE 8 sets us up for 8 byte alignment. */
#define WORD_SIZE	8   
#undef	ALIGN
#define ALIGN(x)	( (x + WORD_SIZE-1) & (~(WORD_SIZE-1)) )

/* block_t
 * When the user allocates space, a BLOCK_SIZE (or larger) block is created in 
 * the pool.  This block is used until all the space is eaten within it.
 * When all the space is gone, a new block is created.
 *
 */
typedef struct block_t {
	char		*data;		/* the real alloc'd space */
	char		*start;		/* first free byte in block */
	char		*end;		/* ptr to end of block */
	struct block_t	*next;		/* ptr to next block */
} block_t;

/* pool_t
 * A pool is a collection of blocks.  The blocks consist of multiple 
 * allocations of memory, but a single allocation cannot be freed by
 * itself.  Once the memory is allocated it is allocated until the
 * entire pool is freed.
 */
typedef struct pool_t {
#ifdef DEBUG_CACHES
	time_t		time_created;
#endif
#ifdef POOL_LOCKING
	CRITICAL	lock;		/* lock for modifying the pool */
#endif
	block_t		*curr_block;	/* current block being used */
	block_t		*used_blocks;	/* blocks that are all used up */
	long		size;		/* size of memory in pool */
	struct pool_t	*next;		/* known_pools list */
} pool_t;

/* known_pools
 * Primarily for debugging, keep a list of all active malloc pools.
 */
static pool_t *known_pools = NULL;
static CRITICAL known_pools_lock = NULL;
static unsigned long pool_blocks_created = 0;
static unsigned long pool_blocks_freed = 0;

/* freelist
 * Internally we maintain a list of free blocks which we try to pull from
 * whenever possible.  This list will never have more than MAX_FREELIST_SIZE
 * bytes within it.
 */
static CRITICAL freelist_lock = NULL;
static block_t	*freelist = NULL;
static unsigned long	freelist_size = 0;
static unsigned long	freelist_max = MAX_FREELIST_SIZE;
static int pool_disable = 0;

int 
pool_internal_init()
{
	if (pool_disable == 0) {
		if (known_pools_lock == NULL) {
			known_pools_lock = crit_init();
			freelist_lock = crit_init();
		}
	}

	return 0;
}

static block_t *
_create_block(int size)
{
	block_t *newblock = NULL;
	long bytes = ALIGN(size);
	block_t	*free_ptr,
		*last_free_ptr = NULL;

	/* check freelist for large enough block first */

	crit_enter(freelist_lock);
	free_ptr = freelist;
	while(free_ptr && ((free_ptr->end - free_ptr->data) < bytes)) {
		last_free_ptr = free_ptr;
		free_ptr = free_ptr->next;
	}

	if (free_ptr) {
		newblock = free_ptr;
		if (last_free_ptr)
			last_free_ptr->next = free_ptr->next;
		else
			freelist = free_ptr->next;
		freelist_size -= (newblock->end - newblock->data);
		crit_exit(freelist_lock);
		bytes = free_ptr->end - free_ptr->data;
	}
	else {
                pool_blocks_created++;
		crit_exit(freelist_lock);
		if (((newblock = (block_t *)PERM_MALLOC(sizeof(block_t))) == NULL) || 
		    ((newblock->data = (char *)PERM_MALLOC(bytes)) == NULL)) {
			ereport(LOG_CATASTROPHE, XP_GetAdminStr(DBT_poolCreateBlockOutOfMemory_));
			if (newblock)
				PERM_FREE(newblock);
			return NULL;
		}
	}
	newblock->start	= newblock->data;
	newblock->end	= newblock->data + bytes;
	newblock->next	= NULL;

	return newblock;
}

/* Caller must hold lock for the pool */
static void 
_free_block(block_t *block)
{

#ifdef POOL_ZERO_DEBUG
	memset(block->data, 0xa, block->end-block->data);
#endif /* POOL_ZERO_DEBUG */

	if ((unsigned long)(freelist_size + block->end - block->data) > freelist_max) {
		/* Just have to delete the whole block! */

		crit_enter(freelist_lock);
                pool_blocks_freed++;
		crit_exit(freelist_lock);

		PERM_FREE(block->data);
#ifdef POOL_ZERO_DEBUG
		memset(block, 0xa, sizeof(block));
#endif /* POOL_ZERO_DEBUG */

		PERM_FREE(block);
		return;
	}
	crit_enter(freelist_lock);
	freelist_size += (block->end - block->data);
	block->start = block->data;

	block->next = freelist;
	freelist = block;
	crit_exit(freelist_lock);
}

/* ptr_in_pool()
 * Checks to see if the given pointer is in the given pool.
 * If true, returns a ptr to the block_t containing the ptr;
 * otherwise returns NULL
 */
block_t * 
_ptr_in_pool(pool_t *pool, void *ptr)
{
	block_t *block_ptr = NULL;

	/* try to find a block which contains this ptr */

	if (	((char *)ptr < (char *)pool->curr_block->end) && 
		((char *)ptr >= (char *)pool->curr_block->data) ) 
		block_ptr = pool->curr_block;
	else 
		for(	block_ptr = pool->used_blocks; 
			block_ptr && 
			(((char *)ptr >= (char *)block_ptr->end) && 
			 ((char *)ptr < (char *)block_ptr->data)); 
			block_ptr = block_ptr->next);

	return block_ptr;
}


NSAPI_PUBLIC pool_handle_t *
pool_create()
{
	pool_t *newpool;

	if (pool_disable)
		return NULL;

	newpool = (pool_t *)PERM_MALLOC(sizeof(pool_t));

	if (newpool) {
		/* Have to initialize now, as pools get created sometimes
		 * before pool_init can be called...
		 */
		if (known_pools_lock == NULL) {
			known_pools_lock = crit_init();
			freelist_lock = crit_init();
		}

		if ( (newpool->curr_block =_create_block(BLOCK_SIZE)) == NULL) {
			ereport(LOG_CATASTROPHE, XP_GetAdminStr(DBT_poolCreateOutOfMemory_));
			PERM_FREE(newpool);
			return NULL;
		}
		newpool->used_blocks = NULL;
		newpool->size = 0;
		newpool->next = NULL;
#ifdef POOL_LOCKING
		newpool->lock = crit_init();
#endif
#ifdef DEBUG_CACHES
		newpool->time_created = time(NULL);
#endif

		/* Add to known pools list */
		crit_enter(known_pools_lock);
		newpool->next = known_pools;
		known_pools = newpool;
		crit_exit(known_pools_lock);
	}
	else 
		ereport(LOG_CATASTROPHE, XP_GetAdminStr(DBT_poolCreateOutOfMemory_1));

	return (pool_handle_t *)newpool;
}

NSAPI_PUBLIC void
pool_destroy(pool_handle_t *pool_handle)
{
	pool_t *pool = (pool_t *)pool_handle;
	block_t *tmp_blk;
	pool_t *last, *search;

	if (pool_disable)
		return;

	crit_enter(known_pools_lock);
#ifdef POOL_LOCKING
	crit_enter(pool->lock);
#endif

	if (pool->curr_block)
		_free_block(pool->curr_block);

	while(pool->used_blocks) {
		tmp_blk = pool->used_blocks;
		pool->used_blocks = pool->used_blocks->next;
		_free_block(tmp_blk);
	}

	/* Remove from the known pools list */
	for (last = NULL, search = known_pools; search; 
		last = search, search = search->next)
		if (search == pool) 
			break;
	if (search) {
		if(last) 
			last->next = search->next;
		else
			known_pools = search->next;
	
	}

#ifdef POOL_LOCKING
	crit_exit(pool->lock);
	crit_terminate(pool->lock);
#endif
	crit_exit(known_pools_lock);

#ifdef POOL_ZERO_DEBUG
	memset(pool, 0xa, sizeof(pool));
#endif /* POOL_ZERO_DEBUG */

	PERM_FREE(pool);

	return;
}


NSAPI_PUBLIC void *
pool_malloc(pool_handle_t *pool_handle, size_t size)
{
	pool_t *pool = (pool_t *)pool_handle;
	long reqsize, blocksize;
	char *ptr;

	if (pool == NULL || pool_disable) {
		return PERM_MALLOC(size);
	}

#ifdef DEBUG
	if (size == 0)
		return NULL;
#endif

#ifdef POOL_LOCKING
	crit_enter(pool->lock);
#endif

        reqsize = ALIGN(size);
	ptr = pool->curr_block->start;
	pool->curr_block->start += reqsize;

	/* does this fit into the last allocated block? */
	if (pool->curr_block->start > pool->curr_block->end) {

		/* Did not fit; time to allocate a new block */

		pool->curr_block->start -= reqsize;  /* keep structs in tact */

		pool->curr_block->next = pool->used_blocks;
		pool->used_blocks = pool->curr_block;
	
		/* Allocate a chunk of memory which is a multiple of BLOCK_SIZE
		 * bytes 
		 */
		blocksize = ( (size + BLOCK_SIZE-1) / BLOCK_SIZE ) * BLOCK_SIZE;
		if ( (pool->curr_block = _create_block(blocksize)) == NULL) {
			ereport(LOG_CATASTROPHE, XP_GetAdminStr(DBT_poolMallocOutOfMemory_));
#ifdef POOL_LOCKING
			crit_exit(pool->lock);
#endif
			return NULL;
		}

                ptr = pool->curr_block->start;
		reqsize = ALIGN(size);
		pool->curr_block->start += reqsize;
	}

	pool->size += reqsize;

#ifdef POOL_LOCKING
	crit_exit(pool->lock);
#endif
	return ptr;
}

void _pool_free_error()
{
	ereport(LOG_WARN, XP_GetAdminStr(DBT_freeUsedWherePermFreeShouldHaveB_));

	return;
}

NSAPI_PUBLIC void
pool_free(pool_handle_t *pool_handle, void *ptr)
{
	if (pool_handle == NULL || pool_disable) {
		PERM_FREE(ptr);
		return;
	}

#ifdef DEBUG
	/* Just to be nice, check to see if the ptr was allocated in a pool.
	 * If not, issue a warning and do a REAL free just to make sure that
	 * we don't leak memory.
	 */
	if ( !_ptr_in_pool((pool_t *)pool_handle, ptr) ) {
		_pool_free_error();

		PERM_FREE(ptr);
	}
#endif
	return;
}

NSAPI_PUBLIC void *
pool_calloc(pool_handle_t *pool_handle, size_t nelem, size_t elsize)
{
	void *ptr;

	if (pool_handle == NULL || pool_disable)
		return PERM_CALLOC(elsize * nelem);

	ptr = pool_malloc(pool_handle, elsize * nelem);
	if (ptr)
		memset(ptr, 0, elsize * nelem);
	return ptr;
}

NSAPI_PUBLIC void *
pool_realloc(pool_handle_t *pool_handle, void *ptr, size_t size)
{
	pool_t *pool = (pool_t *)pool_handle;
	void *newptr;
	block_t *block_ptr;
	size_t oldsize;

	if (pool_handle == NULL || pool_disable)
		return PERM_REALLOC(ptr, size);

	if ( (newptr = pool_malloc(pool_handle, size)) == NULL) 
		return NULL;

	/* With our structure we don't know exactly where the end
	 * of the original block is.  But we do know an upper bound
	 * which is a valid ptr.  Search the outstanding blocks
	 * for the block which contains this ptr, and copy...
	 */
#ifdef POOL_LOCKING
	crit_enter(pool->lock);
#endif

	if ( !(block_ptr = _ptr_in_pool(pool, ptr)) ) {
		/* User is trying to realloc nonmalloc'd space! */
		return newptr;
	}

	oldsize = block_ptr->end - (char *)ptr ;
	if (oldsize > size)
		oldsize = size;
	memmove((char *)newptr, (char *)ptr, oldsize);
#ifdef POOL_LOCKING
	crit_exit(pool->lock);
#endif

	return newptr;
}

NSAPI_PUBLIC char *
pool_strdup(pool_handle_t *pool_handle, const char *orig_str)
{
	char *new_str;
	int len = strlen(orig_str);

	if (pool_handle == NULL || pool_disable)
		return PERM_STRDUP(orig_str);

	new_str = (char *)pool_malloc(pool_handle, len+1);

	if (new_str) 
		memcpy(new_str, orig_str, len+1);

	return new_str;
}

NSAPI_PUBLIC long
pool_space(pool_handle_t *pool_handle)
{
	pool_t *pool = (pool_t *)pool_handle;

	return pool->size;
}

NSAPI_PUBLIC int pool_enabled()
{
#ifndef THREAD_ANY
	/* we don't have USE_NSPR defined so systhread_getdata is undef'ed */
	return 0;
#else
	if (pool_disable || (getThreadMallocKey() == -1) )
		return 0;

	if (!systhread_getdata(getThreadMallocKey()))
		return 0;

	return 1;
#endif
}


/* pool_service_debug()
 * NSAPI service routine to print state information about the existing 
 * pools.  Hopefully useful in debugging.
 *
 */
#define MAX_DEBUG_LINE	1024
#ifdef DEBUG_CACHES  /* XXXrobm causes entanglement in install and admserv cgis */
NSAPI_PUBLIC int
pool_service_debug(pblock *pb, Session *sn, Request *rq) 
{
	char tmp_buf[MAX_DEBUG_LINE];
	char cbuf[DEF_CTIMEBUF];
	int len;
	pool_t *pool_ptr;
	block_t *block_ptr;
	int pool_cnt, block_cnt;

	param_free(pblock_remove("content-type", rq->srvhdrs));
	pblock_nvinsert("content-type", "text/html", rq->srvhdrs);

	protocol_status(sn, rq, PROTOCOL_OK, NULL);
	protocol_start_response(sn, rq);

	len = util_sprintf(tmp_buf, "<H2>Memory pool status report</H2>\n");
	net_write(sn->csd, tmp_buf, len);

	len = util_sprintf(tmp_buf, "Note: The 0 block in each pool is \
the currently used block <P>\n");
	net_write(sn->csd, tmp_buf, len);

	len = util_sprintf(tmp_buf, "Freelist size: %d/%d<P>", freelist_size,
		freelist_max);
	net_write(sn->csd, tmp_buf, len);

	len = util_sprintf(tmp_buf, "Pool disabled: %d<P>", pool_disable);
	net_write(sn->csd, tmp_buf, len);

	len = util_sprintf(tmp_buf, "Blocks created: %d<P> Blocks freed: %d", 
		pool_blocks_created, pool_blocks_freed);
	net_write(sn->csd, tmp_buf, len);

	/* Create an HTML table */
	len = util_sprintf(tmp_buf, "<UL><TABLE BORDER=4>\n"); 
	net_write(sn->csd, tmp_buf, len);
	len = util_sprintf(tmp_buf, "<TH>Pool #</TH>\n");
	net_write(sn->csd, tmp_buf, len);
	len = util_sprintf(tmp_buf, "<TH>Pool size #</TH>\n");
	net_write(sn->csd, tmp_buf, len);
#ifdef DEBUG_CACHES
	len = util_sprintf(tmp_buf, "<TH>Time Created</TH>\n");
	net_write(sn->csd, tmp_buf, len);
#endif
	len = util_sprintf(tmp_buf, "<TH>Blocks</TH>\n");
	net_write(sn->csd, tmp_buf, len);

	crit_enter(known_pools_lock);
	for (pool_cnt = 0, pool_ptr = known_pools; pool_ptr; 
		pool_ptr = pool_ptr->next, pool_cnt++) {

#ifdef POOL_LOCKING
		crit_enter(pool_ptr->lock);
#endif
		len = util_snprintf(tmp_buf, MAX_DEBUG_LINE, 
#ifndef DEBUG_CACHES
"<tr align=right> <td>%d</td> <td>%d</td> <td> <TABLE BORDER=2> <TH>Block #</TH><TH>data</TH><TH>curr size</TH> <TH>max size</TH>\n", 
#else
"<tr align=right> <td>%d</td> <td>%d</td> <td>%s</td> <td> <TABLE BORDER=2> <TH>Block #</TH><TH>data</TH><TH>curr size</TH> <TH>max size</TH>\n", 
#endif
			pool_cnt, pool_space((pool_handle_t *)pool_ptr)
#ifdef DEBUG_CACHES
			, util_ctime(&(pool_ptr->time_created), cbuf, DEF_CTIMEBUF));
#else
			);
#endif
		net_write(sn->csd, tmp_buf, len);

		/* Print the first block */
		len = util_snprintf(tmp_buf, MAX_DEBUG_LINE, "\
<tr align=right> \
<td>%d</td> \
<td>%d</td> \
<td>%d</td> \
<td>%d</td> \
</tr>\n",
			0, pool_ptr->curr_block->data, 
			pool_ptr->curr_block->start -pool_ptr->curr_block->data,
			pool_ptr->curr_block->end - pool_ptr->curr_block->data);

		net_write(sn->csd, tmp_buf, len);

		for (block_cnt = 1, block_ptr = pool_ptr->used_blocks; block_ptr; 
			block_ptr = block_ptr->next, block_cnt++) {

			len = util_snprintf(tmp_buf, MAX_DEBUG_LINE, "\
<tr align=right> \
<td>%d</td> \
<td>%d</td> \
<td>%d</td> \
<td>%d</td> \
</tr>\n",
				block_cnt, block_ptr->data, 
				block_ptr->start - block_ptr->data, 
				block_ptr->end - block_ptr->data);

			net_write(sn->csd, tmp_buf, len);
		}
#ifdef POOL_LOCKING
		crit_exit(pool_ptr->lock);
#endif

		len = util_snprintf(tmp_buf, MAX_DEBUG_LINE, "</TABLE></TD></TR>");
		
		net_write(sn->csd, tmp_buf, len);
	}
	crit_exit(known_pools_lock);

	len = util_sprintf(tmp_buf, "</TABLE></UL>\n");
	net_write(sn->csd, tmp_buf, len);
	
	return REQ_PROCEED;

}
#endif /* 0 */
#endif /* MALLOC_POOLS */