17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*b5016cbbSstephh * Common Development and Distribution License (the "License"). 6*b5016cbbSstephh * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*b5016cbbSstephh * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate * 257c478bd9Sstevel@tonic-gate * alloc.c -- memory allocation wrapper functions, replacable in more 267c478bd9Sstevel@tonic-gate * constrained environments, such as within a DE. 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "alloc.h" 357c478bd9Sstevel@tonic-gate #include "out.h" 367c478bd9Sstevel@tonic-gate #include "stats.h" 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate static struct stats *Malloctotal; 397c478bd9Sstevel@tonic-gate static struct stats *Malloccount; 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate void 427c478bd9Sstevel@tonic-gate alloc_init(void) 437c478bd9Sstevel@tonic-gate { 447c478bd9Sstevel@tonic-gate Malloctotal = stats_new_counter("alloc.total", "bytes allocated", 1); 457c478bd9Sstevel@tonic-gate Malloccount = stats_new_counter("alloc.calls", "total calls", 1); 467c478bd9Sstevel@tonic-gate } 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate void 497c478bd9Sstevel@tonic-gate alloc_fini(void) 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate struct stats *mt, *mc; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate mt = Malloctotal; 547c478bd9Sstevel@tonic-gate mc = Malloccount; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate Malloctotal = NULL; 577c478bd9Sstevel@tonic-gate Malloccount = NULL; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate stats_delete(mt); 607c478bd9Sstevel@tonic-gate stats_delete(mc); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * alloc_malloc -- a malloc() with checks 657c478bd9Sstevel@tonic-gate * 667c478bd9Sstevel@tonic-gate * this routine is typically called via the MALLOC() macro in alloc.h 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate void * 707c478bd9Sstevel@tonic-gate alloc_malloc(size_t nbytes, const char *fname, int line) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate void *retval = malloc(nbytes); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (retval == NULL) 757c478bd9Sstevel@tonic-gate outfl(O_DIE, fname, line, "malloc: out of memory"); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if (Malloctotal) 787c478bd9Sstevel@tonic-gate stats_counter_add(Malloctotal, nbytes); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate if (Malloccount) 817c478bd9Sstevel@tonic-gate stats_counter_bump(Malloccount); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate return (retval); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate /* 877c478bd9Sstevel@tonic-gate * alloc_realloc -- a realloc() with checks 887c478bd9Sstevel@tonic-gate * 897c478bd9Sstevel@tonic-gate * this routine is typically called via the REALLOC() macro in alloc.h 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate void * 927c478bd9Sstevel@tonic-gate alloc_realloc(void *ptr, size_t nbytes, const char *fname, int line) 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate void *retval = realloc(ptr, nbytes); 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate if (retval == NULL) 977c478bd9Sstevel@tonic-gate out(O_DIE, fname, line, "realloc: out of memory"); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate return (retval); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate /* 1037c478bd9Sstevel@tonic-gate * alloc_strdup -- a strdup() with checks 1047c478bd9Sstevel@tonic-gate * 1057c478bd9Sstevel@tonic-gate * this routine is typically called via the STRDUP() macro in alloc.h 1067c478bd9Sstevel@tonic-gate */ 1077c478bd9Sstevel@tonic-gate char * 1087c478bd9Sstevel@tonic-gate alloc_strdup(const char *ptr, const char *fname, int line) 1097c478bd9Sstevel@tonic-gate { 1107c478bd9Sstevel@tonic-gate char *retval = strdup(ptr); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (retval == NULL) 1137c478bd9Sstevel@tonic-gate outfl(O_DIE, fname, line, "strdup: out of memory"); 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate return (retval); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * alloc_free -- a free() with checks 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * this routine is typically called via the FREE() macro in alloc.h 1227c478bd9Sstevel@tonic-gate */ 1237c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 1247c478bd9Sstevel@tonic-gate void 1257c478bd9Sstevel@tonic-gate alloc_free(void *ptr, const char *fname, int line) 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate /* nothing to check in this version */ 1287c478bd9Sstevel@tonic-gate free(ptr); 1297c478bd9Sstevel@tonic-gate } 130*b5016cbbSstephh 131*b5016cbbSstephh /* 132*b5016cbbSstephh * variants that don't maintain size in header - saves space 133*b5016cbbSstephh */ 134*b5016cbbSstephh void * 135*b5016cbbSstephh alloc_xmalloc(size_t nbytes) 136*b5016cbbSstephh { 137*b5016cbbSstephh void *retval; 138*b5016cbbSstephh 139*b5016cbbSstephh retval = malloc(nbytes); 140*b5016cbbSstephh if (retval == NULL) 141*b5016cbbSstephh out(O_DIE, "malloc: out of memory"); 142*b5016cbbSstephh if (Malloctotal) 143*b5016cbbSstephh stats_counter_add(Malloctotal, nbytes); 144*b5016cbbSstephh if (Malloccount) 145*b5016cbbSstephh stats_counter_bump(Malloccount); 146*b5016cbbSstephh return (retval); 147*b5016cbbSstephh } 148*b5016cbbSstephh 149*b5016cbbSstephh /*ARGSUSED*/ 150*b5016cbbSstephh void 151*b5016cbbSstephh alloc_xfree(void *ptr, size_t size) 152*b5016cbbSstephh { 153*b5016cbbSstephh free(ptr); 154*b5016cbbSstephh } 155