summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Philippe Andre <jp.andre@samsung.com>2013-07-31 19:31:50 +0900
committerJean-Philippe Andre <jp.andre@samsung.com>2013-10-28 15:47:13 +0900
commit08648ab2a5033da4f6c835c049238074802fb232 (patch)
tree2f625ed2f96a9bc84bac1b63c9c398aa73afce64
parent88ef4131a0df0735ebef19306285b4bea608cbab (diff)
evas/cserve2: Add _shared_index_remap_check()
Check consistency of index header and remap if necessary.
Diffstat (limited to '')
-rw-r--r--src/lib/evas/cserve2/evas_cs2_client.c126
-rw-r--r--src/lib/evas/cserve2/evas_cs2_private.h9
2 files changed, 108 insertions, 27 deletions
diff --git a/src/lib/evas/cserve2/evas_cs2_client.c b/src/lib/evas/cserve2/evas_cs2_client.c
index 73d0936e66..d7cd8147c5 100644
--- a/src/lib/evas/cserve2/evas_cs2_client.c
+++ b/src/lib/evas/cserve2/evas_cs2_client.c
@@ -1945,6 +1945,105 @@ _shared_image_entry_image_data_match(Image_Entry *ie, const Image_Data *id)
1945 return EINA_FALSE; 1945 return EINA_FALSE;
1946} 1946}
1947 1947
1948#define SHARED_INDEX_CHECK(si, typ) \
1949 if (!_shared_index_remap_check(&(si), sizeof(typ))) return NULL
1950
1951static Eina_Bool
1952_shared_index_remap_check(Shared_Index *si, int elemsize)
1953{
1954 size_t filesize;
1955 Eina_Bool refresh = EINA_FALSE;
1956
1957 // Note: all checks are unlikely to be true.
1958
1959 if (!si || elemsize <= 0) return EINA_FALSE;
1960 if (si->generation_id != _index.generation_id)
1961 {
1962 DBG("Generation ID changed.");
1963 if (si->f && si->data)
1964 {
1965 if (eina_file_refresh(si->f))
1966 {
1967 DBG("Remapping index.");
1968 eina_file_map_free(si->f, si->data);
1969 si->data = NULL;
1970 }
1971 }
1972 else if (si->f)
1973 {
1974 eina_file_close(si->f);
1975 si->f = NULL;
1976 }
1977 si->generation_id = _index.generation_id;
1978 }
1979 if (!si->f)
1980 {
1981 si->data = NULL; // If that was not NULL, the address was invalid.
1982 si->f = eina_file_open(si->path, EINA_TRUE);
1983 if (!si->f)
1984 {
1985 ERR("Could not open index '%s'", si->path);
1986 return EINA_FALSE;
1987 }
1988 }
1989 if (!si->data)
1990 {
1991 filesize = eina_file_size_get(si->f);
1992 if (filesize < sizeof(Shared_Array_Header))
1993 {
1994 ERR("Index is invalid. Got file size %d", (int) filesize);
1995 eina_file_close(si->f);
1996 si->f = NULL;
1997 return EINA_FALSE;
1998 }
1999 si->data = eina_file_map_all(si->f, EINA_FILE_RANDOM);
2000 if (!si->data)
2001 {
2002 ERR("Could not mmap index '%s'", si->path);
2003 eina_file_close(si->f);
2004 si->f = NULL;
2005 return EINA_FALSE;
2006 }
2007 refresh = EINA_TRUE;
2008 }
2009
2010 if (elemsize != si->header->elemsize)
2011 {
2012 ERR("Index is invalid. Expected element size %d, got %d.",
2013 elemsize, si->header->elemsize);
2014 return EINA_FALSE;
2015 }
2016
2017 if (si->count != si->header->count)
2018 {
2019 // generation_id should have been incremented. Maybe we are hitting
2020 // a race condition here, when cserve2 grows an index.
2021 WRN("Reported index count differs from known count: %d vs %d",
2022 si->header->count, si->count);
2023 filesize = eina_file_size_get(si->f);
2024 si->count = (filesize - sizeof(Shared_Array_Header)) / elemsize;
2025 if (si->count > si->header->count)
2026 {
2027 WRN("Index reports %d elements, but file can contain only %d",
2028 si->header->count, si->count);
2029 si->count = si->header->count;
2030 }
2031 refresh = EINA_TRUE;
2032 }
2033
2034 if (!si->entries_by_hkey)
2035 refresh = EINA_TRUE;
2036
2037 if (refresh)
2038 {
2039 if (si->entries_by_hkey) eina_hash_free_buckets(si->entries_by_hkey);
2040 else si->entries_by_hkey = eina_hash_string_small_new(NULL);
2041 si->last_entry_in_hash = 0;
2042 }
2043
2044 return EINA_TRUE;
2045}
2046
1948static const Image_Data * 2047static const Image_Data *
1949_shared_image_entry_image_data_find(Image_Entry *ie) 2048_shared_image_entry_image_data_find(Image_Entry *ie)
1950{ 2049{
@@ -1973,32 +2072,7 @@ _shared_image_entry_image_data_find(Image_Entry *ie)
1973 file_id = fdata->id; 2072 file_id = fdata->id;
1974 } 2073 }
1975 2074
1976 // FIXME. Factorize and simplify. 2075 SHARED_INDEX_CHECK(_index.images, Image_Data);
1977 if (_index.images.count != _index.images.header->count)
1978 {
1979 size_t sz;
1980 WRN("Image entries array has been resized from %d to %d. Remapping.",
1981 _index.images.count, _index.images.header->count);
1982 eina_file_map_free(_index.images.f, (void *) _index.images.header);
1983 eina_file_close(_index.images.f);
1984 _index.images.f = eina_file_open(_index.images.path, EINA_TRUE);
1985 sz = eina_file_size_get(_index.images.f);
1986 _index.images.header = eina_file_map_new(_index.images.f, EINA_FILE_RANDOM,
1987 0, sz); //eina_file_map_all(_index.images.f, EINA_FILE_RANDOM);
1988 _index.images.count = _index.images.header->count;
1989 if (_index.images.count * sizeof(Image_Data) + sizeof(Shared_Array_Header) > sz)
1990 CRIT("New mapping might be too small!");
1991 if (!_index.images.header)
1992 {
1993 ERR("Could not remap the array!");
1994 eina_file_close(_index.images.f);
1995 _index.images.f = NULL;
1996 _index.images.entries.idata = NULL;
1997 return NULL;
1998 }
1999 _index.images.entries.idata =
2000 (Image_Data *) &(_index.images.header[1]);
2001 }
2002 2076
2003 DBG("Looking for loaded image with file id %d", file_id); 2077 DBG("Looking for loaded image with file id %d", file_id);
2004 for (k = 0; k < _index.images.count; k++) 2078 for (k = 0; k < _index.images.count; k++)
diff --git a/src/lib/evas/cserve2/evas_cs2_private.h b/src/lib/evas/cserve2/evas_cs2_private.h
index a2b897b0de..289119f34d 100644
--- a/src/lib/evas/cserve2/evas_cs2_private.h
+++ b/src/lib/evas/cserve2/evas_cs2_private.h
@@ -25,10 +25,16 @@ struct _Data_Entry {
25 25
26struct _Shared_Index { 26struct _Shared_Index {
27 char path[64]; 27 char path[64];
28 int generation_id;
28 Eina_File *f; 29 Eina_File *f;
29 const Shared_Array_Header *header;
30 union 30 union
31 { 31 {
32 const Shared_Array_Header *header;
33 void *data;
34 };
35 union
36 {
37 const char *p; // Random access
32 const Index_Entry *index; // TODO for strings 38 const Index_Entry *index; // TODO for strings
33 const Image_Data *idata; 39 const Image_Data *idata;
34 const File_Data *fdata; 40 const File_Data *fdata;
@@ -40,6 +46,7 @@ struct _Shared_Index {
40}; 46};
41 47
42struct _Index_Table { 48struct _Index_Table {
49 int generation_id;
43 // TODO: use Shared_Index 50 // TODO: use Shared_Index
44 struct { 51 struct {
45 char index_path[64]; 52 char index_path[64];