/*++
"This software program is available to you under a choice of one of two
licenses.  You may choose to be licensed under either the GNU General Public
License (GPL) Version 2, June 1991, available at
http://www.fsf.org/copyleft/gpl.html, or the Intel BSD + Patent License,
the text of which follows:

"Recipient" has requested a license and Intel Corporation ("Intel") is willing
to grant a license for the software entitled InfiniBand(tm) System Software
(the "Software") being provided by Intel Corporation.

The following definitions apply to this License:

"Licensed Patents" means patent claims licensable by Intel Corporation which
are necessarily infringed by the use or sale of the Software alone or when
combined with the operating system referred to below.

"Recipient" means the party to whom Intel delivers this Software.
"Licensee" means Recipient and those third parties that receive a license to
any operating system available under the GNU Public License version 2.0 or
later.

Copyright (c) 1996-2003 Intel Corporation. All rights reserved.

The license is provided to Recipient and Recipient's Licensees under the
following terms.

Redistribution and use in source and binary forms of the Software, with or
without modification, are permitted provided that the following conditions are
met:
Redistributions of source code of the Software may retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form of the Software may reproduce the above
copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

Neither the name of Intel Corporation nor the names of its contributors shall
be used to endorse or promote products derived from this Software without
specific prior written permission.

Intel hereby grants Recipient and Licensees a non-exclusive, worldwide,
royalty-free patent license under Licensed Patents to make, use, sell, offer
to sell, import and otherwise transfer the Software, if any, in source code
and object code form. This license shall include changes to the Software that
are error corrections or other minor changes to the Software that do not add
functionality or features when the Software is incorporated in any version of
a operating system that has been distributed under the GNU General Public
License 2.0 or later.  This patent license shall apply to the combination of
the Software and any operating system licensed under the GNU Public License
version 2.0 or later if, at the time Intel provides the Software to Recipient,
such addition of the Software to the then publicly available versions of such
operating system available under the GNU Public License version 2.0 or later
(whether in gold, beta or alpha form) causes such combination to be covered by
the Licensed Patents. The patent license shall not apply to any other
combinations which include the Software. No hardware per se is licensed
hereunder.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
--*/

/*
 * Abstract:
 *	Implementation of memory allocation tracking functions.
 *
 * Environment:
 *	All
 *
 * $Revision: 1.1 $
 */

#include "cl_memtrack.h"

cl_mem_tracker_t *gp_mem_tracker = NULL;

/*
 * Allocates memory.
 */
void *__cl_malloc_priv(IN const size_t size);

/*
 * Deallocates memory.
 */
void
 __cl_free_priv(IN void *const p_memory);

/*
 * Allocate and initialize the memory tracker object.
 */
static inline void
__cl_mem_track_start(void)
{
	cl_status_t status;

	if (gp_mem_tracker)
		return;

	/* Allocate the memory tracker object. */
	gp_mem_tracker = (cl_mem_tracker_t *)
	    __cl_malloc_priv(sizeof (cl_mem_tracker_t));

	if (!gp_mem_tracker)
		return;

	/* Initialize the free list. */
	cl_qlist_init(&gp_mem_tracker->free_hdr_list);
	/* Initialize the allocation list. */
	cl_qmap_init(&gp_mem_tracker->alloc_map);

	/* Initialize the spin lock to protect list operations. */
	status = cl_spinlock_init(&gp_mem_tracker->lock);
	if (status != CL_SUCCESS) {
		__cl_free_priv(gp_mem_tracker);
		return;
	}

	cl_msg_out("\n\n\n*** Memory tracker object address = %p ***\n\n\n",
		   gp_mem_tracker);
}

/*
 * Clean up memory tracking.
 */
static inline void
__cl_mem_track_stop(void)
{
	cl_map_item_t *p_map_item;
	cl_list_item_t *p_list_item;

	if (!gp_mem_tracker)
		return;

	if (cl_qmap_count(&gp_mem_tracker->alloc_map)) {
		/* There are still items in the list.  Print them out. */
		cl_mem_display();
	}

	/* Free all allocated headers. */
	cl_spinlock_acquire(&gp_mem_tracker->lock);
	while (cl_qmap_count(&gp_mem_tracker->alloc_map)) {
		p_map_item = cl_qmap_head(&gp_mem_tracker->alloc_map);
		cl_qmap_remove_item(&gp_mem_tracker->alloc_map, p_map_item);
		__cl_free_priv(PARENT_STRUCT
			       (p_map_item, cl_malloc_hdr_t, map_item));
	}

	while (cl_qlist_count(&gp_mem_tracker->free_hdr_list)) {
		p_list_item =
		    cl_qlist_remove_head(&gp_mem_tracker->free_hdr_list);
		__cl_free_priv(PARENT_STRUCT
			       (p_list_item, cl_malloc_hdr_t,
				map_item.pool_item.list_item));
	}
	cl_spinlock_release(&gp_mem_tracker->lock);

	/* Destory all objects in the memory tracker object. */
	cl_spinlock_destroy(&gp_mem_tracker->lock);

	/* Free the memory allocated for the memory tracker object. */
	__cl_free_priv(gp_mem_tracker);
}

/*
 * Enables memory allocation tracking.
 */
void
__cl_mem_track(IN const boolean_t start)
{
	if (start)
		__cl_mem_track_start();
	else
		__cl_mem_track_stop();
}

/*
 * Display memory usage.
 */
void
cl_mem_display(void)
{
	cl_map_item_t *p_map_item;
	cl_malloc_hdr_t *p_hdr;

	if (!gp_mem_tracker)
		return;

	cl_spinlock_acquire(&gp_mem_tracker->lock);
	cl_msg_out("\n\n\n*** Memory Usage ***\n");
	p_map_item = cl_qmap_head(&gp_mem_tracker->alloc_map);
	while (p_map_item != cl_qmap_end(&gp_mem_tracker->alloc_map)) {
		/*
		 * Get the pointer to the header.  Note that the object member of the
		 * list item will be used to store the pointer to the user's memory.
		 */
		p_hdr = PARENT_STRUCT(p_map_item, cl_malloc_hdr_t, map_item);

		cl_msg_out
		    ("\tMemory block at %p allocated in file %s line %d\n",
		     p_hdr->p_mem, p_hdr->file_name, p_hdr->line_num);

		p_map_item = cl_qmap_next(p_map_item);
	}
	cl_msg_out("*** End of Memory Usage ***\n\n");
	cl_spinlock_release(&gp_mem_tracker->lock);
}

/*
 * Allocates memory and stores information about the allocation in a list.
 * The contents of the list can be printed out by calling the function
 * "MemoryReportUsage".  Memory allocation will succeed even if the list
 * cannot be created.
 */
void *
__cl_malloc_trk(IN const char *const p_file_name,
		IN const int32_t line_num, IN const size_t size)
{
	cl_malloc_hdr_t *p_hdr;
	cl_list_item_t *p_list_item;
	void *p_mem;
	char temp_buf[FILE_NAME_LENGTH];
	int32_t temp_line;

	/*
	 * Allocate the memory first, so that we give the user's allocation
	 * priority over the the header allocation.
	 */
	p_mem = __cl_malloc_priv(size);

	if (!p_mem)
		return (NULL);

	if (!gp_mem_tracker)
		return (p_mem);

	/*
	 * Make copies of the file name and line number in case those
	 * parameters are in paged pool.
	 */
	temp_line = line_num;
	strncpy(temp_buf, p_file_name, FILE_NAME_LENGTH);
	/* Make sure the string is null terminated. */
	temp_buf[FILE_NAME_LENGTH - 1] = '\0';

	cl_spinlock_acquire(&gp_mem_tracker->lock);

	/* Get a header from the free header list. */
	p_list_item = cl_qlist_remove_head(&gp_mem_tracker->free_hdr_list);
	if (p_list_item != cl_qlist_end(&gp_mem_tracker->free_hdr_list)) {
		/* Set the header pointer to the header retrieved from the list. */
		p_hdr = PARENT_STRUCT(p_list_item, cl_malloc_hdr_t,
				      map_item.pool_item.list_item);
	} else {
		/* We failed to get a free header.  Allocate one. */
		p_hdr = __cl_malloc_priv(sizeof (cl_malloc_hdr_t));
		if (!p_hdr) {
			/* We failed to allocate the header.  Return the user's memory. */
			cl_spinlock_release(&gp_mem_tracker->lock);
			return (p_mem);
		}
	}
	cl_memcpy(p_hdr->file_name, temp_buf, FILE_NAME_LENGTH);
	p_hdr->line_num = temp_line;
	/*
	 * We store the pointer to the memory returned to the user.  This allows
	 * searching the list of allocated memory even if the buffer allocated is
	 * not in the list without dereferencing memory we do not own.
	 */
	p_hdr->p_mem = p_mem;

	/* Insert the header structure into our allocation list. */
	cl_qmap_insert(&gp_mem_tracker->alloc_map, (uintn_t) p_mem,
		       &p_hdr->map_item);
	cl_spinlock_release(&gp_mem_tracker->lock);

	return (p_mem);
}

/*
 * Allocate non-tracked memory.
 */
void *
__cl_malloc_ntrk(IN const size_t size)
{
	return (__cl_malloc_priv(size));
}

void *
__cl_zalloc_trk(IN const char *const p_file_name,
		IN const int32_t line_num, IN const size_t size)
{
	void *p_buffer;

	p_buffer = __cl_malloc_trk(p_file_name, line_num, size);
	if (p_buffer)
		cl_memclr(p_buffer, size);

	return (p_buffer);
}

void *
__cl_zalloc_ntrk(IN const size_t size)
{
	void *p_buffer;

	p_buffer = __cl_malloc_priv(size);
	if (p_buffer)
		cl_memclr(p_buffer, size);

	return (p_buffer);
}

void
__cl_free_trk(IN void *const p_memory)
{
	cl_malloc_hdr_t *p_hdr;
	cl_map_item_t *p_map_item;

	if (gp_mem_tracker) {
		cl_spinlock_acquire(&gp_mem_tracker->lock);

		/*
		 * Removes an item from the allocation tracking list given a pointer
		 * To the user's data and returns the pointer to header referencing the
		 * allocated memory block.
		 */
		p_map_item =
		    cl_qmap_get(&gp_mem_tracker->alloc_map, (uintn_t) p_memory);
		if (p_map_item != cl_qmap_end(&gp_mem_tracker->alloc_map)) {
			/* Get the pointer to the header. */
			p_hdr =
			    PARENT_STRUCT(p_map_item, cl_malloc_hdr_t,
					  map_item);
			/* Remove the item from the list. */
			cl_qmap_remove_item(&gp_mem_tracker->alloc_map,
					    p_map_item);

			/* Return the header to the free header list. */
			cl_qlist_insert_head(&gp_mem_tracker->free_hdr_list,
					     &p_hdr->map_item.pool_item.
					     list_item);
		}
		cl_spinlock_release(&gp_mem_tracker->lock);
	}
	__cl_free_priv(p_memory);
}

void
__cl_free_ntrk(IN void *const p_memory)
{
	__cl_free_priv(p_memory);
}
