eina: use a stringshare to store the filename internally.

T6164
This commit is contained in:
Cedric Bail 2017-10-17 16:14:26 -07:00
parent e0807ce797
commit 6e64a104a6
4 changed files with 39 additions and 29 deletions

View File

@ -792,13 +792,13 @@ eina_file_open(const char *path, Eina_Bool shared)
{
Eina_File *file;
Eina_File *n;
char *filename;
Eina_Stringshare *filename;
struct stat file_stat;
int fd = -1;
EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
filename = eina_file_path_sanitize(path);
filename = eina_file_sanitize(path);
if (!filename) return NULL;
if (shared)
@ -830,7 +830,7 @@ eina_file_open(const char *path, Eina_Bool shared)
if (!file)
{
n = malloc(sizeof(Eina_File) + strlen(filename) + 1);
n = malloc(sizeof(Eina_File));
if (!n)
{
eina_lock_release(&_eina_file_lock_cache);
@ -838,8 +838,7 @@ eina_file_open(const char *path, Eina_Bool shared)
}
memset(n, 0, sizeof(Eina_File));
n->filename = (char*) (n + 1);
strcpy((char*) n->filename, filename);
n->filename = filename;
n->map = eina_hash_new(EINA_KEY_LENGTH(eina_file_map_key_length),
EINA_KEY_CMP(eina_file_map_key_cmp),
EINA_KEY_HASH(eina_file_map_key_hash),
@ -875,14 +874,12 @@ eina_file_open(const char *path, Eina_Bool shared)
eina_lock_release(&_eina_file_lock_cache);
free(filename);
return n;
on_error:
WRN("Could not open file [%s].", filename);
eina_stringshare_del(filename);
free(filename);
if (fd >= 0) close(fd);
return NULL;
}

View File

@ -502,6 +502,7 @@ eina_file_clean_close(Eina_File *file)
// Generic destruction of the file
eina_hash_free(file->rmap); file->rmap = NULL;
eina_hash_free(file->map); file->map = NULL;
eina_stringshare_del(file->filename);
// Backend specific file resource close
eina_file_real_close(file);
@ -515,7 +516,6 @@ EAPI void
eina_file_close(Eina_File *file)
{
Eina_Bool leave = EINA_TRUE;
unsigned int length;
unsigned int key;
if (!file) return ;
@ -529,13 +529,12 @@ eina_file_close(Eina_File *file)
eina_lock_release(&file->lock);
if (leave) goto end;
length = strlen(file->filename) + 1;
key = eina_hash_djb2(file->filename, length);
key = eina_hash_superfast((void*) &file->filename, sizeof (void*));
if (eina_hash_find_by_hash(_eina_file_cache,
file->filename, length, key) == file)
file->filename, 0, key) == file)
{
eina_hash_del_by_key_hash(_eina_file_cache,
file->filename, length, key);
file->filename, 0, key);
}
eina_file_clean_close(file);
@ -564,6 +563,20 @@ eina_file_filename_get(const Eina_File *file)
return file->filename;
}
Eina_Stringshare *
eina_file_sanitize(const char *path)
{
char *filename;
Eina_Stringshare *ss;
filename = eina_file_path_sanitize(path);
if (!filename) return NULL;
ss = eina_stringshare_add(filename);
free(filename);
return ss;
}
/* search '\r' and '\n' by preserving cache locality and page locality
in doing a search inside 4K boundary.
*/
@ -1055,7 +1068,7 @@ eina_file_init(void)
return EINA_FALSE;
}
_eina_file_cache = eina_hash_string_djb2_new(NULL);
_eina_file_cache = eina_hash_stringshared_new(NULL);
if (!_eina_file_cache)
{
ERR("Could not create cache.");

View File

@ -711,7 +711,7 @@ eina_file_open(const char *path, Eina_Bool shared)
{
Eina_File *file;
Eina_File *n;
char *filename;
Eina_Stringshare *filename;
HANDLE handle;
WIN32_FILE_ATTRIBUTE_DATA fad;
ULARGE_INTEGER length;
@ -719,7 +719,7 @@ eina_file_open(const char *path, Eina_Bool shared)
EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);
filename = eina_file_path_sanitize(path);
filename = eina_file_sanitize(path);
if (!filename) return NULL;
/* FIXME: how to emulate shm_open ? Just OpenFileMapping ? */
@ -770,7 +770,7 @@ eina_file_open(const char *path, Eina_Bool shared)
if (!file)
{
n = malloc(sizeof(Eina_File) + strlen(filename) + 1);
n = malloc(sizeof(Eina_File));
if (!n)
{
eina_lock_release(&_eina_file_lock_cache);
@ -778,8 +778,7 @@ eina_file_open(const char *path, Eina_Bool shared)
}
memset(n, 0, sizeof(Eina_File));
n->filename = (char*) (n + 1);
strcpy((char*) n->filename, filename);
n->filename = filename;
n->map = eina_hash_new(EINA_KEY_LENGTH(eina_file_map_key_length),
EINA_KEY_CMP(eina_file_map_key_cmp),
EINA_KEY_HASH(eina_file_map_key_hash),
@ -808,15 +807,13 @@ eina_file_open(const char *path, Eina_Bool shared)
eina_lock_release(&_eina_file_lock_cache);
free(filename);
return n;
close_handle:
CloseHandle(handle);
free_file:
ERR("Could not open file [%s].", filename);
free(filename);
eina_stringshare_del(filename);
return NULL;
}
@ -824,8 +821,9 @@ eina_file_open(const char *path, Eina_Bool shared)
EAPI Eina_Bool
eina_file_unlink(const char *pathname)
{
char *unlink_path = eina_file_path_sanitize(pathname);
Eina_Stringshare *unlink_path = eina_file_sanitize(pathname);
Eina_File *file = eina_hash_find(_eina_file_cache, unlink_path);
Eina_Bool r = EINA_FALSE;
if (file)
{
@ -845,17 +843,16 @@ eina_file_unlink(const char *pathname)
{
CloseHandle(file->handle);
file->handle = INVALID_HANDLE_VALUE;
return EINA_TRUE;
r = EINA_TRUE;
goto finish;
}
}
}
if ( unlink(unlink_path) < 0)
{
return EINA_FALSE;
}
if ( unlink(unlink_path) >= 0) r = EINA_TRUE;
eina_stringshare_del(unlink_path);
return EINA_TRUE;
return r;
}

View File

@ -24,6 +24,7 @@
#include "eina_magic.h"
#include "eina_iterator.h"
#include "eina_accessor.h"
#include "eina_stringshare.h"
#ifndef ABS
# define ABS(x) ((x) < 0 ? -(x) : (x))
@ -142,6 +143,8 @@ Eina_Bool eina_file_mmap_faulty(void *addr, long page_size);
typedef struct _Eina_FreeQ Eina_FreeQ;
#endif
Eina_Stringshare *eina_file_sanitize(const char *path);
void eina_freeq_main_set(Eina_FreeQ *fq);
#include "eina_inline_private.h"