summaryrefslogtreecommitdiff
path: root/src/lib/eo/eo_ptr_indirection.c
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-05-16 13:15:43 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2013-05-16 13:17:12 +0200
commitf2efa264590279b0407c98564f38fc41ba1d5d05 (patch)
treee85f96847578ef28c2a79e18e6e0b5acacd2fb26 /src/lib/eo/eo_ptr_indirection.c
parent380435abdb627159553d8a44d60d0948ba101651 (diff)
eo ptr ind: use of mprotect when EINA_DEBUG_MALLOC is set
- to enable this feature, compile with --with-profile=debug - the mid tables and tables are write protected after modifications, you will segfault if you mess with them
Diffstat (limited to 'src/lib/eo/eo_ptr_indirection.c')
-rw-r--r--src/lib/eo/eo_ptr_indirection.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/lib/eo/eo_ptr_indirection.c b/src/lib/eo/eo_ptr_indirection.c
index 7cf72fd..4440409 100644
--- a/src/lib/eo/eo_ptr_indirection.c
+++ b/src/lib/eo/eo_ptr_indirection.c
@@ -166,6 +166,29 @@ _eo_id_mem_free(void *ptr)
166#endif 166#endif
167} 167}
168 168
169#ifdef EINA_DEBUG_MALLOC
170static void
171_eo_id_mem_protect(void *ptr, Eina_Bool may_not_write)
172{
173# ifdef __linux__
174 Mem_Header *hdr;
175 if (!ptr) return;
176 hdr = (Mem_Header *)(((unsigned char *)ptr) - MEM_HEADER_SIZE);
177 if (hdr->magic != MEM_MAGIC)
178 {
179 ERR("mprotect of eo table region has bad magic!");
180 return;
181 }
182 mprotect(hdr, hdr->size, PROT_READ | ( may_not_write ? 0 : PROT_WRITE) );
183# endif
184}
185# define PROTECT(_ptr_) _eo_id_mem_protect((_ptr_), EINA_TRUE)
186# define UNPROTECT(_ptr_) _eo_id_mem_protect((_ptr_), EINA_FALSE)
187#else
188# define PROTECT(_ptr_)
189# define UNPROTECT(_ptr_)
190#endif
191
169/* Entry */ 192/* Entry */
170typedef struct 193typedef struct
171{ 194{
@@ -266,12 +289,14 @@ _get_available_entry(_Eo_Ids_Table *table)
266 { 289 {
267 /* Serve never used entries first */ 290 /* Serve never used entries first */
268 entry = &(table->entries[table->start]); 291 entry = &(table->entries[table->start]);
292 UNPROTECT(table);
269 table->start++; 293 table->start++;
270 } 294 }
271 else if (table->fifo_head != -1) 295 else if (table->fifo_head != -1)
272 { 296 {
273 /* Pop a free entry from the fifo */ 297 /* Pop a free entry from the fifo */
274 entry = &(table->entries[table->fifo_head]); 298 entry = &(table->entries[table->fifo_head]);
299 UNPROTECT(table);
275 if (entry->next_in_fifo == -1) 300 if (entry->next_in_fifo == -1)
276 table->fifo_head = table->fifo_tail = -1; 301 table->fifo_head = table->fifo_tail = -1;
277 else 302 else
@@ -305,8 +330,10 @@ _search_tables()
305 table = _eo_id_mem_calloc(1, sizeof(_Eo_Ids_Table)); 330 table = _eo_id_mem_calloc(1, sizeof(_Eo_Ids_Table));
306 table->start = 1; 331 table->start = 1;
307 table->fifo_head = table->fifo_tail = -1; 332 table->fifo_head = table->fifo_tail = -1;
308 TABLE_FROM_IDS = table;
309 entry = &(table->entries[0]); 333 entry = &(table->entries[0]);
334 UNPROTECT(_eo_ids_tables[mid_table_id]);
335 TABLE_FROM_IDS = table;
336 PROTECT(_eo_ids_tables[mid_table_id]);
310 } 337 }
311 else 338 else
312 entry = _get_available_entry(table); 339 entry = _get_available_entry(table);
@@ -351,6 +378,7 @@ _eo_id_allocate(const _Eo *obj)
351 entry->ptr = (_Eo *)obj; 378 entry->ptr = (_Eo *)obj;
352 entry->active = 1; 379 entry->active = 1;
353 entry->generation = _eo_generation_counter; 380 entry->generation = _eo_generation_counter;
381 PROTECT(current_table.table);
354 return EO_COMPOSE_ID(current_table.mid_table_id, 382 return EO_COMPOSE_ID(current_table.mid_table_id,
355 current_table.table_id, 383 current_table.table_id,
356 (entry - current_table.table->entries), 384 (entry - current_table.table->entries),
@@ -376,6 +404,7 @@ _eo_id_release(const Eo_Id obj_id)
376 entry = &(table->entries[entry_id]); 404 entry = &(table->entries[entry_id]);
377 if (entry && entry->active && (entry->generation == generation)) 405 if (entry && entry->active && (entry->generation == generation))
378 { 406 {
407 UNPROTECT(table);
379 /* Disable the entry */ 408 /* Disable the entry */
380 entry->active = 0; 409 entry->active = 0;
381 entry->next_in_fifo = -1; 410 entry->next_in_fifo = -1;
@@ -389,6 +418,7 @@ _eo_id_release(const Eo_Id obj_id)
389 table->entries[table->fifo_tail].next_in_fifo = entry_id; 418 table->entries[table->fifo_tail].next_in_fifo = entry_id;
390 table->fifo_tail = entry_id; 419 table->fifo_tail = entry_id;
391 } 420 }
421 PROTECT(table);
392 return; 422 return;
393 } 423 }
394 } 424 }