shithub: orca

Download patch

ref: 10cbab99bac16851969dabb3e7963cbdf829ca58
parent: 5d5828d79e651d07d4556de0d9363621523444d6
author: cancel <cancel@cancel.fm>
date: Mon Nov 26 04:25:33 EST 2018

Change markmap to separate buffer from reusable

--- a/cli_main.c
+++ b/cli_main.c
@@ -1,5 +1,6 @@
 #include "base.h"
 #include "field.h"
+#include "mark.h"
 #include "sim.h"
 #include <getopt.h>
 
@@ -93,8 +94,11 @@
     fprintf(stderr, "File load error: %s.\n", errstr);
     return 1;
   }
+  Markmap_reusable markmap_r;
+  markmap_reusable_init(&markmap_r);
+  markmap_reusable_ensure_size(&markmap_r, field.height, field.width);
   for (int i = 0; i < ticks; ++i) {
-    orca_run(&field);
+    orca_run(&field, markmap_r.buffer);
   }
   field_fput(&field, stdout);
   field_deinit(&field);
--- a/mark.c
+++ b/mark.c
@@ -1,11 +1,13 @@
 #include "mark.h"
 
-void markmap_init(Markmap* map) {
+void markmap_reusable_init(Markmap_reusable* map) {
   map->buffer = NULL;
   map->capacity = 0;
 }
 
-void markmap_ensure_capacity(Markmap* map, Usz capacity) {
+void markmap_reusable_ensure_size(Markmap_reusable* map, Usz height,
+                                  Usz width) {
+  Usz capacity = height * width;
   if (map->capacity < capacity) {
     map->buffer = realloc(map->buffer, capacity);
     map->capacity = capacity;
@@ -12,10 +14,9 @@
   }
 }
 
-void markmap_clear(Markmap* map) {
-  memset(map->buffer, 0, map->capacity);
-}
+void markmap_reusable_deinit(Markmap_reusable* map) { free(map->buffer); }
 
-void markmap_deinit(Markmap* map) {
-  free(map->buffer);
+void markmap_clear(Markmap_buffer map, Usz height, Usz width) {
+  Usz cleared_size = height * width;
+  memset(map, 0, cleared_size);
 }
--- a/mark.h
+++ b/mark.h
@@ -11,41 +11,48 @@
   Mark_flag_sleep_phase1 = 1 << 4,
 } Mark_flags;
 
-typedef struct {
-  U8* buffer;
+typedef U8* Markmap_buffer;
+
+typedef struct Markmap_reusable {
+  Markmap_buffer buffer;
   Usz capacity;
-} Markmap;
+} Markmap_reusable;
 
-void markmap_init(Markmap* map);
-void markmap_ensure_capacity(Markmap* map, Usz capacity);
-void markmap_clear(Markmap* map);
-void markmap_deinit(Markmap* map);
+void markmap_reusable_init(Markmap_reusable* map);
+void markmap_reusable_ensure_size(Markmap_reusable* map, Usz height, Usz width);
+void markmap_reusable_deinit(Markmap_reusable* map);
 
+void markmap_clear(Markmap_buffer map, Usz height, Usz width);
+
 ORCA_FORCE_INLINE
-Mark_flags markmap_peek_relative(Markmap* map, Usz map_height, Usz map_width,
-                                 Usz y, Usz x, Isz offs_y, Isz offs_x);
+Mark_flags markmap_peek_relative(Markmap_buffer map, Usz map_height,
+                                 Usz map_width, Usz y, Usz x, Isz offs_y,
+                                 Isz offs_x);
 ORCA_FORCE_INLINE
-void markmap_poke_relative(Markmap* map, Usz map_height, Usz map_width, Usz y,
-                           Usz x, Isz offs_y, Isz offs_x, Mark_flags flags);
+void markmap_poke_relative(Markmap_buffer map, Usz map_height, Usz map_width,
+                           Usz y, Usz x, Isz offs_y, Isz offs_x,
+                           Mark_flags flags);
 
 // Inline implementation
 
 ORCA_FORCE_INLINE
-Mark_flags markmap_peek_relative(Markmap* map, Usz map_height, Usz map_width,
-                                 Usz y, Usz x, Isz offs_y, Isz offs_x) {
+Mark_flags markmap_peek_relative(Markmap_buffer map, Usz map_height,
+                                 Usz map_width, Usz y, Usz x, Isz offs_y,
+                                 Isz offs_x) {
   Isz y0 = (Isz)y + offs_y;
   Isz x0 = (Isz)x + offs_x;
   if (y0 >= (Isz)map_height || x0 >= (Isz)map_width || y0 < 0 || x0 < 0)
     return Mark_flag_none;
-  return map->buffer[(Usz)y0 * map_width + (Usz)x0];
+  return map[(Usz)y0 * map_width + (Usz)x0];
 }
 
 ORCA_FORCE_INLINE
-void markmap_poke_relative(Markmap* map, Usz map_height, Usz map_width, Usz y,
-                           Usz x, Isz offs_y, Isz offs_x, Mark_flags flags) {
+void markmap_poke_relative(Markmap_buffer map, Usz map_height, Usz map_width,
+                           Usz y, Usz x, Isz offs_y, Isz offs_x,
+                           Mark_flags flags) {
   Isz y0 = (Isz)y + offs_y;
   Isz x0 = (Isz)x + offs_x;
   if (y0 >= (Isz)map_height || x0 >= (Isz)map_width || y0 < 0 || x0 < 0)
     return;
-  map->buffer[(Usz)y0 * map_width + (Usz)x0] = flags;
+  map[(Usz)y0 * map_width + (Usz)x0] = (U8)flags;
 }
--- a/sim.c
+++ b/sim.c
@@ -91,7 +91,8 @@
   }
 }
 
-void orca_run(Field* field) {
+void orca_run(Field* field, Markmap_buffer markmap) {
+  (void)markmap;
   Usz ny = field->height;
   Usz nx = field->width;
   Glyph* field_buffer = field->buffer;
--- a/sim.h
+++ b/sim.h
@@ -1,4 +1,5 @@
 #pragma once
 #include "base.h"
+#include "mark.h"
 
-void orca_run(Field* f);
+void orca_run(Field* field, Markmap_buffer markmap);