17c478bd9Sstevel@tonic-gate /*
29525b14bSRao Shoaib  * Copyright (c) 2005 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1997,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
99525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
109525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
119525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
129525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
139525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
149525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
159525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate 
19*55fea89dSDan Cross /* When this symbol is defined allocations via memget are made slightly
20*55fea89dSDan Cross    bigger and some debugging info stuck before and after the region given
217c478bd9Sstevel@tonic-gate    back to the caller. */
227c478bd9Sstevel@tonic-gate /* #define DEBUGGING_MEMCLUSTER */
237c478bd9Sstevel@tonic-gate #define MEMCLUSTER_ATEND
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include "port_before.h"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/uio.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <netinet/in.h>
337c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
347c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <time.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <isc/memcluster.h>
437c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "port_after.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
487c478bd9Sstevel@tonic-gate #ifndef DEBUGGING_MEMCLUSTER
497c478bd9Sstevel@tonic-gate #define DEBUGGING_MEMCLUSTER
507c478bd9Sstevel@tonic-gate #endif
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define DEF_MAX_SIZE		1100
547c478bd9Sstevel@tonic-gate #define DEF_MEM_TARGET		4096
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate typedef u_int32_t fence_t;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate typedef struct {
597c478bd9Sstevel@tonic-gate 	void *			next;
607c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
617c478bd9Sstevel@tonic-gate #if defined(MEMCLUSTER_RECORD)
627c478bd9Sstevel@tonic-gate 	const char *		file;
637c478bd9Sstevel@tonic-gate 	int			line;
647c478bd9Sstevel@tonic-gate #endif
657c478bd9Sstevel@tonic-gate 	size_t			size;
667c478bd9Sstevel@tonic-gate 	fence_t			fencepost;
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate } memcluster_element;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define SMALL_SIZE_LIMIT sizeof(memcluster_element)
717c478bd9Sstevel@tonic-gate #define P_SIZE sizeof(void *)
727c478bd9Sstevel@tonic-gate #define FRONT_FENCEPOST 0xfebafeba
737c478bd9Sstevel@tonic-gate #define BACK_FENCEPOST 0xabefabef
747c478bd9Sstevel@tonic-gate #define FENCEPOST_SIZE 4
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifndef MEMCLUSTER_LITTLE_MALLOC
777c478bd9Sstevel@tonic-gate #define MEMCLUSTER_BIG_MALLOC 1
787c478bd9Sstevel@tonic-gate #define NUM_BASIC_BLOCKS 64
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate struct stats {
827c478bd9Sstevel@tonic-gate 	u_long			gets;
837c478bd9Sstevel@tonic-gate 	u_long			totalgets;
847c478bd9Sstevel@tonic-gate 	u_long			blocks;
857c478bd9Sstevel@tonic-gate 	u_long			freefrags;
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
889525b14bSRao Shoaib #ifdef DO_PTHREADS
899525b14bSRao Shoaib #include <pthread.h>
909525b14bSRao Shoaib static pthread_mutex_t	memlock = PTHREAD_MUTEX_INITIALIZER;
919525b14bSRao Shoaib #define MEMLOCK		(void)pthread_mutex_lock(&memlock)
929525b14bSRao Shoaib #define MEMUNLOCK	(void)pthread_mutex_unlock(&memlock)
937c478bd9Sstevel@tonic-gate #else
949525b14bSRao Shoaib /*
959525b14bSRao Shoaib  * Catch bad lock usage in non threaded build.
969525b14bSRao Shoaib  */
979525b14bSRao Shoaib static unsigned int	memlock = 0;
989525b14bSRao Shoaib #define MEMLOCK		do { INSIST(memlock == 0); memlock = 1; } while (0)
999525b14bSRao Shoaib #define MEMUNLOCK	do { INSIST(memlock == 1); memlock = 0; } while (0)
1009525b14bSRao Shoaib #endif  /* DO_PTHEADS */
1019525b14bSRao Shoaib 
1029525b14bSRao Shoaib /* Private data. */
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static size_t			max_size;
1057c478bd9Sstevel@tonic-gate static size_t			mem_target;
1069525b14bSRao Shoaib #ifndef MEMCLUSTER_BIG_MALLOC
1077c478bd9Sstevel@tonic-gate static size_t			mem_target_half;
1087c478bd9Sstevel@tonic-gate static size_t			mem_target_fudge;
1099525b14bSRao Shoaib #endif
1107c478bd9Sstevel@tonic-gate static memcluster_element **	freelists;
1117c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
1127c478bd9Sstevel@tonic-gate static memcluster_element **	activelists;
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_BIG_MALLOC
1157c478bd9Sstevel@tonic-gate static memcluster_element *	basic_blocks;
1167c478bd9Sstevel@tonic-gate #endif
1177c478bd9Sstevel@tonic-gate static struct stats *		stats;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* Forward. */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static size_t			quantize(size_t);
1227c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
1237c478bd9Sstevel@tonic-gate static void			check(unsigned char *, int, size_t);
1247c478bd9Sstevel@tonic-gate #endif
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* Public. */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate int
meminit(size_t init_max_size,size_t target_size)1297c478bd9Sstevel@tonic-gate meminit(size_t init_max_size, size_t target_size) {
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
1327c478bd9Sstevel@tonic-gate 	INSIST(sizeof(fence_t) == FENCEPOST_SIZE);
1337c478bd9Sstevel@tonic-gate #endif
1347c478bd9Sstevel@tonic-gate 	if (freelists != NULL) {
1357c478bd9Sstevel@tonic-gate 		errno = EEXIST;
1367c478bd9Sstevel@tonic-gate 		return (-1);
1377c478bd9Sstevel@tonic-gate 	}
1389525b14bSRao Shoaib 	if (init_max_size == 0U)
1397c478bd9Sstevel@tonic-gate 		max_size = DEF_MAX_SIZE;
1407c478bd9Sstevel@tonic-gate 	else
1417c478bd9Sstevel@tonic-gate 		max_size = init_max_size;
1429525b14bSRao Shoaib 	if (target_size == 0U)
1437c478bd9Sstevel@tonic-gate 		mem_target = DEF_MEM_TARGET;
1447c478bd9Sstevel@tonic-gate 	else
1457c478bd9Sstevel@tonic-gate 		mem_target = target_size;
1469525b14bSRao Shoaib #ifndef MEMCLUSTER_BIG_MALLOC
1477c478bd9Sstevel@tonic-gate 	mem_target_half = mem_target / 2;
1487c478bd9Sstevel@tonic-gate 	mem_target_fudge = mem_target + mem_target / 4;
1499525b14bSRao Shoaib #endif
1507c478bd9Sstevel@tonic-gate 	freelists = malloc(max_size * sizeof (memcluster_element *));
1517c478bd9Sstevel@tonic-gate 	stats = malloc((max_size+1) * sizeof (struct stats));
1527c478bd9Sstevel@tonic-gate 	if (freelists == NULL || stats == NULL) {
1537c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1547c478bd9Sstevel@tonic-gate 		return (-1);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 	memset(freelists, 0,
1577c478bd9Sstevel@tonic-gate 	       max_size * sizeof (memcluster_element *));
1587c478bd9Sstevel@tonic-gate 	memset(stats, 0, (max_size + 1) * sizeof (struct stats));
1597c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
1607c478bd9Sstevel@tonic-gate 	activelists = malloc((max_size + 1) * sizeof (memcluster_element *));
1617c478bd9Sstevel@tonic-gate 	if (activelists == NULL) {
1627c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
1637c478bd9Sstevel@tonic-gate 		return (-1);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	memset(activelists, 0,
1667c478bd9Sstevel@tonic-gate 	       (max_size + 1) * sizeof (memcluster_element *));
1677c478bd9Sstevel@tonic-gate #endif
1687c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_BIG_MALLOC
1697c478bd9Sstevel@tonic-gate 	basic_blocks = NULL;
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 	return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate void *
__memget(size_t size)1757c478bd9Sstevel@tonic-gate __memget(size_t size) {
1767c478bd9Sstevel@tonic-gate 	return (__memget_record(size, NULL, 0));
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate void *
__memget_record(size_t size,const char * file,int line)1807c478bd9Sstevel@tonic-gate __memget_record(size_t size, const char *file, int line) {
1817c478bd9Sstevel@tonic-gate 	size_t new_size = quantize(size);
1827c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
1837c478bd9Sstevel@tonic-gate 	memcluster_element *e;
1847c478bd9Sstevel@tonic-gate 	char *p;
1857c478bd9Sstevel@tonic-gate 	fence_t fp = BACK_FENCEPOST;
1867c478bd9Sstevel@tonic-gate #endif
1877c478bd9Sstevel@tonic-gate 	void *ret;
1887c478bd9Sstevel@tonic-gate 
1899525b14bSRao Shoaib 	MEMLOCK;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #if !defined(MEMCLUSTER_RECORD)
1927c478bd9Sstevel@tonic-gate 	UNUSED(file);
1937c478bd9Sstevel@tonic-gate 	UNUSED(line);
1947c478bd9Sstevel@tonic-gate #endif
1959525b14bSRao Shoaib 	if (freelists == NULL) {
1969525b14bSRao Shoaib 		if (meminit(0, 0) == -1) {
1979525b14bSRao Shoaib 			MEMUNLOCK;
1987c478bd9Sstevel@tonic-gate 			return (NULL);
1999525b14bSRao Shoaib 		}
2009525b14bSRao Shoaib 	}
2019525b14bSRao Shoaib 	if (size == 0U) {
2029525b14bSRao Shoaib 		MEMUNLOCK;
2037c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2047c478bd9Sstevel@tonic-gate 		return (NULL);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 	if (size >= max_size || new_size >= max_size) {
2077c478bd9Sstevel@tonic-gate 		/* memget() was called on something beyond our upper limit. */
2087c478bd9Sstevel@tonic-gate 		stats[max_size].gets++;
2097c478bd9Sstevel@tonic-gate 		stats[max_size].totalgets++;
2107c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
2117c478bd9Sstevel@tonic-gate 		e = malloc(new_size);
2127c478bd9Sstevel@tonic-gate 		if (e == NULL) {
2139525b14bSRao Shoaib 			MEMUNLOCK;
2147c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
2157c478bd9Sstevel@tonic-gate 			return (NULL);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 		e->next = NULL;
2187c478bd9Sstevel@tonic-gate 		e->size = size;
2197c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
2207c478bd9Sstevel@tonic-gate 		e->file = file;
2217c478bd9Sstevel@tonic-gate 		e->line = line;
2227c478bd9Sstevel@tonic-gate 		e->next = activelists[max_size];
2237c478bd9Sstevel@tonic-gate 		activelists[max_size] = e;
2247c478bd9Sstevel@tonic-gate #endif
2259525b14bSRao Shoaib 		MEMUNLOCK;
2267c478bd9Sstevel@tonic-gate 		e->fencepost = FRONT_FENCEPOST;
2277c478bd9Sstevel@tonic-gate 		p = (char *)e + sizeof *e + size;
2287c478bd9Sstevel@tonic-gate 		memcpy(p, &fp, sizeof fp);
2297c478bd9Sstevel@tonic-gate 		return ((char *)e + sizeof *e);
2307c478bd9Sstevel@tonic-gate #else
2319525b14bSRao Shoaib 		MEMUNLOCK;
2327c478bd9Sstevel@tonic-gate 		return (malloc(size));
2337c478bd9Sstevel@tonic-gate #endif
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
236*55fea89dSDan Cross 	/*
2377c478bd9Sstevel@tonic-gate 	 * If there are no blocks in the free list for this size, get a chunk
2387c478bd9Sstevel@tonic-gate 	 * of memory and then break it up into "new_size"-sized blocks, adding
2397c478bd9Sstevel@tonic-gate 	 * them to the free list.
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	if (freelists[new_size] == NULL) {
2427c478bd9Sstevel@tonic-gate 		int i, frags;
2437c478bd9Sstevel@tonic-gate 		size_t total_size;
2447c478bd9Sstevel@tonic-gate 		void *new;
2457c478bd9Sstevel@tonic-gate 		char *curr, *next;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_BIG_MALLOC
2487c478bd9Sstevel@tonic-gate 		if (basic_blocks == NULL) {
2497c478bd9Sstevel@tonic-gate 			new = malloc(NUM_BASIC_BLOCKS * mem_target);
2507c478bd9Sstevel@tonic-gate 			if (new == NULL) {
2519525b14bSRao Shoaib 				MEMUNLOCK;
2527c478bd9Sstevel@tonic-gate 				errno = ENOMEM;
2537c478bd9Sstevel@tonic-gate 				return (NULL);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			curr = new;
2567c478bd9Sstevel@tonic-gate 			next = curr + mem_target;
2577c478bd9Sstevel@tonic-gate 			for (i = 0; i < (NUM_BASIC_BLOCKS - 1); i++) {
2587c478bd9Sstevel@tonic-gate 				((memcluster_element *)curr)->next = next;
2597c478bd9Sstevel@tonic-gate 				curr = next;
2607c478bd9Sstevel@tonic-gate 				next += mem_target;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 			/*
2637c478bd9Sstevel@tonic-gate 			 * curr is now pointing at the last block in the
2647c478bd9Sstevel@tonic-gate 			 * array.
2657c478bd9Sstevel@tonic-gate 			 */
2667c478bd9Sstevel@tonic-gate 			((memcluster_element *)curr)->next = NULL;
2677c478bd9Sstevel@tonic-gate 			basic_blocks = new;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 		total_size = mem_target;
2707c478bd9Sstevel@tonic-gate 		new = basic_blocks;
2717c478bd9Sstevel@tonic-gate 		basic_blocks = basic_blocks->next;
2727c478bd9Sstevel@tonic-gate #else
2737c478bd9Sstevel@tonic-gate 		if (new_size > mem_target_half)
2747c478bd9Sstevel@tonic-gate 			total_size = mem_target_fudge;
2757c478bd9Sstevel@tonic-gate 		else
2767c478bd9Sstevel@tonic-gate 			total_size = mem_target;
2777c478bd9Sstevel@tonic-gate 		new = malloc(total_size);
2787c478bd9Sstevel@tonic-gate 		if (new == NULL) {
2799525b14bSRao Shoaib 			MEMUNLOCK;
2807c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
2817c478bd9Sstevel@tonic-gate 			return (NULL);
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate #endif
2847c478bd9Sstevel@tonic-gate 		frags = total_size / new_size;
2857c478bd9Sstevel@tonic-gate 		stats[new_size].blocks++;
2867c478bd9Sstevel@tonic-gate 		stats[new_size].freefrags += frags;
2877c478bd9Sstevel@tonic-gate 		/* Set up a linked-list of blocks of size "new_size". */
2887c478bd9Sstevel@tonic-gate 		curr = new;
2897c478bd9Sstevel@tonic-gate 		next = curr + new_size;
2907c478bd9Sstevel@tonic-gate 		for (i = 0; i < (frags - 1); i++) {
2917c478bd9Sstevel@tonic-gate #if defined (DEBUGGING_MEMCLUSTER)
2927c478bd9Sstevel@tonic-gate 			memset(curr, 0xa5, new_size);
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 			((memcluster_element *)curr)->next = next;
2957c478bd9Sstevel@tonic-gate 			curr = next;
2967c478bd9Sstevel@tonic-gate 			next += new_size;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		/* curr is now pointing at the last block in the array. */
2997c478bd9Sstevel@tonic-gate #if defined (DEBUGGING_MEMCLUSTER)
3007c478bd9Sstevel@tonic-gate 		memset(curr, 0xa5, new_size);
3017c478bd9Sstevel@tonic-gate #endif
3027c478bd9Sstevel@tonic-gate 		((memcluster_element *)curr)->next = freelists[new_size];
3037c478bd9Sstevel@tonic-gate 		freelists[new_size] = new;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/* The free list uses the "rounded-up" size "new_size". */
3077c478bd9Sstevel@tonic-gate #if defined (DEBUGGING_MEMCLUSTER)
3087c478bd9Sstevel@tonic-gate 	e = freelists[new_size];
3097c478bd9Sstevel@tonic-gate 	ret = (char *)e + sizeof *e;
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Check to see if this buffer has been written to while on free list.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	check(ret, 0xa5, new_size - sizeof *e);
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Mark memory we are returning.
3167c478bd9Sstevel@tonic-gate 	 */
3177c478bd9Sstevel@tonic-gate 	memset(ret, 0xe5, size);
3187c478bd9Sstevel@tonic-gate #else
3197c478bd9Sstevel@tonic-gate 	ret = freelists[new_size];
3207c478bd9Sstevel@tonic-gate #endif
3217c478bd9Sstevel@tonic-gate 	freelists[new_size] = freelists[new_size]->next;
3227c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
3237c478bd9Sstevel@tonic-gate 	e->next = NULL;
3247c478bd9Sstevel@tonic-gate 	e->size = size;
3257c478bd9Sstevel@tonic-gate 	e->fencepost = FRONT_FENCEPOST;
3267c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
3277c478bd9Sstevel@tonic-gate 	e->file = file;
3287c478bd9Sstevel@tonic-gate 	e->line = line;
3297c478bd9Sstevel@tonic-gate 	e->next = activelists[size];
3307c478bd9Sstevel@tonic-gate 	activelists[size] = e;
3317c478bd9Sstevel@tonic-gate #endif
3327c478bd9Sstevel@tonic-gate 	p = (char *)e + sizeof *e + size;
3337c478bd9Sstevel@tonic-gate 	memcpy(p, &fp, sizeof fp);
3347c478bd9Sstevel@tonic-gate #endif
3357c478bd9Sstevel@tonic-gate 
336*55fea89dSDan Cross 	/*
3377c478bd9Sstevel@tonic-gate 	 * The stats[] uses the _actual_ "size" requested by the
3387c478bd9Sstevel@tonic-gate 	 * caller, with the caveat (in the code above) that "size" >= the
3397c478bd9Sstevel@tonic-gate 	 * max. size (max_size) ends up getting recorded as a call to
3407c478bd9Sstevel@tonic-gate 	 * max_size.
3417c478bd9Sstevel@tonic-gate 	 */
3427c478bd9Sstevel@tonic-gate 	stats[size].gets++;
3437c478bd9Sstevel@tonic-gate 	stats[size].totalgets++;
3447c478bd9Sstevel@tonic-gate 	stats[new_size].freefrags--;
3459525b14bSRao Shoaib 	MEMUNLOCK;
3467c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
3477c478bd9Sstevel@tonic-gate 	return ((char *)e + sizeof *e);
3487c478bd9Sstevel@tonic-gate #else
3497c478bd9Sstevel@tonic-gate 	return (ret);
3507c478bd9Sstevel@tonic-gate #endif
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
3539525b14bSRao Shoaib /*%
354*55fea89dSDan Cross  * This is a call from an external caller,
355*55fea89dSDan Cross  * so we want to count this as a user "put".
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate void
__memput(void * mem,size_t size)3587c478bd9Sstevel@tonic-gate __memput(void *mem, size_t size) {
3597c478bd9Sstevel@tonic-gate 	__memput_record(mem, size, NULL, 0);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate void
__memput_record(void * mem,size_t size,const char * file,int line)3637c478bd9Sstevel@tonic-gate __memput_record(void *mem, size_t size, const char *file, int line) {
3647c478bd9Sstevel@tonic-gate 	size_t new_size = quantize(size);
3657c478bd9Sstevel@tonic-gate #if defined (DEBUGGING_MEMCLUSTER)
3667c478bd9Sstevel@tonic-gate 	memcluster_element *e;
3677c478bd9Sstevel@tonic-gate 	memcluster_element *el;
3687c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
3697c478bd9Sstevel@tonic-gate 	memcluster_element *prev;
3707c478bd9Sstevel@tonic-gate #endif
3717c478bd9Sstevel@tonic-gate 	fence_t fp;
3727c478bd9Sstevel@tonic-gate 	char *p;
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 
3759525b14bSRao Shoaib 	MEMLOCK;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #if !defined (MEMCLUSTER_RECORD)
3787c478bd9Sstevel@tonic-gate 	UNUSED(file);
3797c478bd9Sstevel@tonic-gate 	UNUSED(line);
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	REQUIRE(freelists != NULL);
3837c478bd9Sstevel@tonic-gate 
3849525b14bSRao Shoaib 	if (size == 0U) {
3859525b14bSRao Shoaib 		MEMUNLOCK;
3867c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate #if defined (DEBUGGING_MEMCLUSTER)
3917c478bd9Sstevel@tonic-gate 	e = (memcluster_element *) ((char *)mem - sizeof *e);
3927c478bd9Sstevel@tonic-gate 	INSIST(e->fencepost == FRONT_FENCEPOST);
3937c478bd9Sstevel@tonic-gate 	INSIST(e->size == size);
3947c478bd9Sstevel@tonic-gate 	p = (char *)e + sizeof *e + size;
3957c478bd9Sstevel@tonic-gate 	memcpy(&fp, p, sizeof fp);
3967c478bd9Sstevel@tonic-gate 	INSIST(fp == BACK_FENCEPOST);
3979525b14bSRao Shoaib 	INSIST(((u_long)mem % 4) == 0);
3987c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
3997c478bd9Sstevel@tonic-gate 	prev = NULL;
4007c478bd9Sstevel@tonic-gate 	if (size == max_size || new_size >= max_size)
4017c478bd9Sstevel@tonic-gate 		el = activelists[max_size];
4027c478bd9Sstevel@tonic-gate 	else
4037c478bd9Sstevel@tonic-gate 		el = activelists[size];
4047c478bd9Sstevel@tonic-gate 	while (el != NULL && el != e) {
4057c478bd9Sstevel@tonic-gate 		prev = el;
4067c478bd9Sstevel@tonic-gate 		el = el->next;
4077c478bd9Sstevel@tonic-gate 	}
4089525b14bSRao Shoaib 	INSIST(el != NULL);	/*%< double free */
4097c478bd9Sstevel@tonic-gate 	if (prev == NULL) {
4107c478bd9Sstevel@tonic-gate 		if (size == max_size || new_size >= max_size)
4117c478bd9Sstevel@tonic-gate 			activelists[max_size] = el->next;
4127c478bd9Sstevel@tonic-gate 		else
4137c478bd9Sstevel@tonic-gate 			activelists[size] = el->next;
4147c478bd9Sstevel@tonic-gate 	} else
4157c478bd9Sstevel@tonic-gate 		prev->next = el->next;
4167c478bd9Sstevel@tonic-gate #endif
4177c478bd9Sstevel@tonic-gate #endif
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	if (size == max_size || new_size >= max_size) {
4207c478bd9Sstevel@tonic-gate 		/* memput() called on something beyond our upper limit */
4217c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
4227c478bd9Sstevel@tonic-gate 		free(e);
4237c478bd9Sstevel@tonic-gate #else
4247c478bd9Sstevel@tonic-gate 		free(mem);
4257c478bd9Sstevel@tonic-gate #endif
4267c478bd9Sstevel@tonic-gate 
4279525b14bSRao Shoaib 		INSIST(stats[max_size].gets != 0U);
4287c478bd9Sstevel@tonic-gate 		stats[max_size].gets--;
4299525b14bSRao Shoaib 		MEMUNLOCK;
4307c478bd9Sstevel@tonic-gate 		return;
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	/* The free list uses the "rounded-up" size "new_size": */
4347c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
4359525b14bSRao Shoaib 	memset(mem, 0xa5, new_size - sizeof *e); /*%< catch write after free */
4369525b14bSRao Shoaib 	e->size = 0;	/*%< catch double memput() */
4377c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
4387c478bd9Sstevel@tonic-gate 	e->file = file;
4397c478bd9Sstevel@tonic-gate 	e->line = line;
4407c478bd9Sstevel@tonic-gate #endif
4417c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_ATEND
4427c478bd9Sstevel@tonic-gate 	e->next = NULL;
4437c478bd9Sstevel@tonic-gate 	el = freelists[new_size];
4447c478bd9Sstevel@tonic-gate 	while (el != NULL && el->next != NULL)
4457c478bd9Sstevel@tonic-gate 		el = el->next;
4467c478bd9Sstevel@tonic-gate 	if (el)
4477c478bd9Sstevel@tonic-gate 		el->next = e;
4487c478bd9Sstevel@tonic-gate 	else
4497c478bd9Sstevel@tonic-gate 		freelists[new_size] = e;
4507c478bd9Sstevel@tonic-gate #else
4517c478bd9Sstevel@tonic-gate 	e->next = freelists[new_size];
4527c478bd9Sstevel@tonic-gate 	freelists[new_size] = (void *)e;
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate #else
4557c478bd9Sstevel@tonic-gate 	((memcluster_element *)mem)->next = freelists[new_size];
4567c478bd9Sstevel@tonic-gate 	freelists[new_size] = (memcluster_element *)mem;
4577c478bd9Sstevel@tonic-gate #endif
4587c478bd9Sstevel@tonic-gate 
459*55fea89dSDan Cross 	/*
4607c478bd9Sstevel@tonic-gate 	 * The stats[] uses the _actual_ "size" requested by the
4617c478bd9Sstevel@tonic-gate 	 * caller, with the caveat (in the code above) that "size" >= the
4627c478bd9Sstevel@tonic-gate 	 * max. size (max_size) ends up getting recorded as a call to
4637c478bd9Sstevel@tonic-gate 	 * max_size.
4647c478bd9Sstevel@tonic-gate 	 */
4659525b14bSRao Shoaib 	INSIST(stats[size].gets != 0U);
4667c478bd9Sstevel@tonic-gate 	stats[size].gets--;
4677c478bd9Sstevel@tonic-gate 	stats[new_size].freefrags++;
4689525b14bSRao Shoaib 	MEMUNLOCK;
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate void *
__memget_debug(size_t size,const char * file,int line)4727c478bd9Sstevel@tonic-gate __memget_debug(size_t size, const char *file, int line) {
4737c478bd9Sstevel@tonic-gate 	void *ptr;
4747c478bd9Sstevel@tonic-gate 	ptr = __memget_record(size, file, line);
4757c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s:%d: memget(%lu) -> %p\n", file, line,
4767c478bd9Sstevel@tonic-gate 		(u_long)size, ptr);
4777c478bd9Sstevel@tonic-gate 	return (ptr);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate void
__memput_debug(void * ptr,size_t size,const char * file,int line)4817c478bd9Sstevel@tonic-gate __memput_debug(void *ptr, size_t size, const char *file, int line) {
4827c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s:%d: memput(%p, %lu)\n", file, line, ptr,
4837c478bd9Sstevel@tonic-gate 		(u_long)size);
4847c478bd9Sstevel@tonic-gate 	__memput_record(ptr, size, file, line);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4879525b14bSRao Shoaib /*%
4887c478bd9Sstevel@tonic-gate  * Print the stats[] on the stream "out" with suitable formatting.
4897c478bd9Sstevel@tonic-gate  */
4907c478bd9Sstevel@tonic-gate void
memstats(FILE * out)4917c478bd9Sstevel@tonic-gate memstats(FILE *out) {
4927c478bd9Sstevel@tonic-gate 	size_t i;
4937c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
4947c478bd9Sstevel@tonic-gate 	memcluster_element *e;
4957c478bd9Sstevel@tonic-gate #endif
4967c478bd9Sstevel@tonic-gate 
4979525b14bSRao Shoaib 	MEMLOCK;
4989525b14bSRao Shoaib 
4999525b14bSRao Shoaib 	if (freelists == NULL) {
5009525b14bSRao Shoaib 		MEMUNLOCK;
5017c478bd9Sstevel@tonic-gate 		return;
5029525b14bSRao Shoaib 	}
5037c478bd9Sstevel@tonic-gate 	for (i = 1; i <= max_size; i++) {
5047c478bd9Sstevel@tonic-gate 		const struct stats *s = &stats[i];
5057c478bd9Sstevel@tonic-gate 
5069525b14bSRao Shoaib 		if (s->totalgets == 0U && s->gets == 0U)
5077c478bd9Sstevel@tonic-gate 			continue;
5089525b14bSRao Shoaib 		fprintf(out, "%s%5lu: %11lu gets, %11lu rem",
5097c478bd9Sstevel@tonic-gate 			(i == max_size) ? ">=" : "  ",
5109525b14bSRao Shoaib 			(unsigned long)i, s->totalgets, s->gets);
5119525b14bSRao Shoaib 		if (s->blocks != 0U)
5127c478bd9Sstevel@tonic-gate 			fprintf(out, " (%lu bl, %lu ff)",
5137c478bd9Sstevel@tonic-gate 				s->blocks, s->freefrags);
5147c478bd9Sstevel@tonic-gate 		fputc('\n', out);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate #ifdef MEMCLUSTER_RECORD
5177c478bd9Sstevel@tonic-gate 	fprintf(out, "Active Memory:\n");
5187c478bd9Sstevel@tonic-gate 	for (i = 1; i <= max_size; i++) {
5197c478bd9Sstevel@tonic-gate 		if ((e = activelists[i]) != NULL)
5207c478bd9Sstevel@tonic-gate 			while (e != NULL) {
5219525b14bSRao Shoaib 				fprintf(out, "%s:%d %p:%lu\n",
5227c478bd9Sstevel@tonic-gate 				        e->file != NULL ? e->file :
5237c478bd9Sstevel@tonic-gate 						"<UNKNOWN>", e->line,
5249525b14bSRao Shoaib 					(char *)e + sizeof *e,
5259525b14bSRao Shoaib 					(u_long)e->size);
5267c478bd9Sstevel@tonic-gate 				e = e->next;
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate #endif
5309525b14bSRao Shoaib 	MEMUNLOCK;
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate int
memactive(void)5347c478bd9Sstevel@tonic-gate memactive(void) {
5357c478bd9Sstevel@tonic-gate 	size_t i;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (stats == NULL)
5387c478bd9Sstevel@tonic-gate 		return (0);
5397c478bd9Sstevel@tonic-gate 	for (i = 1; i <= max_size; i++)
5409525b14bSRao Shoaib 		if (stats[i].gets != 0U)
5417c478bd9Sstevel@tonic-gate 			return (1);
5427c478bd9Sstevel@tonic-gate 	return (0);
5437c478bd9Sstevel@tonic-gate }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate /* Private. */
5467c478bd9Sstevel@tonic-gate 
5479525b14bSRao Shoaib /*%
5487c478bd9Sstevel@tonic-gate  * Round up size to a multiple of sizeof(void *).  This guarantees that a
5497c478bd9Sstevel@tonic-gate  * block is at least sizeof void *, and that we won't violate alignment
5507c478bd9Sstevel@tonic-gate  * restrictions, both of which are needed to make lists of blocks.
5517c478bd9Sstevel@tonic-gate  */
5527c478bd9Sstevel@tonic-gate static size_t
quantize(size_t size)5537c478bd9Sstevel@tonic-gate quantize(size_t size) {
5547c478bd9Sstevel@tonic-gate 	int remainder;
5557c478bd9Sstevel@tonic-gate 	/*
556*55fea89dSDan Cross 	 * If there is no remainder for the integer division of
5577c478bd9Sstevel@tonic-gate 	 *
5587c478bd9Sstevel@tonic-gate 	 *	(rightsize/P_SIZE)
5597c478bd9Sstevel@tonic-gate 	 *
5607c478bd9Sstevel@tonic-gate 	 * then we already have a good size; if not, then we need
5617c478bd9Sstevel@tonic-gate 	 * to round up the result in order to get a size big
5627c478bd9Sstevel@tonic-gate 	 * enough to satisfy the request _and_ aligned on P_SIZE boundaries.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	remainder = size % P_SIZE;
5657c478bd9Sstevel@tonic-gate 	if (remainder != 0)
5667c478bd9Sstevel@tonic-gate 		size += P_SIZE - remainder;
5677c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
5687c478bd9Sstevel@tonic-gate 	return (size + SMALL_SIZE_LIMIT + sizeof (int));
5697c478bd9Sstevel@tonic-gate #else
5707c478bd9Sstevel@tonic-gate 	return (size);
5717c478bd9Sstevel@tonic-gate #endif
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate #if defined(DEBUGGING_MEMCLUSTER)
5757c478bd9Sstevel@tonic-gate static void
check(unsigned char * a,int value,size_t len)5767c478bd9Sstevel@tonic-gate check(unsigned char *a, int value, size_t len) {
5777c478bd9Sstevel@tonic-gate 	size_t i;
5787c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
5797c478bd9Sstevel@tonic-gate 		INSIST(a[i] == value);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate #endif
5829525b14bSRao Shoaib 
5839525b14bSRao Shoaib /*! \file */
584