Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
 * Copyright (c) 2000-2001, 2004 Proofpoint, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: heap.c,v 1.52 2013-11-22 20:51:43 ca Exp $")

/*
**  debugging memory allocation package
**  See heap.html for documentation.
*/

#include <string.h>

#include <sm/assert.h>
#include <sm/debug.h>
#include <sm/exc.h>
#include <sm/heap.h>
#include <sm/io.h>
#include <sm/signal.h>
#include <sm/xtrap.h>

/* undef all macro versions of the "functions" so they can be specified here */
#undef sm_malloc
#undef sm_malloc_x
#undef sm_malloc_tagged
#undef sm_malloc_tagged_x
#undef sm_free
#undef sm_free_tagged
#undef sm_realloc
#if SM_HEAP_CHECK
# undef sm_heap_register
# undef sm_heap_checkptr
# undef sm_heap_report
#endif /* SM_HEAP_CHECK */

#if SM_HEAP_CHECK
SM_DEBUG_T SmHeapCheck = SM_DEBUG_INITIALIZER("sm_check_heap",
    "@(#)$Debug: sm_check_heap - check sm_malloc, sm_realloc, sm_free calls $");
# define HEAP_CHECK sm_debug_active(&SmHeapCheck, 1)
static int	ptrhash __P((void *p));
#endif /* SM_HEAP_CHECK */

const SM_EXC_TYPE_T SmHeapOutOfMemoryType =
{
	SmExcTypeMagic,
	"F:sm.heap",
	"",
	sm_etype_printf,
	"out of memory",
};

SM_EXC_T SmHeapOutOfMemory = SM_EXC_INITIALIZER(&SmHeapOutOfMemoryType, NULL);


/*
**  The behaviour of malloc with size==0 is platform dependent (it
**  says so in the C standard): it can return NULL or non-NULL.  We
**  don't want sm_malloc_x(0) to raise an exception on some platforms
**  but not others, so this case requires special handling.  We've got
**  two choices: "size = 1" or "return NULL". We use the former in the
**  following.
**	If we had something like autoconf we could figure out the
**	behaviour of the platform and either use this hack or just
**	use size.
*/

#define MALLOC_SIZE(size)	((size) == 0 ? 1 : (size))

/*
**  SM_MALLOC_X -- wrapper around malloc(), raises an exception on error.
**
**	Parameters:
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to memory region.
**
**	Note:
**		sm_malloc_x only gets called from source files in which heap
**		debugging is disabled at compile time.  Otherwise, a call to
**		sm_malloc_x is macro expanded to a call to sm_malloc_tagged_x.
**
**	Exceptions:
**		F:sm_heap -- out of memory
*/

void *
sm_malloc_x(size)
	size_t size;
{
	void *ptr;

	ENTER_CRITICAL();
	ptr = malloc(MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	if (ptr == NULL)
		sm_exc_raise_x(&SmHeapOutOfMemory);
	return ptr;
}

#if !SM_HEAP_CHECK

/*
**  SM_MALLOC -- wrapper around malloc()
**
**	Parameters:
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to memory region.
*/

void *
sm_malloc(size)
	size_t size;
{
	void *ptr;

	ENTER_CRITICAL();
	ptr = malloc(MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	return ptr;
}

/*
**  SM_REALLOC -- wrapper for realloc()
**
**	Parameters:
**		ptr -- pointer to old memory area.
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to new memory area, NULL on failure.
*/

void *
sm_realloc(ptr, size)
	void *ptr;
	size_t size;
{
	void *newptr;

	ENTER_CRITICAL();
	newptr = realloc(ptr, MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	return newptr;
}

/*
**  SM_REALLOC_X -- wrapper for realloc()
**
**	Parameters:
**		ptr -- pointer to old memory area.
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to new memory area.
**
**	Exceptions:
**		F:sm_heap -- out of memory
*/

void *
sm_realloc_x(ptr, size)
	void *ptr;
	size_t size;
{
	void *newptr;

	ENTER_CRITICAL();
	newptr = realloc(ptr, MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	if (newptr == NULL)
		sm_exc_raise_x(&SmHeapOutOfMemory);
	return newptr;
}
/*
**  SM_FREE -- wrapper around free()
**
**	Parameters:
**		ptr -- pointer to memory region.
**
**	Returns:
**		none.
*/

void
sm_free(ptr)
	void *ptr;
{
	if (ptr == NULL)
		return;
	ENTER_CRITICAL();
	free(ptr);
	LEAVE_CRITICAL();
	return;
}

#else /* !SM_HEAP_CHECK */

/*
**  Each allocated block is assigned a "group number".
**  By default, all blocks are assigned to group #1.
**  By convention, group #0 is for memory that is never freed.
**  You can use group numbers any way you want, in order to help make
**  sense of sm_heap_report output.
*/

int SmHeapGroup = 1;
int SmHeapMaxGroup = 1;

/*
**  Total number of bytes allocated.
**  This is only maintained if the sm_check_heap debug category is active.
*/

size_t SmHeapTotal = 0;

/*
**  High water mark: the most that SmHeapTotal has ever been.
*/

size_t SmHeapMaxTotal = 0;

/*
**  Maximum number of bytes that may be allocated at any one time.
**  0 means no limit.
**  This is only honoured if sm_check_heap is active.
*/

SM_DEBUG_T SmHeapLimit = SM_DEBUG_INITIALIZER("sm_heap_limit",
    "@(#)$Debug: sm_heap_limit - max # of bytes permitted in heap $");

/*
**  This is the data structure that keeps track of all currently
**  allocated blocks of memory known to the heap package.
*/

typedef struct sm_heap_item SM_HEAP_ITEM_T;
struct sm_heap_item
{
	void		*hi_ptr;
	size_t		hi_size;
	char		*hi_tag;
	int		hi_num;
	int		hi_group;
	SM_HEAP_ITEM_T	*hi_next;
};

#define SM_HEAP_TABLE_SIZE	256
static SM_HEAP_ITEM_T *SmHeapTable[SM_HEAP_TABLE_SIZE];

/*
**  This is a randomly generated table
**  which contains exactly one occurrence
**  of each of the numbers between 0 and 255.
**  It is used by ptrhash.
*/

static unsigned char hashtab[SM_HEAP_TABLE_SIZE] =
{
	161, 71, 77,187, 15,229,  9,176,221,119,239, 21, 85,138,203, 86,
	102, 65, 80,199,235, 32,140, 96,224, 78,126,127,144,  0, 11,179,
	 64, 30,120, 23,225,226, 33, 50,205,167,130,240,174, 99,206, 73,
	231,210,189,162, 48, 93,246, 54,213,141,135, 39, 41,192,236,193,
	157, 88, 95,104,188, 63,133,177,234,110,158,214,238,131,233, 91,
	125, 82, 94, 79, 66, 92,151, 45,252, 98, 26,183,  7,191,171,106,
	145,154,251,100,113,  5, 74, 62, 76,124, 14,217,200, 75,115,190,
	103, 28,198,196,169,219, 37,118,150, 18,152,175, 49,136,  6,142,
	 89, 19,243,254, 47,137, 24,166,180, 10, 40,186,202, 46,184, 67,
	148,108,181, 81, 25,241, 13,139, 58, 38, 84,253,201, 12,116, 17,
	195, 22,112, 69,255, 43,147,222,111, 56,194,216,149,244, 42,173,
	232,220,249,105,207, 51,197,242, 72,211,208, 59,122,230,237,170,
	165, 44, 68,123,129,245,143,101,  8,209,215,247,185, 57,218, 53,
	114,121,  3,128,  4,204,212,146,  2,155, 83,250, 87, 29, 31,159,
	 60, 27,107,156,227,182,  1, 61, 36,160,109, 97, 90, 20,168,132,
	223,248, 70,164, 55,172, 34, 52,163,117, 35,153,134, 16,178,228
};

/*
**  PTRHASH -- hash a pointer value
**
**	Parameters:
**		p -- pointer.
**
**	Returns:
**		hash value.
**
**  ptrhash hashes a pointer value to a uniformly distributed random
**  number between 0 and 255.
**
**  This hash algorithm is based on Peter K. Pearson,
**  "Fast Hashing of Variable-Length Text Strings",
**  in Communications of the ACM, June 1990, vol 33 no 6.
*/

static int
ptrhash(p)
	void *p;
{
	int h;

	if (sizeof(void*) == 4 && sizeof(unsigned long) == 4)
	{
		unsigned long n = (unsigned long)p;

		h = hashtab[n & 0xFF];
		h = hashtab[h ^ ((n >> 8) & 0xFF)];
		h = hashtab[h ^ ((n >> 16) & 0xFF)];
		h = hashtab[h ^ ((n >> 24) & 0xFF)];
	}
# if 0
	else if (sizeof(void*) == 8 && sizeof(unsigned long) == 8)
	{
		unsigned long n = (unsigned long)p;

		h = hashtab[n & 0xFF];
		h = hashtab[h ^ ((n >> 8) & 0xFF)];
		h = hashtab[h ^ ((n >> 16) & 0xFF)];
		h = hashtab[h ^ ((n >> 24) & 0xFF)];
		h = hashtab[h ^ ((n >> 32) & 0xFF)];
		h = hashtab[h ^ ((n >> 40) & 0xFF)];
		h = hashtab[h ^ ((n >> 48) & 0xFF)];
		h = hashtab[h ^ ((n >> 56) & 0xFF)];
	}
# endif /* 0 */
	else
	{
		unsigned char *cp = (unsigned char *)&p;
		int i;

		h = 0;
		for (i = 0; i < sizeof(void*); ++i)
			h = hashtab[h ^ cp[i]];
	}
	return h;
}

/*
**  SM_MALLOC_TAGGED -- wrapper around malloc(), debugging version.
**
**	Parameters:
**		size -- size of requested memory.
**		tag -- tag for debugging.
**		num -- additional value for debugging.
**		group -- heap group for debugging.
**
**	Returns:
**		Pointer to memory region.
*/

void *
sm_malloc_tagged(size, tag, num, group)
	size_t size;
	char *tag;
	int num;
	int group;
{
	void *ptr;

	if (!HEAP_CHECK)
	{
		ENTER_CRITICAL();
		ptr = malloc(MALLOC_SIZE(size));
		LEAVE_CRITICAL();
		return ptr;
	}

	if (sm_xtrap_check())
		return NULL;
	if (sm_debug_active(&SmHeapLimit, 1)
	    && sm_debug_level(&SmHeapLimit) < SmHeapTotal + size)
		return NULL;
	ENTER_CRITICAL();
	ptr = malloc(MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	if (ptr != NULL && !sm_heap_register(ptr, size, tag, num, group))
	{
		ENTER_CRITICAL();
		free(ptr);
		LEAVE_CRITICAL();
		ptr = NULL;
	}
	SmHeapTotal += size;
	if (SmHeapTotal > SmHeapMaxTotal)
		SmHeapMaxTotal = SmHeapTotal;
	return ptr;
}

/*
**  SM_MALLOC_TAGGED_X -- wrapper around malloc(), debugging version.
**
**	Parameters:
**		size -- size of requested memory.
**		tag -- tag for debugging.
**		num -- additional value for debugging.
**		group -- heap group for debugging.
**
**	Returns:
**		Pointer to memory region.
**
**	Exceptions:
**		F:sm_heap -- out of memory
*/

void *
sm_malloc_tagged_x(size, tag, num, group)
	size_t size;
	char *tag;
	int num;
	int group;
{
	void *ptr;

	if (!HEAP_CHECK)
	{
		ENTER_CRITICAL();
		ptr = malloc(MALLOC_SIZE(size));
		LEAVE_CRITICAL();
		if (ptr == NULL)
			sm_exc_raise_x(&SmHeapOutOfMemory);
		return ptr;
	}

	sm_xtrap_raise_x(&SmHeapOutOfMemory);
	if (sm_debug_active(&SmHeapLimit, 1)
	    && sm_debug_level(&SmHeapLimit) < SmHeapTotal + size)
	{
		sm_exc_raise_x(&SmHeapOutOfMemory);
	}
	ENTER_CRITICAL();
	ptr = malloc(MALLOC_SIZE(size));
	LEAVE_CRITICAL();
	if (ptr != NULL && !sm_heap_register(ptr, size, tag, num, group))
	{
		ENTER_CRITICAL();
		free(ptr);
		LEAVE_CRITICAL();
		ptr = NULL;
	}
	if (ptr == NULL)
		sm_exc_raise_x(&SmHeapOutOfMemory);
	SmHeapTotal += size;
	if (SmHeapTotal > SmHeapMaxTotal)
		SmHeapMaxTotal = SmHeapTotal;
	return ptr;
}

/*
**  SM_HEAP_REGISTER -- register a pointer into the heap for debugging.
**
**	Parameters:
**		ptr -- pointer to register.
**		size -- size of requested memory.
**		tag -- tag for debugging.
**		num -- additional value for debugging.
**		group -- heap group for debugging.
**
**	Returns:
**		true iff successfully registered (not yet in table).
*/

bool
sm_heap_register(ptr, size, tag, num, group)
	void *ptr;
	size_t size;
	char *tag;
	int num;
	int group;
{
	int i;
	SM_HEAP_ITEM_T *hi;

	if (!HEAP_CHECK)
		return true;
	SM_REQUIRE(ptr != NULL);
	i = ptrhash(ptr);
# if SM_CHECK_REQUIRE

	/*
	** We require that ptr is not already in SmHeapTable.
	*/

	for (hi = SmHeapTable[i]; hi != NULL; hi = hi->hi_next)
	{
		if (hi->hi_ptr == ptr)
			sm_abort("sm_heap_register: ptr %p is already registered (%s:%d)",
				 ptr, hi->hi_tag, hi->hi_num);
	}
# endif /* SM_CHECK_REQUIRE */
	ENTER_CRITICAL();
	hi = (SM_HEAP_ITEM_T *) malloc(sizeof(SM_HEAP_ITEM_T));
	LEAVE_CRITICAL();
	if (hi == NULL)
		return false;
	hi->hi_ptr = ptr;
	hi->hi_size = size;
	hi->hi_tag = tag;
	hi->hi_num = num;
	hi->hi_group = group;
	hi->hi_next = SmHeapTable[i];
	SmHeapTable[i] = hi;
	return true;
}
/*
**  SM_REALLOC -- wrapper for realloc(), debugging version.
**
**	Parameters:
**		ptr -- pointer to old memory area.
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to new memory area, NULL on failure.
*/

void *
sm_realloc(ptr, size)
	void *ptr;
	size_t size;
{
	void *newptr;
	SM_HEAP_ITEM_T *hi, **hp;

	if (!HEAP_CHECK)
	{
		ENTER_CRITICAL();
		newptr = realloc(ptr, MALLOC_SIZE(size));
		LEAVE_CRITICAL();
		return newptr;
	}

	if (ptr == NULL)
		return sm_malloc_tagged(size, "realloc", 0, SmHeapGroup);

	for (hp = &SmHeapTable[ptrhash(ptr)]; *hp != NULL; hp = &(**hp).hi_next)
	{
		if ((**hp).hi_ptr == ptr)
		{
			if (sm_xtrap_check())
				return NULL;
			hi = *hp;
			if (sm_debug_active(&SmHeapLimit, 1)
			    && sm_debug_level(&SmHeapLimit)
			       < SmHeapTotal - hi->hi_size + size)
			{
				return NULL;
			}
			ENTER_CRITICAL();
			newptr = realloc(ptr, MALLOC_SIZE(size));
			LEAVE_CRITICAL();
			if (newptr == NULL)
				return NULL;
			SmHeapTotal = SmHeapTotal - hi->hi_size + size;
			if (SmHeapTotal > SmHeapMaxTotal)
				SmHeapMaxTotal = SmHeapTotal;
			*hp = hi->hi_next;
			hi->hi_ptr = newptr;
			hi->hi_size = size;
			hp = &SmHeapTable[ptrhash(newptr)];
			hi->hi_next = *hp;
			*hp = hi;
			return newptr;
		}
	}
	sm_abort("sm_realloc: bad argument (%p)", ptr);
	/* NOTREACHED */
	return NULL;	/* keep Irix compiler happy */
}

/*
**  SM_REALLOC_X -- wrapper for realloc(), debugging version.
**
**	Parameters:
**		ptr -- pointer to old memory area.
**		size -- size of requested memory.
**
**	Returns:
**		Pointer to new memory area.
**
**	Exceptions:
**		F:sm_heap -- out of memory
*/

void *
sm_realloc_x(ptr, size)
	void *ptr;
	size_t size;
{
	void *newptr;
	SM_HEAP_ITEM_T *hi, **hp;

	if (!HEAP_CHECK)
	{
		ENTER_CRITICAL();
		newptr = realloc(ptr, MALLOC_SIZE(size));
		LEAVE_CRITICAL();
		if (newptr == NULL)
			sm_exc_raise_x(&SmHeapOutOfMemory);
		return newptr;
	}

	if (ptr == NULL)
		return sm_malloc_tagged_x(size, "realloc", 0, SmHeapGroup);

	for (hp = &SmHeapTable[ptrhash(ptr)]; *hp != NULL; hp = &(**hp).hi_next)
	{
		if ((**hp).hi_ptr == ptr)
		{
			sm_xtrap_raise_x(&SmHeapOutOfMemory);
			hi = *hp;
			if (sm_debug_active(&SmHeapLimit, 1)
			    && sm_debug_level(&SmHeapLimit)
			       < SmHeapTotal - hi->hi_size + size)
			{
				sm_exc_raise_x(&SmHeapOutOfMemory);
			}
			ENTER_CRITICAL();
			newptr = realloc(ptr, MALLOC_SIZE(size));
			LEAVE_CRITICAL();
			if (newptr == NULL)
				sm_exc_raise_x(&SmHeapOutOfMemory);
			SmHeapTotal = SmHeapTotal - hi->hi_size + size;
			if (SmHeapTotal > SmHeapMaxTotal)
				SmHeapMaxTotal = SmHeapTotal;
			*hp = hi->hi_next;
			hi->hi_ptr = newptr;
			hi->hi_size = size;
			hp = &SmHeapTable[ptrhash(newptr)];
			hi->hi_next = *hp;
			*hp = hi;
			return newptr;
		}
	}
	sm_abort("sm_realloc_x: bad argument (%p)", ptr);
	/* NOTREACHED */
	return NULL;	/* keep Irix compiler happy */
}

/*
**  SM_FREE_TAGGED -- wrapper around free(), debugging version.
**
**	Parameters:
**		ptr -- pointer to memory region.
**		tag -- tag for debugging.
**		num -- additional value for debugging.
**
**	Returns:
**		none.
*/

void
sm_free_tagged(ptr, tag, num)
	void *ptr;
	char *tag;
	int num;
{
	SM_HEAP_ITEM_T **hp;

	if (ptr == NULL)
		return;
	if (!HEAP_CHECK)
	{
		ENTER_CRITICAL();
		free(ptr);
		LEAVE_CRITICAL();
		return;
	}
	for (hp = &SmHeapTable[ptrhash(ptr)]; *hp != NULL; hp = &(**hp).hi_next)
	{
		if ((**hp).hi_ptr == ptr)
		{
			SM_HEAP_ITEM_T *hi = *hp;

			*hp = hi->hi_next;

			/*
			**  Fill the block with zeros before freeing.
			**  This is intended to catch problems with
			**  dangling pointers.  The block is filled with
			**  zeros, not with some non-zero value, because
			**  it is common practice in some C code to store
			**  a zero in a structure member before freeing the
			**  structure, as a defense against dangling pointers.
			*/

			(void) memset(ptr, 0, hi->hi_size);
			SmHeapTotal -= hi->hi_size;
			ENTER_CRITICAL();
			free(ptr);
			free(hi);
			LEAVE_CRITICAL();
			return;
		}
	}
	sm_abort("sm_free: bad argument (%p) (%s:%d)", ptr, tag, num);
}

/*
**  SM_HEAP_CHECKPTR_TAGGED -- check whether ptr is a valid argument to sm_free
**
**	Parameters:
**		ptr -- pointer to memory region.
**		tag -- tag for debugging.
**		num -- additional value for debugging.
**
**	Returns:
**		none.
**
**	Side Effects:
**		aborts if check fails.
*/

void
sm_heap_checkptr_tagged(ptr, tag, num)
	void *ptr;
	char *tag;
	int num;
{
	SM_HEAP_ITEM_T *hp;

	if (!HEAP_CHECK)
		return;
	if (ptr == NULL)
		return;
	for (hp = SmHeapTable[ptrhash(ptr)]; hp != NULL; hp = hp->hi_next)
	{
		if (hp->hi_ptr == ptr)
			return;
	}
	sm_abort("sm_heap_checkptr(%p): bad ptr (%s:%d)", ptr, tag, num);
}

/*
**  SM_HEAP_REPORT -- output "map" of used heap.
**
**	Parameters:
**		stream -- the file pointer to write to.
**		verbosity -- how much info?
**
**	Returns:
**		none.
*/

void
sm_heap_report(stream, verbosity)
	SM_FILE_T *stream;
	int verbosity;
{
	int i;
	unsigned long group0total, group1total, otherstotal, grandtotal;

	if (!HEAP_CHECK || verbosity <= 0)
		return;
	group0total = group1total = otherstotal = grandtotal = 0;
	for (i = 0; i < sizeof(SmHeapTable) / sizeof(SmHeapTable[0]); ++i)
	{
		SM_HEAP_ITEM_T *hi = SmHeapTable[i];

		while (hi != NULL)
		{
			if (verbosity > 2
			    || (verbosity > 1 && hi->hi_group != 0))
			{
				sm_io_fprintf(stream, SM_TIME_DEFAULT,
					"%4d %*lx %7lu bytes",
					hi->hi_group,
					(int) sizeof(void *) * 2,
					(long)hi->hi_ptr,
					(unsigned long)hi->hi_size);
				if (hi->hi_tag != NULL)
				{
					sm_io_fprintf(stream, SM_TIME_DEFAULT,
						"  %s",
						hi->hi_tag);
					if (hi->hi_num)
					{
						sm_io_fprintf(stream,
							SM_TIME_DEFAULT,
							":%d",
							hi->hi_num);
					}
				}
				sm_io_fprintf(stream, SM_TIME_DEFAULT, "\n");
			}
			switch (hi->hi_group)
			{
			  case 0:
				group0total += hi->hi_size;
				break;
			  case 1:
				group1total += hi->hi_size;
				break;
			  default:
				otherstotal += hi->hi_size;
				break;
			}
			grandtotal += hi->hi_size;
			hi = hi->hi_next;
		}
	}
	sm_io_fprintf(stream, SM_TIME_DEFAULT,
		"heap max=%lu, total=%lu, ",
		(unsigned long) SmHeapMaxTotal, grandtotal);
	sm_io_fprintf(stream, SM_TIME_DEFAULT,
		"group 0=%lu, group 1=%lu, others=%lu\n",
		group0total, group1total, otherstotal);
	if (grandtotal != SmHeapTotal)
	{
		sm_io_fprintf(stream, SM_TIME_DEFAULT,
			"BUG => SmHeapTotal: got %lu, expected %lu\n",
			(unsigned long) SmHeapTotal, grandtotal);
	}
}
#endif /* !SM_HEAP_CHECK */