From a19e3705a228735eacadd713b28d49fe33bb726b Mon Sep 17 00:00:00 2001 From: sijanec Date: Wed, 7 Apr 2021 23:15:16 +0200 Subject: UTF-8, image support, memory leaks fixed --- src/structs.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/structs.c') diff --git a/src/structs.c b/src/structs.c index 2d83f74..dce460e 100644 --- a/src/structs.c +++ b/src/structs.c @@ -1,4 +1,4 @@ -#define SC_ALLOC_CHUNK 128 /* how many x to alloc when allocing (for performance so we don't call malloc over and over again) */ +#define SC_ALLOC_CHUNK 1 /* how many x to alloc when allocing (for performance so we don't call malloc over and over again) */ #define SC_IN_STRUCT_ARRAY(type, name) _Atomic(type **) name; _Atomic(size_t) name##_sizeof; _Atomic(size_t) name##_length #define SC_CWLE(c, name) (pthread_rwlock_wrlock(name) ? (SC_LOG(SC_LOG_ERROR,c,SC_I18N_LOCKING " " #name " " SC_I18N_FAILED)||1) :0) #define SC_CRLE(c, name) (pthread_rwlock_rdlock(name) ? (SC_LOG(SC_LOG_ERROR,c,SC_I18N_LOCKING " " #name " " SC_I18N_FAILED)||1) :0) @@ -11,12 +11,14 @@ #define SC_LOG_WARNING (1 << 1) #define SC_LOG_INFO (1 << 2) #define SC_LOG_DEBUG (1 << 3) -#define SC_BIGGER_ARRAY(name, type) do { \ - name = realloc(name, sizeof(name[0])*name##_sizeof*SC_REALLOC_K); \ - for (size_t i = name##_sizeof; i < name##_sizeof*SC_REALLOC_K; i++) \ +#define SC_BIGGER_ARRAY(name, type, shallinit) do { \ + name = realloc(name, sizeof(name[0])*ceil(name##_sizeof*SC_REALLOC_K)); \ + for (size_t i = name##_sizeof; shallinit && (i < ceil(name##_sizeof*SC_REALLOC_K)); i++) \ name[i] = type##_init(); \ - name##_sizeof = name##_sizeof*SC_REALLOC_K; \ + name##_sizeof = ceil(name##_sizeof*SC_REALLOC_K); /* ceil je ZELO pomemben, če je chunk 1 recimo */ \ } while (0); +#define SC_OPT_TYPE unsigned char +#define SC_OPT_IMAGE (1 << 0) struct sc_logentry { unsigned char type; /* SC_LOG_ERROR, SC_LOG_WARNING, SC_LOG_INFO, SC_LOG_DEBUG */ size_t line; @@ -30,8 +32,8 @@ struct sc_logentry * sc_logentry_init (); /* defined in log.c */ struct sc_result { struct sc_query * query; /* nofree - free from sc_cache */ - char * url; /* yesfree */ - char * desc; /* yesfree */ + char * url; /* yesfree - url of referer page when image searching */ + char * desc; /* yesfree - url of image when image searching */ char * title; /* yesfree */ time_t date; /* some search engines like to extract a date from a website, store that here - not implemented */ unsigned short int rating; /* some search engines like to extract a rating from a website, store that here */ /* not implementd */ @@ -62,6 +64,7 @@ struct sc_query { char * string; /* yesfree - query string, stripped of any excess characters that should be excluded from indexing */ time_t lookup_time; /* time of last lookup */ unsigned char engines; /* with what engine(s) was the query done - bitmask - if there are results from multiple engines */ + SC_OPT_TYPE opt; /* some options */ }; struct sc_query * sc_query_init () { struct sc_query * q = calloc(1, sizeof(struct sc_query)); @@ -80,6 +83,7 @@ int sc_query_free (struct sc_query * q) { free(q->string); /* if they were not alloced, they are NULL, if they were free'd somewhere else, they are also set to NULL */ for (size_t i = 0; i < q->results_sizeof; i++) sc_result_free(q->results[i]); + free(q->results); free(q); return 1; } @@ -95,9 +99,9 @@ struct sc_cache * sc_cache_init() { c->logentries_sizeof = SC_ALLOC_CHUNK; c->queries = calloc(c->queries_sizeof, sizeof(struct sc_query *)); c->logentries = calloc(c->logentries_sizeof, sizeof(struct sc_logentry *)); - for (size_t i = 0; i < c->queries_sizeof; i++) { - c->queries[i] = sc_query_init(); - c->queries[i]->cache = c; + for (size_t i = 0; i < c->logentries_sizeof; i++) { + /* c->queries[i] = sc_query_init(); */ /* queries are not inited for performance reasons, they are inited by query function */ + /* c->queries[i]->cache = c; */ c->logentries[i] = sc_logentry_init(); } #define SC_CILI(name) do { name##_lock = malloc(sizeof(pthread_rwlock_t)); pthread_rwlock_init(name##_lock, NULL); } while (0) @@ -108,11 +112,13 @@ struct sc_cache * sc_cache_init() { int sc_cache_free(struct sc_cache * c) { if (!c) return -1; + fprintf(stderr, "c->queries_sizeof = %lu\n", c->queries_sizeof); for (size_t i = 0; i < c->queries_sizeof; i++) sc_query_free(c->queries[i]); free(c->queries); for (size_t i = 0; i < c->logentries_sizeof; i++) sc_logentry_free(c->logentries[i]); + free(c->logentries); #define SC_CFLD(name) do { pthread_rwlock_destroy(name##_lock); free(name##_lock); } while(0) SC_CFLD(c->queries); SC_CFLD(c->logentries); -- cgit v1.2.3