17c478bd9Sstevel@tonic-gate /* 2*058561cbSjbeck * Copyright (c) 2000-2001, 2006 Sendmail, Inc. and its suppliers. 37c478bd9Sstevel@tonic-gate * All rights reserved. 47c478bd9Sstevel@tonic-gate * 57c478bd9Sstevel@tonic-gate * By using this file, you agree to the terms and conditions set 67c478bd9Sstevel@tonic-gate * forth in the LICENSE file which can be found at the top level of 77c478bd9Sstevel@tonic-gate * the sendmail distribution. 87c478bd9Sstevel@tonic-gate * 9*058561cbSjbeck * $Id: heap.h,v 1.23 2006/08/15 00:53:46 ca Exp $ 107c478bd9Sstevel@tonic-gate */ 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate /* 157c478bd9Sstevel@tonic-gate ** Sendmail debugging memory allocation package. 167c478bd9Sstevel@tonic-gate ** See libsm/heap.html for documentation. 177c478bd9Sstevel@tonic-gate */ 187c478bd9Sstevel@tonic-gate 197c478bd9Sstevel@tonic-gate #ifndef SM_HEAP_H 207c478bd9Sstevel@tonic-gate # define SM_HEAP_H 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate # include <sm/io.h> 237c478bd9Sstevel@tonic-gate # include <stdlib.h> 247c478bd9Sstevel@tonic-gate # include <sm/debug.h> 257c478bd9Sstevel@tonic-gate # include <sm/exc.h> 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* change default to 0 for production? */ 287c478bd9Sstevel@tonic-gate # ifndef SM_HEAP_CHECK 297c478bd9Sstevel@tonic-gate # define SM_HEAP_CHECK 1 307c478bd9Sstevel@tonic-gate # endif /* ! SM_HEAP_CHECK */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate # if SM_HEAP_CHECK 337c478bd9Sstevel@tonic-gate # define sm_malloc_x(sz) sm_malloc_tagged_x(sz, __FILE__, __LINE__, SmHeapGroup) 347c478bd9Sstevel@tonic-gate # define sm_malloc(size) sm_malloc_tagged(size, __FILE__, __LINE__, SmHeapGroup) 357c478bd9Sstevel@tonic-gate # define sm_free(ptr) sm_free_tagged(ptr, __FILE__, __LINE__) 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate extern void *sm_malloc_tagged __P((size_t, char *, int, int)); 387c478bd9Sstevel@tonic-gate extern void *sm_malloc_tagged_x __P((size_t, char *, int, int)); 397c478bd9Sstevel@tonic-gate extern void sm_free_tagged __P((void *, char *, int)); 407c478bd9Sstevel@tonic-gate extern void *sm_realloc_x __P((void *, size_t)); 417c478bd9Sstevel@tonic-gate extern bool sm_heap_register __P((void *, size_t, char *, int, int)); 427c478bd9Sstevel@tonic-gate extern void sm_heap_checkptr_tagged __P((void *, char *, int)); 437c478bd9Sstevel@tonic-gate extern void sm_heap_report __P((SM_FILE_T *, int)); 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate # else /* SM_HEAP_CHECK */ 467c478bd9Sstevel@tonic-gate # define sm_malloc_tagged(size, file, line, grp) sm_malloc(size) 477c478bd9Sstevel@tonic-gate # define sm_malloc_tagged_x(size, file, line, grp) sm_malloc_x(size) 487c478bd9Sstevel@tonic-gate # define sm_free_tagged(ptr, file, line) sm_free(ptr) 497c478bd9Sstevel@tonic-gate # define sm_heap_register(ptr, size, file, line, grp) (true) 507c478bd9Sstevel@tonic-gate # define sm_heap_checkptr_tagged(ptr, tag, num) ((void)0) 517c478bd9Sstevel@tonic-gate # define sm_heap_report(file, verbose) ((void)0) 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate extern void *sm_malloc __P((size_t)); 547c478bd9Sstevel@tonic-gate extern void *sm_malloc_x __P((size_t)); 557c478bd9Sstevel@tonic-gate extern void *sm_realloc_x __P((void *, size_t)); 567c478bd9Sstevel@tonic-gate extern void sm_free __P((void *)); 577c478bd9Sstevel@tonic-gate # endif /* SM_HEAP_CHECK */ 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate extern void *sm_realloc __P((void *, size_t)); 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate # define sm_heap_checkptr(ptr) sm_heap_checkptr_tagged(ptr, __FILE__, __LINE__) 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #if 0 647c478bd9Sstevel@tonic-gate /* 657c478bd9Sstevel@tonic-gate ** sm_f[mc]alloc are plug in replacements for malloc and calloc 667c478bd9Sstevel@tonic-gate ** which can be used in a context requiring a function pointer, 677c478bd9Sstevel@tonic-gate ** and which are compatible with sm_free. Warning: sm_heap_report 687c478bd9Sstevel@tonic-gate ** cannot report where storage leaked by sm_f[mc]alloc was allocated. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* XXX unused right now */ 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate extern void * 747c478bd9Sstevel@tonic-gate sm_fmalloc __P(( 757c478bd9Sstevel@tonic-gate size_t)); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate extern void * 787c478bd9Sstevel@tonic-gate sm_fcalloc __P(( 797c478bd9Sstevel@tonic-gate size_t, 807c478bd9Sstevel@tonic-gate size_t)); 817c478bd9Sstevel@tonic-gate #endif /* 0 */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate ** Allocate 'permanent' storage that can be freed but may still be 857c478bd9Sstevel@tonic-gate ** allocated when the process exits. sm_heap_report will not complain 867c478bd9Sstevel@tonic-gate ** about a storage leak originating from a call to sm_pmalloc. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate # define sm_pmalloc(size) sm_malloc_tagged(size, __FILE__, __LINE__, 0) 907c478bd9Sstevel@tonic-gate # define sm_pmalloc_x(size) sm_malloc_tagged_x(size, __FILE__, __LINE__, 0) 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate # define sm_heap_group() SmHeapGroup 937c478bd9Sstevel@tonic-gate # define sm_heap_setgroup(g) (SmHeapGroup = (g)) 947c478bd9Sstevel@tonic-gate # define sm_heap_newgroup() (SmHeapGroup = ++SmHeapMaxGroup) 957c478bd9Sstevel@tonic-gate 96*058561cbSjbeck #define SM_FREE(ptr) \ 97*058561cbSjbeck do \ 98*058561cbSjbeck { \ 99*058561cbSjbeck if ((ptr) != NULL) \ 100*058561cbSjbeck { \ 101*058561cbSjbeck sm_free(ptr); \ 102*058561cbSjbeck (ptr) = NULL; \ 103*058561cbSjbeck } \ 104*058561cbSjbeck } while (0) 105*058561cbSjbeck 1067c478bd9Sstevel@tonic-gate extern int SmHeapGroup; 1077c478bd9Sstevel@tonic-gate extern int SmHeapMaxGroup; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate extern SM_DEBUG_T SmHeapTrace; 1107c478bd9Sstevel@tonic-gate extern SM_DEBUG_T SmHeapCheck; 1117c478bd9Sstevel@tonic-gate extern SM_EXC_T SmHeapOutOfMemory; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate #endif /* ! SM_HEAP_H */ 114