Compare commits

...

2 Commits

Author SHA1 Message Date
Carsten Haitzler cdd3c35bb3 eo: make eo ptr indirection caches flexible 2024-04-09 10:30:47 +01:00
Vincent Torri 49976ca388 use eina_file_access() instead of access() if possible 2024-04-09 07:33:56 +01:00
10 changed files with 141 additions and 147 deletions

View File

@ -22,15 +22,6 @@ static Eina_Module *_ecore_evas_vnc = NULL;
# define ECORE_EVAS_ENGINE_NAME "module.so"
#endif
static inline Eina_Bool
_file_exists(const char *file)
{
if (!file) return EINA_FALSE;
if (access(file, F_OK) == -1) return EINA_FALSE;
return EINA_TRUE;
}
static Eina_Module *
_ecore_evas_vnc_server_module_try_load(const char *prefix,
@ -219,7 +210,7 @@ _ecore_evas_available_engines_get(void)
eina_strbuf_append_printf(buf, "%s/%s/" ECORE_EVAS_ENGINE_NAME,
info->path, MODULE_ARCH);
if (_file_exists(eina_strbuf_string_get(buf)))
if (eina_file_access(eina_strbuf_string_get(buf), EINA_FILE_ACCESS_MODE_EXIST))
{
const char *name;

View File

@ -606,111 +606,19 @@ ecore_file_dir_get(const char *file)
EAPI Eina_Bool
ecore_file_can_read(const char *file)
{
if (!file) return EINA_FALSE;
if (!access(file, R_OK)) return EINA_TRUE;
return EINA_FALSE;
return eina_file_access(file, EINA_FILE_ACCESS_MODE_READ);
}
EAPI Eina_Bool
ecore_file_can_write(const char *file)
{
if (!file) return EINA_FALSE;
if (!access(file, W_OK)) return EINA_TRUE;
return EINA_FALSE;
return eina_file_access(file, EINA_FILE_ACCESS_MODE_WRITE);
}
EAPI Eina_Bool
ecore_file_can_exec(const char *file)
{
#ifdef _WIN32
HANDLE h;
HANDLE fm;
char *base;
char *base_nt;
LARGE_INTEGER sz;
WORD characteristics;
#endif
if (!file || !*file) return EINA_FALSE;
#ifdef _WIN32
/*
* we parse the file to check if it is a PE file (EXE or DLL)
* and we finally check whether it's a DLL or not.
* Reference :
* https://docs.microsoft.com/en-us/windows/win32/debug/pe-format
*/
h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE)
goto test_bat;
if (!GetFileSizeEx(h, &sz))
goto close_h;
/* a PE file must have at least the DOS and NT headers */
if (sz.QuadPart < (LONGLONG)(sizeof(IMAGE_DOS_HEADER) + sizeof(IMAGE_NT_HEADERS)))
goto close_h;
fm = CreateFileMapping(h, NULL, PAGE_READONLY, 0, 0, NULL);
if (fm == NULL)
goto close_h;
base = (char *)MapViewOfFile(fm, FILE_MAP_READ, 0, 0, 0);
CloseHandle(fm);
if (base == NULL)
goto close_h;
/*
* the PE file begins with the DOS header.
* First magic number : the DOS header must begin with a DOS magic number,
* that is "MZ", that is 0x5a4d, stored in a WORD.
*/
if (*((WORD *)base) != 0x5a4d)
goto unmap_view;
/*
* The position of the NT header is located at the offset 0x3c.
*/
base_nt = base + *((DWORD *)(base + 0x3c));
/*
* The NT header begins with the magic number "PE\0\0", that is
* 0x00004550, stored in a DWORD.
*/
if (*((DWORD *)base_nt) != 0x00004550)
goto unmap_view;
/*
* to get informations about executable (EXE or DLL), we look at
* the 'Characteristics' member of the NT header, located at the offset
* 22 (4 for the magic number, 18 for the offset) from base_nt.
* https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#characteristics
*/
characteristics = *((WORD *)(base_nt + 4 + 18));
UnmapViewOfFile(base);
CloseHandle(h);
/*
* 0x0002 : if set, EXE or DLL
* 0x2000 : if set, DLL
*/
if ((characteristics & 0x0002) && !(characteristics & 0x2000))
return EINA_TRUE;
unmap_view:
UnmapViewOfFile(base);
close_h:
CloseHandle(h);
test_bat:
/*
* a .bat file, considered as an executable, is only a text file,
* so we rely on the extension. Not the best but we cannot do more.
*/
return eina_str_has_extension(file, ".bat");
#else
if (!access(file, X_OK)) return EINA_TRUE;
#endif
return EINA_FALSE;
return eina_file_access(file, EINA_FILE_ACCESS_MODE_EXEC);
}
EAPI char *

View File

@ -1568,6 +1568,7 @@ eet_open(const char *file,
{
if (mode == EET_FILE_MODE_READ_WRITE)
{
/* do not use eina_file_access() here */
ret = access(file, W_OK);
if ((ret != 0) && (errno != ENOENT)) return NULL;
}
@ -1606,6 +1607,7 @@ open_error:
size = 0;
fp = NULL;
/* do not use eina_file_access() here */
ret = access(file, W_OK);
if ((ret != 0) && (errno != ENOENT)) return NULL;
}

View File

@ -460,23 +460,23 @@ eeze_disk_removable_get(Eeze_Disk *disk)
EAPI Eina_Bool
eeze_disk_can_mount(void)
{
if (sizeof(EEZE_MOUNT_BIN) == sizeof(""))
return EINA_FALSE;
return access(EEZE_MOUNT_BIN, X_OK | R_OK) == 0;
return eina_file_access(EEZE_MOUNT_BIN,
EINA_FILE_ACCESS_MODE_EXEC |
EINA_FILE_ACCESS_MODE_READ);
}
EAPI Eina_Bool
eeze_disk_can_unmount(void)
{
if (sizeof(EEZE_UNMOUNT_BIN) == sizeof(""))
return EINA_FALSE;
return access(EEZE_UNMOUNT_BIN, X_OK | R_OK) == 0;
return eina_file_access(EEZE_UNMOUNT_BIN,
EINA_FILE_ACCESS_MODE_EXEC |
EINA_FILE_ACCESS_MODE_READ);
}
EAPI Eina_Bool
eeze_disk_can_eject(void)
{
if (sizeof(EEZE_EJECT_BIN) == sizeof(""))
return EINA_FALSE;
return access(EEZE_EJECT_BIN, X_OK | R_OK) == 0;
return eina_file_access(EEZE_EJECT_BIN,
EINA_FILE_ACCESS_MODE_EXEC |
EINA_FILE_ACCESS_MODE_READ);
}

View File

@ -56,7 +56,7 @@ static Eina_Bool
_eeze_mount_lock_mtab(void)
{
// DBG("Locking mlock: %s", mnt_lock_get_linkfile(_eeze_mtab_lock));
if (EINA_LIKELY(access("/etc/mtab", W_OK)))
if (EINA_LIKELY(!eina_file_access("/etc/mtab", EINA_FILE_ACCESS_MODE_WRITE)))
{
INF("Insufficient privs for mtab lock, continuing without lock");
return EINA_TRUE;

View File

@ -276,7 +276,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0)
/* 1. is argv0 abs path? */
if (!eina_file_path_relative(argv0))
{
if (access(argv0, X_OK) == 0)
if (eina_file_access(argv0, EINA_FILE_ACCESS_MODE_EXEC))
{
INF("Executable argv0 is full path = %s", argv0);
STRDUP_REP(pfx->exe_path, argv0);
@ -296,7 +296,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0)
eina_file_path_join(joined, len, buf2, argv0);
if (realpath(joined, buf))
{
if (access(buf, X_OK) == 0)
if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC))
{
INF("Executable relative argv0=%s, cwd=%s, realpath=%s",
argv0, buf2, buf);
@ -340,7 +340,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0)
strcpy(buf2 + len + 1, argv0);
if (realpath(buf2, buf))
{
if (access(buf, X_OK) == 0)
if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC))
{
STRDUP_REP(pfx->exe_path, buf);
INF("Path %s is executable", pfx->exe_path);

View File

@ -1044,7 +1044,7 @@ elm_quicklaunch_prepare(int argc,
strcat(p, "../lib/");
strcat(p, exename);
strcat(p, LIBEXT);
if (access(exe2, R_OK | X_OK) != 0)
if (!eina_file_access(exe2, EINA_FILE_ACCESS_MODE_EXEC | EINA_FILE_ACCESS_MODE_READ))
ELM_SAFE_FREE(exe2, free);
/* Try linking to executable first. Works with PIE files. */
@ -1348,7 +1348,7 @@ elm_quicklaunch_exe_path_get(const char *exe, const char *cwd)
EINA_LIST_FOREACH(pathlist, l, pathitr)
{
snprintf(buf, sizeof(buf), "%s/%s", pathitr, exe);
if (!access(buf, R_OK | X_OK)) return strdup(buf);
if (eina_file_access(buf, EINA_FILE_ACCESS_MODE_EXEC | EINA_FILE_ACCESS_MODE_READ)) return strdup(buf);
}
return NULL;
}

View File

@ -94,19 +94,20 @@ _eo_obj_pointer_get(const Eo_Id obj_id, const char *func_name, const char *file,
EINA_PREFETCH(&(data->tables[0]));
domain = (obj_id >> SHIFT_DOMAIN) & MASK_DOMAIN;
tdata = _eo_table_data_table_get(data, domain);
EINA_PREFETCH(&(tdata->cache.id));
if (EINA_UNLIKELY(!tdata)) goto err;
_eo_cache_prefetch(tdata);
if (EINA_LIKELY(domain != EFL_ID_DOMAIN_SHARED))
{
if (obj_id == tdata->cache.id)
return tdata->cache.object;
_Eo_Object *obj;
obj = _eo_cache_find(tdata, obj_id);
if (obj) return obj;
mid_table_id = (obj_id >> SHIFT_MID_TABLE_ID) & MASK_MID_TABLE_ID;
EINA_PREFETCH_NOCACHE(&(tdata->eo_ids_tables[mid_table_id])); //prefetch for line 119
EINA_PREFETCH(&(tdata->eo_ids_tables[mid_table_id])); //prefetch for line 119
table_id = (obj_id >> SHIFT_TABLE_ID) & MASK_TABLE_ID;
EINA_PREFETCH_NOCACHE((tdata->eo_ids_tables[mid_table_id] + table_id)); //prefetch for line 121
EINA_PREFETCH((tdata->eo_ids_tables[mid_table_id] + table_id)); //prefetch for line 121
entry_id = (obj_id >> SHIFT_ENTRY_ID) & MASK_ENTRY_ID;
generation = obj_id & MASK_GENERATIONS;
@ -126,8 +127,7 @@ _eo_obj_pointer_get(const Eo_Id obj_id, const char *func_name, const char *file,
if (entry->active && (entry->generation == generation))
{
// Cache the result of that lookup
tdata->cache.object = entry->ptr;
tdata->cache.id = obj_id;
_eo_cache_store(tdata, obj_id, entry->ptr);
return entry->ptr;
}
}
@ -136,17 +136,19 @@ _eo_obj_pointer_get(const Eo_Id obj_id, const char *func_name, const char *file,
}
else
{
_Eo_Object *obj;
eina_lock_take(&(_eo_table_data_shared_data->obj_lock));
if (obj_id == tdata->cache.id)
// yes we return keeping the lock locked. that's why
// you must call _eo_obj_pointer_done() wrapped
// by EO_OBJ_DONE() to release
return tdata->cache.object;
obj = _eo_cache_find(tdata, obj_id);
if (obj) return obj;
mid_table_id = (obj_id >> SHIFT_MID_TABLE_ID) & MASK_MID_TABLE_ID;
EINA_PREFETCH_NOCACHE(&(tdata->eo_ids_tables[mid_table_id]));
EINA_PREFETCH(&(tdata->eo_ids_tables[mid_table_id]));
table_id = (obj_id >> SHIFT_TABLE_ID) & MASK_TABLE_ID;
EINA_PREFETCH_NOCACHE((tdata->eo_ids_tables[mid_table_id] + table_id));
EINA_PREFETCH((tdata->eo_ids_tables[mid_table_id] + table_id));
entry_id = (obj_id >> SHIFT_ENTRY_ID) & MASK_ENTRY_ID;
generation = obj_id & MASK_GENERATIONS;
@ -167,8 +169,7 @@ _eo_obj_pointer_get(const Eo_Id obj_id, const char *func_name, const char *file,
if (entry->active && (entry->generation == generation))
{
// Cache the result of that lookup
tdata->cache.object = entry->ptr;
tdata->cache.id = obj_id;
_eo_cache_store(tdata, obj_id, entry->ptr);
// yes we return keeping the lock locked. that's why
// you must call _eo_obj_pointer_done() wrapped
// by EO_OBJ_DONE() to release

View File

@ -305,13 +305,19 @@ typedef struct
typedef struct _Eo_Id_Data Eo_Id_Data;
typedef struct _Eo_Id_Table_Data Eo_Id_Table_Data;
#define CACHENUM 2
#define CACHELINE 64
#define CACHELRU 1
struct _Eo_Id_Table_Data
{
/* Cached eoid lookups */
#if CACHENUM > 0
Eo_Id cache_id[CACHENUM];
_Eo_Object *cache_object[CACHENUM];
#endif
struct
{
Eo_Id id;
_Eo_Object *object;
const Eo *isa_id;
const Efl_Class *klass;
Eina_Bool isa;
@ -343,6 +349,100 @@ extern Eina_TLS _eo_table_data;
extern Eo_Id_Data *_eo_table_data_shared;
extern Eo_Id_Table_Data *_eo_table_data_shared_data;
#ifndef CACHELRU
static inline unsigned int
_eo_cache_slot_get(void)
{
# if CACHENUM > 0
static unsigned int num = 0;
return (++num) % CACHENUM;
# endif
}
#endif
static inline void
_eo_cache_prefetch(Eo_Id_Table_Data *tdata EINA_UNUSED)
{
#if CACHENUM > 0
int i;
for (i = 0; i < CACHENUM; i += (CACHELINE / sizeof(void *)))
{
EINA_PREFETCH(&(tdata->cache_id[i]));
if ((sizeof(void *) * CACHENUM) >= CACHELINE)
{
EINA_PREFETCH(&(tdata->cache_object[i]));
}
}
#endif
}
static inline _Eo_Object *
_eo_cache_find(Eo_Id_Table_Data *tdata EINA_UNUSED, Eo_Id obj_id EINA_UNUSED)
{
#if CACHENUM > 0
int i;
for (i = 0; i < CACHENUM; i++)
{
if (obj_id == tdata->cache_id[i]) return tdata->cache_object[i];
}
#endif
return NULL;
}
static inline void
_eo_cache_store(Eo_Id_Table_Data *tdata EINA_UNUSED, Eo_Id obj_id EINA_UNUSED, _Eo_Object *obj EINA_UNUSED)
{
#if CACHENUM > 0
# ifdef CACHELRU
# if CACHENUM > 1
memmove(&tdata->cache_id[1], &tdata->cache_id[0],
(CACHENUM - 1) * sizeof(tdata->cache_id[0]));
memmove(&tdata->cache_object[1], &tdata->cache_object[0],
(CACHENUM - 1) * sizeof(tdata->cache_object[0]));
# endif
tdata->cache_id[0] = obj_id;
tdata->cache_object[0] = obj;
# else
int slot = _eo_cache_slot_get();
tdata->cache_id[slot] = obj_id;
tdata->cache_object[slot] = obj;
# endif
#endif
}
static inline void
_eo_cache_invalidate(Eo_Id_Table_Data *tdata EINA_UNUSED, Eo_Id obj_id EINA_UNUSED)
{
#if CACHENUM > 0
int i;
for (i = 0; i < CACHENUM; i++)
{
if (obj_id == tdata->cache_id[i])
{
# ifdef CACHELRU
if (EINA_LIKELY((CACHENUM - 1 - i) > 0))
{
memmove(&tdata->cache_id[i], &tdata->cache_id[i + 1],
(CACHENUM - 1 - i) * sizeof(tdata->cache_id[0]));
memmove(&tdata->cache_object[i], &tdata->cache_object[i + 1],
(CACHENUM - 1 - i) * sizeof(tdata->cache_object[0]));
}
tdata->cache_id[CACHENUM - 1] = 0;
tdata->cache_object[CACHENUM - 1] = NULL;
# else
tdata->cache_id[i] = 0;
tdata->cache_object[i] = NULL;
# endif
return;
}
}
#endif
}
static inline Eo_Id_Table_Data *
_eo_table_data_table_new(Efl_Id_Domain domain)
{
@ -672,11 +772,7 @@ _eo_id_release(const Eo_Id obj_id)
tdata->current_table = NULL;
}
// In case an object is destroyed, wipe out the cache
if (tdata->cache.id == obj_id)
{
tdata->cache.id = 0;
tdata->cache.object = NULL;
}
_eo_cache_invalidate(tdata, obj_id);
if ((Eo_Id)tdata->cache.isa_id == obj_id)
{
tdata->cache.isa_id = NULL;
@ -722,11 +818,7 @@ _eo_id_release(const Eo_Id obj_id)
tdata->current_table = NULL;
}
// In case an object is destroyed, wipe out the cache
if (tdata->cache.id == obj_id)
{
tdata->cache.id = 0;
tdata->cache.object = NULL;
}
_eo_cache_invalidate(tdata, obj_id);
if ((Eo_Id)tdata->cache.isa_id == obj_id)
{
tdata->cache.isa_id = NULL;

View File

@ -852,7 +852,7 @@ ethumb_file_set(Ethumb *e, const char *path, const char *key)
sanitized_path = eina_file_path_sanitize(path);
DBG("ethumb=%p, path=%s, key=%s", e, sanitized_path ? sanitized_path : "", key ? key : "");
if (sanitized_path && access(sanitized_path, R_OK))
if (sanitized_path && !eina_file_access(sanitized_path, EINA_FILE_ACCESS_MODE_READ))
{
free(sanitized_path);
return EINA_FALSE;