2013-04-18 04:19:02 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "eo_ptr_indirection.h"
|
2013-05-03 12:28:32 -07:00
|
|
|
#include <assert.h>
|
2013-04-23 07:38:44 -07:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
/* Start of pointer indirection:
|
|
|
|
*
|
|
|
|
* This feature is responsible of hiding from the developer the real pointer of
|
|
|
|
* the Eo object to supply a better memory management by preventing bad usage
|
|
|
|
* of the pointers.
|
|
|
|
*
|
2013-05-16 01:55:32 -07:00
|
|
|
* Eo * is no more a pointer but indexes to an entry into an ids table.
|
|
|
|
* For a better memory usage:
|
|
|
|
* - a tree structure is used, composed of a top level table pointing at
|
|
|
|
* mid tables pointing at tables composed of entries.
|
|
|
|
* - tables are allocated when needed (i.e no more empty entries in allocated tables.
|
2013-05-17 01:35:16 -07:00
|
|
|
* - empty tables are freed, except one kept as spare table.
|
2013-04-18 04:19:02 -07:00
|
|
|
*
|
2013-05-16 01:55:32 -07:00
|
|
|
* An Eo id is contructed by bits manipulation of table indexes and a generation.
|
|
|
|
*
|
|
|
|
* id = Mid Table | Table | Entry | Generation
|
2013-04-18 04:19:02 -07:00
|
|
|
*
|
|
|
|
* Generation helps finding abuse of ids. When an entry is assigned to an
|
|
|
|
* object, a generation is inserted into the id. If the developer uses this id
|
|
|
|
* although the object is freed and another one has replaced it into the same
|
|
|
|
* entry of the table, the generation will be different and an error will
|
|
|
|
* occur when accessing with the old id.
|
|
|
|
*
|
2013-05-16 01:55:32 -07:00
|
|
|
* Each Table is composed of:
|
|
|
|
* - an index 'start' indicating which free entry is the next one to use.
|
|
|
|
* - 2 indexes 'fifo_head' and 'fifo_tail' defining a fifo,
|
2013-05-03 12:28:32 -07:00
|
|
|
* that will help us to store the entries to be reused. It stores only the
|
2013-04-18 04:19:02 -07:00
|
|
|
* entries that have been used at least one time. The entries that have
|
|
|
|
* never been used are "pointed" by the start parameter.
|
2013-05-03 12:28:32 -07:00
|
|
|
* - entries composed of:
|
|
|
|
* - a pointer to the object
|
2013-05-16 01:55:32 -07:00
|
|
|
* - an index 'next_in_fifo' used to chain the free entries in the fifo
|
2013-05-03 12:28:32 -07:00
|
|
|
* - a flag indicating if the entry is active
|
|
|
|
* - a generation assigned to the object
|
2013-05-05 06:08:55 -07:00
|
|
|
*
|
2013-05-03 12:28:32 -07:00
|
|
|
* When an entry is searched into a table, we first use one of the entries that
|
2013-05-16 01:55:32 -07:00
|
|
|
* has never been used. If there is none, we try to pop from the fifo.
|
|
|
|
* If a such entry doesn't exist, we pass to the next table.
|
|
|
|
* When an entry is found, we reserve it to the object pointer
|
|
|
|
* then contruct and return the related Eo id.
|
|
|
|
*
|
2013-05-05 06:08:55 -07:00
|
|
|
* Assigning all the entries of a table before trying to reuse them from
|
|
|
|
* the fifo ensures that we are not going to soon recycle a released entry,
|
|
|
|
* thus minimize the risks of an aggressive del() then use() on a single entry.
|
2013-05-16 01:55:32 -07:00
|
|
|
*
|
2013-05-05 06:08:55 -07:00
|
|
|
* The indexes and a reference to the last table which served an entry is kept
|
|
|
|
* and is reused prior to the others untill it is full.
|
2013-05-03 12:28:32 -07:00
|
|
|
* When an object is freed, the entry into the table is released by appending
|
2013-05-16 01:55:32 -07:00
|
|
|
* it to the fifo.
|
2013-04-18 04:19:02 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if SIZEOF_UINTPTR_T == 4
|
|
|
|
/* 32 bits */
|
2013-05-16 01:55:32 -07:00
|
|
|
# define BITS_MID_TABLE_ID 5
|
|
|
|
# define BITS_TABLE_ID 5
|
|
|
|
# define BITS_ENTRY_ID 12
|
|
|
|
# define BITS_GENERATION_COUNTER 10
|
2013-05-16 02:46:20 -07:00
|
|
|
# define DROPPED_TABLES 0
|
2013-05-17 01:35:16 -07:00
|
|
|
# define DROPPED_ENTRIES 4
|
2013-05-03 12:28:32 -07:00
|
|
|
typedef int16_t Table_Index;
|
|
|
|
typedef uint16_t Generation_Counter;
|
2013-04-18 04:19:02 -07:00
|
|
|
#else
|
|
|
|
/* 64 bits */
|
2013-05-16 01:55:32 -07:00
|
|
|
# define BITS_MID_TABLE_ID 11
|
|
|
|
# define BITS_TABLE_ID 11
|
|
|
|
# define BITS_ENTRY_ID 12
|
|
|
|
# define BITS_GENERATION_COUNTER 30
|
2013-05-16 02:46:20 -07:00
|
|
|
# define DROPPED_TABLES 2
|
2013-05-17 01:35:16 -07:00
|
|
|
# define DROPPED_ENTRIES 3
|
2013-05-03 12:28:32 -07:00
|
|
|
typedef int16_t Table_Index;
|
|
|
|
typedef uint32_t Generation_Counter;
|
2013-04-18 04:19:02 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Shifts macros to manipulate the Eo id */
|
2013-05-16 01:55:32 -07:00
|
|
|
#define SHIFT_MID_TABLE_ID (BITS_TABLE_ID + \
|
|
|
|
BITS_ENTRY_ID + BITS_GENERATION_COUNTER)
|
|
|
|
#define SHIFT_TABLE_ID (BITS_ENTRY_ID + BITS_GENERATION_COUNTER)
|
|
|
|
#define SHIFT_ENTRY_ID (BITS_GENERATION_COUNTER)
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-16 02:46:20 -07:00
|
|
|
/* Maximum ranges - a few tables and entries are dropped to minimize the amount
|
|
|
|
* of wasted bytes, see _eo_id_mem_alloc */
|
2013-05-16 01:55:32 -07:00
|
|
|
#define MAX_MID_TABLE_ID (1 << BITS_MID_TABLE_ID)
|
2013-05-16 02:46:20 -07:00
|
|
|
#define MAX_TABLE_ID ((1 << BITS_TABLE_ID) - DROPPED_TABLES )
|
|
|
|
#define MAX_ENTRY_ID ((1 << BITS_ENTRY_ID) - DROPPED_ENTRIES)
|
2013-05-16 01:55:32 -07:00
|
|
|
#define MAX_GENERATIONS (1 << BITS_GENERATION_COUNTER)
|
|
|
|
|
|
|
|
/* Masks */
|
|
|
|
#define MASK_MID_TABLE_ID (MAX_MID_TABLE_ID - 1)
|
2013-05-16 02:46:20 -07:00
|
|
|
#define MASK_TABLE_ID ((1 << BITS_TABLE_ID) - 1)
|
|
|
|
#define MASK_ENTRY_ID ((1 << BITS_ENTRY_ID) - 1)
|
2013-05-16 01:55:32 -07:00
|
|
|
#define MASK_GENERATIONS (MAX_GENERATIONS - 1)
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-16 01:55:32 -07:00
|
|
|
#define MEM_HEADER_SIZE 16
|
|
|
|
#define MEM_PAGE_SIZE 4096
|
|
|
|
#define MEM_MAGIC 0x3f61ec8a
|
2013-04-23 07:38:44 -07:00
|
|
|
|
|
|
|
typedef struct _Mem_Header
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
size_t magic;
|
|
|
|
} Mem_Header;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
_eo_id_mem_alloc(size_t size)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
void *ptr;
|
|
|
|
Mem_Header *hdr;
|
|
|
|
size_t newsize;
|
|
|
|
newsize = MEM_PAGE_SIZE * ((size + MEM_HEADER_SIZE + MEM_PAGE_SIZE - 1) /
|
|
|
|
MEM_PAGE_SIZE);
|
|
|
|
ptr = mmap(NULL, newsize, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (ptr == MAP_FAILED)
|
|
|
|
{
|
|
|
|
ERR("mmap of eo id table region failed!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
hdr = ptr;
|
2013-04-27 15:17:07 -07:00
|
|
|
hdr->size = newsize;
|
2013-04-23 07:38:44 -07:00
|
|
|
hdr->magic = MEM_MAGIC;
|
2013-05-16 02:46:20 -07:00
|
|
|
/* DBG("asked:%lu allocated:%lu wasted:%lu bytes", size, newsize, (newsize - size)); */
|
2013-04-23 07:38:44 -07:00
|
|
|
return (void *)(((unsigned char *)ptr) + MEM_HEADER_SIZE);
|
|
|
|
#else
|
|
|
|
return malloc(size);
|
2013-04-27 15:43:23 -07:00
|
|
|
#endif
|
2013-04-23 07:38:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
_eo_id_mem_calloc(size_t num, size_t size)
|
|
|
|
{
|
|
|
|
void *ptr = _eo_id_mem_alloc(num * size);
|
|
|
|
if (!ptr) return NULL;
|
|
|
|
memset(ptr, 0, num * size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_eo_id_mem_free(void *ptr)
|
|
|
|
{
|
|
|
|
#ifdef __linux__
|
|
|
|
Mem_Header *hdr;
|
|
|
|
if (!ptr) return;
|
|
|
|
hdr = (Mem_Header *)(((unsigned char *)ptr) - MEM_HEADER_SIZE);
|
|
|
|
if (hdr->magic != MEM_MAGIC)
|
|
|
|
{
|
|
|
|
ERR("unmap of eo table region has bad magic!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
munmap(hdr, hdr->size);
|
|
|
|
#else
|
|
|
|
free(ptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-16 04:15:43 -07:00
|
|
|
#ifdef EINA_DEBUG_MALLOC
|
|
|
|
static void
|
|
|
|
_eo_id_mem_protect(void *ptr, Eina_Bool may_not_write)
|
|
|
|
{
|
|
|
|
# ifdef __linux__
|
|
|
|
Mem_Header *hdr;
|
|
|
|
if (!ptr) return;
|
|
|
|
hdr = (Mem_Header *)(((unsigned char *)ptr) - MEM_HEADER_SIZE);
|
|
|
|
if (hdr->magic != MEM_MAGIC)
|
|
|
|
{
|
|
|
|
ERR("mprotect of eo table region has bad magic!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mprotect(hdr, hdr->size, PROT_READ | ( may_not_write ? 0 : PROT_WRITE) );
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
# define PROTECT(_ptr_) _eo_id_mem_protect((_ptr_), EINA_TRUE)
|
|
|
|
# define UNPROTECT(_ptr_) _eo_id_mem_protect((_ptr_), EINA_FALSE)
|
|
|
|
#else
|
|
|
|
# define PROTECT(_ptr_)
|
|
|
|
# define UNPROTECT(_ptr_)
|
|
|
|
#endif
|
|
|
|
|
2013-04-27 15:43:23 -07:00
|
|
|
/* Entry */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Pointer to the object */
|
|
|
|
_Eo *ptr;
|
2013-05-16 01:55:32 -07:00
|
|
|
/* Indicates where to find the next entry to recycle */
|
|
|
|
Table_Index next_in_fifo;
|
2013-04-27 15:43:23 -07:00
|
|
|
/* Active flag */
|
|
|
|
unsigned int active : 1;
|
|
|
|
/* Generation */
|
2013-05-16 01:55:32 -07:00
|
|
|
unsigned int generation : BITS_GENERATION_COUNTER;
|
2013-05-03 12:28:32 -07:00
|
|
|
|
2013-04-27 15:43:23 -07:00
|
|
|
} _Eo_Id_Entry;
|
|
|
|
|
2013-04-18 04:19:02 -07:00
|
|
|
/* Table */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Indicates where start the "never used" entries */
|
|
|
|
Table_Index start;
|
2013-05-03 12:28:32 -07:00
|
|
|
/* Indicates where to find the next entry to recycle */
|
2013-05-16 01:55:32 -07:00
|
|
|
Table_Index fifo_head;
|
2013-05-03 12:28:32 -07:00
|
|
|
/* Indicates where to add an entry to recycle */
|
2013-05-16 01:55:32 -07:00
|
|
|
Table_Index fifo_tail;
|
2013-05-16 05:56:41 -07:00
|
|
|
/* Packed mid table and table indexes */
|
|
|
|
Eo_Id partial_id;
|
2013-05-17 01:35:16 -07:00
|
|
|
/* Counter of free entries */
|
|
|
|
unsigned int free_entries;
|
2013-05-16 01:55:32 -07:00
|
|
|
/* Entries of the table holding real pointers and generations */
|
|
|
|
_Eo_Id_Entry entries[MAX_ENTRY_ID];
|
2013-04-18 04:19:02 -07:00
|
|
|
} _Eo_Ids_Table;
|
|
|
|
|
|
|
|
/* Tables handling pointers indirection */
|
2013-05-16 01:55:32 -07:00
|
|
|
static _Eo_Ids_Table **_eo_ids_tables[MAX_MID_TABLE_ID] = { NULL };
|
2013-05-05 06:08:55 -07:00
|
|
|
|
|
|
|
/* Current table used for following allocations */
|
2013-05-17 00:23:00 -07:00
|
|
|
static _Eo_Ids_Table *_current_table = NULL;
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-17 01:35:16 -07:00
|
|
|
/* Spare empty table */
|
|
|
|
static _Eo_Ids_Table *_empty_table = NULL;
|
|
|
|
|
2013-04-18 04:19:02 -07:00
|
|
|
/* Next generation to use when assigning a new entry to a Eo pointer */
|
2013-05-16 02:50:21 -07:00
|
|
|
Generation_Counter _eo_generation_counter = 0;
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
/* Macro used to compose an Eo id */
|
2013-05-16 05:56:41 -07:00
|
|
|
#define EO_COMPOSE_PARTIAL_ID(MID_TABLE, TABLE) \
|
|
|
|
(((Eo_Id)(MID_TABLE & MASK_MID_TABLE_ID) << SHIFT_MID_TABLE_ID) | \
|
|
|
|
((Eo_Id)(TABLE & MASK_TABLE_ID) << SHIFT_TABLE_ID))
|
|
|
|
|
|
|
|
#define EO_COMPOSE_FINAL_ID(PARTIAL_ID, ENTRY, GENERATION) \
|
|
|
|
(PARTIAL_ID | \
|
|
|
|
((ENTRY & MASK_ENTRY_ID) << SHIFT_ENTRY_ID) | \
|
|
|
|
(GENERATION & MASK_GENERATIONS ))
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
/* Macro to extract from an Eo id the indexes of the tables */
|
2013-05-16 05:56:41 -07:00
|
|
|
#define EO_DECOMPOSE_ID(ID, MID_TABLE, TABLE, ENTRY, GENERATION) \
|
|
|
|
MID_TABLE = (ID >> SHIFT_MID_TABLE_ID) & MASK_MID_TABLE_ID; \
|
|
|
|
TABLE = (ID >> SHIFT_TABLE_ID) & MASK_TABLE_ID; \
|
|
|
|
ENTRY = (ID >> SHIFT_ENTRY_ID) & MASK_ENTRY_ID; \
|
2013-05-16 01:55:32 -07:00
|
|
|
GENERATION = ID & MASK_GENERATIONS;
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
/* Macro used for readability */
|
2013-05-16 01:55:32 -07:00
|
|
|
#define TABLE_FROM_IDS _eo_ids_tables[mid_table_id][table_id]
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
_Eo *
|
|
|
|
_eo_obj_pointer_get(const Eo_Id obj_id)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_EO_ID
|
2013-04-27 15:43:23 -07:00
|
|
|
_Eo_Id_Entry *entry;
|
2013-05-03 12:28:32 -07:00
|
|
|
Generation_Counter generation;
|
2013-05-16 01:55:32 -07:00
|
|
|
Table_Index mid_table_id, table_id, entry_id;
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-16 01:55:32 -07:00
|
|
|
EO_DECOMPOSE_ID(obj_id, mid_table_id, table_id, entry_id, generation);
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-17 02:39:01 -07:00
|
|
|
/* Check the validity of the entry */
|
2013-05-16 01:55:32 -07:00
|
|
|
if (_eo_ids_tables[mid_table_id] && TABLE_FROM_IDS)
|
2013-04-27 15:43:23 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
entry = &(TABLE_FROM_IDS->entries[entry_id]);
|
2013-04-27 15:43:23 -07:00
|
|
|
if (entry && entry->active && (entry->generation == generation))
|
|
|
|
return entry->ptr;
|
|
|
|
}
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
ERR("obj_id %p is not pointing to a valid object. Maybe it has already been freed.",
|
|
|
|
(void *)obj_id);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return (_Eo *)obj_id;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-05 06:08:55 -07:00
|
|
|
static inline _Eo_Id_Entry *
|
2013-05-05 09:19:14 -07:00
|
|
|
_get_available_entry(_Eo_Ids_Table *table)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-04-27 15:43:23 -07:00
|
|
|
_Eo_Id_Entry *entry = NULL;
|
2013-05-05 06:08:55 -07:00
|
|
|
|
2013-05-16 01:55:32 -07:00
|
|
|
if (table->start != MAX_ENTRY_ID)
|
2013-05-05 06:08:55 -07:00
|
|
|
{
|
|
|
|
/* Serve never used entries first */
|
|
|
|
entry = &(table->entries[table->start]);
|
2013-05-16 04:15:43 -07:00
|
|
|
UNPROTECT(table);
|
2013-05-05 06:08:55 -07:00
|
|
|
table->start++;
|
2013-05-17 01:35:16 -07:00
|
|
|
table->free_entries--;
|
2013-05-05 06:08:55 -07:00
|
|
|
}
|
2013-05-16 01:55:32 -07:00
|
|
|
else if (table->fifo_head != -1)
|
2013-05-05 06:08:55 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
/* Pop a free entry from the fifo */
|
|
|
|
entry = &(table->entries[table->fifo_head]);
|
2013-05-16 04:15:43 -07:00
|
|
|
UNPROTECT(table);
|
2013-05-16 01:55:32 -07:00
|
|
|
if (entry->next_in_fifo == -1)
|
|
|
|
table->fifo_head = table->fifo_tail = -1;
|
2013-05-05 06:08:55 -07:00
|
|
|
else
|
2013-05-16 01:55:32 -07:00
|
|
|
table->fifo_head = entry->next_in_fifo;
|
2013-05-17 01:35:16 -07:00
|
|
|
table->free_entries--;
|
2013-05-05 06:08:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline _Eo_Id_Entry *
|
2013-05-05 09:19:14 -07:00
|
|
|
_search_tables()
|
2013-05-05 06:08:55 -07:00
|
|
|
{
|
|
|
|
_Eo_Ids_Table *table;
|
|
|
|
_Eo_Id_Entry *entry;
|
|
|
|
|
2013-05-16 02:50:21 -07:00
|
|
|
for (Table_Index mid_table_id = 0; mid_table_id < MAX_MID_TABLE_ID; mid_table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
if (!_eo_ids_tables[mid_table_id])
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-05 06:08:55 -07:00
|
|
|
/* Allocate a new intermediate table */
|
2013-05-16 01:55:32 -07:00
|
|
|
_eo_ids_tables[mid_table_id] = _eo_id_mem_calloc(MAX_TABLE_ID, sizeof(_Eo_Ids_Table*));
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
2013-05-05 06:08:55 -07:00
|
|
|
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index table_id = 0; table_id < MAX_TABLE_ID; table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
table = TABLE_FROM_IDS;
|
2013-05-05 06:08:55 -07:00
|
|
|
|
2013-05-03 12:28:32 -07:00
|
|
|
if (!table)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-17 01:35:16 -07:00
|
|
|
if (_empty_table)
|
|
|
|
{
|
|
|
|
/* Recycle the available empty table */
|
|
|
|
table = _empty_table;
|
|
|
|
_empty_table = NULL;
|
|
|
|
UNPROTECT(table);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-17 02:39:01 -07:00
|
|
|
/* Allocate a new table */
|
2013-05-17 01:35:16 -07:00
|
|
|
table = _eo_id_mem_calloc(1, sizeof(_Eo_Ids_Table));
|
|
|
|
}
|
2013-05-17 02:39:01 -07:00
|
|
|
/* Initialize the table and reserve the first entry */
|
2013-05-03 12:28:32 -07:00
|
|
|
table->start = 1;
|
2013-05-17 01:35:16 -07:00
|
|
|
table->free_entries = MAX_ENTRY_ID - 1;
|
2013-05-16 01:55:32 -07:00
|
|
|
table->fifo_head = table->fifo_tail = -1;
|
2013-05-16 05:56:41 -07:00
|
|
|
table->partial_id = EO_COMPOSE_PARTIAL_ID(mid_table_id, table_id);
|
2013-05-03 12:28:32 -07:00
|
|
|
entry = &(table->entries[0]);
|
2013-05-16 04:15:43 -07:00
|
|
|
UNPROTECT(_eo_ids_tables[mid_table_id]);
|
|
|
|
TABLE_FROM_IDS = table;
|
|
|
|
PROTECT(_eo_ids_tables[mid_table_id]);
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
2013-05-03 12:28:32 -07:00
|
|
|
else
|
2013-05-05 09:19:14 -07:00
|
|
|
entry = _get_available_entry(table);
|
2013-05-03 12:28:32 -07:00
|
|
|
|
2013-05-05 06:08:55 -07:00
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
/* Store table info into current table */
|
2013-05-17 00:23:00 -07:00
|
|
|
_current_table = table;
|
2013-05-05 06:08:55 -07:00
|
|
|
return entry;
|
|
|
|
}
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-05 06:08:55 -07:00
|
|
|
|
|
|
|
ERR("no more available entries to store eo objects");
|
2013-05-17 00:23:00 -07:00
|
|
|
_current_table = NULL;
|
2013-05-05 06:08:55 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Eo_Id
|
|
|
|
_eo_id_allocate(const _Eo *obj)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_EO_ID
|
|
|
|
_Eo_Id_Entry *entry = NULL;
|
|
|
|
|
2013-05-17 00:23:00 -07:00
|
|
|
if (_current_table)
|
|
|
|
entry = _get_available_entry(_current_table);
|
2013-05-05 06:08:55 -07:00
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
{
|
2013-05-05 09:19:14 -07:00
|
|
|
entry = _search_tables();
|
2013-05-05 06:08:55 -07:00
|
|
|
if (!entry)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:50:21 -07:00
|
|
|
/* [1;max-1] thus we never generate an Eo_Id equal to 0 */
|
|
|
|
_eo_generation_counter++;
|
|
|
|
if (_eo_generation_counter == MAX_GENERATIONS)
|
|
|
|
_eo_generation_counter = 1;
|
2013-05-17 02:39:01 -07:00
|
|
|
/* Fill the entry and return it's Eo Id */
|
2013-05-05 06:08:55 -07:00
|
|
|
entry->ptr = (_Eo *)obj;
|
|
|
|
entry->active = 1;
|
|
|
|
entry->generation = _eo_generation_counter;
|
2013-05-17 00:23:00 -07:00
|
|
|
PROTECT(_current_table);
|
|
|
|
return EO_COMPOSE_FINAL_ID(_current_table->partial_id,
|
|
|
|
(entry - _current_table->entries),
|
2013-05-16 05:56:41 -07:00
|
|
|
entry->generation);
|
2013-04-18 04:19:02 -07:00
|
|
|
#else
|
|
|
|
return (Eo_Id)obj;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_eo_id_release(const Eo_Id obj_id)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_EO_ID
|
2013-05-03 12:28:32 -07:00
|
|
|
_Eo_Ids_Table *table;
|
2013-04-27 15:43:23 -07:00
|
|
|
_Eo_Id_Entry *entry;
|
2013-05-03 12:28:32 -07:00
|
|
|
Generation_Counter generation;
|
2013-05-16 01:55:32 -07:00
|
|
|
Table_Index mid_table_id, table_id, entry_id;
|
|
|
|
EO_DECOMPOSE_ID(obj_id, mid_table_id, table_id, entry_id, generation);
|
2013-04-18 04:19:02 -07:00
|
|
|
|
2013-05-17 02:39:01 -07:00
|
|
|
/* Check the validity of the entry */
|
2013-05-16 01:55:32 -07:00
|
|
|
if (_eo_ids_tables[mid_table_id] && (table = TABLE_FROM_IDS))
|
2013-04-27 15:43:23 -07:00
|
|
|
{
|
2013-05-03 12:28:32 -07:00
|
|
|
entry = &(table->entries[entry_id]);
|
2013-04-27 15:43:23 -07:00
|
|
|
if (entry && entry->active && (entry->generation == generation))
|
|
|
|
{
|
2013-05-16 04:15:43 -07:00
|
|
|
UNPROTECT(table);
|
2013-05-17 01:35:16 -07:00
|
|
|
table->free_entries++;
|
2013-04-27 15:43:23 -07:00
|
|
|
/* Disable the entry */
|
|
|
|
entry->active = 0;
|
2013-05-16 01:55:32 -07:00
|
|
|
entry->next_in_fifo = -1;
|
|
|
|
/* Push the entry into the fifo */
|
|
|
|
if (table->fifo_tail == -1)
|
2013-05-04 12:21:32 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
table->fifo_head = table->fifo_tail = entry_id;
|
2013-05-04 12:21:32 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
table->entries[table->fifo_tail].next_in_fifo = entry_id;
|
|
|
|
table->fifo_tail = entry_id;
|
2013-05-04 12:21:32 -07:00
|
|
|
}
|
2013-05-16 04:15:43 -07:00
|
|
|
PROTECT(table);
|
2013-05-17 01:35:16 -07:00
|
|
|
if (table->free_entries == MAX_ENTRY_ID)
|
|
|
|
{
|
|
|
|
UNPROTECT(_eo_ids_tables[mid_table_id]);
|
|
|
|
TABLE_FROM_IDS = NULL;
|
|
|
|
PROTECT(_eo_ids_tables[mid_table_id]);
|
|
|
|
/* Recycle or free the empty table */
|
|
|
|
if (!_empty_table)
|
|
|
|
_empty_table = table;
|
|
|
|
else
|
|
|
|
_eo_id_mem_free(table);
|
|
|
|
if (_current_table == table)
|
|
|
|
_current_table = NULL;
|
|
|
|
}
|
2013-04-27 15:43:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 04:19:02 -07:00
|
|
|
|
|
|
|
ERR("obj_id %p is not pointing to a valid object. Maybe it has already been freed.", (void *)obj_id);
|
|
|
|
#else
|
|
|
|
(void) obj_id;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_eo_free_ids_tables()
|
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index mid_table_id = 0; mid_table_id < MAX_MID_TABLE_ID; mid_table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
if (_eo_ids_tables[mid_table_id])
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index table_id = 0; table_id < MAX_TABLE_ID; table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
if (TABLE_FROM_IDS)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
_eo_id_mem_free(TABLE_FROM_IDS);
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
|
|
|
}
|
2013-05-16 01:55:32 -07:00
|
|
|
_eo_id_mem_free(_eo_ids_tables[mid_table_id]);
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
2013-05-16 01:55:32 -07:00
|
|
|
_eo_ids_tables[mid_table_id] = NULL;
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
2013-05-17 01:35:16 -07:00
|
|
|
if (_empty_table) _eo_id_mem_free(_empty_table);
|
|
|
|
_empty_table = _current_table = NULL;
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef EFL_DEBUG
|
|
|
|
void
|
|
|
|
_eo_print()
|
|
|
|
{
|
2013-04-27 15:43:23 -07:00
|
|
|
_Eo_Id_Entry *entry;
|
2013-04-18 04:19:02 -07:00
|
|
|
unsigned long obj_number = 0;
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index mid_table_id = 0; mid_table_id < MAX_MID_TABLE_ID; mid_table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
if (_eo_ids_tables[mid_table_id])
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index table_id = 0; table_id < MAX_TABLE_ID; table_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
if (TABLE_FROM_IDS)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
for (Table_Index entry_id = 0; entry_id < MAX_ENTRY_ID; entry_id++)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
2013-05-16 01:55:32 -07:00
|
|
|
entry = &(TABLE_FROM_IDS->entries[entry_id]);
|
2013-04-27 15:43:23 -07:00
|
|
|
if (entry->active)
|
2013-04-18 04:19:02 -07:00
|
|
|
{
|
|
|
|
printf("%ld: %p -> (%p, %p, %p, %p)\n", obj_number++,
|
2013-04-27 15:43:23 -07:00
|
|
|
entry->ptr,
|
2013-05-16 01:55:32 -07:00
|
|
|
(void *)mid_table_id, (void *)table_id, (void *)entry_id,
|
2013-04-27 15:43:23 -07:00
|
|
|
(void *)entry->generation);
|
2013-04-18 04:19:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|