aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/os')
-rw-r--r--src/nvim/os/env.c72
-rw-r--r--src/nvim/os/event.c41
-rw-r--r--src/nvim/os/fs.c4
-rw-r--r--src/nvim/os/input.c4
-rw-r--r--src/nvim/os/job_private.h9
-rw-r--r--src/nvim/os/pipe_process.c4
-rw-r--r--src/nvim/os/pty_process.c4
-rw-r--r--src/nvim/os/rstream.c10
-rw-r--r--src/nvim/os/shell.c10
-rw-r--r--src/nvim/os/signal.c16
-rw-r--r--src/nvim/os/wstream.c36
11 files changed, 91 insertions, 119 deletions
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c
index 30e44341a9..be4b22de3a 100644
--- a/src/nvim/os/env.c
+++ b/src/nvim/os/env.c
@@ -37,6 +37,19 @@ int os_setenv(const char *name, const char *value, int overwrite)
return setenv(name, value, overwrite);
}
+/// Unset environment variable
+///
+/// For systems where unsetenv() is not available the value will be set as an
+/// empty string
+int os_unsetenv(const char *name)
+{
+#ifdef HAVE_UNSETENV
+ return unsetenv(name);
+#else
+ return os_setenv(name, "", 1);
+#endif
+}
+
char *os_getenvname_at_index(size_t index)
{
# if defined(HAVE__NSGETENVIRON)
@@ -113,7 +126,7 @@ void init_homedir(void)
char_u *var;
/* In case we are called a second time (when 'encoding' changes). */
- free(homedir);
+ xfree(homedir);
homedir = NULL;
var = (char_u *)os_getenv("HOME");
@@ -144,7 +157,7 @@ void init_homedir(void)
void free_homedir(void)
{
- free(homedir);
+ xfree(homedir);
}
#endif
@@ -304,7 +317,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one,
char_u *p = vim_strsave(var);
if (mustfree) {
- free(var);
+ xfree(var);
}
var = p;
mustfree = true;
@@ -318,7 +331,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one,
char_u *p = vim_strsave_escaped(var, (char_u *)" \t");
if (mustfree)
- free(var);
+ xfree(var);
var = p;
mustfree = true;
}
@@ -341,7 +354,7 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one,
copy_char = false;
}
if (mustfree)
- free(var);
+ xfree(var);
}
if (copy_char) { /* copy at least one char */
@@ -371,33 +384,33 @@ void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, bool esc, bool one,
/// Check if the directory "vimdir/<version>" or "vimdir/runtime" exists.
/// Return NULL if not, return its name in allocated memory otherwise.
/// @param vimdir directory to test
-static char_u *vim_version_dir(char_u *vimdir)
+static char *vim_version_dir(char *vimdir)
{
char_u *p;
if (vimdir == NULL || *vimdir == NUL)
return NULL;
- p = concat_fnames(vimdir, (char_u *)VIM_VERSION_NODOT, true);
+ p = concat_fnames((char_u *)vimdir, (char_u *)VIM_VERSION_NODOT, true);
if (os_isdir(p))
- return p;
- free(p);
- p = concat_fnames(vimdir, (char_u *)RUNTIME_DIRNAME, true);
+ return (char *)p;
+ xfree(p);
+ p = concat_fnames((char_u *)vimdir, (char_u *)RUNTIME_DIRNAME, true);
if (os_isdir(p))
- return p;
- free(p);
+ return (char *)p;
+ xfree(p);
return NULL;
}
/// If the string between "p" and "pend" ends in "name/", return "pend" minus
/// the length of "name/". Otherwise return "pend".
-static char_u *remove_tail(char_u *p, char_u *pend, char_u *name)
+static char *remove_tail(char *p, char *pend, char *name)
{
- int len = (int)STRLEN(name) + 1;
- char_u *newend = pend - len;
+ size_t len = STRLEN(name) + 1;
+ char *newend = pend - len;
if (newend >= p
- && fnamencmp(newend, name, len - 1) == 0
- && (newend == p || after_pathsep(p, newend)))
+ && fnamencmp((char_u *)newend, (char_u *)name, len - 1) == 0
+ && (newend == p || after_pathsep((char_u *)p, (char_u *)newend)))
return newend;
return pend;
}
@@ -442,7 +455,7 @@ char_u *vim_getenv(char_u *name, bool *mustfree)
if (p != NULL && *p == NUL) /* empty is the same as not set */
p = NULL;
if (p != NULL) {
- p = vim_version_dir(p);
+ p = (char_u *)vim_version_dir((char *)p);
if (p != NULL)
*mustfree = true;
else
@@ -464,12 +477,14 @@ char_u *vim_getenv(char_u *name, bool *mustfree)
/* remove "doc/" from 'helpfile', if present */
if (p == p_hf)
- pend = remove_tail(p, pend, (char_u *)"doc");
+ pend = (char_u *)remove_tail((char *)p, (char *)pend, "doc");
/* for $VIM, remove "runtime/" or "vim54/", if present */
if (!vimruntime) {
- pend = remove_tail(p, pend, (char_u *)RUNTIME_DIRNAME);
- pend = remove_tail(p, pend, (char_u *)VIM_VERSION_NODOT);
+ pend = (char_u *)remove_tail((char *)p, (char *)pend,
+ RUNTIME_DIRNAME);
+ pend = (char_u *)remove_tail((char *)p, (char *)pend,
+ VIM_VERSION_NODOT);
}
/* remove trailing path separator */
@@ -481,7 +496,7 @@ char_u *vim_getenv(char_u *name, bool *mustfree)
p = vim_strnsave(p, (size_t)(pend - p));
if (!os_isdir(p)) {
- free(p);
+ xfree(p);
p = NULL;
} else {
*mustfree = true;
@@ -495,13 +510,14 @@ char_u *vim_getenv(char_u *name, bool *mustfree)
if (p == NULL) {
/* Only use default_vimruntime_dir when it is not empty */
if (vimruntime && *default_vimruntime_dir != NUL) {
- p = default_vimruntime_dir;
+ p = (char_u *)default_vimruntime_dir;
*mustfree = false;
} else if (*default_vim_dir != NUL) {
- if (vimruntime && (p = vim_version_dir(default_vim_dir)) != NULL) {
+ if (vimruntime
+ && (p = (char_u *)vim_version_dir(default_vim_dir)) != NULL) {
*mustfree = true;
} else {
- p = default_vim_dir;
+ p = (char_u *)default_vim_dir;
*mustfree = false;
}
}
@@ -631,7 +647,7 @@ void home_replace(buf_T *buf, char_u *src, char_u *dst, int dstlen, bool one)
*dst = NUL;
if (homedir_env != homedir_env_orig)
- free(homedir_env);
+ xfree(homedir_env);
}
/// Like home_replace, store the replaced string in allocated memory.
@@ -660,7 +676,7 @@ void vim_setenv(char_u *name, char_u *val)
if (*val != NUL && STRICMP(name, "VIMRUNTIME") == 0) {
char_u *buf = concat_str(val, (char_u *)"/lang");
bindtextdomain(VIMPACKAGE, (char *)buf);
- free(buf);
+ xfree(buf);
}
}
@@ -675,7 +691,7 @@ char_u *get_env_name(expand_T *xp, int idx)
char *envname = os_getenvname_at_index((size_t)idx);
if (envname) {
STRLCPY(name, envname, ENVNAMELEN);
- free(envname);
+ xfree(envname);
return name;
} else {
return NULL;
diff --git a/src/nvim/os/event.c b/src/nvim/os/event.c
index fd9ff5b230..0560da1e2e 100644
--- a/src/nvim/os/event.c
+++ b/src/nvim/os/event.c
@@ -28,12 +28,6 @@
#define _destroy_event(x) // do nothing
KLIST_INIT(Event, Event, _destroy_event)
-typedef struct {
- bool timed_out;
- int ms;
- uv_timer_t *timer;
-} TimerData;
-
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/event.c.generated.h"
#endif
@@ -52,7 +46,6 @@ void event_init(void)
// early msgpack-rpc initialization
msgpack_rpc_init_method_table();
msgpack_rpc_helpers_init();
- wstream_init();
// Initialize input events
input_init();
// Timer to wake the event loop if a timeout argument is passed to
@@ -107,19 +100,12 @@ void event_poll(int ms)
uv_run_mode run_mode = UV_RUN_ONCE;
uv_timer_t timer;
- uv_prepare_t timer_prepare;
- TimerData timer_data = {.ms = ms, .timed_out = false, .timer = &timer};
if (ms > 0) {
uv_timer_init(uv_default_loop(), &timer);
- // This prepare handle that actually starts the timer
- uv_prepare_init(uv_default_loop(), &timer_prepare);
- // Timeout passed as argument to the timer
- timer.data = &timer_data;
- // We only start the timer after the loop is running, for that we
- // use a prepare handle(pass the interval as data to it)
- timer_prepare.data = &timer_data;
- uv_prepare_start(&timer_prepare, timer_prepare_cb);
+ // Use a repeating timeout of ms milliseconds to make sure
+ // we do not block indefinitely for I/O.
+ uv_timer_start(&timer, timer_cb, (uint64_t)ms, (uint64_t)ms);
} else if (ms == 0) {
// For ms == 0, we need to do a non-blocking event poll by
// setting the run mode to UV_RUN_NOWAIT.
@@ -129,17 +115,19 @@ void event_poll(int ms)
loop(run_mode);
if (ms > 0) {
- // Ensure the timer-related handles are closed and run the event loop
+ // Ensure the timer handle is closed and run the event loop
// once more to let libuv perform it's cleanup
uv_timer_stop(&timer);
- uv_prepare_stop(&timer_prepare);
uv_close((uv_handle_t *)&timer, NULL);
- uv_close((uv_handle_t *)&timer_prepare, NULL);
loop(UV_RUN_NOWAIT);
}
recursive--; // Can re-enter uv_run now
- process_events_from(immediate_events);
+
+ // In case this is run before event_init, don't process any events.
+ if (immediate_events) {
+ process_events_from(immediate_events);
+ }
}
bool event_has_deferred(void)
@@ -183,19 +171,8 @@ static void process_events_from(klist_t(Event) *queue)
}
}
-// Set a flag in the `event_poll` loop for signaling of a timeout
static void timer_cb(uv_timer_t *handle)
{
- TimerData *data = handle->data;
- data->timed_out = true;
-}
-
-static void timer_prepare_cb(uv_prepare_t *handle)
-{
- TimerData *data = handle->data;
- assert(data->ms > 0);
- uv_timer_start(data->timer, timer_cb, (uint32_t)data->ms, 0);
- uv_prepare_stop(handle);
}
static void loop(uv_run_mode run_mode)
diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c
index d583323b1f..2a41001cde 100644
--- a/src/nvim/os/fs.c
+++ b/src/nvim/os/fs.c
@@ -152,14 +152,14 @@ static bool is_executable_in_path(const char_u *name, char_u **abspath)
*abspath = save_absolute_path(buf);
}
- free(buf);
+ xfree(buf);
return true;
}
if (*e != ':') {
// End of $PATH without finding any executable called name.
- free(buf);
+ xfree(buf);
return false;
}
diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c
index a409a9ed13..8002d528ed 100644
--- a/src/nvim/os/input.c
+++ b/src/nvim/os/input.c
@@ -128,7 +128,9 @@ bool os_char_avail(void)
// Check for CTRL-C typed by reading all available characters.
void os_breakcheck(void)
{
- event_poll(0);
+ if (!disable_breakcheck && !got_int) {
+ event_poll(0);
+ }
}
/// Test whether a file descriptor refers to a terminal.
diff --git a/src/nvim/os/job_private.h b/src/nvim/os/job_private.h
index af13d2e636..983106d918 100644
--- a/src/nvim/os/job_private.h
+++ b/src/nvim/os/job_private.h
@@ -11,6 +11,7 @@
#include "nvim/os/pty_process.h"
#include "nvim/os/shell.h"
#include "nvim/log.h"
+#include "nvim/memory.h"
struct job {
// Job id the index in the job table plus one.
@@ -104,12 +105,12 @@ static inline void job_decref(Job *job)
// Invoke the exit_cb
job_exit_callback(job);
// Free all memory allocated for the job
- free(job->proc_stdin->data);
- free(job->proc_stdout->data);
- free(job->proc_stderr->data);
+ xfree(job->proc_stdin->data);
+ xfree(job->proc_stdout->data);
+ xfree(job->proc_stderr->data);
shell_free_argv(job->opts.argv);
process_destroy(job);
- free(job);
+ xfree(job);
}
}
diff --git a/src/nvim/os/pipe_process.c b/src/nvim/os/pipe_process.c
index 5535c3fe93..2ac305e967 100644
--- a/src/nvim/os/pipe_process.c
+++ b/src/nvim/os/pipe_process.c
@@ -72,8 +72,8 @@ void pipe_process_init(Job *job)
void pipe_process_destroy(Job *job)
{
UvProcess *pipeproc = job->process;
- free(pipeproc->proc.data);
- free(pipeproc);
+ xfree(pipeproc->proc.data);
+ xfree(pipeproc);
job->process = NULL;
}
diff --git a/src/nvim/os/pty_process.c b/src/nvim/os/pty_process.c
index 9a2721f769..c64f3f9932 100644
--- a/src/nvim/os/pty_process.c
+++ b/src/nvim/os/pty_process.c
@@ -65,8 +65,8 @@ void pty_process_init(Job *job) FUNC_ATTR_NONNULL_ALL
void pty_process_destroy(Job *job) FUNC_ATTR_NONNULL_ALL
{
- free(job->opts.term_name);
- free(job->process);
+ xfree(job->opts.term_name);
+ xfree(job->process);
job->process = NULL;
}
diff --git a/src/nvim/os/rstream.c b/src/nvim/os/rstream.c
index 29b8a5a9e1..702f282d53 100644
--- a/src/nvim/os/rstream.c
+++ b/src/nvim/os/rstream.c
@@ -162,8 +162,8 @@ size_t rbuffer_available(RBuffer *rbuffer)
void rbuffer_free(RBuffer *rbuffer)
{
- free(rbuffer->data);
- free(rbuffer);
+ xfree(rbuffer->data);
+ xfree(rbuffer);
}
/// Creates a new RStream instance. A RStream encapsulates all the boilerplate
@@ -216,7 +216,7 @@ void rstream_free(RStream *rstream)
}
rbuffer_free(rstream->buffer);
- free(rstream);
+ xfree(rstream);
}
/// Sets the underlying `uv_stream_t` instance
@@ -401,8 +401,8 @@ static void fread_idle_cb(uv_idle_t *handle)
static void close_cb(uv_handle_t *handle)
{
- free(handle->data);
- free(handle);
+ xfree(handle->data);
+ xfree(handle);
}
static void rbuffer_relocate(RBuffer *rbuffer)
diff --git a/src/nvim/os/shell.c b/src/nvim/os/shell.c
index 6fcb62a5f3..4f5928ba8a 100644
--- a/src/nvim/os/shell.c
+++ b/src/nvim/os/shell.c
@@ -80,11 +80,11 @@ void shell_free_argv(char **argv)
while (*p != NULL) {
// Free each argument
- free(*p);
+ xfree(*p);
p++;
}
- free(argv);
+ xfree(argv);
}
/// Calls the user-configured 'shell' (p_sh) for running a command or wildcard
@@ -128,11 +128,11 @@ int os_call_shell(char_u *cmd, ShellOpts opts, char_u *extra_args)
emsg_silent,
forward_output);
- free(input.data);
+ xfree(input.data);
if (output) {
(void)write_output(output, nread, true, true);
- free(output);
+ xfree(output);
}
if (!emsg_silent && status != 0 && !(opts & kShellOptSilent)) {
@@ -250,7 +250,7 @@ static int shell(const char *cmd,
if (buf.len == 0) {
// no data received from the process, return NULL
*output = NULL;
- free(buf.data);
+ xfree(buf.data);
} else {
// NUL-terminate to make the output directly usable as a C string
buf.data[buf.len] = NUL;
diff --git a/src/nvim/os/signal.c b/src/nvim/os/signal.c
index a332ad2314..f824543003 100644
--- a/src/nvim/os/signal.c
+++ b/src/nvim/os/signal.c
@@ -1,9 +1,8 @@
+#include <assert.h>
#include <stdbool.h>
#include <uv.h>
-#include "nvim/lib/klist.h"
-
#include "nvim/ascii.h"
#include "nvim/vim.h"
#include "nvim/globals.h"
@@ -15,10 +14,6 @@
#include "nvim/os/signal.h"
#include "nvim/os/event.h"
-#define SignalEventFreer(x)
-KMEMPOOL_INIT(SignalEventPool, int, SignalEventFreer)
-kmempool_t(SignalEventPool) *signal_event_pool = NULL;
-
static uv_signal_t spipe, shup, squit, sterm;
#ifdef SIGPWR
static uv_signal_t spwr;
@@ -32,7 +27,6 @@ static bool rejecting_deadly;
void signal_init(void)
{
- signal_event_pool = kmp_init(SignalEventPool);
uv_signal_init(uv_default_loop(), &spipe);
uv_signal_init(uv_default_loop(), &shup);
uv_signal_init(uv_default_loop(), &squit);
@@ -119,18 +113,16 @@ static void deadly_signal(int signum)
static void signal_cb(uv_signal_t *handle, int signum)
{
- int *n = kmp_alloc(SignalEventPool, signal_event_pool);
- *n = signum;
+ assert(signum >= 0);
event_push((Event) {
.handler = on_signal_event,
- .data = n
+ .data = (void *)(uintptr_t)signum
}, false);
}
static void on_signal_event(Event event)
{
- int signum = *((int *)event.data);
- kmp_free(SignalEventPool, signal_event_pool, event.data);
+ int signum = (int)(uintptr_t)event.data;
switch (signum) {
#ifdef SIGPWR
diff --git a/src/nvim/os/wstream.c b/src/nvim/os/wstream.c
index 13c6c0429f..73896c381d 100644
--- a/src/nvim/os/wstream.c
+++ b/src/nvim/os/wstream.c
@@ -5,8 +5,6 @@
#include <uv.h>
-#include "nvim/lib/klist.h"
-
#include "nvim/os/uv_helpers.h"
#include "nvim/os/wstream.h"
#include "nvim/os/wstream_defs.h"
@@ -41,24 +39,10 @@ typedef struct {
uv_write_t uv_req;
} WRequest;
-#define WRequestFreer(x)
-KMEMPOOL_INIT(WRequestPool, WRequest, WRequestFreer)
-kmempool_t(WRequestPool) *wrequest_pool = NULL;
-#define WBufferFreer(x)
-KMEMPOOL_INIT(WBufferPool, WBuffer, WBufferFreer)
-kmempool_t(WBufferPool) *wbuffer_pool = NULL;
-
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/wstream.c.generated.h"
#endif
-/// Initialize pools for reusing commonly created objects
-void wstream_init(void)
-{
- wrequest_pool = kmp_init(WRequestPool);
- wbuffer_pool = kmp_init(WBufferPool);
-}
-
/// Creates a new WStream instance. A WStream encapsulates all the boilerplate
/// necessary for writing to a libuv stream.
///
@@ -92,7 +76,7 @@ void wstream_free(WStream *wstream) {
uv_close((uv_handle_t *)wstream->stream, close_cb);
} else {
handle_set_wstream((uv_handle_t *)wstream->stream, NULL);
- free(wstream);
+ xfree(wstream);
}
} else {
wstream->freed = true;
@@ -163,7 +147,7 @@ bool wstream_write(WStream *wstream, WBuffer *buffer)
wstream->curmem += buffer->size;
- WRequest *data = kmp_alloc(WRequestPool, wrequest_pool);
+ WRequest *data = xmalloc(sizeof(WRequest));
data->wstream = wstream;
data->buffer = buffer;
data->uv_req.data = data;
@@ -173,7 +157,7 @@ bool wstream_write(WStream *wstream, WBuffer *buffer)
uvbuf.len = buffer->size;
if (uv_write(&data->uv_req, wstream->stream, &uvbuf, 1, write_cb)) {
- kmp_free(WRequestPool, wrequest_pool, data);
+ xfree(data);
goto err;
}
@@ -202,7 +186,7 @@ WBuffer *wstream_new_buffer(char *data,
size_t refcount,
wbuffer_data_finalizer cb)
{
- WBuffer *rv = kmp_alloc(WBufferPool, wbuffer_pool);
+ WBuffer *rv = xmalloc(sizeof(WBuffer));
rv->size = size;
rv->refcount = refcount;
rv->cb = cb;
@@ -232,11 +216,11 @@ static void write_cb(uv_write_t *req, int status)
if (data->wstream->free_handle) {
uv_close((uv_handle_t *)data->wstream->stream, close_cb);
} else {
- free(data->wstream);
+ xfree(data->wstream);
}
}
- kmp_free(WRequestPool, wrequest_pool, data);
+ xfree(data);
}
void wstream_release_wbuffer(WBuffer *buffer)
@@ -246,14 +230,14 @@ void wstream_release_wbuffer(WBuffer *buffer)
buffer->cb(buffer->data);
}
- kmp_free(WBufferPool, wbuffer_pool, buffer);
+ xfree(buffer);
}
}
static void close_cb(uv_handle_t *handle)
{
- free(handle_get_wstream(handle));
- free(handle->data);
- free(handle);
+ xfree(handle_get_wstream(handle));
+ xfree(handle->data);
+ xfree(handle);
}