Commit Graph

13 Commits

Author SHA1 Message Date
Andrew Pamment
03c8db54ab Add fopen_node_path 2018-10-24 20:08:52 +10:00
Andrew Pamment
4ba7eda791 Add fopen_bbs_path function 2018-10-20 09:50:28 +10:00
Dan Cross
69c7286ea7 util.c: munmap takes a pointer, not a file descriptor.
The first argument to munmap(2) should be a pointer to
an mmap'ed region, not a file descriptor.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-16 11:00:01 +10:00
Dan Cross
aba49d7a20 Added file2stralloc to read a file directly into a stralloc.
Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-13 10:22:33 +10:00
Dan Cross
cf766e3e67 Trivial clang-format changes
Changes from a clang-format run.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-12 10:12:45 +10:00
Andrew Pamment
e196292503 Merge branch 'master' of ssh://git.magickabbs.com/home/andrew/repositories/MagickaBBS 2018-10-11 11:47:53 +10:00
Dan Cross
82b6ec3a3b More use of ptr_vector; avoid unnecessary copies.
Recast more code in terms of the ptr_vector abstraction.

The mail_menu.c code also made a lot of unnecessary copies
of strings.  For example, there was this code sequence:

    for (i = z; i < lines - 1; i++) {
            free(content[i]);
            content[i] = strdup(content[i + 1]);
    }
    free(content[i]);
    lines--;
    content = (char **)realloc(content, sizeof(char *) * lines);

Here, `content` represents an array of lines of text.
This code is removing an element from somewhere in that
array (possibly in the middle), and then shifting the
remaining elements over one position.

But observe the calls to `free` and `strdup` in the loop
body: the content is already dynamically allocated.  We
free whatever was in the selected position, and then make
*another copy* of the data in the next position to put
into the now-available slot in the array: repeat for the
remainder of the array's elements.

Instead, we could change this code to just shift things
down:

    free(content[z]);
    for (i = z; i < (lines - 1); ++i)
            content[i] = content[i + 1];
    --lines;
    ncontent = realloc(content, sizeof(char *) * lines);
    assert(ncontent == NULL);
    content = ncontent;

However, the ptr_vector abstraction provides us a function,
`ptr_vector_del` that deletes an element from the array and
returns the pointer, so we can rewrite this as simply:

    free(ptr_vector_del(&content, z));

No additional malloc()/free() required, which means less
pressure on the memory allocator and less copying of data.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-11 11:44:19 +10:00
Andrew Pamment
f70565325e fix bug in new ptr vector append 2018-10-10 11:12:04 +10:00
Dan Cross
f74c418f47 clang-format: Minor whitespace issues.
These are entirely my fault, sadly.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-10 10:26:48 +10:00
Dan Cross
54093060cb More cleanups.
More cleaning up construction of arrays of things.
Introduce a utility function called, `split_on_space`
that tokenizes a string on a space character; use
it in most places where `strtok()` had been called.

More use of the ptr_vector type.  Introduce a utility
function to get access to the pointers without consuming
the vector; this is used in the files code.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-10 10:25:47 +10:00
Dan Cross
540e359080 Cleanups and pointer vectors.
A repeated pattern in Magicka is to append to dynamically
sized arrays via malloc()/realloc().  Introduce the notion
of a "pointer vector": that is, a growable vector of
pointers, that can be reused to implement that logic more
safely and efficiently (this implementation uses power-of-two
growing).

Many malloc()/realloc() calls were not checked; these
assert() that the return value from realloc() is not NULL.

Add a method to consume the pointer vector: that is, realloc()
it to the current length and return the underlying pointers.

Make the `fmt` argument to dolog() const.
Include <sys/wait.h> in bluewave.c to squash a warning.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-10 10:25:42 +10:00
Dan Cross
4827dcf8e4 Add a pointer vector abstraction.
There are lots of places where we want a growable
vector of pointers.  Add one.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-10 10:25:37 +10:00
Dan Cross
fa014f3a88 Simplify dynamic memory management.
Add utility routines and use them to simplify the
use of dynamically allocated memory.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
2018-10-10 10:25:29 +10:00