aboutsummaryrefslogtreecommitdiffstats
path: root/indep.c
diff options
context:
space:
mode:
authorAIDA Shinra <shinra@j10n.org>2013-10-14 13:31:01 +0000
committerTatsuya Kinoshita <tats@debian.org>2013-10-14 13:31:01 +0000
commitec81194f386f35b0d914e0fc5c727cd217c62c91 (patch)
tree1a64910741c607abe7d19b92bdfa34f3506507ae /indep.c
parentMerge from upstream on 2012-05-22 (diff)
downloadw3m-ec81194f386f35b0d914e0fc5c727cd217c62c91.tar.gz
w3m-ec81194f386f35b0d914e0fc5c727cd217c62c91.zip
Workaround of GC crash on Cygwin64
Patch from <http://www.j10n.org/files/w3m-cvs-1.1055-win64gc.patch>, [w3m-dev:04469] on 2013-10-14.
Diffstat (limited to '')
-rw-r--r--indep.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/indep.c b/indep.c
index 65b04aa..cb14be2 100644
--- a/indep.c
+++ b/indep.c
@@ -707,6 +707,111 @@ shell_quote(char *str)
return str;
}
+void *
+xrealloc(void *ptr, size_t size)
+{
+ void *newptr = realloc(ptr, size);
+ if (newptr == NULL) {
+ fprintf(stderr, "Out of memory\n");
+ exit(-1);
+ }
+ return newptr;
+}
+
+/* Define this as a separate function in case the free() has
+ * an incompatible prototype. */
+void
+xfree(void *ptr)
+{
+ free(ptr);
+}
+
+void *
+w3m_GC_realloc_atomic(void *ptr, size_t size)
+{
+ return ptr ? GC_REALLOC(ptr, size) : GC_MALLOC_ATOMIC(size);
+}
+
+void
+w3m_GC_free(void *ptr)
+{
+ GC_FREE(ptr);
+}
+
+void
+growbuf_init(struct growbuf *gb)
+{
+ gb->ptr = NULL;
+ gb->length = 0;
+ gb->area_size = 0;
+ gb->realloc_proc = &w3m_GC_realloc_atomic;
+ gb->free_proc = &w3m_GC_free;
+}
+
+void
+growbuf_init_without_GC(struct growbuf *gb)
+{
+ gb->ptr = NULL;
+ gb->length = 0;
+ gb->area_size = 0;
+ gb->realloc_proc = &xrealloc;
+ gb->free_proc = &xfree;
+}
+
+void
+growbuf_clear(struct growbuf *gb)
+{
+ (*gb->free_proc) (gb->ptr);
+ gb->ptr = NULL;
+ gb->length = 0;
+ gb->area_size = 0;
+}
+
+Str
+growbuf_to_Str(struct growbuf *gb)
+{
+ Str s;
+
+ if (gb->free_proc == &w3m_GC_free) {
+ growbuf_reserve(gb, gb->length + 1);
+ gb->ptr[gb->length] = '\0';
+ s = New(struct _Str);
+ s->ptr = gb->ptr;
+ s->length = gb->length;
+ s->area_size = gb->area_size;
+ } else {
+ s = Strnew_charp_n(gb->ptr, gb->length);
+ (*gb->free_proc) (gb->ptr);
+ }
+ gb->ptr = NULL;
+ gb->length = 0;
+ gb->area_size = 0;
+ return s;
+}
+
+void
+growbuf_reserve(struct growbuf *gb, int leastarea)
+{
+ int newarea;
+
+ if (gb->area_size < leastarea) {
+ newarea = gb->area_size * 3 / 2;
+ if (newarea < leastarea)
+ newarea = leastarea;
+ newarea += 16;
+ gb->ptr = (*gb->realloc_proc) (gb->ptr, newarea);
+ gb->area_size = newarea;
+ }
+}
+
+void
+growbuf_append(struct growbuf *gb, const char *src, int len)
+{
+ growbuf_reserve(gb, gb->length + len);
+ memcpy(&gb->ptr[gb->length], src, len);
+ gb->length += len;
+}
+
static char *
w3m_dir(const char *name, char *dft)
{