robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0abc9ccc19
master
1#include <assert.h> 2#include <stdbool.h> 3#include <stddef.h> 4#include <uv.h> 5 6#include "nvim/event/defs.h" 7#include "nvim/event/stream.h" 8#include "nvim/event/wstream.h" 9#include "nvim/macros_defs.h" 10#include "nvim/memory.h" 11#include "nvim/types_defs.h" 12 13#define DEFAULT_MAXMEM 1024 * 1024 * 2000 14 15typedef struct { 16Stream * stream ; 17WBuffer * buffer ; 18uv_write_t uv_req ; 19}WRequest ; 20 21#include "event/wstream.c.generated.h" 22 23void wstream_init_fd (Loop * loop ,Stream * stream ,int fd ,size_t maxmem ) 24FUNC_ATTR_NONNULL_ARG (1 )FUNC_ATTR_NONNULL_ARG (2 ) 25{ 26stream_init (loop ,stream ,fd ,NULL ); 27wstream_init (stream ,maxmem ); 28} 29 30void wstream_init_stream (Stream * stream ,uv_stream_t * uvstream ,size_t maxmem ) 31FUNC_ATTR_NONNULL_ARG (1 )FUNC_ATTR_NONNULL_ARG (2 ) 32{ 33stream_init (NULL ,stream ,-1 ,uvstream ); 34wstream_init (stream ,maxmem ); 35} 36 37void wstream_init (Stream * stream ,size_t maxmem ) 38{ 39stream -> maxmem = maxmem ?maxmem :DEFAULT_MAXMEM ; 40} 41 42/// Sets a callback that will be called on completion of a write request, 43/// indicating failure/success. 44/// 45/// This affects all requests currently in-flight as well. Overwrites any 46/// possible earlier callback. 47/// 48/// @note This callback will not fire if the write request couldn't even be 49/// queued properly (i.e.: when `wstream_write() returns an error`). 50/// 51/// @param stream The `Stream` instance 52/// @param cb The callback 53void wstream_set_write_cb (Stream * stream ,stream_write_cb cb ,void * data ) 54FUNC_ATTR_NONNULL_ARG (1 ,2 ) 55{ 56stream -> write_cb = cb ; 57stream -> cb_data = data ; 58} 59 60/// Queues data for writing to the backing file descriptor of a `Stream` 61/// instance. This will fail if the write would cause the Stream use more 62/// memory than specified by `maxmem`. 63/// 64/// @param stream The `Stream` instance 65/// @param buffer The buffer which contains data to be written 66/// @return 0 on success, or libuv error code on failure 67int wstream_write (Stream * stream ,WBuffer * buffer ) 68FUNC_ATTR_NONNULL_ALL 69{ 70assert (stream -> maxmem ); 71// This should not be called after a stream was freed 72assert (!stream -> closed ); 73 74int err = 0 ; 75uv_buf_t uvbuf ; 76uvbuf .base = buffer -> data ; 77uvbuf .len = UV_BUF_LEN (buffer -> size ); 78 79if (!stream -> uvstream ) { 80uv_fs_t req ; 81 82// Synchronous write 83 84#ifdef __EMSCRIPTEN__ 85// Pass -1 so the write uses the current file position. 86err = uv_fs_write (stream -> uv .idle .loop ,& req ,stream -> fd ,& uvbuf ,1 ,-1 ,NULL ); 87#else 88err = uv_fs_write (stream -> uv .idle .loop ,& req ,stream -> fd ,& uvbuf ,1 ,stream -> fpos ,NULL ); 89#endif 90uv_fs_req_cleanup (& req ); 91 92wstream_release_wbuffer (buffer ); 93 94assert (stream -> write_cb == NULL ); 95 96stream -> fpos += MAX (req .result ,0 ); 97return req .result > 0 ?0 :err != 0 ?err :UV_UNKNOWN ; 98 } 99 100if (stream -> curmem > stream -> maxmem ) { 101err = UV_ENOMEM ; 102 gotofail ; 103 } 104 105stream -> curmem += buffer -> size ; 106 107WRequest * data = xmalloc (sizeof (WRequest )); 108data -> stream = stream ; 109data -> buffer = buffer ; 110data -> uv_req .data = data ; 111 112if ((err = uv_write (& data -> uv_req ,stream -> uvstream ,& uvbuf ,1 ,write_cb ))!= 0 ) { 113xfree (data ); 114 gotofail ; 115 } 116 117stream -> pending_reqs ++ ; 118assert (err == 0 ); 119return 0 ; 120 121fail : 122wstream_release_wbuffer (buffer ); 123assert (err != 0 ); 124return err ; 125} 126 127/// Creates a WBuffer object for holding output data. Instances of this 128/// object can be reused across Stream instances, and the memory is freed 129/// automatically when no longer needed (it tracks the number of references 130/// internally) 131/// 132/// @param data Data stored by the WBuffer 133/// @param size The size of the data array 134/// @param refcount The number of references for the WBuffer. This will be used 135/// by Stream instances to decide when a WBuffer should be freed. 136/// @param cb Pointer to function that will be responsible for freeing 137/// the buffer data (passing `xfree` will work as expected). 138/// @return The allocated WBuffer instance 139WBuffer * wstream_new_buffer (char * data ,size_t size ,size_t refcount ,wbuffer_data_finalizer cb ) 140FUNC_ATTR_NONNULL_ARG (1 ) 141{ 142WBuffer * rv = xmalloc (sizeof (WBuffer )); 143rv -> size = size ; 144rv -> refcount = refcount ; 145rv -> cb = cb ; 146rv -> data = data ; 147 148return rv ; 149} 150 151static void write_cb (uv_write_t * req ,int status ) 152{ 153WRequest * data = req -> data ; 154 155data -> stream -> curmem -= data -> buffer -> size ; 156 157wstream_release_wbuffer (data -> buffer ); 158 159if (data -> stream -> write_cb ) { 160data -> stream -> write_cb (data -> stream ,data -> stream -> cb_data ,status ); 161 } 162 163data -> stream -> pending_reqs -- ; 164 165if (data -> stream -> closed && data -> stream -> pending_reqs == 0 ) { 166// Last pending write; free the stream. 167stream_close_handle (data -> stream ); 168 } 169 170xfree (data ); 171} 172 173void wstream_release_wbuffer (WBuffer * buffer ) 174FUNC_ATTR_NONNULL_ALL 175{ 176if (!-- buffer -> refcount ) { 177if (buffer -> cb ) { 178buffer -> cb (buffer -> data ); 179 } 180 181xfree (buffer ); 182 } 183}