2008-09-18 07:16:47 -07:00
|
|
|
/* EINA - EFL data type library
|
|
|
|
* Copyright (C) 2008 Cedric Bail
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library;
|
|
|
|
* if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef EINA_INLINE_MEMPOOL_X_
|
|
|
|
#define EINA_INLINE_MEMPOOL_X_
|
|
|
|
|
2011-12-02 07:27:35 -08:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-09-12 01:03:21 -07:00
|
|
|
/**
|
|
|
|
* @addtogroup Eina_Memory_Pool_Group Memory Pool
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
/* Memory Pool */
|
2011-04-11 10:45:01 -07:00
|
|
|
typedef struct _Eina_Mempool_Backend_ABI1 Eina_Mempool_Backend_ABI1;
|
|
|
|
typedef struct _Eina_Mempool_Backend_ABI2 Eina_Mempool_Backend_ABI2;
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
struct _Eina_Mempool_Backend
|
|
|
|
{
|
2008-09-24 05:55:31 -07:00
|
|
|
const char *name;
|
2008-09-18 07:16:47 -07:00
|
|
|
void *(*init)(const char *context, const char *options, va_list args);
|
|
|
|
void (*free)(void *data, void *element);
|
|
|
|
void *(*alloc)(void *data, unsigned int size);
|
|
|
|
void *(*realloc)(void *data, void *element, unsigned int size);
|
|
|
|
void (*garbage_collect)(void *data);
|
|
|
|
void (*statistics)(void *data);
|
|
|
|
void (*shutdown)(void *data);
|
2011-04-11 07:07:42 -07:00
|
|
|
void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
|
2008-09-18 07:16:47 -07:00
|
|
|
};
|
|
|
|
|
2011-04-11 10:45:01 -07:00
|
|
|
struct _Eina_Mempool_Backend_ABI1
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
void *(*init)(const char *context, const char *options, va_list args);
|
|
|
|
void (*free)(void *data, void *element);
|
|
|
|
void *(*alloc)(void *data, unsigned int size);
|
|
|
|
void *(*realloc)(void *data, void *element, unsigned int size);
|
|
|
|
void (*garbage_collect)(void *data);
|
|
|
|
void (*statistics)(void *data);
|
|
|
|
void (*shutdown)(void *data);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _Eina_Mempool_Backend_ABI2
|
|
|
|
{
|
|
|
|
void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
|
|
|
|
};
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
struct _Eina_Mempool
|
|
|
|
{
|
2011-04-11 10:45:01 -07:00
|
|
|
Eina_Mempool_Backend_ABI1 backend;
|
2008-09-18 07:16:47 -07:00
|
|
|
void *backend_data;
|
2011-04-12 01:52:59 -07:00
|
|
|
Eina_Mempool_Backend_ABI2 *backend2;
|
2008-09-18 07:16:47 -07:00
|
|
|
};
|
|
|
|
|
2010-09-12 01:03:21 -07:00
|
|
|
/**
|
2011-12-02 07:34:11 -08:00
|
|
|
* @brief Re-allocate an amount memory by the given mempool.
|
2010-09-12 01:03:21 -07:00
|
|
|
*
|
|
|
|
* @param mp The mempool.
|
|
|
|
* @param element The element to re-allocate.
|
|
|
|
* @param size The size in bytes to re-allocate.
|
|
|
|
* @return The newly re-allocated data.
|
|
|
|
*
|
2011-12-02 07:34:11 -08:00
|
|
|
* This function re-allocates and returns @p element with @p size bytes using the
|
|
|
|
* mempool @p mp. If not used anymore, the data must be freed with eina_mempool_free().
|
|
|
|
* @warning No checks are done for @p mp.
|
2010-09-12 01:03:21 -07:00
|
|
|
*/
|
2008-09-18 07:16:47 -07:00
|
|
|
static inline void *
|
|
|
|
eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size)
|
|
|
|
{
|
|
|
|
return mp->backend.realloc(mp->backend_data, element, size);
|
|
|
|
}
|
|
|
|
|
2010-09-12 01:03:21 -07:00
|
|
|
/**
|
2011-12-02 07:34:11 -08:00
|
|
|
* @brief Allocate memory using the given mempool.
|
2010-09-12 01:03:21 -07:00
|
|
|
*
|
|
|
|
* @param mp The mempool.
|
|
|
|
* @param size The size in bytes to allocate.
|
|
|
|
* @return The newly allocated data.
|
|
|
|
*
|
2011-12-02 07:34:11 -08:00
|
|
|
* This function allocates and returns @p size bytes using the mempool @p mp.
|
|
|
|
* If not used anymore, the data must be freed with eina_mempool_free().
|
|
|
|
* @warning No checks are done for @p mp.
|
2010-09-12 01:03:21 -07:00
|
|
|
*/
|
2008-09-18 07:16:47 -07:00
|
|
|
static inline void *
|
2009-06-24 09:56:49 -07:00
|
|
|
eina_mempool_malloc(Eina_Mempool *mp, unsigned int size)
|
2008-09-18 07:16:47 -07:00
|
|
|
{
|
|
|
|
return mp->backend.alloc(mp->backend_data, size);
|
|
|
|
}
|
|
|
|
|
2011-12-02 07:27:35 -08:00
|
|
|
/**
|
2011-12-02 07:34:11 -08:00
|
|
|
* @brief Allocate and zero memory using the given mempool.
|
2011-12-02 07:27:35 -08:00
|
|
|
*
|
|
|
|
* @param mp The mempool.
|
|
|
|
* @param size The size in bytes to allocate.
|
|
|
|
* @return The newly allocated data.
|
|
|
|
*
|
2011-12-02 07:34:11 -08:00
|
|
|
* This function allocates, zeroes, and returns @p size bytes using the mempool @p mp.
|
|
|
|
* If not used anymore, the data must be freed with eina_mempool_free().
|
|
|
|
* @warning No checks are done for @p mp.
|
|
|
|
* @since 1.2
|
2011-12-02 07:27:35 -08:00
|
|
|
*/
|
|
|
|
static inline void *
|
|
|
|
eina_mempool_calloc(Eina_Mempool *mp, unsigned int size)
|
|
|
|
{
|
|
|
|
void *r = mp->backend.alloc(mp->backend_data, size);
|
|
|
|
if (!r) return NULL;
|
|
|
|
memset(r, 0, size);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-09-12 01:03:21 -07:00
|
|
|
/**
|
2011-12-02 07:34:11 -08:00
|
|
|
* @brief Free resources previously allocated by the given mempool.
|
2010-09-12 01:03:21 -07:00
|
|
|
*
|
|
|
|
* @param mp The mempool.
|
|
|
|
* @param element The data to free.
|
|
|
|
*
|
|
|
|
* This function frees @p element allocated by @p mp. @p element must
|
2011-12-02 07:34:11 -08:00
|
|
|
* have been obtained from eina_mempool_malloc(), eina_mempool_calloc(), or
|
|
|
|
* eina_mempool_realloc().
|
|
|
|
* @warning No checks are done for @p mp.
|
2010-09-12 01:03:21 -07:00
|
|
|
*/
|
2008-09-18 07:16:47 -07:00
|
|
|
static inline void
|
|
|
|
eina_mempool_free(Eina_Mempool *mp, void *element)
|
|
|
|
{
|
2012-04-03 06:31:29 -07:00
|
|
|
if (!element) return ;
|
2008-09-18 07:16:47 -07:00
|
|
|
mp->backend.free(mp->backend_data, element);
|
|
|
|
}
|
|
|
|
|
2010-09-12 01:03:21 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2008-09-18 07:16:47 -07:00
|
|
|
#endif
|