Blame IbAccess/Common/Public/iheapmanager.h

Packit 857059
/* BEGIN_ICS_COPYRIGHT3 ****************************************
Packit 857059
Packit 857059
Copyright (c) 2015, Intel Corporation
Packit 857059
Packit 857059
Redistribution and use in source and binary forms, with or without
Packit 857059
modification, are permitted provided that the following conditions are met:
Packit 857059
Packit 857059
    * Redistributions of source code must retain the above copyright notice,
Packit 857059
      this list of conditions and the following disclaimer.
Packit 857059
    * Redistributions in binary form must reproduce the above copyright
Packit 857059
      notice, this list of conditions and the following disclaimer in the
Packit 857059
     documentation and/or other materials provided with the distribution.
Packit 857059
    * Neither the name of Intel Corporation nor the names of its contributors
Packit 857059
      may be used to endorse or promote products derived from this software
Packit 857059
      without specific prior written permission.
Packit 857059
Packit 857059
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 857059
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 857059
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Packit 857059
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
Packit 857059
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 857059
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Packit 857059
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Packit 857059
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Packit 857059
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 857059
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 857059
Packit 857059
** END_ICS_COPYRIGHT3   ****************************************/
Packit 857059
Packit 857059
/* [ICS VERSION STRING: unknown] */
Packit 857059
Packit 857059
#ifndef _IBA_PUBLIC_IHEAP_MANAGER_H
Packit 857059
#define _IBA_PUBLIC_IHEAP_MANAGER_H
Packit 857059
Packit 857059
#include "iba/public/datatypes.h"
Packit 857059
#include "iba/public/ispinlock.h"
Packit 857059
#include "iba/public/ilist.h"
Packit 857059
Packit 857059
#if defined(__cplusplus)
Packit 857059
extern "C" {
Packit 857059
#endif
Packit 857059
Packit 857059
/* A simple heap is implemented
Packit 857059
 * This can be given a range of addresses to manage, such as HFI local memory
Packit 857059
 * and will provide allocate and free functions against that memory
Packit 857059
 * No pointers are maintained in the memory managed by the heap itself
Packit 857059
 * This means the memory can be in a slower access location, such as across
Packit 857059
 * PCI without impacting the performance of these functions
Packit 857059
 */
Packit 857059
Packit 857059
#define HEAP_DEBUG 0	/* set to 1 to enable heap debugging */
Packit 857059
#define HEAP_MANAGER_DEBUG 0	/* set to 1 to enable heap manager logging */
Packit 857059
Packit 857059
/* HEAP_OBJECT flags */
Packit 857059
#define HEAP_OBJECT_PREALLOCATED 1	/* part of initial sizing, don't release */
Packit 857059
#define HEAP_OBJECT_IN_USE		2
Packit 857059
Packit 857059
#define HEAP_ERROR_RATE_LIMIT (10*60)/* report malloc errors no faster than this */
Packit 857059
Packit 857059
typedef struct _HEAP_OBJECT
Packit 857059
{
Packit 857059
	union {
Packit 857059
		LIST_ITEM		list_item;	/* for internal use, bucket_lists */
Packit 857059
		struct {					/* valid only when allocated */
Packit 857059
			uint64 		addr;		/* heap start + offset */
Packit 857059
			ATOMIC_UINT	refcount;	/* refcount for shared memory */
Packit 857059
		} info;
Packit 857059
	} u;
Packit 857059
	uint8				flag; 		/* HEAP_OBJECT flags above */
Packit 857059
	uint32				size;		/* actual size in bytes */
Packit 857059
	uint32				offset;		/* offset from heap start address */
Packit 857059
		/* these lists track all blocks */
Packit 857059
	struct _HEAP_OBJECT *prev;		/* for internal use, links for buddies */
Packit 857059
	struct _HEAP_OBJECT *next;		/* for internal use, links for buddies */
Packit 857059
} HEAP_OBJECT;
Packit 857059
Packit 857059
typedef struct _HEAP_MANAGER
Packit 857059
{
Packit 857059
	uint32		available_memory;	/* unassigned memory at end of heap */
Packit 857059
	uint32		max_memory;			/* total size of heap */
Packit 857059
	uint64		start_addr;			/* physical start address of memory */
Packit 857059
	uint32		cur_offset;			/* offset of 1st unassigned memory */
Packit 857059
	SPIN_LOCK	heap_lock;
Packit 857059
	QUICK_LIST	bucket_lists[32];	/* free memory chunck in power of 2 order*/
Packit 857059
	uint32		block_size;			/* smallest block size in bytes */
Packit 857059
	uint32		water_mark;			/* not used yet */
Packit 857059
	uint32		tag;
Packit 857059
	HEAP_OBJECT	*heap_object;
Packit 857059
	const char	name[100];	
Packit 857059
	HEAP_OBJECT *last_obj;			/* all blocks */
Packit 857059
	uint32		last_error_time;	/* timestamp when last error reported */
Packit 857059
#if HEAP_DEBUG	
Packit 857059
	uint32		malloc_count;
Packit 857059
#endif	
Packit 857059
} HEAP_MANAGER;
Packit 857059
Packit 857059
typedef struct _HEAP_INIT
Packit 857059
{
Packit 857059
	uint64		start_addr;				/* initial address of heap */
Packit 857059
	uint32		block_size;				/* size of blocks */
Packit 857059
	uint32		buck_init_count[32];	/* count of initial assignment of
Packit 857059
										 * buffers to each bucket
Packit 857059
										 */
Packit 857059
	uint32		max_memory;				/* should be power of 2 */
Packit 857059
	uint32		water_mark;				/* Minimal amount memory before
Packit 857059
										 * rearange from bucket
Packit 857059
										 */  	
Packit 857059
} HEAP_INIT;	
Packit 857059
Packit 857059
static __inline void HeapManagerIncBucketInit(HEAP_INIT *heap_init,
Packit 857059
											uint32 log2_size)
Packit 857059
{
Packit 857059
	heap_init->buck_init_count[log2_size]++;
Packit 857059
}
Packit 857059
Packit 857059
void HeapManagerInitState(HEAP_MANAGER *heap_mng);
Packit 857059
FSTATUS HeapManagerInit(HEAP_MANAGER* heap_mng, const HEAP_INIT *heap_init,
Packit 857059
						const char* name, uint32 tag);
Packit 857059
void HeapManagerDestroy(HEAP_MANAGER *heap_mng);
Packit 857059
HEAP_OBJECT * HeapManagerAllocate(HEAP_MANAGER *heap_mng, uint32 size);
Packit 857059
FSTATUS HeapManagerDeallocate(HEAP_MANAGER *heap_mng, HEAP_OBJECT *heap_obj);
Packit 857059
void HeapManagerRelease(HEAP_MANAGER *heap_mng, HEAP_OBJECT *heap_obj);
Packit 857059
Packit 857059
void HeapManagerPrintSummary(HEAP_MANAGER *heap_mng);
Packit 857059
uint32 HeapManagerGetAvailable(HEAP_MANAGER *heap_mng);
Packit 857059
uint32 HeapManagerGetUsed(HEAP_MANAGER *heap_mng);
Packit 857059
uint32 HeapManagerGetMaxMemory(HEAP_MANAGER *heap_mng);
Packit 857059
Packit 857059
void HeapManagerIncRefCount(HEAP_MANAGER *heap_mng, HEAP_OBJECT *heap_obj);
Packit 857059
void HeapManagerDecRefCount(HEAP_MANAGER *heap_mng, HEAP_OBJECT *heap_obj);
Packit 857059
static __inline uint32 HeapManagerGetRefCount(HEAP_OBJECT *heap_obj)
Packit 857059
{
Packit 857059
	return AtomicRead(&heap_obj->u.info.refcount);
Packit 857059
}
Packit 857059
static __inline uint64 HeapManagerGetAddr(HEAP_OBJECT *heap_obj)
Packit 857059
{
Packit 857059
	return heap_obj->u.info.addr;
Packit 857059
}
Packit 857059
static __inline uint32 HeapManagerGetSize(HEAP_OBJECT *heap_obj)
Packit 857059
{
Packit 857059
	return heap_obj->size;
Packit 857059
}
Packit 857059
Packit 857059
#if defined(__cplusplus)
Packit 857059
}
Packit 857059
#endif
Packit 857059
Packit 857059
#endif /* _IBA_PUBLIC_IHEAP_MANAGER_H */