From e8043640517a052582c1fe0c66d1bc744422f348 Mon Sep 17 00:00:00 2001
From: Bruce Cowan <bruce@bcowan.me.uk>
Date: Sun, 26 Jul 2020 13:21:24 +0100
Subject: [PATCH] Lots of build system stuff, plus GCC attributes

---
 lib/meson.build         | 34 +++++++++++++++++++++++++++++++---
 lib/types/array.c       |  2 +-
 lib/types/meson.build   |  7 -------
 lib/types/slist.h       |  7 ++++---
 lib/utils/macros.h      | 29 +++++++++++++++++++++++++++++
 lib/utils/mem.h         | 12 ++++++++----
 lib/utils/meson.build   |  7 -------
 meson.build             | 16 ++++++++++++++--
 src/meson.build         |  6 +++---
 src/pthread/meson.build |  8 +++++---
 src/sockets/meson.build |  8 +++-----
 test/meson.build        |  9 ++++-----
 12 files changed, 102 insertions(+), 43 deletions(-)
 delete mode 100644 lib/types/meson.build
 create mode 100644 lib/utils/macros.h
 delete mode 100644 lib/utils/meson.build

diff --git a/lib/meson.build b/lib/meson.build
index 40709a6..2709e2d 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -1,6 +1,34 @@
-# SPDX-FileCopyrightText: 2018-2019 Bruce Cowan
+# SPDX-FileCopyrightText: 2018-2020 Bruce Cowan
 #
 # SPDX-License-Identifier: CC0-1.0
 
-subdir('utils')
-subdir('types')
+utils_src = [
+  'types/array.c',
+  'types/hashtable.c',
+  'types/slist.c',
+  'utils/mem.c',
+  'utils/utils.c',
+]
+
+utils_deps = [
+  config_h,
+  libm,
+]
+
+utils_includes = [
+  'types',
+  'utils',
+]
+
+utils_lib = library(
+  'utils',
+  utils_src,
+  dependencies: utils_deps,
+  include_directories: utils_includes,
+)
+
+utils_dep = declare_dependency(
+  link_with: utils_lib,
+  dependencies: utils_deps,
+  include_directories: utils_includes,
+)
diff --git a/lib/types/array.c b/lib/types/array.c
index 9a67fd5..14b1aa9 100644
--- a/lib/types/array.c
+++ b/lib/types/array.c
@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: 2018, 2019 Bruce Cowan <bruce@bcowan.me.uk>
+ * SPDX-FileCopyrightText: 2018-2020 Bruce Cowan <bruce@bcowan.me.uk>
  *
  * SPDX-License-Identifier: Apache-2.0
  */
diff --git a/lib/types/meson.build b/lib/types/meson.build
deleted file mode 100644
index cc2e3e4..0000000
--- a/lib/types/meson.build
+++ /dev/null
@@ -1,7 +0,0 @@
-# SPDX-FileCopyrightText: 2018-2020 Bruce Cowan <bruce@bcowan.me.uk>
-#
-# SPDX-License-Identifier: CC0-1.0
-
-types_src = ['array.c', 'hashtable.c', 'slist.c']
-types_lib = static_library('types', types_src, dependencies: [libm, utils_dep])
-types_dep = declare_dependency(link_with: types_lib, include_directories: '.')
diff --git a/lib/types/slist.h b/lib/types/slist.h
index 452eff5..4096a79 100644
--- a/lib/types/slist.h
+++ b/lib/types/slist.h
@@ -1,11 +1,12 @@
 /*
- * SPDX-FileCopyrightText: 2019 Bruce Cowan <bruce@bcowan.me.uk>
+ * SPDX-FileCopyrightText: 2019-2020 Bruce Cowan <bruce@bcowan.me.uk>
  *
  * SPDX-License-Identifier: Apache-2.0
  */
 
 #pragma once
 
+#include "macros.h"
 #include "types.h"
 
 typedef struct _SList SList;
@@ -16,7 +17,7 @@ struct _SList
 };
 
 SList * slist_prepend (SList      *list,
-                       void       *data);
+                       void       *data) ATTR_WARN_UNUSED_RESULT;
 
 SList * slist_find    (SList      *slist,
                        const void *data);
@@ -28,6 +29,6 @@ void    slist_foreach (SList      *list,
 
 SList * slist_remove   (SList   *list,
                         void    *data,
-                        FreeFunc func);
+                        FreeFunc func) ATTR_WARN_UNUSED_RESULT;
 void    slist_free_all (SList   *list,
                         FreeFunc func);
diff --git a/lib/utils/macros.h b/lib/utils/macros.h
new file mode 100644
index 0000000..93dcf80
--- /dev/null
+++ b/lib/utils/macros.h
@@ -0,0 +1,29 @@
+/*
+ * SPDX-FileCopyrightText: 2020 Bruce Cowan <bruce@bcowan.me.uk>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#pragma once
+
+#include "config.h"
+
+#ifdef HAVE_ATTR_ALLOC_SIZE
+  #define ATTR_ALLOC_SIZE(x) __attribute__((alloc_size(x)))
+  #define ATTR_ALLOC_SIZE2(x,y) __attribute__((alloc_size(x,y)))
+#else
+  #define ATTR_ALLOC_SIZE(x)
+  #define ATTR_ALLOC_SIZE2(x,y)
+#endif
+
+#ifdef HAVE_ATTR_MALLOC
+  #define ATTR_MALLOC __attribute__((malloc))
+#else
+  #define ATTR_MALLOC
+#endif
+
+#ifdef HAVE_ATTR_WARN_UNUSED_RESULT
+  #define ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+  #define ATTR_WARN_UNUSED_RESULT
+#endif
diff --git a/lib/utils/mem.h b/lib/utils/mem.h
index cfbc898..cf84a47 100644
--- a/lib/utils/mem.h
+++ b/lib/utils/mem.h
@@ -1,16 +1,20 @@
 /*
- * SPDX-FileCopyrightText: 2018, 2019 Bruce Cowan <bruce@bcowan.me.uk>
+ * SPDX-FileCopyrightText: 2018-2020 Bruce Cowan <bruce@bcowan.me.uk>
  *
  * SPDX-License-Identifier: Apache-2.0
  */
 
 #pragma once
 
+#include "macros.h"
+
 #include <stddef.h>
 
 void * check_calloc       (size_t nmemb,
-                           size_t size);
-void * check_malloc       (size_t  size);
+                           size_t size) ATTR_ALLOC_SIZE2(1,2) ATTR_MALLOC;
+
+void * check_malloc       (size_t  size) ATTR_ALLOC_SIZE(1) ATTR_MALLOC;
+
 void * check_reallocarray (void   *ptr,
                            size_t  nmemb,
-                           size_t  size);
+                           size_t  size) ATTR_ALLOC_SIZE2(2,3) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
diff --git a/lib/utils/meson.build b/lib/utils/meson.build
deleted file mode 100644
index 2f3ce2b..0000000
--- a/lib/utils/meson.build
+++ /dev/null
@@ -1,7 +0,0 @@
-# SPDX-FileCopyrightText: 2019-2020 Bruce Cowan
-#
-# SPDX-License-Identifier: CC0-1.0
-
-utils_lib = static_library('utils', ['mem.c', 'utils.c'], dependencies: config_dep)
-utils_dep = declare_dependency(link_with: utils_lib,
-  include_directories: '.')
diff --git a/meson.build b/meson.build
index d932226..09c749b 100644
--- a/meson.build
+++ b/meson.build
@@ -18,12 +18,24 @@ if cc.has_function('reallocarray', prefix: '#define _GNU_SOURCE\n#include <stdli
   conf_data.set('HAVE_REALLOCARRAY', 1)
 endif
 
+attrs = ['alloc_size', 'malloc', 'warn_unused_result']
+foreach attr: attrs
+  if cc.has_function_attribute(attr)
+    conf_data.set('HAVE_ATTR_' + attr.to_upper(), 1)
+  endif
+endforeach
+
 if cc.has_header('endian.h')
   has_endian_h = true
 endif
 
-configure_file(configuration: conf_data, output: 'config.h')
-config_dep = declare_dependency(include_directories: '.')
+config_h = declare_dependency(
+  sources: configure_file(
+    output: 'config.h',
+    configuration: conf_data,
+  ),
+  include_directories: '.',
+)
 
 if host_machine.system() == 'windows'
   socket_deps = [cc.find_library('ws2_32')]
diff --git a/src/meson.build b/src/meson.build
index 4b20046..14b9a01 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -3,7 +3,7 @@
 
 executable('add', 'add.c', dependencies: utils_dep)
 executable('angle', 'angle.c', dependencies: libm)
-executable('array-length', 'array-length.c', dependencies: types_dep)
+executable('array-length', 'array-length.c', dependencies: utils_dep)
 executable('binary', 'binary.c')
 executable('binary2', 'binary2.c', dependencies: utils_dep)
 executable('complex', 'complex.c', dependencies: libm)
@@ -15,9 +15,9 @@ executable('fixed-sizeof', 'fixed-sizeof.c')
 executable('fgets', 'fgets.c')
 executable('gcd', 'gcd.c')
 executable('getchar', 'getchar.c')
-executable('hashtable-test', 'hashtable-test.c', dependencies: types_dep)
+executable('hashtable-test', 'hashtable-test.c', dependencies: utils_dep)
 executable('kepler', 'kepler.c', dependencies: libm)
-executable('list-test', 'list-test.c', dependencies: types_dep)
+executable('list-test', 'list-test.c', dependencies: utils_dep)
 executable('next', 'next.c', dependencies: libm)
 executable('parrot', 'parrot.c')
 executable('sizeof', 'sizeof.c')
diff --git a/src/pthread/meson.build b/src/pthread/meson.build
index d8d7939..ad63623 100644
--- a/src/pthread/meson.build
+++ b/src/pthread/meson.build
@@ -1,6 +1,8 @@
-# SPDX-FileCopyrightText: 2019 Bruce Cowan <bruce@bcowan.me.uk>
+# SPDX-FileCopyrightText: 2019-2020 Bruce Cowan <bruce@bcowan.me.uk>
 #
 # SPDX-License-Identifier: CC0-1.0
 
-executable('thread-incr', 'thread-incr.c',
-           dependencies: [thread_dep, utils_dep])
+executable('thread-incr',
+  'thread-incr.c',
+  dependencies: [thread_dep, utils_dep]
+)
diff --git a/src/sockets/meson.build b/src/sockets/meson.build
index dfb0264..261c7d0 100644
--- a/src/sockets/meson.build
+++ b/src/sockets/meson.build
@@ -1,11 +1,9 @@
-# SPDX-FileCopyrightText: 2019 Bruce Cowan <bruce@bcowan.me.uk>
+# SPDX-FileCopyrightText: 2019-2020 Bruce Cowan <bruce@bcowan.me.uk>
 #
 # SPDX-License-Identifier: CC0-1.0
 
 if host_machine.system() == 'windows'
-    udp_server_src = ['udp-server-win.c']
+    executable('udp-server', 'udp-server-win.c', dependencies: socket_deps)
 else
-    udp_server_src = ['udp-server.c']
+    executable('udp-server', 'udp-server.c', dependencies: socket_deps)
 endif
-
-executable('udp-server', udp_server_src, dependencies: socket_deps)
diff --git a/test/meson.build b/test/meson.build
index 651b6cb..5fd4095 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,20 +1,19 @@
-# SPDX-FileCopyrightText: 2018 Bruce Cowan <bruce@bcowan.me.uk>
+# SPDX-FileCopyrightText: 2018-2020 Bruce Cowan <bruce@bcowan.me.uk>
 #
 # SPDX-License-Identifier: CC0-1.0
 
 avg = executable('array-average',
                  'array-average.c',
-                 dependencies: [types_dep, glib_dep])
+                 dependencies: [utils_dep, glib_dep])
 
 basic = executable('array-basic',
                    'array-basic.c',
-                   dependencies: [types_dep, glib_dep])
+                   dependencies: [utils_dep, glib_dep])
 
 empty = executable('array-empty',
                    'array-empty.c',
-                   dependencies: [types_dep, glib_dep])
+                   dependencies: [utils_dep, glib_dep])
 
 test('array-average', avg)
 test('array-basic', basic)
 test('array-empty', empty)
-
-- 
GitLab