foo
[gbajam22] / src / util.c
index 9bba42f..c734f85 100644 (file)
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+#include <string.h>
 #include "util.h"
 #include "debug.h"
 
@@ -23,3 +25,54 @@ void *iwram_sbrk(intptr_t delta)
        iwram_brk(top + delta);
        return prev;
 }
+
+int ispow2(unsigned int x)
+{
+       int cnt = 0;
+       while(x) {
+               if(x & 1) cnt++;
+               x >>= 1;
+       }
+       return cnt == 1;
+}
+
+
+
+void *malloc_nf_impl(size_t sz, const char *file, int line)
+{
+       void *p;
+       if(!(p = malloc(sz))) {
+               panic(get_pc(), "%s:%d malloc %lu\n", file, line, (unsigned long)sz);
+       }
+       return p;
+}
+
+void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line)
+{
+       void *p;
+       if(!(p = calloc(num, sz))) {
+               panic(get_pc(), "%s:%d calloc %lu\n", file, line, (unsigned long)(num * sz));
+       }
+       return p;
+}
+
+void *realloc_nf_impl(void *p, size_t sz, const char *file, int line)
+{
+       if(!(p = realloc(p, sz))) {
+               panic(get_pc(), "%s:%d realloc %lu\n", file, line, (unsigned long)sz);
+       }
+       return p;
+}
+
+char *strdup_nf_impl(const char *s, const char *file, int line)
+{
+       int len;
+       char *res;
+
+       len = strlen(s);
+       if(!(res = malloc(len + 1))) {
+               panic(get_pc(), "%s:%d strdup\n", file, line);
+       }
+       memcpy(res, s, len + 1);
+       return res;
+}