// Exit because of an input read error. static void read_error_exit(void) { if (silent_mode) /* Normal way to exit for "ex -s" */ getout(0); STRCPY(IObuff, _("Vim: Error reading input, exiting...\n")); preserve_exit(); }
void *xmalloc(size_t size) { void *ret = try_malloc(size); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } return ret; }
// This function handles deadly signals. // It tries to preserve any swap files and exit properly. // (partly from Elvis). // NOTE: Avoid unsafe functions, such as allocating memory, they can result in // a deadlock. static void deadly_signal(int signum) { // Set the v:dying variable. set_vim_var_nr(VV_DYING, 1); snprintf((char *)IObuff, sizeof(IObuff), "Vim: Caught deadly signal '%s'\n", signal_name(signum)); // Preserve files and exit. preserve_exit(); }
// This function handles deadly signals. // It tries to preserve any swap files and exit properly. // (partly from Elvis). // NOTE: Avoid unsafe functions, such as allocating memory, they can result in // a deadlock. static void deadly_signal(int signum) { // Set the v:dying variable. set_vim_var_nr(VV_DYING, 1); sprintf((char *)IObuff, "Vim: Caught deadly signal '%s'\n", signal_name(signum)); // Preserve files and exit. This sets the really_exiting flag to prevent // calling free(). preserve_exit(); }
void *xmallocz(size_t size) { size_t total_size = size + 1; void *ret; if (total_size < size) { OUT_STR("Vim: Data too large to fit into virtual memory space\n"); preserve_exit(); } ret = xmalloc(total_size); ((char*)ret)[size] = 0; return ret; }
char * xstrdup(const char *str) { char *ret = strdup(str); if (!ret) { try_to_free_memory(); ret = strdup(str); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } } return ret; }
void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); if (!ret && !size) ret = realloc(ptr, 1); if (!ret) { try_to_free_memory(); ret = realloc(ptr, size); if (!ret && !size) ret = realloc(ptr, 1); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } } return ret; }
void *xcalloc(size_t count, size_t size) { void *ret = calloc(count, size); if (!ret && (!count || !size)) ret = calloc(1, 1); if (!ret) { try_to_free_memory(); ret = calloc(count, size); if (!ret && (!count || !size)) ret = calloc(1, 1); if (!ret) { OUT_STR("Vim: Error: Out of memory.\n"); preserve_exit(); } } return ret; }
// Called by the by the 'idle' handle to emulate a reading event static void fread_idle_cb(uv_idle_t *handle) { uv_fs_t req; RStream *rstream = handle_get_rstream((uv_handle_t *)handle); rstream->uvbuf.len = rbuffer_available(rstream->buffer); rstream->uvbuf.base = rbuffer_write_ptr(rstream->buffer); // the offset argument to uv_fs_read is int64_t, could someone really try // to read more than 9 quintillion (9e18) bytes? // upcast is meant to avoid tautological condition warning on 32 bits uintmax_t fpos_intmax = rstream->fpos; if (fpos_intmax > INT64_MAX) { ELOG("stream offset overflow"); preserve_exit(); } // Synchronous read uv_fs_read( uv_default_loop(), &req, rstream->fd, &rstream->uvbuf, 1, (int64_t) rstream->fpos, NULL); uv_fs_req_cleanup(&req); if (req.result <= 0) { uv_idle_stop(rstream->fread_idle); return; } // no errors (req.result (ssize_t) is positive), it's safe to cast. size_t nread = (size_t) req.result; rbuffer_produced(rstream->buffer, nread); rstream->fpos += nread; }