diff options
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/filesystem.c | 40 | ||||
-rw-r--r-- | src/path.c | 4 | ||||
-rw-r--r-- | src/posix/filesystem_posix.c | 17 | ||||
-rw-r--r-- | test/test_allocator.c | 2 | ||||
-rw-r--r-- | test/test_btree.c | 10 |
6 files changed, 40 insertions, 34 deletions
diff --git a/meson.build b/meson.build index 4142103..926d5df 100644 --- a/meson.build +++ b/meson.build @@ -372,6 +372,7 @@ if cc.get_id() == 'emscripten' '-mbulk-memory', '-pthread', ['-s', 'ENVIRONMENT=node,worker'], + ['-s', 'INITIAL_MEMORY=33554432'], ] library_c_args += wasm_c_args diff --git a/src/filesystem.c b/src/filesystem.c index c3e8f10..56e1fb2 100644 --- a/src/filesystem.c +++ b/src/filesystem.c @@ -37,31 +37,35 @@ zix_create_directories(ZixAllocator* const allocator, // Allocate a working copy of the path to chop along the way const size_t path_len = strlen(dir_path); char* const path = (char*)zix_malloc(allocator, path_len + 1U); - memcpy(path, dir_path, path_len + 1U); - - // Start at the root directory (past any name) - ZixPathIter p = zix_path_begin(path); - while (p.state < ZIX_PATH_FILE_NAME) { - p = zix_path_next(path, p); - } + ZixStatus st = path ? ZIX_STATUS_SUCCESS : ZIX_STATUS_NO_MEM; + if (path) { + // Copy directory path as prefix + memcpy(path, dir_path, path_len + 1U); + + // Start at the root directory (past any name) + ZixPathIter p = zix_path_begin(path); + while (p.state < ZIX_PATH_FILE_NAME) { + p = zix_path_next(path, p); + } - // Create each directory down the path - ZixStatus st = ZIX_STATUS_SUCCESS; - while (p.state != ZIX_PATH_END) { - const char old_end = path[p.range.end]; + // Create each directory down the path + while (p.state != ZIX_PATH_END) { + const char old_end = path[p.range.end]; - path[p.range.end] = '\0'; - if (zix_file_type(path) != ZIX_FILE_TYPE_DIRECTORY) { - if ((st = zix_create_directory(path))) { - break; + path[p.range.end] = '\0'; + if (zix_file_type(path) != ZIX_FILE_TYPE_DIRECTORY) { + if ((st = zix_create_directory(path))) { + break; + } } + + path[p.range.end] = old_end; + p = zix_path_next(path, p); } - path[p.range.end] = old_end; - p = zix_path_next(path, p); + zix_free(allocator, path); } - zix_free(allocator, path); return st; } @@ -435,9 +435,7 @@ zix_path_begin(const char* const path) { const ZixPathIter iter = {zix_path_root_name_range(path), ZIX_PATH_ROOT_NAME}; - return (iter.range.end > iter.range.begin) ? iter - : path ? zix_path_next(path, iter) - : zix_path_next("", iter); + return (iter.range.end > iter.range.begin) ? iter : zix_path_next(path, iter); } ZixPathIter diff --git a/src/posix/filesystem_posix.c b/src/posix/filesystem_posix.c index 8d65611..a5ac857 100644 --- a/src/posix/filesystem_posix.c +++ b/src/posix/filesystem_posix.c @@ -429,22 +429,23 @@ zix_current_path(ZixAllocator* const allocator) { #if defined(PATH_MAX) // Some POSIX systems have a static PATH_MAX so we can store it on the stack - char buffer[PATH_MAX] = {0}; - if (getcwd(buffer, PATH_MAX)) { - return copy_path(allocator, buffer, strlen(buffer)); - } + char buffer[PATH_MAX] = {0}; + char* const cwd = getcwd(buffer, PATH_MAX); + return cwd ? copy_path(allocator, cwd, strlen(cwd)) : NULL; #elif USE_PATHCONF // Others don't so we have to query PATH_MAX at runtime to allocate the result const size_t size = max_path_size(); char* const buffer = (char*)zix_calloc(allocator, size, 1); char* const current = getcwd(buffer, size); - if (current) { - return current; + if (!current) { + zix_free(allocator, buffer); } - zix_free(allocator, buffer); -#endif + return current; +#else return NULL; + +#endif } diff --git a/test/test_allocator.c b/test/test_allocator.c index 425cc41..3474533 100644 --- a/test/test_allocator.c +++ b/test/test_allocator.c @@ -107,6 +107,8 @@ test_bump_allocator(void) assert(aligned <= buffer + sizeof(buffer)); assert((uintptr_t)aligned % 128 == 0U); + assert(!zix_aligned_alloc(&allocator.base, 8, 896)); + zix_aligned_free(&allocator.base, aligned); zix_free(&allocator.base, reclaimed); // Correct, but a noop zix_free(&allocator.base, malloced); // Correct, but a noop diff --git a/test/test_btree.c b/test/test_btree.c index 9694a7b..7392ff8 100644 --- a/test/test_btree.c +++ b/test/test_btree.c @@ -221,9 +221,9 @@ test_remove_cases(void) even multiples. This spreads the load around to hit as many cases as possible. */ - static const uintptr_t s1 = 2U; - static const uintptr_t s2 = 255U; - const size_t n_insertions = s1 * s2 * 1000U; + static const uintptr_t s1 = 3U; + static const uintptr_t s2 = 511U; + const size_t n_insertions = s1 * s2 * 450U; ZixBTree* const t = zix_btree_new(NULL, int_cmp, NULL); @@ -628,9 +628,9 @@ main(int argc, char** argv) test_remove_cases(); test_failed_alloc(); - const unsigned n_tests = 3U; + const unsigned n_tests = 2U; const size_t n_elems = - (argc > 1) ? zix_test_size_arg(argv[1], 4U, 1U << 20U) : 131072U; + (argc > 1) ? zix_test_size_arg(argv[1], 4U, 1U << 20U) : (1U << 16U); printf("Running %u tests with %zu elements", n_tests, n_elems); for (unsigned i = 0; i < n_tests; ++i) { |