From 25a367862dbdfb0fdfdfc6f619af3f0a7e0fe79f Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 30 Jun 2025 15:29:44 -0700 Subject: Use nullptr --- cstring/src/cstring.c | 2 +- filesystem/src/filesystem.c | 6 +++--- filesystem/src/path.c | 2 +- list/include/list.h | 8 ++++---- list/test/list_test.c | 2 +- mem/src/mem.c | 13 +++++++------ mempool/src/mempool.c | 10 +++++----- plugin/src/plugin.c | 18 +++++++++--------- 8 files changed, 31 insertions(+), 30 deletions(-) diff --git a/cstring/src/cstring.c b/cstring/src/cstring.c index e308589..100c130 100644 --- a/cstring/src/cstring.c +++ b/cstring/src/cstring.c @@ -23,7 +23,7 @@ string string_new(const char* cstr) { void string_del(string* str) { if (str->data) { free((void*)str->data); - str->data = 0; + str->data = nullptr; str->length = 0; } } diff --git a/filesystem/src/filesystem.c b/filesystem/src/filesystem.c index b228e85..b0c207a 100644 --- a/filesystem/src/filesystem.c +++ b/filesystem/src/filesystem.c @@ -27,11 +27,11 @@ size_t get_file_size(FILE* file) { void* read_file(const char* filepath) { assert(filepath); - void* data = 0; + void* data = nullptr; FILE* file = fopen(filepath, "rb"); if (!file) { - return 0; + return nullptr; } const size_t file_size = get_file_size(file); if (file_size == (size_t)-1) { @@ -53,5 +53,5 @@ cleanup: if (data) { free(data); } - return 0; + return nullptr; } diff --git a/filesystem/src/path.c b/filesystem/src/path.c index 2ce5a04..544a5d1 100644 --- a/filesystem/src/path.c +++ b/filesystem/src/path.c @@ -21,7 +21,7 @@ path path_new(const char* str) { void path_del(path* path) { if (path) { free(path->data); - path->data = 0; + path->data = nullptr; path->size = 0; } } diff --git a/list/include/list.h b/list/include/list.h index 24291e1..aadcb88 100644 --- a/list/include/list.h +++ b/list/include/list.h @@ -23,7 +23,7 @@ static inline void* alloc(size_t size) { /// Create a new empty list. #define make_list(type) \ - (type##_list) { 0 } + (type##_list) { nullptr } /// Delete the list. #define del_list(list) \ @@ -32,7 +32,7 @@ static inline void* alloc(size_t size) { node = node->next; \ free(cur); \ } \ - list.head = 0; + list.head = nullptr; /// Prepend a value to the list. #define list_add(list, value) \ @@ -76,10 +76,10 @@ static inline void* alloc(size_t size) { node->next->prev = node->prev; \ } \ if (list.head == node) { \ - list.head = 0; \ + list.head = nullptr; \ } \ free(node); \ - node = 0; \ + node = nullptr; \ } /// Iterate over all the items in the list. diff --git a/list/test/list_test.c b/list/test/list_test.c index 418e156..4ac5b5b 100644 --- a/list/test/list_test.c +++ b/list/test/list_test.c @@ -52,7 +52,7 @@ TEST_CASE(list_remove_by_value) { TEST_CASE(list_remove_by_address) { const int N = 3; - int* ptrs[3] = {0}; + int* ptrs[3] = {nullptr}; int_list list = make_list(int); for (int i = 0; i < N; ++i) { diff --git a/mem/src/mem.c b/mem/src/mem.c index 4f5e5ef..c2af518 100644 --- a/mem/src/mem.c +++ b/mem/src/mem.c @@ -46,11 +46,11 @@ void mem_del_(Memory* mem) { if (mem->dynamic) { if (mem->chunks) { free(mem->chunks); - mem->chunks = 0; + mem->chunks = nullptr; } if (mem->blocks) { free(mem->blocks); - mem->blocks = 0; + mem->blocks = nullptr; } } } @@ -113,10 +113,11 @@ void* mem_alloc_(Memory* mem, size_t num_blocks) { return &mem->blocks[chunk_idx * mem->block_size_bytes]; } else { if (mem->trap) { - FAIL("Memory allocation failed, increase the allocator's capacity or " - "avoid fragmentation."); + FAIL( + "Memory allocation failed, increase the allocator's capacity or " + "avoid fragmentation."); } - return 0; // Large-enough free chunk not found. + return nullptr; // Large-enough free chunk not found. } } @@ -172,7 +173,7 @@ void mem_free_(Memory* mem, void** chunk_ptr) { mem->num_used_blocks--; - *chunk_ptr = 0; + *chunk_ptr = nullptr; } // The handle is the chunk's index. We don't call it an index in the public API diff --git a/mempool/src/mempool.c b/mempool/src/mempool.c index 46f1053..444d602 100644 --- a/mempool/src/mempool.c +++ b/mempool/src/mempool.c @@ -34,7 +34,7 @@ bool mempool_make_( block_info = calloc(num_blocks, sizeof(BlockInfo)); blocks = calloc(num_blocks, block_size_bytes); pool->dynamic = true; - if ((block_info == 0) || (blocks == 0)) { + if ((block_info == nullptr) || (blocks == nullptr)) { return false; } } else { @@ -55,11 +55,11 @@ void mempool_del_(mempool* pool) { if (pool->dynamic) { if (pool->block_info) { free(pool->block_info); - pool->block_info = 0; + pool->block_info = nullptr; } if (pool->blocks) { free(pool->blocks); - pool->blocks = 0; + pool->blocks = nullptr; } } } @@ -81,7 +81,7 @@ void* mempool_alloc_(mempool* pool) { if (pool->trap) { FAIL("mempool allocation failed, increase the pool's capacity."); } - return 0; // Pool is full. + return nullptr; // Pool is full. } // Allocate the block. @@ -124,7 +124,7 @@ void mempool_free_(mempool* pool, void** block_ptr) { pool->num_used_blocks--; - *block_ptr = 0; + *block_ptr = nullptr; } void* mempool_get_block_(const mempool* pool, size_t block_index) { diff --git a/plugin/src/plugin.c b/plugin/src/plugin.c index e2aae1f..3a0ef5d 100644 --- a/plugin/src/plugin.c +++ b/plugin/src/plugin.c @@ -64,14 +64,14 @@ static bool load_library(Plugin* plugin) { // Handle reloading a previously-loaded library. if (plugin->handle) { dlclose(plugin->handle); - plugin->handle = 0; + plugin->handle = nullptr; } const mstring lib = plugin_lib_path(plugin); // If the plugin fails to load, make sure to keep the plugin's old handle to // handle the error gracefully. This handles reload failures, specifically. - void* handle = 0; + void* handle = nullptr; if ((handle = dlopen(mstring_cstr(&lib), RTLD_NOW))) { LOGD("Plugin [%s] loaded successfully", mstring_cstr(&plugin->filename)); plugin->handle = handle; @@ -86,7 +86,7 @@ static bool load_library(Plugin* plugin) { static void delete_plugin_state(Plugin* plugin) { if (plugin->state) { free(plugin->state); - plugin->state = 0; + plugin->state = nullptr; } } @@ -105,7 +105,7 @@ static void destroy_plugin(Plugin* plugin) { if (plugin) { if (plugin->handle) { dlclose(plugin->handle); - plugin->handle = 0; + plugin->handle = nullptr; } delete_plugin_state(plugin); } @@ -118,7 +118,7 @@ Plugin* load_plugin(PluginEngine* eng, const char* filename) { Plugin plugin = (Plugin){.eng = eng, .filename = mstring_make(filename)}; if (!load_library(&plugin)) { - return 0; + return nullptr; } list_add(eng->plugins, plugin); @@ -132,7 +132,7 @@ void delete_plugin(Plugin** pPlugin) { assert(plugin->eng); destroy_plugin(plugin); list_remove_ptr(plugin->eng->plugins, plugin); - *pPlugin = 0; + *pPlugin = nullptr; } } @@ -148,7 +148,7 @@ bool plugin_reloaded(Plugin* plugin) { // ----------------------------------------------------------------------------- PluginEngine* new_plugin_engine(const PluginEngineDesc* desc) { - PluginEngine* eng = 0; + PluginEngine* eng = nullptr; if (!(eng = calloc(1, sizeof(PluginEngine)))) { goto cleanup; @@ -173,7 +173,7 @@ PluginEngine* new_plugin_engine(const PluginEngineDesc* desc) { cleanup: delete_plugin_engine(&eng); - return 0; + return nullptr; } void delete_plugin_engine(PluginEngine** pEng) { @@ -191,7 +191,7 @@ void delete_plugin_engine(PluginEngine** pEng) { close(eng->inotify_instance); } free(eng); - *pEng = 0; + *pEng = nullptr; } } -- cgit v1.2.3