diff options
| author | 3gg <3gg@shellblade.net> | 2025-06-30 15:29:44 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-06-30 15:29:44 -0700 |
| commit | 25a367862dbdfb0fdfdfc6f619af3f0a7e0fe79f (patch) | |
| tree | 60269cf9a704c059495f52c3ad5a23794b2698c3 | |
| parent | 9b3ba8c80360c09dd32ee9645c536161b5dd0cff (diff) | |
Use nullptr
| -rw-r--r-- | cstring/src/cstring.c | 2 | ||||
| -rw-r--r-- | filesystem/src/filesystem.c | 6 | ||||
| -rw-r--r-- | filesystem/src/path.c | 2 | ||||
| -rw-r--r-- | list/include/list.h | 8 | ||||
| -rw-r--r-- | list/test/list_test.c | 2 | ||||
| -rw-r--r-- | mem/src/mem.c | 13 | ||||
| -rw-r--r-- | mempool/src/mempool.c | 10 | ||||
| -rw-r--r-- | 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) { | |||
| 23 | void string_del(string* str) { | 23 | void string_del(string* str) { |
| 24 | if (str->data) { | 24 | if (str->data) { |
| 25 | free((void*)str->data); | 25 | free((void*)str->data); |
| 26 | str->data = 0; | 26 | str->data = nullptr; |
| 27 | str->length = 0; | 27 | str->length = 0; |
| 28 | } | 28 | } |
| 29 | } | 29 | } |
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) { | |||
| 27 | void* read_file(const char* filepath) { | 27 | void* read_file(const char* filepath) { |
| 28 | assert(filepath); | 28 | assert(filepath); |
| 29 | 29 | ||
| 30 | void* data = 0; | 30 | void* data = nullptr; |
| 31 | 31 | ||
| 32 | FILE* file = fopen(filepath, "rb"); | 32 | FILE* file = fopen(filepath, "rb"); |
| 33 | if (!file) { | 33 | if (!file) { |
| 34 | return 0; | 34 | return nullptr; |
| 35 | } | 35 | } |
| 36 | const size_t file_size = get_file_size(file); | 36 | const size_t file_size = get_file_size(file); |
| 37 | if (file_size == (size_t)-1) { | 37 | if (file_size == (size_t)-1) { |
| @@ -53,5 +53,5 @@ cleanup: | |||
| 53 | if (data) { | 53 | if (data) { |
| 54 | free(data); | 54 | free(data); |
| 55 | } | 55 | } |
| 56 | return 0; | 56 | return nullptr; |
| 57 | } | 57 | } |
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) { | |||
| 21 | void path_del(path* path) { | 21 | void path_del(path* path) { |
| 22 | if (path) { | 22 | if (path) { |
| 23 | free(path->data); | 23 | free(path->data); |
| 24 | path->data = 0; | 24 | path->data = nullptr; |
| 25 | path->size = 0; | 25 | path->size = 0; |
| 26 | } | 26 | } |
| 27 | } | 27 | } |
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) { | |||
| 23 | 23 | ||
| 24 | /// Create a new empty list. | 24 | /// Create a new empty list. |
| 25 | #define make_list(type) \ | 25 | #define make_list(type) \ |
| 26 | (type##_list) { 0 } | 26 | (type##_list) { nullptr } |
| 27 | 27 | ||
| 28 | /// Delete the list. | 28 | /// Delete the list. |
| 29 | #define del_list(list) \ | 29 | #define del_list(list) \ |
| @@ -32,7 +32,7 @@ static inline void* alloc(size_t size) { | |||
| 32 | node = node->next; \ | 32 | node = node->next; \ |
| 33 | free(cur); \ | 33 | free(cur); \ |
| 34 | } \ | 34 | } \ |
| 35 | list.head = 0; | 35 | list.head = nullptr; |
| 36 | 36 | ||
| 37 | /// Prepend a value to the list. | 37 | /// Prepend a value to the list. |
| 38 | #define list_add(list, value) \ | 38 | #define list_add(list, value) \ |
| @@ -76,10 +76,10 @@ static inline void* alloc(size_t size) { | |||
| 76 | node->next->prev = node->prev; \ | 76 | node->next->prev = node->prev; \ |
| 77 | } \ | 77 | } \ |
| 78 | if (list.head == node) { \ | 78 | if (list.head == node) { \ |
| 79 | list.head = 0; \ | 79 | list.head = nullptr; \ |
| 80 | } \ | 80 | } \ |
| 81 | free(node); \ | 81 | free(node); \ |
| 82 | node = 0; \ | 82 | node = nullptr; \ |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | /// Iterate over all the items in the list. | 85 | /// 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) { | |||
| 52 | TEST_CASE(list_remove_by_address) { | 52 | TEST_CASE(list_remove_by_address) { |
| 53 | const int N = 3; | 53 | const int N = 3; |
| 54 | 54 | ||
| 55 | int* ptrs[3] = {0}; | 55 | int* ptrs[3] = {nullptr}; |
| 56 | 56 | ||
| 57 | int_list list = make_list(int); | 57 | int_list list = make_list(int); |
| 58 | for (int i = 0; i < N; ++i) { | 58 | 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) { | |||
| 46 | if (mem->dynamic) { | 46 | if (mem->dynamic) { |
| 47 | if (mem->chunks) { | 47 | if (mem->chunks) { |
| 48 | free(mem->chunks); | 48 | free(mem->chunks); |
| 49 | mem->chunks = 0; | 49 | mem->chunks = nullptr; |
| 50 | } | 50 | } |
| 51 | if (mem->blocks) { | 51 | if (mem->blocks) { |
| 52 | free(mem->blocks); | 52 | free(mem->blocks); |
| 53 | mem->blocks = 0; | 53 | mem->blocks = nullptr; |
| 54 | } | 54 | } |
| 55 | } | 55 | } |
| 56 | } | 56 | } |
| @@ -113,10 +113,11 @@ void* mem_alloc_(Memory* mem, size_t num_blocks) { | |||
| 113 | return &mem->blocks[chunk_idx * mem->block_size_bytes]; | 113 | return &mem->blocks[chunk_idx * mem->block_size_bytes]; |
| 114 | } else { | 114 | } else { |
| 115 | if (mem->trap) { | 115 | if (mem->trap) { |
| 116 | FAIL("Memory allocation failed, increase the allocator's capacity or " | 116 | FAIL( |
| 117 | "avoid fragmentation."); | 117 | "Memory allocation failed, increase the allocator's capacity or " |
| 118 | "avoid fragmentation."); | ||
| 118 | } | 119 | } |
| 119 | return 0; // Large-enough free chunk not found. | 120 | return nullptr; // Large-enough free chunk not found. |
| 120 | } | 121 | } |
| 121 | } | 122 | } |
| 122 | 123 | ||
| @@ -172,7 +173,7 @@ void mem_free_(Memory* mem, void** chunk_ptr) { | |||
| 172 | 173 | ||
| 173 | mem->num_used_blocks--; | 174 | mem->num_used_blocks--; |
| 174 | 175 | ||
| 175 | *chunk_ptr = 0; | 176 | *chunk_ptr = nullptr; |
| 176 | } | 177 | } |
| 177 | 178 | ||
| 178 | // The handle is the chunk's index. We don't call it an index in the public API | 179 | // 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_( | |||
| 34 | block_info = calloc(num_blocks, sizeof(BlockInfo)); | 34 | block_info = calloc(num_blocks, sizeof(BlockInfo)); |
| 35 | blocks = calloc(num_blocks, block_size_bytes); | 35 | blocks = calloc(num_blocks, block_size_bytes); |
| 36 | pool->dynamic = true; | 36 | pool->dynamic = true; |
| 37 | if ((block_info == 0) || (blocks == 0)) { | 37 | if ((block_info == nullptr) || (blocks == nullptr)) { |
| 38 | return false; | 38 | return false; |
| 39 | } | 39 | } |
| 40 | } else { | 40 | } else { |
| @@ -55,11 +55,11 @@ void mempool_del_(mempool* pool) { | |||
| 55 | if (pool->dynamic) { | 55 | if (pool->dynamic) { |
| 56 | if (pool->block_info) { | 56 | if (pool->block_info) { |
| 57 | free(pool->block_info); | 57 | free(pool->block_info); |
| 58 | pool->block_info = 0; | 58 | pool->block_info = nullptr; |
| 59 | } | 59 | } |
| 60 | if (pool->blocks) { | 60 | if (pool->blocks) { |
| 61 | free(pool->blocks); | 61 | free(pool->blocks); |
| 62 | pool->blocks = 0; | 62 | pool->blocks = nullptr; |
| 63 | } | 63 | } |
| 64 | } | 64 | } |
| 65 | } | 65 | } |
| @@ -81,7 +81,7 @@ void* mempool_alloc_(mempool* pool) { | |||
| 81 | if (pool->trap) { | 81 | if (pool->trap) { |
| 82 | FAIL("mempool allocation failed, increase the pool's capacity."); | 82 | FAIL("mempool allocation failed, increase the pool's capacity."); |
| 83 | } | 83 | } |
| 84 | return 0; // Pool is full. | 84 | return nullptr; // Pool is full. |
| 85 | } | 85 | } |
| 86 | 86 | ||
| 87 | // Allocate the block. | 87 | // Allocate the block. |
| @@ -124,7 +124,7 @@ void mempool_free_(mempool* pool, void** block_ptr) { | |||
| 124 | 124 | ||
| 125 | pool->num_used_blocks--; | 125 | pool->num_used_blocks--; |
| 126 | 126 | ||
| 127 | *block_ptr = 0; | 127 | *block_ptr = nullptr; |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | void* mempool_get_block_(const mempool* pool, size_t block_index) { | 130 | 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) { | |||
| 64 | // Handle reloading a previously-loaded library. | 64 | // Handle reloading a previously-loaded library. |
| 65 | if (plugin->handle) { | 65 | if (plugin->handle) { |
| 66 | dlclose(plugin->handle); | 66 | dlclose(plugin->handle); |
| 67 | plugin->handle = 0; | 67 | plugin->handle = nullptr; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | const mstring lib = plugin_lib_path(plugin); | 70 | const mstring lib = plugin_lib_path(plugin); |
| 71 | 71 | ||
| 72 | // If the plugin fails to load, make sure to keep the plugin's old handle to | 72 | // If the plugin fails to load, make sure to keep the plugin's old handle to |
| 73 | // handle the error gracefully. This handles reload failures, specifically. | 73 | // handle the error gracefully. This handles reload failures, specifically. |
| 74 | void* handle = 0; | 74 | void* handle = nullptr; |
| 75 | if ((handle = dlopen(mstring_cstr(&lib), RTLD_NOW))) { | 75 | if ((handle = dlopen(mstring_cstr(&lib), RTLD_NOW))) { |
| 76 | LOGD("Plugin [%s] loaded successfully", mstring_cstr(&plugin->filename)); | 76 | LOGD("Plugin [%s] loaded successfully", mstring_cstr(&plugin->filename)); |
| 77 | plugin->handle = handle; | 77 | plugin->handle = handle; |
| @@ -86,7 +86,7 @@ static bool load_library(Plugin* plugin) { | |||
| 86 | static void delete_plugin_state(Plugin* plugin) { | 86 | static void delete_plugin_state(Plugin* plugin) { |
| 87 | if (plugin->state) { | 87 | if (plugin->state) { |
| 88 | free(plugin->state); | 88 | free(plugin->state); |
| 89 | plugin->state = 0; | 89 | plugin->state = nullptr; |
| 90 | } | 90 | } |
| 91 | } | 91 | } |
| 92 | 92 | ||
| @@ -105,7 +105,7 @@ static void destroy_plugin(Plugin* plugin) { | |||
| 105 | if (plugin) { | 105 | if (plugin) { |
| 106 | if (plugin->handle) { | 106 | if (plugin->handle) { |
| 107 | dlclose(plugin->handle); | 107 | dlclose(plugin->handle); |
| 108 | plugin->handle = 0; | 108 | plugin->handle = nullptr; |
| 109 | } | 109 | } |
| 110 | delete_plugin_state(plugin); | 110 | delete_plugin_state(plugin); |
| 111 | } | 111 | } |
| @@ -118,7 +118,7 @@ Plugin* load_plugin(PluginEngine* eng, const char* filename) { | |||
| 118 | Plugin plugin = (Plugin){.eng = eng, .filename = mstring_make(filename)}; | 118 | Plugin plugin = (Plugin){.eng = eng, .filename = mstring_make(filename)}; |
| 119 | 119 | ||
| 120 | if (!load_library(&plugin)) { | 120 | if (!load_library(&plugin)) { |
| 121 | return 0; | 121 | return nullptr; |
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | list_add(eng->plugins, plugin); | 124 | list_add(eng->plugins, plugin); |
| @@ -132,7 +132,7 @@ void delete_plugin(Plugin** pPlugin) { | |||
| 132 | assert(plugin->eng); | 132 | assert(plugin->eng); |
| 133 | destroy_plugin(plugin); | 133 | destroy_plugin(plugin); |
| 134 | list_remove_ptr(plugin->eng->plugins, plugin); | 134 | list_remove_ptr(plugin->eng->plugins, plugin); |
| 135 | *pPlugin = 0; | 135 | *pPlugin = nullptr; |
| 136 | } | 136 | } |
| 137 | } | 137 | } |
| 138 | 138 | ||
| @@ -148,7 +148,7 @@ bool plugin_reloaded(Plugin* plugin) { | |||
| 148 | // ----------------------------------------------------------------------------- | 148 | // ----------------------------------------------------------------------------- |
| 149 | 149 | ||
| 150 | PluginEngine* new_plugin_engine(const PluginEngineDesc* desc) { | 150 | PluginEngine* new_plugin_engine(const PluginEngineDesc* desc) { |
| 151 | PluginEngine* eng = 0; | 151 | PluginEngine* eng = nullptr; |
| 152 | 152 | ||
| 153 | if (!(eng = calloc(1, sizeof(PluginEngine)))) { | 153 | if (!(eng = calloc(1, sizeof(PluginEngine)))) { |
| 154 | goto cleanup; | 154 | goto cleanup; |
| @@ -173,7 +173,7 @@ PluginEngine* new_plugin_engine(const PluginEngineDesc* desc) { | |||
| 173 | 173 | ||
| 174 | cleanup: | 174 | cleanup: |
| 175 | delete_plugin_engine(&eng); | 175 | delete_plugin_engine(&eng); |
| 176 | return 0; | 176 | return nullptr; |
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | void delete_plugin_engine(PluginEngine** pEng) { | 179 | void delete_plugin_engine(PluginEngine** pEng) { |
| @@ -191,7 +191,7 @@ void delete_plugin_engine(PluginEngine** pEng) { | |||
| 191 | close(eng->inotify_instance); | 191 | close(eng->inotify_instance); |
| 192 | } | 192 | } |
| 193 | free(eng); | 193 | free(eng); |
| 194 | *pEng = 0; | 194 | *pEng = nullptr; |
| 195 | } | 195 | } |
| 196 | } | 196 | } |
| 197 | 197 | ||
