Sha256: c9fcdeef3c9b2065db1621cd37a0fd31f6b000c81fa0b2005abd8e5bb8f99637

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

/*
*   Copyright (c) 2016, Jiri Techet
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License version 2 or (at your option) any later version.
*
*   Defines generic pool for object reuse reducing the amount of allocations
*   and deallocations.
*/

/*
*   INCLUDE FILES
*/
#include "general.h"  /* must always come first */

#include "routines.h"
#include "objpool.h"

/*
*   DATA DECLARATIONS
*/

struct sObjPool {
	ptrArray *array;
	unsigned int size;
	objPoolCreateFunc createFunc;
	objPoolDeleteFunc deleteFunc;
	objPoolClearFunc clearFunc;
};

/*
*   FUNCTION DEFINITIONS
*/
extern objPool *objPoolNew (unsigned int size,
	objPoolCreateFunc createFunc, objPoolDeleteFunc deleteFunc, objPoolClearFunc clearFunc)
{
	objPool* const result = xMalloc (1, objPool);
	result->array = ptrArrayNew (deleteFunc);
	result->size = size;
	result->createFunc = createFunc;
	result->deleteFunc = deleteFunc;
	result->clearFunc = clearFunc;
	return result;
}

extern void objPoolDelete (objPool *pool)
{
	ptrArrayDelete (pool->array);
	eFree (pool);
}

extern void *objPoolGet (objPool *pool)
{
	void *obj;

	if (ptrArrayCount (pool->array) > 0)
	{
		obj = ptrArrayLast (pool->array);
		ptrArrayRemoveLast (pool->array);
	}
	else
		obj = pool->createFunc ();

	if (pool->clearFunc)
		pool->clearFunc (obj);

	return obj;
}

extern void objPoolPut (objPool *pool, void *obj)
{
	if (obj == NULL)
		return;

	if (ptrArrayCount (pool->array) < pool->size)
		ptrArrayAdd (pool->array, obj);
	else
		pool->deleteFunc (obj);
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ctags.rb-1.1.4 ext/vendor/ctags/main/objpool.c
ctags.rb-1.1.3 ext/vendor/ctags/main/objpool.c