diff options
| author | Randy Palamar <randy@rnpnr.xyz> | 2025-12-21 15:54:51 -0700 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2025-12-22 09:17:08 -0700 |
| commit | 88c25def890ba911196a91a7fa0936c15f069a92 (patch) | |
| tree | a0f910634081848a06396b55c9d9d509a4d31c2a | |
| parent | fbc898a9cd132078b0128b70091f4aadbec519e3 (diff) | |
| download | vis-88c25def890ba911196a91a7fa0936c15f069a92.tar.gz vis-88c25def890ba911196a91a7fa0936c15f069a92.tar.xz | |
text: remove text-internal.h
this is not really useful anymore
| -rw-r--r-- | text-internal.h | 33 | ||||
| -rw-r--r-- | text-io.c | 72 | ||||
| -rw-r--r-- | text.c | 63 |
3 files changed, 79 insertions, 89 deletions
diff --git a/text-internal.h b/text-internal.h deleted file mode 100644 index 13647d6..0000000 --- a/text-internal.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef TEXT_INTERNAL -#define TEXT_INTERNAL - -#include "text.h" - -/* Block holding the file content, either readonly mmap(2)-ed from the original - * file or heap allocated to store the modifications. - */ -typedef struct { - size_t size; /* maximal capacity */ - size_t len; /* current used length / insertion position */ - char *data; /* actual data */ - enum { /* type of allocation */ - BLOCK_TYPE_MMAP_ORIG, /* mmap(2)-ed from an external file */ - BLOCK_TYPE_MMAP, /* mmap(2)-ed from a temporary file only known to this process */ - BLOCK_TYPE_MALLOC, /* heap allocated block using malloc(3) */ - } type; -} Block; - -VIS_INTERNAL Block *block_alloc(size_t size); -VIS_INTERNAL Block *block_read(size_t size, int fd); -VIS_INTERNAL Block *block_mmap(size_t size, int fd, off_t offset); -VIS_INTERNAL Block *block_load(int dirfd, const char *filename, enum TextLoadMethod method, struct stat *info); -VIS_INTERNAL void block_free(Block*); -VIS_INTERNAL bool block_capacity(Block*, size_t len); -VIS_INTERNAL const char *block_append(Block*, const char *data, size_t len); -VIS_INTERNAL bool block_insert(Block*, size_t pos, const char *data, size_t len); -VIS_INTERNAL bool block_delete(Block*, size_t pos, size_t len); - -VIS_INTERNAL Block *text_block_mmaped(Text*); -VIS_INTERNAL void text_saved(Text*, struct stat *meta); - -#endif @@ -1,5 +1,4 @@ #include "text.h" -#include "text-internal.h" #include "text-util.h" #include "util.h" @@ -14,7 +13,8 @@ #define BLOCK_MMAP_SIZE (1 << 26) /* allocate a new block of MAX(size, BLOCK_SIZE) bytes */ -Block *block_alloc(size_t size) { +static Block *block_alloc(size_t size) +{ Block *blk = calloc(1, sizeof *blk); if (!blk) return NULL; @@ -29,7 +29,19 @@ Block *block_alloc(size_t size) { return blk; } -Block *block_read(size_t size, int fd) { +static void block_free(Block *blk) +{ + if (!blk) + return; + if (blk->type == BLOCK_TYPE_MALLOC) + free(blk->data); + else if ((blk->type == BLOCK_TYPE_MMAP_ORIG || blk->type == BLOCK_TYPE_MMAP) && blk->data) + munmap(blk->data, blk->size); + free(blk); +} + +static Block *block_read(size_t size, int fd) +{ Block *blk = block_alloc(size); if (!blk) return NULL; @@ -51,7 +63,8 @@ Block *block_read(size_t size, int fd) { return blk; } -Block *block_mmap(size_t size, int fd, off_t offset) { +static Block *block_mmap(size_t size, int fd, off_t offset) +{ Block *blk = calloc(1, sizeof *blk); if (!blk) return NULL; @@ -68,7 +81,8 @@ Block *block_mmap(size_t size, int fd, off_t offset) { return blk; } -Block *block_load(int dirfd, const char *filename, enum TextLoadMethod method, struct stat *info) { +static Block *block_load(int dirfd, const char *filename, enum TextLoadMethod method, struct stat *info) +{ Block *block = NULL; int fd = openat(dirfd, filename, O_RDONLY); if (fd == -1) @@ -94,23 +108,15 @@ out: return block; } -void block_free(Block *blk) { - if (!blk) - return; - if (blk->type == BLOCK_TYPE_MALLOC) - free(blk->data); - else if ((blk->type == BLOCK_TYPE_MMAP_ORIG || blk->type == BLOCK_TYPE_MMAP) && blk->data) - munmap(blk->data, blk->size); - free(blk); -} - /* check whether block has enough free space to store len bytes */ -bool block_capacity(Block *blk, size_t len) { +static bool block_capacity(Block *blk, size_t len) +{ return blk->size - blk->len >= len; } /* append data to block, assumes there is enough space available */ -const char *block_append(Block *blk, const char *data, size_t len) { +static const char *block_append(Block *blk, const char *data, size_t len) +{ char *dest = memcpy(blk->data + blk->len, data, len); blk->len += len; return dest; @@ -118,7 +124,8 @@ const char *block_append(Block *blk, const char *data, size_t len) { /* insert data into block at an arbitrary position, this should only be used with * data of the most recently created piece. */ -bool block_insert(Block *blk, size_t pos, const char *data, size_t len) { +static bool block_insert(Block *blk, size_t pos, const char *data, size_t len) +{ if (pos > blk->len || !block_capacity(blk, len)) return false; if (blk->len == pos) @@ -132,7 +139,8 @@ bool block_insert(Block *blk, size_t pos, const char *data, size_t len) { /* delete data from a block at an arbitrary position, this should only be used with * data of the most recently created piece. */ -bool block_delete(Block *blk, size_t pos, size_t len) { +static bool block_delete(Block *blk, size_t pos, size_t len) +{ size_t end; if (!addu(pos, len, &end) || end > blk->len) return false; @@ -146,6 +154,32 @@ bool block_delete(Block *blk, size_t pos, size_t len) { return true; } +static Block *text_block_mmaped(Text *txt) +{ + Block *block = array_get_ptr(&txt->blocks, 0); + if (block && block->type == BLOCK_TYPE_MMAP_ORIG && block->size) + return block; + return NULL; +} + +/* preserve the current text content such that it can be restored by + * means of undo/redo operations */ +void text_snapshot(Text *txt) +{ + if (txt->current_revision) + txt->last_revision = txt->current_revision; + txt->current_revision = NULL; + txt->cache = NULL; +} + +static void text_saved(Text *txt, struct stat *meta) +{ + if (meta) + txt->info = *meta; + txt->saved_revision = txt->history; + text_snapshot(txt); +} + Text *text_load(const char *filename) { return text_load_method(filename, TEXT_LOAD_AUTO); } @@ -3,19 +3,6 @@ #include "array.h" #include "text.h" -#include "text-internal.h" - -#include "text-common.c" -#include "text-io.c" -#include "text-iterator.c" -#include "text-motions.c" -#include "text-objects.c" -#if CONFIG_TRE - #include "text-regex-tre.c" -#else - #include "text-regex.c" -#endif -#include "text-util.c" /* A piece holds a reference (but doesn't itself store) a certain amount of data. * All active pieces chained together form the whole content of the document. @@ -78,6 +65,20 @@ typedef struct { size_t lineno; /* line number in file i.e. number of '\n' in [0, pos) */ } LineCache; +/* Block holding the file content, either readonly mmap(2)-ed from the original + * file or heap allocated to store the modifications. + */ +typedef struct { + size_t size; /* maximal capacity */ + size_t len; /* current used length / insertion position */ + char *data; /* actual data */ + enum { /* type of allocation */ + BLOCK_TYPE_MMAP_ORIG, /* mmap(2)-ed from an external file */ + BLOCK_TYPE_MMAP, /* mmap(2)-ed from a temporary file only known to this process */ + BLOCK_TYPE_MALLOC, /* heap allocated block using malloc(3) */ + } type; +} Block; + /* The main struct holding all information of a given file */ struct Text { Array blocks; /* blocks which hold text content */ @@ -93,6 +94,18 @@ struct Text { LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */ }; +#include "text-common.c" +#include "text-io.c" +#include "text-iterator.c" +#include "text-motions.c" +#include "text-objects.c" +#if CONFIG_TRE + #include "text-regex-tre.c" +#else + #include "text-regex.c" +#endif +#include "text-util.c" + /* block management */ static const char *block_store(Text*, const char *data, size_t len); /* cache layer */ @@ -626,20 +639,6 @@ struct stat text_stat(const Text *txt) { return txt->info; } -void text_saved(Text *txt, struct stat *meta) { - if (meta) - txt->info = *meta; - txt->saved_revision = txt->history; - text_snapshot(txt); -} - -Block *text_block_mmaped(Text *txt) { - Block *block = array_get_ptr(&txt->blocks, 0); - if (block && block->type == BLOCK_TYPE_MMAP_ORIG && block->size) - return block; - return NULL; -} - /* A delete operation can either start/stop midway through a piece or at * a boundary. In the former case a new piece is created to represent the * remaining text before/after the modification point. @@ -743,16 +742,6 @@ bool text_delete_range(Text *txt, const Filerange *r) { return text_delete(txt, r->start, text_range_size(r)); } -/* preserve the current text content such that it can be restored by - * means of undo/redo operations */ -void text_snapshot(Text *txt) { - if (txt->current_revision) - txt->last_revision = txt->current_revision; - txt->current_revision = NULL; - txt->cache = NULL; -} - - void text_free(Text *txt) { if (!txt) return; |
