optimizations
[gbajam22] / src / util.c
index 9bba42f..2bd0338 100644 (file)
@@ -1,7 +1,15 @@
+#include <stdlib.h>
+#include <string.h>
 #include "util.h"
 #include "debug.h"
 
+#ifdef BUILD_GBA
 extern char __iheap_start;
+#else
+#define IWRAM_POOL_SZ  32768
+#define __iheap_start  iwram[0]
+static char iwram[IWRAM_POOL_SZ];
+#endif
 static char *top = &__iheap_start;
 
 int iwram_brk(void *addr)
@@ -23,3 +31,82 @@ 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;
+}
+
+#ifndef BUILD_GBA
+/* utility functions which are implemented in assembly on GBA builds */
+
+void fillblock_16byte(void *dest, uint32_t val, int count)
+{
+       int i;
+       uint32_t *p = dest;
+
+       for(i=0; i<count; i++) {
+               p[0] = val;
+               p[1] = val;
+               p[2] = val;
+               p[3] = val;
+               p += 4;
+       }
+}
+
+void *get_pc(void)
+{
+       return 0;       /* not useful on PC builds */
+}
+
+void *get_sp(void)
+{
+       return (void*)-1;       /* not useful on PC builds */
+}
+#endif