Commit: 187a7cc8c482721b03ad1136589a87b608191a70
Parent: 80815f16a643e22fe3aef871fe217f9741380379
Author: opask
Date: Thu, 2 Aug 2018 06:22:14 -0600
simplification in memory.c, minor simplification in cache.c, related cleanup
Diffstat:
14 files changed, 44 insertions(+), 174 deletions(-)
diff --git a/cache.c b/cache.c
@@ -3,6 +3,8 @@
* This file is a part of the Links program, released under GPL.
*/
+#include <errno.h>
+#include <search.h>
#include "links.h"
static struct list_head cache = {&cache, &cache};
@@ -15,10 +17,9 @@ static void *cache_root = NULL;
static int ce_compare(const void *p1, const void *p2)
{
- const unsigned char *u1 = (const unsigned char *)p1;
- const unsigned char *u2 = (const unsigned char *)p2;
- if (u1 == u2) return 0;
- return strcmp(cast_const_char u1, cast_const_char u2);
+ if (p1 == p2)
+ return 0;
+ return strcmp(p1, p2);
}
static void cache_add_to_tree(struct cache_entry *e)
@@ -27,12 +28,9 @@ static void cache_add_to_tree(struct cache_entry *e)
if (!e->url[0])
return;
-retry:
- p = tsearch(e->url, &cache_root, ce_compare);
- if (!p) {
- out_of_memory(0, cast_uchar "tsearch", 0);
- goto retry;
- }
+ if (!(p = tsearch(e->url, &cache_root, ce_compare)))
+ die("tsearch: %s\n", strerror(errno));
+
if ((unsigned char *)*p != e->url)
internal("cache_add_to_tree: url '%s' is already present", e->url);
}
@@ -166,7 +164,7 @@ int get_connection_cache_entry(struct connection *c)
int new_cache_entry(unsigned char *url, struct cache_entry **f)
{
struct cache_entry *e;
- shrink_memory(SH_CHECK_QUOTA, 0);
+ shrink_memory(SH_CHECK_QUOTA);
url = remove_proxy_prefix(url);
e = mem_calloc(sizeof(struct cache_entry) + strlen((const char *)url));
strcpy(cast_char e->url, cast_const_char url);
@@ -468,13 +466,13 @@ static int shrink_file_cache(int u)
if (e->decompressed_len)
free_decompressed_data(e);
else delete_cache_entry(e);
- r |= ST_SOMETHING_FREED;
+ r = 1;
goto ret;
}
if (!e->refcount && e->decompressed_len
&& cache_size + decompressed_cache_size > (my_uintptr_t)memory_cache_size) {
free_decompressed_data(e);
- r |= ST_SOMETHING_FREED;
+ r = 1;
}
ccs += (my_uintptr_t)e->data_size;
ccs2 += e->decompressed_len;
@@ -521,7 +519,7 @@ static int shrink_file_cache(int u)
if (f->tgc) {
lf = lf->prev;
delete_cache_entry(f);
- r |= ST_SOMETHING_FREED;
+ r = 1;
}
}
ret:
diff --git a/compress.c b/compress.c
@@ -236,7 +236,7 @@ static int decode_gzip(struct terminal *term, struct cache_entry *ce, int defl,
after_inflateend:
if (memory_error) {
free(p);
- if (out_of_memory(0, NULL, 0))
+ if (out_of_memory())
goto retry_after_memory_error;
decompress_error(term, ce, cast_uchar "zlib", z.msg ? (unsigned char *)z.msg : TEXT_(T_OUT_OF_MEMORY), errp);
return 1;
diff --git a/dip.c b/dip.c
@@ -907,7 +907,7 @@ void scale_color(unsigned short *in, int ix, int iy, unsigned short **out,
free(in);
if (ox && (unsigned)ox * (unsigned)oy / (unsigned)ox != (unsigned)oy) overalloc();
if ((unsigned)ox * (unsigned)oy > MAXINT / 3 / sizeof(**out)) overalloc();
- *out=mem_calloc_mayfail(ox*oy*sizeof(**out)*3);
+ *out = mem_calloc(ox * oy * sizeof(**out) * 3);
return;
}
if (display_optimize&&ox*3<=ix){
@@ -1491,13 +1491,15 @@ const unsigned char *png_data, int png_length)
NULL, my_png_error, my_png_warning);
#endif
if (!png_ptr) {
- if (out_of_memory(0, NULL, 0)) goto retry1;
+ if (out_of_memory())
+ goto retry1;
fatal_exit("png_create_read_struct failed");
}
retry2:
info_ptr=png_create_info_struct(png_ptr);
if (!info_ptr) {
- if (out_of_memory(0, NULL, 0)) goto retry2;
+ if (out_of_memory())
+ goto retry2;
fatal_exit("png_create_info_struct failed");
}
png_set_read_fn(png_ptr, &work, (png_rw_ptr)read_stored_data);
diff --git a/dns.c b/dns.c
@@ -355,7 +355,7 @@ int find_host_no_cache(unsigned char *name, struct lookup_result *addr, void **q
retry:
q = (struct dnsquery *)malloc(sizeof(struct dnsquery) + strlen(cast_const_char name));
if (!q) {
- if (out_of_memory(0, NULL, 0))
+ if (out_of_memory())
goto retry;
fn(data, 1);
return 0;
diff --git a/error.c b/error.c
@@ -66,17 +66,13 @@ void int_error(char *m, ...)
#endif
}
-void *mem_calloc_(size_t size, int mayfail)
+void *mem_calloc(size_t size)
{
void *p;
- debug_test_free(NULL, 0);
if (!size)
return NULL;
- retry:
- if (!(p = calloc(1, size))) {
- if (out_of_memory_fl(0, !mayfail ? cast_uchar "calloc" : NULL, size, NULL, 0)) goto retry;
- return NULL;
- }
+ if (!(p = calloc(1, size)))
+ die("calloc: %s\n", strerror(errno));
return p;
}
diff --git a/https.c b/https.c
@@ -42,7 +42,7 @@ static void *malloc_hook(size_t size file_line_arg)
void *p;
in_ssl_malloc_hook++;
if (!size) size = 1;
- do p = malloc(size); while (!p && out_of_memory(0, NULL, 0));
+ do p = malloc(size); while (!p && out_of_memory());
in_ssl_malloc_hook--;
return p;
}
@@ -53,7 +53,7 @@ static void *realloc_hook(void *ptr, size_t size file_line_arg)
if (!ptr) return malloc_hook(size pass_file_line);
in_ssl_malloc_hook++;
if (!size) size = 1;
- do p = realloc(ptr, size); while (!p && out_of_memory(0, NULL, 0));
+ do p = realloc(ptr, size); while (!p && out_of_memory());
in_ssl_malloc_hook--;
return p;
}
diff --git a/links.h b/links.h
@@ -207,9 +207,7 @@ static inline int safe_add_function(int x, int y, unsigned char *file, int line)
#define safe_add(x, y) safe_add_function(x, y, (unsigned char *)__FILE__, __LINE__)
-void *mem_calloc_(size_t size, int mayfail);
-#define mem_calloc(x) mem_calloc_(x, 0)
-#define mem_calloc_mayfail(x) mem_calloc_(x, 1)
+void *mem_calloc(size_t size);
unsigned char *memacpy(const unsigned char *src, size_t len);
unsigned char *stracpy(const unsigned char *src);
@@ -285,7 +283,7 @@ static inline unsigned char *init_str()
{
unsigned char *p;
- p = mem_calloc_(1L, 0);
+ p = mem_calloc(1L);
return p;
}
@@ -418,15 +416,10 @@ void os_detach_console(void);
#define MF_GPI 1
-void heap_trim(void);
-int shrink_memory(int, int);
+int shrink_memory(int);
void register_cache_upcall(int (*)(int), int, unsigned char *);
void free_all_caches(void);
-extern int malloc_try_hard;
-int out_of_memory_fl(int flags, unsigned char *msg, size_t size, unsigned char *file, int line);
-#define out_of_memory(flags, msg, size) out_of_memory_fl(flags, msg, size, (unsigned char *)__FILE__, __LINE__)
-
-#define debug_test_free(file, line)
+int out_of_memory(void);
/* select.c */
diff --git a/memory.c b/memory.c
@@ -15,46 +15,14 @@ struct cache_upcall {
static struct list_head cache_upcalls = { &cache_upcalls, &cache_upcalls }; /* cache_upcall */
-void heap_trim(void)
-{
-#if defined(HAVE__HEAPMIN)
- _heapmin();
-#endif
-#if defined(HAVE_MALLOC_TRIM)
- malloc_trim(0);
-#endif
-}
-
-int shrink_memory(int type, int flags)
+int shrink_memory(int type)
{
struct cache_upcall *c;
struct list_head *lc;
int a = 0;
foreach(struct cache_upcall, c, lc, cache_upcalls) {
- if (flags && !(c->flags & flags)) continue;
a |= c->upcall(type);
}
-#if defined(HAVE__HEAPMIN) || defined(HAVE_MALLOC_TRIM)
- {
- static uttime last_heapmin = 0;
- static uttime min_interval = 0;
- uttime now = get_time();
- /* malloc_trim degrades performance (unlike _heapmin), so we call it less often */
- if (type == SH_FREE_ALL || (now - last_heapmin >= min_interval &&
-#if defined(HAVE_MALLOC_TRIM)
- now - last_heapmin >= MALLOC_TRIM_INTERVAL
-#else
- (a & ST_SOMETHING_FREED || now - last_heapmin >= HEAPMIN_INTERVAL)
-#endif
- )) {
- uttime after;
- heap_trim();
- after = get_time();
- min_interval = HEAPMIN_FACTOR * (after - now);
- last_heapmin = after;
- }
- }
-#endif
return a;
}
@@ -81,7 +49,7 @@ void free_all_caches(void)
a |= x;
b &= x;
}
- } while (a & ST_SOMETHING_FREED);
+ } while (a);
if (!(b & ST_CACHE_EMPTY)) {
unsigned char *m = init_str();
int l = 0;
@@ -95,98 +63,10 @@ void free_all_caches(void)
free_list(struct cache_upcall, cache_upcalls);
}
-int malloc_try_hard = 0;
-
-int out_of_memory_fl(int flags, unsigned char *msg, size_t size, unsigned char *file, int line)
+int out_of_memory(void)
{
- int sh;
-retry:
- sh = shrink_memory(SH_FREE_SOMETHING, flags);
- /*fprintf(stderr, "out of memory: %d, %d (%s,%d)\n", flags, sh, msg, size);*/
- if (sh & ST_SOMETHING_FREED) return 1;
- if (flags) {
- flags = 0;
- goto retry;
- }
- if (!malloc_try_hard) {
- malloc_try_hard = 1;
+ if (shrink_memory(SH_FREE_SOMETHING))
return 1;
- }
- if (!msg) return 0;
- fatal_tty_exit();
-
- fprintf(stderr, "\n");
- fprintf(stderr, "File cache: %lu bytes, %lu files, %lu locked, %lu loading\n", (unsigned long)cache_info(CI_BYTES), (unsigned long)cache_info(CI_FILES), (unsigned long)cache_info(CI_LOCKED), (unsigned long)cache_info(CI_LOADING));
- fprintf(stderr, "Decompressed cache: %lu bytes, %lu files, %lu locked\n", (unsigned long)decompress_info(CI_BYTES), (unsigned long)decompress_info(CI_FILES), (unsigned long)decompress_info(CI_LOCKED));
-#ifdef G
- if (F) {
- fprintf(stderr, "Image cache: %lu bytes, %lu files, %lu locked\n", (unsigned long)imgcache_info(CI_BYTES), (unsigned long)imgcache_info(CI_FILES), (unsigned long)imgcache_info(CI_LOCKED));
- fprintf(stderr, "Font cache: %lu bytes, %lu letters\n", (unsigned long)fontcache_info(CI_BYTES), (unsigned long)fontcache_info(CI_FILES));
- }
-#endif
- fprintf(stderr, "Formatted document cache: %lu documents, %lu locked\n", formatted_info(CI_FILES), formatted_info(CI_LOCKED));
- fprintf(stderr, "DNS cache: %lu servers", dns_info(CI_FILES));
- fprintf(stderr, ", TLS session cache: %lu servers", session_info(CI_FILES));
- fprintf(stderr, "\n");
-
- if (file) fatal_exit("ERROR: out of memory (%s(%lu) at %s:%d returned NULL)", msg, (unsigned long)size, file, line);
- else fatal_exit("ERROR: out of memory (%s(%lu) returned NULL)", msg, (unsigned long)size);
return 0;
}
-
-#ifdef DEBUG_TEST_FREE
-
-struct debug_test_free_slot {
- list_entry_1st
- unsigned char *file;
- int line;
- unsigned long count;
- list_entry_last
-};
-
-static struct list_head debug_test_free_slots = { &debug_test_free_slots, &debug_test_free_slots };
-
-#define DEBUG_TEST_FREE_DEFAULT_PROB 1024
-#define DEBUG_TEST_FREE_INIT_COUNT 16
-
-void debug_test_free(unsigned char *file, int line)
-{
- struct debug_test_free_slot *sl = NULL;
- struct list_head *lsl;
- unsigned long prob;
- if (!file) {
- prob = DEBUG_TEST_FREE_DEFAULT_PROB;
- goto fixed_prob;
- }
- foreach(struct debug_test_free_slot, sl, lsl, debug_test_free_slots) {
- if (sl->line == line && (sl->file == file || !strcmp(cast_const_char sl->file, cast_const_char file))) {
- del_from_list(sl);
- goto have_it;
- }
- }
- retry:
- sl = malloc(sizeof(struct debug_test_free_slot));
- if (!sl) {
- if (out_of_memory(0, NULL, 0))
- goto retry;
- return;
- }
- sl->file = file;
- sl->line = line;
- sl->count = DEBUG_TEST_FREE_INIT_COUNT;
- have_it:
- add_to_list(debug_test_free_slots, sl);
- prob = sl->count;
- sl->count++;
-
- fixed_prob:
- if (!prob) prob = 1;
- if (!(random() % prob)) {
- if (shrink_memory(SH_FREE_SOMETHING, 0) & ST_SOMETHING_FREED) {
- /*if (sl) sl->count++;*/
- }
- }
-}
-
-#endif
diff --git a/menu.c b/menu.c
@@ -440,7 +440,7 @@ static void resource_info_menu(struct terminal *term, void *d, void *ses_)
static void flush_caches(struct terminal *term, void *d, void *e)
{
abort_background_connections();
- shrink_memory(SH_FREE_ALL, 0);
+ shrink_memory(SH_FREE_ALL);
}
/* jde v historii na polozku id_ptr */
@@ -1292,7 +1292,7 @@ static void data_cleanup(void)
free_cookies();
free_auth();
abort_all_connections();
- shrink_memory(SH_FREE_ALL, 0);
+ shrink_memory(SH_FREE_ALL);
}
static unsigned char http_proxy[MAX_STR_LEN];
@@ -2048,7 +2048,7 @@ static void cache_refresh(void *xxx)
}
#endif
max_format_cache_entries = atoi(cast_const_char doc_str);
- shrink_memory(SH_CHECK_QUOTA, 0);
+ shrink_memory(SH_CHECK_QUOTA);
}
static unsigned char * const cache_texts[] = { TEXT_(T_MEMORY_CACHE_SIZE__KB), TEXT_(T_NUMBER_OF_FORMATTED_DOCUMENTS), TEXT_(T_AGGRESSIVE_CACHE) };
diff --git a/png.c b/png.c
@@ -127,13 +127,15 @@ void png_start(struct cached_image *cimg)
NULL, img_my_png_error, img_my_png_warning);
#endif
if (!png_ptr) {
- if (out_of_memory(0, NULL, 0)) goto retry1;
+ if (out_of_memory())
+ goto retry1;
fatal_exit("png_create_read_struct failed");
}
retry2:
info_ptr=png_create_info_struct(png_ptr);
if (!info_ptr) {
- if (out_of_memory(0, NULL, 0)) goto retry2;
+ if (out_of_memory())
+ goto retry2;
fatal_exit("png_create_info_struct failed");
}
if (setjmp(png_jmpbuf(png_ptr))){
diff --git a/sched.c b/sched.c
@@ -973,7 +973,7 @@ void detach_connection(struct status *stat, off_t pos, int want_to_restart, int
for (i = 0; i < PRI_CANCEL; i++) n_users += c->pri[i];
if (!n_users) internal("detaching free connection");
if (n_users != 1) return;
- shrink_memory(SH_CHECK_QUOTA, 0);
+ shrink_memory(SH_CHECK_QUOTA);
detach_cache_entry(c->cache);
c->detached = 1;
detach_done:
diff --git a/session.c b/session.c
@@ -1527,7 +1527,7 @@ struct f_data *cached_format_html(struct f_data_c *fd, struct object_request *rq
detach_f_data(&fd->f_data);
f = format_html(fd, rq, url, opt, cch);
if (f) f->fd = fd;
- shrink_memory(SH_CHECK_QUOTA, 0);
+ shrink_memory(SH_CHECK_QUOTA);
ret_f:
calculate_scrollbars(fd, f);
return f;
diff --git a/terminal.c b/terminal.c
@@ -1381,7 +1381,6 @@ void exec_on_terminal(struct terminal *term, unsigned char *path, unsigned char
}
#endif
}
- heap_trim();
if ((blockh = start_thread(exec_thread, param, paraml + 1, *delet != 0)) == -1) {
if (fg == 1) {
if (!F) unblock_itrm(term->fdin);
diff --git a/x.c b/x.c
@@ -1593,7 +1593,7 @@ static void x_register_bitmap(struct bitmap *bmp)
retry:
image=XCreateImage(x_display,x_default_visual,x_depth,ZPixmap,0,0,bmp->x,bmp->y,x_bitmap_scanline_pad<<3,bmp->skip);
if (!image){
- if (out_of_memory(0, NULL, 0))
+ if (out_of_memory())
goto retry;
free(p);
goto cant_create;
@@ -2041,7 +2041,7 @@ static void *x_prepare_strip(struct bitmap *bmp, int top, int lines)
retry:
x_data = xmalloc(bmp->skip * lines);
if (!x_data) {
- if (out_of_memory(0, NULL, 0))
+ if (out_of_memory())
goto retry;
return NULL;
}
@@ -2049,7 +2049,7 @@ static void *x_prepare_strip(struct bitmap *bmp, int top, int lines)
retry2:
image=XCreateImage(x_display,x_default_visual,x_depth,ZPixmap,0,0,bmp->x,lines,x_bitmap_scanline_pad<<3,bmp->skip);
if (!image) {
- if (out_of_memory(0, NULL, 0))
+ if (out_of_memory())
goto retry2;
free(x_data);
return NULL;