Continue 'poison pattern' with malloc and friends master
authorMiles Rout <miles.rout@gmail.com>
Mon, 25 Nov 2019 10:36:01 +0000 (23:36 +1300)
committerMiles Rout <miles.rout@gmail.com>
Mon, 25 Nov 2019 10:36:01 +0000 (23:36 +1300)
as/include/_memory.h [new file with mode: 0644]
as/include/memory.h [new file with mode: 0644]
as/src/as.c
as/src/memory.c [new file with mode: 0644]

diff --git a/as/include/_memory.h b/as/include/_memory.h
new file mode 100644 (file)
index 0000000..f8e4603
--- /dev/null
@@ -0,0 +1,3 @@
+void *emalloc(size_t);
+void *ecalloc(size_t, size_t);
+void *erealloc(void *, size_t);
diff --git a/as/include/memory.h b/as/include/memory.h
new file mode 100644 (file)
index 0000000..f0aaa9a
--- /dev/null
@@ -0,0 +1,4 @@
+#include "_memory.h"
+#pragma GCC poison malloc
+#pragma GCC poison calloc
+#pragma GCC poison realloc
index 27b81f3e5081d91280093e3a23cf4e0fb05d9c7d..574471421a88cbe5283b76cea189a5250b4478ea 100644 (file)
@@ -1,10 +1,16 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include "printf.h"
 #include "printf.h"
+#include "memory.h"
 
 int main()
 {
 
 int main()
 {
+       void *x;
+
        efprintf(stdout, "Hello, world!\n");
        efprintf(stdout, "Hello, world!\n");
+       x = emalloc(sizeof(int));
+       free(x);
 
        return 0;
 }
 
        return 0;
 }
diff --git a/as/src/memory.c b/as/src/memory.c
new file mode 100644 (file)
index 0000000..486ffe7
--- /dev/null
@@ -0,0 +1,31 @@
+#include <stdlib.h>
+#include "_memory.h"
+
+#include "abort.h"
+
+void *emalloc(size_t n)
+{
+       void *p = malloc(n);
+       if (p == NULL)
+               abort_with_error("Memory allocation failure\n");
+
+       return p;
+}
+
+void *ecalloc(size_t n, size_t m)
+{
+       void *p = calloc(n, m);
+       if (p == NULL)
+               abort_with_error("Memory allocation failure\n");
+
+       return p;
+}
+
+void *erealloc(void *q, size_t n)
+{
+       void *p = realloc(q, n);
+       if (p == NULL)
+               abort_with_error("Memory allocation failure\n");
+
+       return p;
+}